Skip to content

Commit

Permalink
fix golangci-lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Dec 28, 2023
1 parent 656c127 commit 89f4b82
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 87 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
issues:
exclude-rules:
- path: '(.+)_test\.go'
linters:
- errcheck
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions annotate/relation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion annotate/way_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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)
}
}
}
40 changes: 2 additions & 38 deletions osm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"encoding/xml"
"io/ioutil"
"io"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -280,50 +280,14 @@ 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 {
t.Fatalf("unable to open %s: %v", filename, err)
}
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)
}
Expand Down
2 changes: 0 additions & 2 deletions osmapi/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 24 additions & 6 deletions osmgeojson/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand Down
33 changes: 16 additions & 17 deletions osmpbf/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -202,29 +202,28 @@ 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()

if err == io.EOF {
break
} else if err != nil {
ft.T.Fatal(err)
ft.Fatal(err)
}

switch v := e.(type) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 89f4b82

Please sign in to comment.