File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,9 @@ package wkb
3
3
import (
4
4
"database/sql"
5
5
"database/sql/driver"
6
+ "encoding/hex"
6
7
"errors"
8
+ "fmt"
7
9
8
10
"github.com/paulmach/orb"
9
11
)
@@ -93,6 +95,17 @@ func (s *GeometryScanner) Scan(d interface{}) error {
93
95
return nil
94
96
}
95
97
98
+ // go-pg will return ST_AsBinary(*) data as `\xhexencoded` which
99
+ // needs to be converted to true binary for further decoding.
100
+ // Code detects the \x prefix and then converts the rest from Hex to binary.
101
+ if len (data ) > 2 && data [0 ] == byte ('\\' ) && data [1 ] == byte ('x' ) {
102
+ n , err := hex .Decode (data , data [2 :])
103
+ if err != nil {
104
+ return fmt .Errorf ("thought the data was hex, but it is not: %v" , err )
105
+ }
106
+ data = data [:n ]
107
+ }
108
+
96
109
switch g := s .g .(type ) {
97
110
case nil :
98
111
m , err := Unmarshal (data )
Original file line number Diff line number Diff line change @@ -49,6 +49,38 @@ func TestScanNil(t *testing.T) {
49
49
})
50
50
}
51
51
52
+ func TestScanHexData (t * testing.T ) {
53
+ cases := []struct {
54
+ name string
55
+ data []byte
56
+ expected orb.Point
57
+ }{
58
+ {
59
+ name : "point" ,
60
+ data : []byte (`\x0101000000e0d57267266e4840b22ac24d46b50240` ),
61
+ expected : orb.Point {48.860547 , 2.338513 },
62
+ },
63
+ }
64
+
65
+ for _ , tc := range cases {
66
+ t .Run (tc .name , func (t * testing.T ) {
67
+ p := orb.Point {}
68
+ s := Scanner (& p )
69
+
70
+ err := s .Scan (tc .data )
71
+ if err != nil {
72
+ t .Fatalf ("scan error: %v" , err )
73
+ }
74
+
75
+ if ! p .Equal (tc .expected ) {
76
+ t .Errorf ("unequal data" )
77
+ t .Log (p )
78
+ t .Log (tc .expected )
79
+ }
80
+ })
81
+ }
82
+ }
83
+
52
84
func TestScanPoint (t * testing.T ) {
53
85
cases := []struct {
54
86
name string
You can’t perform that action at this time.
0 commit comments