Skip to content

Commit 7426e34

Browse files
committed
encoding/ewkb: add ValuePrefixSRID
1 parent 3aa9785 commit 7426e34

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

encoding/ewkb/scanner.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,32 @@ func (v value) Value() (driver.Value, error) {
146146
}
147147
return val, err
148148
}
149+
150+
type valuePrefixSRID struct {
151+
srid int
152+
v orb.Geometry
153+
}
154+
155+
// ValuePrefixSRID will create a driver.Valuer that will WKB the geometry
156+
// but add the srid as a 4 byte prefix.
157+
//
158+
// db.Exec("INSERT INTO table (point_column) VALUES (?)", ewkb.Value(p, 4326))
159+
func ValuePrefixSRID(g orb.Geometry, srid int) driver.Valuer {
160+
return valuePrefixSRID{srid: srid, v: g}
161+
162+
}
163+
164+
func (v valuePrefixSRID) Value() (driver.Value, error) {
165+
val, err := Marshal(v.v, 0)
166+
if val == nil {
167+
return nil, err
168+
}
169+
170+
if err != nil {
171+
return nil, err
172+
}
173+
174+
data := make([]byte, 4, 4+len(val))
175+
binary.LittleEndian.PutUint32(data, uint32(v.srid))
176+
return append(data, val...), nil
177+
}

encoding/ewkb/scanner_test.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,12 @@ func TestScanPoint_errors(t *testing.T) {
374374
}
375375
}
376376

377-
func TestPrefixScannerPrefixSRID(t *testing.T) {
377+
func TestScannerPrefixSRID(t *testing.T) {
378378
cases := []struct {
379379
name string
380380
data []byte
381381
srid int
382-
expected orb.Point
382+
expected orb.Geometry
383383
}{
384384
{
385385
name: "point",
@@ -391,16 +391,15 @@ func TestPrefixScannerPrefixSRID(t *testing.T) {
391391

392392
for _, tc := range cases {
393393
t.Run(tc.name, func(t *testing.T) {
394-
var p orb.Point
395-
s := ScannerPrefixSRID(&p)
394+
s := ScannerPrefixSRID(nil)
396395
err := s.Scan(tc.data)
397396
if err != nil {
398397
t.Fatalf("scan error: %v", err)
399398
}
400399

401-
if !p.Equal(tc.expected) {
400+
if !orb.Equal(s.Geometry, tc.expected) {
402401
t.Errorf("unequal data")
403-
t.Log(p)
402+
t.Log(s.Geometry)
404403
t.Log(tc.expected)
405404
}
406405

@@ -411,6 +410,38 @@ func TestPrefixScannerPrefixSRID(t *testing.T) {
411410
}
412411
}
413412

413+
func TestValuePrefixSRID(t *testing.T) {
414+
cases := []struct {
415+
name string
416+
geom orb.Geometry
417+
srid int
418+
expected []byte
419+
}{
420+
{
421+
name: "point",
422+
geom: orb.Point{4, 5},
423+
srid: 4326,
424+
expected: append([]byte{230, 16, 0, 0}, MustMarshal(orb.Point{4, 5}, 0)...),
425+
},
426+
}
427+
428+
for _, tc := range cases {
429+
t.Run(tc.name, func(t *testing.T) {
430+
v := ValuePrefixSRID(tc.geom, tc.srid)
431+
data, err := v.Value()
432+
if err != nil {
433+
t.Fatalf("value error: %v", err)
434+
}
435+
436+
if !bytes.Equal(data.([]byte), tc.expected) {
437+
t.Errorf("unequal data")
438+
t.Log(data)
439+
t.Log(tc.expected)
440+
}
441+
})
442+
}
443+
}
444+
414445
func TestScanMultiPoint(t *testing.T) {
415446
cases := []struct {
416447
name string

0 commit comments

Comments
 (0)