Skip to content

Commit 16c6a8e

Browse files
authored
Merge pull request #5 from paulmach/id-interface
Feature.ID -> interface{}
2 parents ca31371 + 2adc13e commit 16c6a8e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

feature.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
// A Feature corresponds to GeoJSON feature object
88
type Feature struct {
9-
ID json.Number `json:"id,omitempty"`
9+
ID interface{} `json:"id,omitempty"`
1010
Type string `json:"type"`
1111
BoundingBox []float64 `json:"bbox,omitempty"`
1212
Geometry *Geometry `json:"geometry"`

feature_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ func TestUnmarshalFeature(t *testing.T) {
4747
}
4848
}
4949

50+
func TestMarshalFeatureID(t *testing.T) {
51+
f := &Feature{
52+
ID: "asdf",
53+
}
54+
55+
data, err := f.MarshalJSON()
56+
if err != nil {
57+
t.Fatalf("should marshal, %v", err)
58+
}
59+
60+
if !bytes.Equal(data, []byte(`{"id":"asdf","type":"Feature","geometry":null,"properties":null}`)) {
61+
t.Errorf("data not correct")
62+
t.Logf("%v", string(data))
63+
}
64+
65+
f.ID = 123
66+
data, err = f.MarshalJSON()
67+
if err != nil {
68+
t.Fatalf("should marshal, %v", err)
69+
70+
}
71+
72+
if !bytes.Equal(data, []byte(`{"id":123,"type":"Feature","geometry":null,"properties":null}`)) {
73+
t.Errorf("data not correct")
74+
t.Logf("%v", string(data))
75+
}
76+
}
77+
5078
func TestUnmarshalFeatureID(t *testing.T) {
5179
rawJSON := `
5280
{ "type": "Feature",
@@ -59,8 +87,8 @@ func TestUnmarshalFeatureID(t *testing.T) {
5987
t.Fatalf("should unmarshal feature without issue, err %v", err)
6088
}
6189

62-
if v, err := f.ID.Int64(); v != 123 {
63-
t.Errorf("should parse id as number, got %d %v", v, err)
90+
if v, ok := f.ID.(float64); !ok || v != 123 {
91+
t.Errorf("should parse id as number, got %T %f", f.ID, v)
6492
}
6593

6694
rawJSON = `
@@ -74,7 +102,7 @@ func TestUnmarshalFeatureID(t *testing.T) {
74102
t.Fatalf("should unmarshal feature without issue, err %v", err)
75103
}
76104

77-
if v := f.ID.String(); v != "abcd" {
78-
t.Errorf("should parse id as string, got %s", v)
105+
if v, ok := f.ID.(string); !ok || v != "abcd" {
106+
t.Errorf("should parse id as string, got %T %s", f.ID, v)
79107
}
80108
}

0 commit comments

Comments
 (0)