Skip to content

Commit

Permalink
Merge branch 'deflate' into deflate
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach authored Dec 28, 2023
2 parents 14df2f7 + b3aa927 commit 82384b5
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 109 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: golangci-lint
on:
push:
branches:
- master
pull_request:

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.21'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55
24 changes: 9 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,29 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.19.13'
cache: false

- name: Install dependencies
run: |
go version
go-version: '1.21'

- name: Run build
run: go build .

- name: Run tests
run: go test -v -coverprofile=profile.cov ./...
run: go build .

- name: Run vet & lint
- name: Run vet
run: |
go vet .
- name: Run tests
run: go test -v -coverprofile=profile.cov ./...

- name: codecov
uses: codecov/codecov-action@v1
with:
file: ./profile.cov
file: ./profile.cov
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"

Check failure on line 7 in annotate/way_test.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"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)
}
}
}
19 changes: 13 additions & 6 deletions osm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package osm
import (
"encoding/xml"
"fmt"
"regexp"
)

// These values should be returned if the osm data is actual
Expand Down Expand Up @@ -367,13 +366,21 @@ func (o *OSM) UnmarshalJSON(data []byte) error {
return nil
}

var jsonTypeRegexp = regexp.MustCompile(`"type"\s*:\s*"([^"]*)"`)
type typeStruct struct {
Type string `json:"type"`
}

func findType(index int, data []byte) (string, error) {
matches := jsonTypeRegexp.FindAllSubmatch(data, 1)
if len(matches) > 0 {
return string(matches[0][1]), nil
ts := typeStruct{}
err := unmarshalJSON(data, &ts)
if err != nil {
// should not happened due to previous decoding succeeded
return "", err
}

if ts.Type == "" {
return "", fmt.Errorf("could not find type in element index %d", index)
}

return "", fmt.Errorf("could not find type in element index %d", index)
return ts.Type, nil
}
64 changes: 25 additions & 39 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 @@ -183,7 +183,10 @@ func TestOSM_UnmarshalJSON(t *testing.T) {
{"type":"way","id":456,"visible":false,"timestamp":"0001-01-01T00:00:00Z","nodes":[]},
{"type":"relation","id":789,"visible":false,"timestamp":"0001-01-01T00:00:00Z","members":[]},
{"type":"changeset","id":10,"created_at":"0001-01-01T00:00:00Z","closed_at":"0001-01-01T00:00:00Z","open":false},
{"type":"user","id":16,"name":"","img":{"href":""},"changesets":{"count":0},"traces":{"count":0},"home":{"lat":0,"lon":0,"zoom":0},"languages":null,"blocks":{"received":{"count":0,"active":0}},"messages":{"received":{"count":0,"unread":0},"sent":{"count":0}},"created_at":"0001-01-01T00:00:00Z"},
{"type":"user","id":16,"name":"","img":{"href":""},
"changesets":{"count":0},"traces":{"count":0},"home":{"lat":0,"lon":0,"zoom":0},"languages":null,
"blocks":{"received":{"count":0,"active":0}},"messages":{"received":{"count":0,"unread":0},"sent":{"count":0}},
"created_at":"0001-01-01T00:00:00Z"},
{"type":"note","id":15,"lat":0,"lon":0,"date_created":null,"date_closed":null,"comments":null}
]}`)

Expand Down Expand Up @@ -244,6 +247,25 @@ func TestOSM_UnmarshalJSON_Version(t *testing.T) {
}
}

func TestOSM_UnmarshalJSON_Type(t *testing.T) {
data := []byte(`{
"version":0.6,"generator":"osm-go",
"elements":[
{"type":"relation","id":120,"timestamp":"0001-01-01T00:00:00Z","tags":{"type":"route"}},
{"tags":{"type":"route","other":"asdf"},"type":"relation","id":121,"timestamp":"0001-01-01T00:00:00Z"}
]}`)

o := &OSM{}
err := json.Unmarshal(data, &o)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}

if l := len(o.Relations); l != 2 {
t.Errorf("incorrect number of relations: %v", l)
}
}

func TestOSM_MarshalXML(t *testing.T) {
o := &OSM{
Version: "0.7",
Expand Down Expand Up @@ -280,50 +302,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
Loading

0 comments on commit 82384b5

Please sign in to comment.