|
|
@@ -317,6 +317,159 @@ var marshalTests = []struct {
|
|
|
},
|
|
|
}
|
|
|
|
|
|
+var marshalNilTests = []struct {
|
|
|
+ Info *TypeInfo
|
|
|
+ Data []byte
|
|
|
+ Value interface{}
|
|
|
+}{
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeInt},
|
|
|
+ []byte(nil),
|
|
|
+ nil,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeVarchar},
|
|
|
+ []byte("nullable string"),
|
|
|
+ func() *string {
|
|
|
+ value := "nullable string"
|
|
|
+ return &value
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeVarchar},
|
|
|
+ []byte{},
|
|
|
+ (*string)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeInt},
|
|
|
+ []byte("\x7f\xff\xff\xff"),
|
|
|
+ func() *int {
|
|
|
+ var value int = math.MaxInt32
|
|
|
+ return &value
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeInt},
|
|
|
+ []byte(nil),
|
|
|
+ (*int)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeTimeUUID},
|
|
|
+ []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
|
|
|
+ &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeTimeUUID},
|
|
|
+ []byte{},
|
|
|
+ (*UUID)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeTimestamp},
|
|
|
+ []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
|
|
|
+ func() *time.Time {
|
|
|
+ t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
|
|
|
+ return &t
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeTimestamp},
|
|
|
+ []byte(nil),
|
|
|
+ (*time.Time)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeBoolean},
|
|
|
+ []byte("\x00"),
|
|
|
+ func() *bool {
|
|
|
+ b := false
|
|
|
+ return &b
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeBoolean},
|
|
|
+ []byte("\x01"),
|
|
|
+ func() *bool {
|
|
|
+ b := true
|
|
|
+ return &b
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeBoolean},
|
|
|
+ []byte(nil),
|
|
|
+ (*bool)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeFloat},
|
|
|
+ []byte("\x40\x49\x0f\xdb"),
|
|
|
+ func() *float32 {
|
|
|
+ f := float32(3.14159265)
|
|
|
+ return &f
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeFloat},
|
|
|
+ []byte(nil),
|
|
|
+ (*float32)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeDouble},
|
|
|
+ []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
|
|
|
+ func() *float64 {
|
|
|
+ d := float64(3.14159265)
|
|
|
+ return &d
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeDouble},
|
|
|
+ []byte(nil),
|
|
|
+ (*float64)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeInet},
|
|
|
+ []byte("\x7F\x00\x00\x01"),
|
|
|
+ func() *net.IP {
|
|
|
+ ip := net.ParseIP("127.0.0.1").To4()
|
|
|
+ return &ip
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeInet},
|
|
|
+ []byte(nil),
|
|
|
+ (*net.IP)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
|
|
|
+ []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
|
|
|
+ func() *[]int {
|
|
|
+ l := []int{1, 2}
|
|
|
+ return &l
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
|
|
|
+ []byte(nil),
|
|
|
+ (*[]int)(nil),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeMap,
|
|
|
+ Key: &TypeInfo{Type: TypeVarchar},
|
|
|
+ Elem: &TypeInfo{Type: TypeInt},
|
|
|
+ },
|
|
|
+ []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
|
|
|
+ func() *map[string]int {
|
|
|
+ m := map[string]int{"foo": 1}
|
|
|
+ return &m
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ &TypeInfo{Type: TypeMap,
|
|
|
+ Key: &TypeInfo{Type: TypeVarchar},
|
|
|
+ Elem: &TypeInfo{Type: TypeInt},
|
|
|
+ },
|
|
|
+ []byte(nil),
|
|
|
+ (*map[string]int)(nil),
|
|
|
+ },
|
|
|
+}
|
|
|
+
|
|
|
func decimalize(s string) *inf.Dec {
|
|
|
i, _ := new(inf.Dec).SetString(s)
|
|
|
return i
|
|
|
@@ -335,18 +488,21 @@ func TestMarshal(t *testing.T) {
|
|
|
continue
|
|
|
}
|
|
|
if !bytes.Equal(data, test.Data) {
|
|
|
- t.Errorf("marshalTest[%d]: expected %q, got %q.", i, test.Data, data)
|
|
|
+ t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestMarshalNil(t *testing.T) {
|
|
|
- data, err := Marshal(&TypeInfo{Type: TypeInt}, nil)
|
|
|
- if err != nil {
|
|
|
- t.Errorf("failed to marshal nil with err: %v", err)
|
|
|
- }
|
|
|
- if data != nil {
|
|
|
- t.Errorf("expected nil, got %v", data)
|
|
|
+ for i, test := range marshalNilTests {
|
|
|
+ data, err := Marshal(test.Info, test.Value)
|
|
|
+ if err != nil {
|
|
|
+ t.Errorf("marshalNilTest[%d]: %v", i, err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if !bytes.Equal(data, test.Data) {
|
|
|
+ t.Errorf("marshalNilTest[%d]: expected %q, got %q.(%#v)", i, test.Data, data, test.Value)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|