Skip to content
This repository was archived by the owner on Jul 12, 2025. It is now read-only.

Commit fcee893

Browse files
AmineChikhaouijackc
authored andcommitted
ltree: fix DecodeBinary by correctly checking the 1st byte
Start adding tests
1 parent 0f1512e commit fcee893

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

ltree.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pgtype
22

33
import (
44
"database/sql/driver"
5+
"fmt"
56
)
67

78
type Ltree Text
@@ -42,7 +43,20 @@ func (dst *Ltree) DecodeText(ci *ConnInfo, src []byte) error {
4243
}
4344

4445
func (dst *Ltree) DecodeBinary(ci *ConnInfo, src []byte) error {
45-
return (*Text)(dst).DecodeBinary(ci, src)
46+
if src == nil {
47+
*dst = Ltree{Status: Null}
48+
return nil
49+
}
50+
51+
// Get Ltree version, only 1 is allowed
52+
version := src[0]
53+
if version != 1 {
54+
return fmt.Errorf("unsupported ltree version %d", version)
55+
}
56+
57+
ltreeStr := string(src[1:])
58+
*dst = Ltree{String: ltreeStr, Status: Present}
59+
return nil
4660
}
4761

4862
func (Ltree) PreferredParamFormat() int16 {

ltree_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package pgtype_test
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/jackc/pgtype"
8+
"github.com/jackc/pgtype/testutil"
9+
)
10+
11+
func TestLtreeTranscode(t *testing.T) {
12+
values := []interface{}{
13+
&pgtype.Ltree{String: "", Status: pgtype.Present},
14+
&pgtype.Ltree{String: "All.foo.one", Status: pgtype.Present},
15+
&pgtype.Ltree{Status: pgtype.Null},
16+
}
17+
18+
testutil.TestSuccessfulTranscodeEqFunc(
19+
t, "ltree", values, func(ai, bi interface{}) bool {
20+
a := ai.(pgtype.Ltree)
21+
b := bi.(pgtype.Ltree)
22+
23+
if a.String != b.String || a.Status != b.Status {
24+
return false
25+
}
26+
return true
27+
},
28+
)
29+
30+
}
31+
32+
func TestLtreeSet(t *testing.T) {
33+
successfulTests := []struct {
34+
src interface{}
35+
result pgtype.Ltree
36+
}{
37+
{src: "All.foo.bar", result: pgtype.Ltree{String: "All.foo.bar", Status: pgtype.Present}},
38+
{src: (*string)(nil), result: pgtype.Ltree{Status: pgtype.Null}},
39+
}
40+
for i, tt := range successfulTests {
41+
var dst pgtype.Ltree
42+
err := dst.Set(tt.src)
43+
if err != nil {
44+
t.Errorf("%d: %v", i, err)
45+
}
46+
if !reflect.DeepEqual(dst, tt.result) {
47+
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.src, tt.result, dst)
48+
}
49+
}
50+
}

pgtype.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ const (
8484
TstzrangeArrayOID = 3911
8585
Int8rangeOID = 3926
8686
Int8multirangeOID = 4536
87-
LtreeOID = 16407
8887
)
8988

9089
type Status byte
@@ -328,7 +327,6 @@ func NewConnInfo() *ConnInfo {
328327
ci.RegisterDataType(DataType{Value: &Varbit{}, Name: "varbit", OID: VarbitOID})
329328
ci.RegisterDataType(DataType{Value: &Varchar{}, Name: "varchar", OID: VarcharOID})
330329
ci.RegisterDataType(DataType{Value: &XID{}, Name: "xid", OID: XIDOID})
331-
ci.RegisterDataType(DataType{Value: &Ltree{}, Name: "ltree", OID: LtreeOID})
332330

333331
registerDefaultPgTypeVariants := func(name, arrayName string, value interface{}) {
334332
ci.RegisterDefaultPgType(value, name)

0 commit comments

Comments
 (0)