Skip to content

Commit

Permalink
correctly unmarshal elements with a type tag
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Dec 27, 2023
1 parent e652b50 commit 03d7101
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
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
}
24 changes: 23 additions & 1 deletion osm_test.go
Original file line number Diff line number Diff line change
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

0 comments on commit 03d7101

Please sign in to comment.