Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osm: support version as json number or string #46

Merged
merged 2 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// Note that any errors encountered during marshaling will be different.
var CustomJSONMarshaler interface {
Marshal(v interface{}) ([]byte, error)
} = nil
}

// CustomJSONUnmarshaler can be set to have the code use a different
// json unmarshaler than the default in the standard library.
Expand All @@ -51,7 +51,7 @@ var CustomJSONMarshaler interface {
// Note that any errors encountered during unmarshaling will be different.
var CustomJSONUnmarshaler interface {
Unmarshal(data []byte, v interface{}) error
} = nil
}

func marshalJSON(v interface{}) ([]byte, error) {
if CustomJSONMarshaler == nil {
Expand Down
11 changes: 8 additions & 3 deletions osm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
// OSM represents the core osm data
// designed to parse http://wiki.openstreetmap.org/wiki/OSM_XML
type OSM struct {
// JSON APIs can return version as a string or number, converted to string
// for consistency.
Version string `xml:"version,attr,omitempty"`
Generator string `xml:"generator,attr,omitempty"`

Expand Down Expand Up @@ -286,7 +288,10 @@ func (o *OSM) marshalInnerElementsXML(e *xml.Encoder) error {
// http://overpass-api.de/output_formats.html#json
func (o *OSM) UnmarshalJSON(data []byte) error {
s := struct {
Version string `json:"version"`
// Version can be string or number,
// openstreetmap.org returns string
// overpass returns number
Version interface{} `json:"version"`
Generator string `json:"generator"`
Copyright string `json:"copyright"`
Attribution string `json:"attribution"`
Expand All @@ -299,7 +304,7 @@ func (o *OSM) UnmarshalJSON(data []byte) error {
return err
}

o.Version = s.Version
o.Version = fmt.Sprintf("%v", s.Version)
o.Generator = s.Generator
o.Copyright = s.Copyright
o.Attribution = s.Attribution
Expand Down Expand Up @@ -365,7 +370,7 @@ func (o *OSM) UnmarshalJSON(data []byte) error {
var jsonTypeRegexp = regexp.MustCompile(`"type"\s*:\s*"([^"]*)"`)

func findType(index int, data []byte) (string, error) {
matches := jsonTypeRegexp.FindAllSubmatch(data, -1)
matches := jsonTypeRegexp.FindAllSubmatch(data, 1)
if len(matches) > 0 {
return string(matches[0][1]), nil
}
Expand Down
18 changes: 18 additions & 0 deletions osm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ func TestOSM_UnmarshalJSON(t *testing.T) {
}
}

func TestOSM_UnmarshalJSON_Version(t *testing.T) {
data := []byte(`{
"version":0.6,"generator":"osm-go",
"elements":[
{"type":"node","id":123,"lat":0,"lon":0,"visible":false,"timestamp":"0001-01-01T00:00:00Z"}
]}`)

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

if o.Version != "0.6" {
t.Errorf("incorrect version %v != 0.6", o.Version)
}
}

func TestOSM_MarshalXML(t *testing.T) {
o := &OSM{
Version: "0.7",
Expand Down