From 05ecd44a052314089c41756d10b5e17a2fba0495 Mon Sep 17 00:00:00 2001 From: Paul Mach Date: Wed, 27 Dec 2023 16:21:15 -0800 Subject: [PATCH] fix golangci-lint errors --- .golangci.yml | 5 +++++ README.md | 6 ++++-- annotate/relation_test.go | 7 ++++-- annotate/way_test.go | 6 +++++- change_test.go | 17 --------------- diff_test.go | 10 +++++++-- osm_test.go | 40 ++--------------------------------- osmapi/options.go | 2 -- osmgeojson/benchmarks_test.go | 30 ++++++++++++++++++++------ osmpbf/decode_test.go | 33 ++++++++++++++--------------- 10 files changed, 69 insertions(+), 87 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..8e5e857 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,5 @@ +issues: + exclude-rules: + - path: '(.+)_test\.go' + linters: + - errcheck diff --git a/README.md b/README.md index b8e29f6..9d65d09 100644 --- a/README.md +++ b/README.md @@ -113,13 +113,15 @@ import ( jsoniter "github.com/json-iterator/go" "github.com/paulmach/osm" ) + var c = jsoniter.Config{ EscapeHTML: true, SortMapKeys: false, MarshalFloatWith6Digits: true, }.Froze() -CustomJSONMarshaler = c -CustomJSONUnmarshaler = c + +osmm.CustomJSONMarshaler = c +osm.CustomJSONUnmarshaler = c ``` The above change can have dramatic performance implications, see the benchmarks below diff --git a/annotate/relation_test.go b/annotate/relation_test.go index 1619bbe..015c144 100644 --- a/annotate/relation_test.go +++ b/annotate/relation_test.go @@ -4,7 +4,7 @@ import ( "context" "encoding/xml" "fmt" - "io/ioutil" + "os" "reflect" "testing" "time" @@ -42,7 +42,10 @@ func TestRelation(t *testing.T) { t.Errorf("expected relations not equal, file saved to %s", filename) data, _ := xml.MarshalIndent(&osm.OSM{Relations: relations}, "", " ") - ioutil.WriteFile(filename, data, 0644) + err := os.WriteFile(filename, data, 0644) + if err != nil { + t.Fatalf("write error: %v", err) + } } } } diff --git a/annotate/way_test.go b/annotate/way_test.go index 47cfc3e..42d35ce 100644 --- a/annotate/way_test.go +++ b/annotate/way_test.go @@ -5,6 +5,7 @@ import ( "encoding/xml" "fmt" "io/ioutil" + "os" "reflect" "testing" @@ -36,7 +37,10 @@ func TestWays(t *testing.T) { t.Errorf("expected way not equal, file saved to %s", filename) data, _ := xml.MarshalIndent(&osm.OSM{Ways: o.Ways}, "", " ") - ioutil.WriteFile(filename, data, 0644) + err := os.WriteFile(filename, data, 0644) + if err != nil { + t.Fatalf("write error: %v", err) + } } } } diff --git a/change_test.go b/change_test.go index 73c86a1..444d754 100644 --- a/change_test.go +++ b/change_test.go @@ -236,23 +236,6 @@ func TestChange_HistoryDatasource(t *testing.T) { } } -func cleanXMLNameFromChange(c *Change) { - c.Version = "" - c.Generator = "" - c.Copyright = "" - c.Attribution = "" - c.License = "" - if c.Create != nil { - cleanXMLNameFromOSM(c.Create) - } - if c.Modify != nil { - cleanXMLNameFromOSM(c.Modify) - } - if c.Delete != nil { - cleanXMLNameFromOSM(c.Delete) - } -} - func BenchmarkChange_MarshalXML(b *testing.B) { filename := "testdata/changeset_38162206.osc" data := readFile(b, filename) diff --git a/diff_test.go b/diff_test.go index 517b9cc..0955747 100644 --- a/diff_test.go +++ b/diff_test.go @@ -148,7 +148,10 @@ func BenchmarkDiff_Marshal(b *testing.B) { b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { - xml.Marshal(diff) + _, err := xml.Marshal(diff) + if err != nil { + b.Fatalf("marshal error: %v", err) + } } } @@ -159,6 +162,9 @@ func BenchmarkDiff_Unmarshal(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { diff := &Diff{} - xml.Unmarshal(data, &diff) + err := xml.Unmarshal(data, &diff) + if err != nil { + b.Fatalf("unmarshal error: %v", err) + } } } diff --git a/osm_test.go b/osm_test.go index e9500e0..f615e23 100644 --- a/osm_test.go +++ b/osm_test.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "encoding/xml" - "io/ioutil" + "io" "os" "reflect" "testing" @@ -280,42 +280,6 @@ func TestOSM_MarshalXML(t *testing.T) { } } -func flattenOSM(c *Change) *OSM { - o := c.Create - if o == nil { - o = &OSM{} - } - - if c.Modify != nil { - o.Nodes = append(o.Nodes, c.Modify.Nodes...) - o.Ways = append(o.Ways, c.Modify.Ways...) - o.Relations = append(o.Relations, c.Modify.Relations...) - } - - if c.Delete != nil { - o.Nodes = append(o.Nodes, c.Delete.Nodes...) - o.Ways = append(o.Ways, c.Delete.Ways...) - o.Relations = append(o.Relations, c.Delete.Relations...) - } - - return o -} - -func cleanXMLNameFromOSM(o *OSM) { - for _, n := range o.Nodes { - n.XMLName = xmlNameJSONTypeNode{} - } - - for _, w := range o.Ways { - w.XMLName = xmlNameJSONTypeWay{} - } - - for _, r := range o.Relations { - // r.XMLName = xml.Name{} - r.XMLName = xmlNameJSONTypeRel{} - } -} - func readFile(t testing.TB, filename string) []byte { f, err := os.Open(filename) if err != nil { @@ -323,7 +287,7 @@ func readFile(t testing.TB, filename string) []byte { } defer f.Close() - data, err := ioutil.ReadAll(f) + data, err := io.ReadAll(f) if err != nil { t.Fatalf("unable to read file %s: %v", filename, err) } diff --git a/osmapi/options.go b/osmapi/options.go index c54d2b6..744a19b 100644 --- a/osmapi/options.go +++ b/osmapi/options.go @@ -24,8 +24,6 @@ func (o *at) applyFeature(p []string) ([]string, error) { return append(p, "at="+o.t.UTC().Format("2006-01-02T15:04:05Z")), nil } -func (o *at) feature() {} - // NotesOption defines a valid option for the osmapi.Notes by bounding box api. type NotesOption interface { applyNotes([]string) ([]string, error) diff --git a/osmgeojson/benchmarks_test.go b/osmgeojson/benchmarks_test.go index 1dc71dd..4877187 100644 --- a/osmgeojson/benchmarks_test.go +++ b/osmgeojson/benchmarks_test.go @@ -14,7 +14,10 @@ func BenchmarkConvert(b *testing.B) { b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { - Convert(o) + _, err := Convert(o) + if err != nil { + b.Fatalf("convert error: %v", err) + } } } @@ -25,7 +28,10 @@ func BenchmarkConvertAnnotated(b *testing.B) { b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { - Convert(o) + _, err := Convert(o) + if err != nil { + b.Fatalf("convert error: %v", err) + } } } @@ -35,7 +41,10 @@ func BenchmarkConvert_NoID(b *testing.B) { b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { - Convert(o, NoID(true)) + _, err := Convert(o, NoID(true)) + if err != nil { + b.Fatalf("convert error: %v", err) + } } } @@ -45,7 +54,10 @@ func BenchmarkConvert_NoMeta(b *testing.B) { b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { - Convert(o, NoMeta(true)) + _, err := Convert(o, NoMeta(true)) + if err != nil { + b.Fatalf("convert error: %v", err) + } } } @@ -55,7 +67,10 @@ func BenchmarkConvert_NoRelationMembership(b *testing.B) { b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { - Convert(o, NoRelationMembership(true)) + _, err := Convert(o, NoRelationMembership(true)) + if err != nil { + b.Fatalf("convert error: %v", err) + } } } @@ -65,7 +80,10 @@ func BenchmarkConvert_NoIDsMetaMembership(b *testing.B) { b.ReportAllocs() b.ResetTimer() for n := 0; n < b.N; n++ { - Convert(o, NoID(true), NoMeta(true), NoRelationMembership(true)) + _, err := Convert(o, NoID(true), NoMeta(true), NoRelationMembership(true)) + if err != nil { + b.Fatalf("convert error: %v", err) + } } } diff --git a/osmpbf/decode_test.go b/osmpbf/decode_test.go index 6b6785c..5d7340d 100644 --- a/osmpbf/decode_test.go +++ b/osmpbf/decode_test.go @@ -175,25 +175,25 @@ func (ft *OSMFileTest) downloadTestOSMFile() { if _, err := os.Stat(ft.FileName); os.IsNotExist(err) { out, err := os.Create(ft.FileName) if err != nil { - ft.T.Fatal(err) + ft.Fatal(err) } defer out.Close() resp, err := http.Get(ft.FileURL) if err != nil { - ft.T.Fatal(err) + ft.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - ft.T.Fatalf("test status code invalid: %v", resp.StatusCode) + ft.Fatalf("test status code invalid: %v", resp.StatusCode) } if _, err := io.Copy(out, resp.Body); err != nil { - ft.T.Fatal(err) + ft.Fatal(err) } } else if err != nil { - ft.T.Fatal(err) + ft.Fatal(err) } } @@ -202,21 +202,20 @@ func (ft *OSMFileTest) testDecode() { f, err := os.Open(ft.FileName) if err != nil { - ft.T.Fatal(err) + ft.Fatal(err) } defer f.Close() d := newDecoder(context.Background(), &Scanner{}, f) err = d.Start(runtime.GOMAXPROCS(-1)) if err != nil { - ft.T.Fatal(err) + ft.Fatal(err) } var n *osm.Node var w *osm.Way var r *osm.Relation var nc, wc, rc uint64 - var id string idsOrder := make([]string, 0, len(IDsExpectedOrder)) for { e, err := d.Next() @@ -224,7 +223,7 @@ func (ft *OSMFileTest) testDecode() { if err == io.EOF { break } else if err != nil { - ft.T.Fatal(err) + ft.Fatal(err) } switch v := e.(type) { @@ -233,7 +232,7 @@ func (ft *OSMFileTest) testDecode() { if v.ID == ft.ExpNode.ID { n = v } - id = fmt.Sprintf("node/%d", v.ID) + id := fmt.Sprintf("node/%d", v.ID) if _, ok := IDs[id]; ok { idsOrder = append(idsOrder, id) } @@ -242,7 +241,7 @@ func (ft *OSMFileTest) testDecode() { if v.ID == ft.ExpWay.ID { w = v } - id = fmt.Sprintf("way/%d", v.ID) + id := fmt.Sprintf("way/%d", v.ID) if _, ok := IDs[id]; ok { idsOrder = append(idsOrder, id) } @@ -251,7 +250,7 @@ func (ft *OSMFileTest) testDecode() { if v.ID == ft.ExpRel.ID { r = v } - id = fmt.Sprintf("relation/%d", v.ID) + id := fmt.Sprintf("relation/%d", v.ID) if _, ok := IDs[id]; ok { idsOrder = append(idsOrder, id) } @@ -260,21 +259,21 @@ func (ft *OSMFileTest) testDecode() { d.Close() if !reflect.DeepEqual(ft.ExpNode, n) { - ft.T.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpNode, n) + ft.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpNode, n) } roundCoordinates(w) if !reflect.DeepEqual(ft.ExpWay, w) { - ft.T.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpWay, w) + ft.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpWay, w) } if !reflect.DeepEqual(ft.ExpRel, r) { - ft.T.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpRel, r) + ft.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpRel, r) } if ft.ExpNodeCount != nc || ft.ExpWayCount != wc || ft.ExpRelCount != rc { - ft.T.Errorf("\nExpected %7d nodes, %7d ways, %7d relations\nGot %7d nodes, %7d ways, %7d relations.", + ft.Errorf("\nExpected %7d nodes, %7d ways, %7d relations\nGot %7d nodes, %7d ways, %7d relations.", ft.ExpNodeCount, ft.ExpWayCount, ft.ExpRelCount, nc, wc, rc) } if !reflect.DeepEqual(ft.IDsExpOrder, idsOrder) { - ft.T.Errorf("\nExpected: %v\nGot: %v", ft.IDsExpOrder, idsOrder) + ft.Errorf("\nExpected: %v\nGot: %v", ft.IDsExpOrder, idsOrder) } }