Skip to content

Commit dfaabc7

Browse files
authored
Merge pull request #3 from gojuno/fix-scan
Geometry can be scanned from string
2 parents 8792ee8 + bdd7536 commit dfaabc7

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: go
22

33
go:
4-
- 1.2
5-
- 1.3
4+
- 1.7
5+
- tip
66

77
after_script:
88
- FIXED=$(go vet ./... | wc -l); if [ $FIXED -gt 0 ]; then echo "go vet - $FIXED issues(s), please fix." && exit 2; fi

geometry.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,14 @@ func (g *Geometry) UnmarshalJSON(data []byte) error {
159159
// The columns must be received as GeoJSON Geometry.
160160
// When using PostGIS a spatial column would need to be wrapped in ST_AsGeoJSON.
161161
func (g *Geometry) Scan(value interface{}) error {
162-
data, ok := value.([]byte)
163-
if !ok {
162+
var data []byte
163+
164+
switch value.(type) {
165+
case string:
166+
data = []byte(value.(string))
167+
case []byte:
168+
data = value.([]byte)
169+
default:
164170
return errors.New("unable to parse this type into geojson")
165171
}
166172

geometry_test.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,24 +263,47 @@ func TestUnmarshalGeometryCollection(t *testing.T) {
263263
}
264264
}
265265

266-
func TestGeometryScan(t *testing.T) {
266+
func TestGeometryScanFail(t *testing.T) {
267267
g := &Geometry{}
268268

269269
err := g.Scan(123)
270270
if err == nil {
271271
t.Errorf("should return error if not the correct data type")
272272
}
273+
}
273274

274-
err = g.Scan([]byte(`{"type":"Point","coordinates":[-93.787988,32.392335]}`))
275-
if err != nil {
276-
t.Fatalf("should parse without error, got %v", err)
275+
func TestGeometryScan(t *testing.T) {
276+
cases := []struct {
277+
name string
278+
value interface{}
279+
}{
280+
{
281+
name: "Scan from bytes",
282+
value: []byte(`{"type":"Point","coordinates":[-93.787988,32.392335]}`),
283+
},
284+
{
285+
name: "Scan from string",
286+
value: `{"type":"Point","coordinates":[-93.787988,32.392335]}`,
287+
},
277288
}
278289

279-
if !g.IsPoint() {
280-
t.Errorf("should be point, but got %v", g)
281-
}
290+
for _, tc := range cases {
291+
t.Run(tc.name, func(t *testing.T) {
292+
g := &Geometry{}
293+
294+
err := g.Scan(tc.value)
295+
if err != nil {
296+
t.Fatalf("should parse without error, got %v", err)
297+
}
298+
299+
if !g.IsPoint() {
300+
t.Errorf("should be point, but got %v", g)
301+
}
282302

283-
if g.Point[0] != -93.787988 || g.Point[1] != 32.392335 {
284-
t.Errorf("incorrect point data, got %v", g.Point)
303+
if g.Point[0] != -93.787988 || g.Point[1] != 32.392335 {
304+
t.Errorf("incorrect point data, got %v", g.Point)
305+
}
306+
})
285307
}
308+
286309
}

0 commit comments

Comments
 (0)