diff --git a/Gopkg.lock b/Gopkg.lock index b49f46e7..bab07235 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -242,8 +242,7 @@ version = "v1.0.2" [[projects]] - branch = "master" - digest = "1:0a6da4a08925415ab3326e140985ec628417903637461e9b68c07a03dfcc918d" + digest = "1:fc81262a6ad5aeec27e1bd15356f790e6b2d8fd14acb6bd5ff3f0f51bf67417f" name = "github.com/tidwall/geojson" packages = [ ".", @@ -251,7 +250,8 @@ "geometry", ] pruneopts = "" - revision = "d0a98d02b48e887b4de454d80c61ef5fb9312482" + revision = "6baab67ab6a9bac4abf153ab779c736254a37fd1" + version = "v1.1.0" [[projects]] digest = "1:3ddca2bd5496c6922a2a9e636530e178a43c2a534ea6634211acdc7d10222794" diff --git a/Gopkg.toml b/Gopkg.toml index cca1c7b3..fbf4b850 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -23,7 +23,6 @@ required = [ "github.com/tidwall/lotsa", "github.com/mmcloughlin/geohash", - "github.com/tidwall/geojson", "github.com/tidwall/evio" ] @@ -35,6 +34,10 @@ required = [ branch = "master" name = "github.com/tidwall/boxtree" +[[constraint]] + name = "github.com/tidwall/geojson" + version = "1.1.0" + [[constraint]] name = "github.com/Shopify/sarama" version = "1.13.0" diff --git a/internal/collection/string.go b/internal/collection/string.go index b7207957..ad7dc042 100644 --- a/internal/collection/string.go +++ b/internal/collection/string.go @@ -58,6 +58,11 @@ func (s String) JSON() string { return string(s.AppendJSON(nil)) } +// MarshalJSON ... +func (s String) MarshalJSON() ([]byte, error) { + return s.AppendJSON(nil), nil +} + // Within ... func (s String) Within(obj geojson.Object) bool { return false diff --git a/tests/json_test.go b/tests/json_test.go index 17ee8a68..e0d02c1d 100644 --- a/tests/json_test.go +++ b/tests/json_test.go @@ -30,7 +30,7 @@ func json_JSET_geojson_test(mc *mockServer) error { {"JSET", "mykey", "myid1", "coordinates.1", 44}, {"OK"}, {"JGET", "mykey", "myid1"}, {`{"type":"Point","coordinates":[-115,44]}`}, {"SET", "mykey", "myid1", "OBJECT", `{"type":"Feature","geometry":{"type":"Point","coordinates":[-115,44]}}`}, {"OK"}, - {"JGET", "mykey", "myid1"}, {`{"type":"Feature","geometry":{"type":"Point","coordinates":[-115,44]}}`}, + {"JGET", "mykey", "myid1"}, {`{"type":"Feature","geometry":{"type":"Point","coordinates":[-115,44]},"properties":{}}`}, {"JGET", "mykey", "myid1", "geometry.type"}, {"Point"}, {"JSET", "mykey", "myid1", "properties.tags.-1", "southwest"}, {"OK"}, {"JSET", "mykey", "myid1", "properties.tags.-1", "united states"}, {"OK"}, diff --git a/tests/testcmd_test.go b/tests/testcmd_test.go index 5a2151a3..d6d65231 100644 --- a/tests/testcmd_test.go +++ b/tests/testcmd_test.go @@ -111,7 +111,7 @@ func testcmd_INTERSECTS_CLIP_test(mc *mockServer) error { {"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "BOUNDS", 37.732906137107, -122.44126439094543, 37.73421283683962, -122.43980526924135}, {"[1 " + poly9 + "]"}, {"TEST", "OBJECT", poly8, "INTERSECTS", "CLIP", "BOUNDS", 37.733, -122.4408378, 37.7341129, -122.44}, {"[1 " + poly8 + "]"}, - {"TEST", "OBJECT", multipoly5, "INTERSECTS", "CLIP", "BOUNDS", 37.73227823422744, -122.44120001792908, 37.73319038868677, -122.43955314159392}, {"[1 " + `{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-122.4408378,37.73319038868677],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.73319038868677],[-122.4408378,37.73319038868677]]]}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-122.44091033935547,37.73227823422744],[-122.43994474411011,37.73227823422744],[-122.43994474411011,37.73254976045042],[-122.44091033935547,37.73254976045042],[-122.44091033935547,37.73227823422744]]]}}]}` + "]"}, + {"TEST", "OBJECT", multipoly5, "INTERSECTS", "CLIP", "BOUNDS", 37.73227823422744, -122.44120001792908, 37.73319038868677, -122.43955314159392}, {"[1 " + `{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-122.4408378,37.73319038868677],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.73319038868677],[-122.4408378,37.73319038868677]]]},"properties":{}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-122.44091033935547,37.73227823422744],[-122.43994474411011,37.73227823422744],[-122.43994474411011,37.73254976045042],[-122.44091033935547,37.73254976045042],[-122.44091033935547,37.73227823422744]]]},"properties":{}}]}` + "]"}, {"TEST", "OBJECT", poly101, "INTERSECTS", "CLIP", "BOUNDS", 37.73315644825698, -122.44054287672043, 37.73349585185455, -122.44008690118788}, {"0"}, }) } diff --git a/tests/tests_test.go b/tests/tests_test.go index f342df1b..6624fa64 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -57,7 +57,9 @@ func runSubTest(t *testing.T, name string, mc *mockServer, test func(t *testing. } func runStep(t *testing.T, mc *mockServer, name string, step func(mc *mockServer) error) { + t.Helper() t.Run(name, func(t *testing.T) { + t.Helper() if err := func() error { // reset the current server mc.ResetConn() diff --git a/vendor/github.com/tidwall/geojson/circle.go b/vendor/github.com/tidwall/geojson/circle.go index a27dd8d1..127dfe24 100644 --- a/vendor/github.com/tidwall/geojson/circle.go +++ b/vendor/github.com/tidwall/geojson/circle.go @@ -53,6 +53,11 @@ func (g *Circle) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *Circle) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + // String ... func (g *Circle) String() string { return string(g.AppendJSON(nil)) diff --git a/vendor/github.com/tidwall/geojson/collection.go b/vendor/github.com/tidwall/geojson/collection.go index 9ddd89e4..0edd0493 100644 --- a/vendor/github.com/tidwall/geojson/collection.go +++ b/vendor/github.com/tidwall/geojson/collection.go @@ -90,6 +90,11 @@ func (g *collection) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *collection) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + // String ... func (g *collection) String() string { return string(g.AppendJSON(nil)) diff --git a/vendor/github.com/tidwall/geojson/feature.go b/vendor/github.com/tidwall/geojson/feature.go index 62b9b7ac..da5e405f 100644 --- a/vendor/github.com/tidwall/geojson/feature.go +++ b/vendor/github.com/tidwall/geojson/feature.go @@ -77,7 +77,7 @@ func (g *Feature) Members() string { func (g *Feature) AppendJSON(dst []byte) []byte { dst = append(dst, `{"type":"Feature","geometry":`...) dst = g.base.AppendJSON(dst) - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, true) dst = append(dst, '}') return dst @@ -93,6 +93,11 @@ func (g *Feature) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *Feature) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + // Spatial ... func (g *Feature) Spatial() Spatial { return g diff --git a/vendor/github.com/tidwall/geojson/feature_test.go b/vendor/github.com/tidwall/geojson/feature_test.go index 5ba83c42..f851b28b 100644 --- a/vendor/github.com/tidwall/geojson/feature_test.go +++ b/vendor/github.com/tidwall/geojson/feature_test.go @@ -1,28 +1,32 @@ package geojson -import "testing" +import ( + "testing" + + "github.com/tidwall/gjson" +) func TestFeatureParse(t *testing.T) { - p := expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]}}`, nil) + p := expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"properties":{}}`, nil) expect(t, p.Center() == P(1, 2)) expectJSON(t, `{"type":"Feature"}`, errGeometryMissing) expectJSON(t, `{"type":"Feature","geometry":null}`, errDataInvalid) - expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":null}`, nil) - expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":[4,true]}`, nil) + expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":null,"properties":{}}`, nil) + expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":[4,true],"properties":{}}`, nil) expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":"15","properties":{"a":"b"}}`, nil) - expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":[1,2,3,4]}`, nil) - expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2],"bbox":[1,2,3,4]},"id":[4,true]}`, nil) + expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":[1,2,3,4],"properties":{}}`, nil) + expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2],"bbox":[1,2,3,4]},"id":[4,true],"properties":{}}`, nil) } func TestFeatureVarious(t *testing.T) { - var g = expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]}}`, nil) - expect(t, string(g.AppendJSON(nil)) == `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]}}`) + var g = expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"properties":{}}`, nil) + expect(t, string(g.AppendJSON(nil)) == `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"properties":{}}`) expect(t, g.Rect() == R(1, 2, 1, 2)) expect(t, g.Center() == P(1, 2)) expect(t, !g.Empty()) g = expectJSONOpts(t, - `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":[1,2,3,4]}`, + `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":[1,2,3,4],"properties":{}}`, nil, nil) expect(t, !g.Empty()) expect(t, g.Rect() == R(1, 2, 1, 2)) @@ -30,10 +34,44 @@ func TestFeatureVarious(t *testing.T) { } -// func TestFeaturePoly(t *testing.T) { -// p := expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]}}`, nil) -// expect(t, p.Intersects(PO(1, 2))) -// expect(t, p.Contains(PO(1, 2))) -// expect(t, p.Within(PO(1, 2))) +func TestFeatureProperties(t *testing.T) { + obj, err := Parse(`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]}}`, nil) + if err != nil { + t.Fatal(err) + } + json := obj.JSON() + if !gjson.Valid(json) { + t.Fatal("invalid json") + } + if !gjson.Get(json, "properties").Exists() { + t.Fatal("expected 'properties' member") + } -// } + obj, err = Parse(`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":true}`, nil) + if err != nil { + t.Fatal(err) + } + json = obj.JSON() + if !gjson.Valid(json) { + t.Fatal("invalid json") + } + if gjson.Get(json, "properties").Type != gjson.True { + t.Fatal("expected 'properties' member to be 'true'") + } + + obj, err = Parse(`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":{}}`, nil) + if err != nil { + t.Fatal(err) + } + json = obj.JSON() + if !gjson.Valid(json) { + t.Fatal("invalid json") + } + if !gjson.Get(json, "properties").Exists() { + t.Fatal("expected 'properties' member") + } + if gjson.Get(json, "id").String() != "{}" { + t.Fatal("expected 'id' member") + } + +} diff --git a/vendor/github.com/tidwall/geojson/featurecollection.go b/vendor/github.com/tidwall/geojson/featurecollection.go index 87ce6a87..43e05872 100644 --- a/vendor/github.com/tidwall/geojson/featurecollection.go +++ b/vendor/github.com/tidwall/geojson/featurecollection.go @@ -28,7 +28,7 @@ func (g *FeatureCollection) AppendJSON(dst []byte) []byte { } dst = append(dst, ']') if g.extra != nil { - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, false) } dst = append(dst, '}') strings.Index("", " ") @@ -45,6 +45,11 @@ func (g *FeatureCollection) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *FeatureCollection) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + func parseJSONFeatureCollection( keys *parseKeys, opts *ParseOptions, ) (Object, error) { diff --git a/vendor/github.com/tidwall/geojson/geometrycollection.go b/vendor/github.com/tidwall/geojson/geometrycollection.go index 1d2cec49..2cb64045 100644 --- a/vendor/github.com/tidwall/geojson/geometrycollection.go +++ b/vendor/github.com/tidwall/geojson/geometrycollection.go @@ -28,7 +28,7 @@ func (g *GeometryCollection) AppendJSON(dst []byte) []byte { } dst = append(dst, ']') if g.extra != nil { - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, false) } dst = append(dst, '}') strings.Index("", " ") @@ -45,6 +45,11 @@ func (g *GeometryCollection) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *GeometryCollection) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + func parseJSONGeometryCollection( keys *parseKeys, opts *ParseOptions, ) (Object, error) { diff --git a/vendor/github.com/tidwall/geojson/linestring.go b/vendor/github.com/tidwall/geojson/linestring.go index a94101f6..3047bff9 100644 --- a/vendor/github.com/tidwall/geojson/linestring.go +++ b/vendor/github.com/tidwall/geojson/linestring.go @@ -46,7 +46,7 @@ func (g *LineString) AppendJSON(dst []byte) []byte { dst = append(dst, `{"type":"LineString","coordinates":`...) dst, _ = appendJSONSeries(dst, &g.base, g.extra, 0) if g.extra != nil { - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, false) } dst = append(dst, '}') return dst @@ -62,6 +62,11 @@ func (g *LineString) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *LineString) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + // Spatial ... func (g *LineString) Spatial() Spatial { return g diff --git a/vendor/github.com/tidwall/geojson/multilinestring.go b/vendor/github.com/tidwall/geojson/multilinestring.go index d780ca59..65506e27 100644 --- a/vendor/github.com/tidwall/geojson/multilinestring.go +++ b/vendor/github.com/tidwall/geojson/multilinestring.go @@ -30,7 +30,7 @@ func (g *MultiLineString) AppendJSON(dst []byte) []byte { } dst = append(dst, ']') if g.extra != nil { - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, false) } dst = append(dst, '}') return dst @@ -58,6 +58,11 @@ func (g *MultiLineString) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *MultiLineString) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + func parseJSONMultiLineString( keys *parseKeys, opts *ParseOptions, ) (Object, error) { diff --git a/vendor/github.com/tidwall/geojson/multipoint.go b/vendor/github.com/tidwall/geojson/multipoint.go index d0d299df..a0e2d72a 100644 --- a/vendor/github.com/tidwall/geojson/multipoint.go +++ b/vendor/github.com/tidwall/geojson/multipoint.go @@ -30,7 +30,7 @@ func (g *MultiPoint) AppendJSON(dst []byte) []byte { } dst = append(dst, ']') if g.extra != nil { - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, false) } dst = append(dst, '}') return dst @@ -46,6 +46,11 @@ func (g *MultiPoint) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *MultiPoint) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + func parseJSONMultiPoint(keys *parseKeys, opts *ParseOptions) (Object, error) { var g MultiPoint var err error diff --git a/vendor/github.com/tidwall/geojson/multipolygon.go b/vendor/github.com/tidwall/geojson/multipolygon.go index 023f689f..543b58eb 100644 --- a/vendor/github.com/tidwall/geojson/multipolygon.go +++ b/vendor/github.com/tidwall/geojson/multipolygon.go @@ -30,7 +30,7 @@ func (g *MultiPolygon) AppendJSON(dst []byte) []byte { } dst = append(dst, ']') if g.extra != nil { - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, false) } dst = append(dst, '}') return dst @@ -57,6 +57,11 @@ func (g *MultiPolygon) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *MultiPolygon) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + func parseJSONMultiPolygon( keys *parseKeys, opts *ParseOptions, ) (Object, error) { diff --git a/vendor/github.com/tidwall/geojson/object.go b/vendor/github.com/tidwall/geojson/object.go index 2b1c89c0..fe023f83 100644 --- a/vendor/github.com/tidwall/geojson/object.go +++ b/vendor/github.com/tidwall/geojson/object.go @@ -43,6 +43,7 @@ type Object interface { NumPoints() int ForEach(iter func(geom Object) bool) bool Spatial() Spatial + MarshalJSON() ([]byte, error) } var _ = []Object{ @@ -88,7 +89,7 @@ type ParseOptions struct { // IndexGeometryKind is the kind of index implementation. // Default is QuadTreeCompressed IndexGeometryKind geometry.IndexKind - RequireValid bool + RequireValid bool } // DefaultParseOptions ... @@ -96,7 +97,7 @@ var DefaultParseOptions = &ParseOptions{ IndexChildren: 64, IndexGeometry: 64, IndexGeometryKind: geometry.QuadTree, - RequireValid: false, + RequireValid: false, } // Parse a GeoJSON object @@ -244,10 +245,17 @@ func appendJSONPoint(dst []byte, point geometry.Point, ex *extra, idx int) []byt return dst } -func (ex *extra) appendJSONExtra(dst []byte) []byte { +func (ex *extra) appendJSONExtra(dst []byte, propertiesRequired bool) []byte { if ex != nil && ex.members != "" { dst = append(dst, ',') dst = append(dst, ex.members[1:len(ex.members)-1]...) + if propertiesRequired { + if !gjson.Get(ex.members, "properties").Exists() { + dst = append(dst, `,"properties":{}`...) + } + } + } else if propertiesRequired { + dst = append(dst, `,"properties":{}`...) } return dst diff --git a/vendor/github.com/tidwall/geojson/object_test.go b/vendor/github.com/tidwall/geojson/object_test.go index b73880af..355cffae 100644 --- a/vendor/github.com/tidwall/geojson/object_test.go +++ b/vendor/github.com/tidwall/geojson/object_test.go @@ -101,9 +101,9 @@ func expect(t testing.TB, what bool) { } if !what { if t == nil { - panic("exception failure") + panic("expectation failure") } else { - t.Fatal("expection failure") + t.Fatal("expectation failure") } } } diff --git a/vendor/github.com/tidwall/geojson/point.go b/vendor/github.com/tidwall/geojson/point.go index e18a4fe8..fa955404 100644 --- a/vendor/github.com/tidwall/geojson/point.go +++ b/vendor/github.com/tidwall/geojson/point.go @@ -63,7 +63,7 @@ func (g *Point) Base() geometry.Point { func (g *Point) AppendJSON(dst []byte) []byte { dst = append(dst, `{"type":"Point","coordinates":`...) dst = appendJSONPoint(dst, g.base, g.extra, 0) - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, false) dst = append(dst, '}') return dst } @@ -73,6 +73,11 @@ func (g *Point) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *Point) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + // String ... func (g *Point) String() string { return string(g.AppendJSON(nil)) diff --git a/vendor/github.com/tidwall/geojson/polygon.go b/vendor/github.com/tidwall/geojson/polygon.go index f235eb5f..d5ab34dd 100644 --- a/vendor/github.com/tidwall/geojson/polygon.go +++ b/vendor/github.com/tidwall/geojson/polygon.go @@ -52,7 +52,7 @@ func (g *Polygon) AppendJSON(dst []byte) []byte { } dst = append(dst, ']') if g.extra != nil { - dst = g.extra.appendJSONExtra(dst) + dst = g.extra.appendJSONExtra(dst, false) } dst = append(dst, '}') return dst @@ -63,6 +63,11 @@ func (g *Polygon) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *Polygon) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + // String ... func (g *Polygon) String() string { return string(g.AppendJSON(nil)) diff --git a/vendor/github.com/tidwall/geojson/rect.go b/vendor/github.com/tidwall/geojson/rect.go index 4b2f42ad..19dd43d9 100644 --- a/vendor/github.com/tidwall/geojson/rect.go +++ b/vendor/github.com/tidwall/geojson/rect.go @@ -56,6 +56,11 @@ func (g *Rect) JSON() string { return string(g.AppendJSON(nil)) } +// MarshalJSON ... +func (g *Rect) MarshalJSON() ([]byte, error) { + return g.AppendJSON(nil), nil +} + // String ... func (g *Rect) String() string { return string(g.AppendJSON(nil))