Jelajahi Sumber

marshal: fix is nullable check (#959)

When we read null from the wire it is encoded as -1 which gets returned
from the frame as []byte(nil), this is distinct from the framer reading
a length 0 byte which is returned as []byte{}, these both have 0 length
but one is nil the other is empty.

Fix marshalling to correctly handle nil and empty values.
Chris Bannister 8 tahun lalu
induk
melakukan
b581ab491d
2 mengubah file dengan 13 tambahan dan 3 penghapusan
  1. 1 1
      marshal.go
  2. 12 2
      marshal_test.go

+ 1 - 1
marshal.go

@@ -177,7 +177,7 @@ func isNullableValue(value interface{}) bool {
 }
 }
 
 
 func isNullData(info TypeInfo, data []byte) bool {
 func isNullData(info TypeInfo, data []byte) bool {
-	return len(data) == 0
+	return data == nil
 }
 }
 
 
 func unmarshalNullable(info TypeInfo, data []byte, value interface{}) error {
 func unmarshalNullable(info TypeInfo, data []byte, value interface{}) error {

+ 12 - 2
marshal_test.go

@@ -543,7 +543,7 @@ var marshalTests = []struct {
 	},
 	},
 	{
 	{
 		NativeType{proto: 2, typ: TypeVarchar},
 		NativeType{proto: 2, typ: TypeVarchar},
-		[]byte{},
+		[]byte(nil),
 		(*string)(nil),
 		(*string)(nil),
 		nil,
 		nil,
 		nil,
 		nil,
@@ -574,7 +574,7 @@ var marshalTests = []struct {
 	},
 	},
 	{
 	{
 		NativeType{proto: 2, typ: TypeTimeUUID},
 		NativeType{proto: 2, typ: TypeTimeUUID},
-		[]byte{},
+		[]byte(nil),
 		(*UUID)(nil),
 		(*UUID)(nil),
 		nil,
 		nil,
 		nil,
 		nil,
@@ -858,6 +858,16 @@ var marshalTests = []struct {
 		nil,
 		nil,
 		nil,
 		nil,
 	},
 	},
+	{
+		NativeType{proto: 2, typ: TypeVarchar},
+		[]byte{},
+		func() interface{} {
+			var s string
+			return &s
+		}(),
+		nil,
+		nil,
+	},
 }
 }
 
 
 func decimalize(s string) *inf.Dec {
 func decimalize(s string) *inf.Dec {