Переглянути джерело

Merge pull request #396 from probkiizokna/fix_marshaling_pointer_to_interface

Fixed marshaling of pointer to interface.
Chris Bannister 10 роки тому
батько
коміт
f5e3c561e4
2 змінених файлів з 17 додано та 4 видалено
  1. 4 4
      marshal.go
  2. 13 0
      marshal_test.go

+ 4 - 4
marshal.go

@@ -50,10 +50,6 @@ func Marshal(info TypeInfo, value interface{}) ([]byte, error) {
 		panic("protocol version not set")
 	}
 
-	if v, ok := value.(Marshaler); ok {
-		return v.MarshalCQL(info)
-	}
-
 	if valueRef := reflect.ValueOf(value); valueRef.Kind() == reflect.Ptr {
 		if valueRef.IsNil() {
 			return nil, nil
@@ -62,6 +58,10 @@ func Marshal(info TypeInfo, value interface{}) ([]byte, error) {
 		}
 	}
 
+	if v, ok := value.(Marshaler); ok {
+		return v.MarshalCQL(info)
+	}
+
 	switch info.Type() {
 	case TypeVarchar, TypeAscii, TypeBlob:
 		return marshalVarchar(info, value)

+ 13 - 0
marshal_test.go

@@ -527,6 +527,19 @@ var marshalTests = []struct {
 		[]byte(nil),
 		(*map[string]int)(nil),
 	},
+	{
+		NativeType{ proto: 2, typ: TypeVarchar},
+		[]byte("HELLO WORLD"),
+		func() *CustomString {
+			customString := CustomString("hello world")
+			return &customString
+		}(),
+	},
+	{
+		NativeType{ proto: 2, typ: TypeVarchar},
+		[]byte(nil),
+		(*CustomString)(nil),
+	},
 }
 
 func decimalize(s string) *inf.Dec {