Explorar el Código

Support unmarshaling of "uuid" type

Allows unmarshaling of CQL "uuid" values in addition to the existing
"timeuuid" support. Only "timeuuid" may be unmarshaled to time.Time
values, however.

Also adds a test for this type.
Pete Hopkins hace 12 años
padre
commit
9fadbef9b9
Se han modificado 2 ficheros con 17 adiciones y 2 borrados
  1. 11 2
      marshal.go
  2. 6 0
      marshal_test.go

+ 11 - 2
marshal.go

@@ -86,6 +86,8 @@ func Unmarshal(info *TypeInfo, data []byte, value interface{}) error {
 		return unmarshalMap(info, data, value)
 	case TypeTimeUUID:
 		return unmarshalTimeUUID(info, data, value)
+	case TypeUUID:
+		return unmarshalUUID(info, data, value)
 	case TypeInet:
 		return unmarshalInet(info, data, value)
 	}
@@ -912,13 +914,19 @@ func marshalUUID(info *TypeInfo, value interface{}) ([]byte, error) {
 	return nil, marshalErrorf("can not marshal %T into %s", value, info)
 }
 
-func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
+func unmarshalUUID(info *TypeInfo, data []byte, value interface{}) error {
 	switch v := value.(type) {
 	case Unmarshaler:
 		return v.UnmarshalCQL(info, data)
 	case *uuid.UUID:
 		*v = uuid.FromBytes(data)
 		return nil
+	}
+	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
+}
+
+func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
+	switch v := value.(type) {
 	case *time.Time:
 		id := uuid.FromBytes(data)
 		if id.Version() != 1 {
@@ -926,8 +934,9 @@ func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
 		}
 		*v = id.Time()
 		return nil
+	default:
+		return unmarshalUUID(info, data, value)
 	}
-	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
 }
 
 func unmarshalInet(info *TypeInfo, data []byte, value interface{}) error {

+ 6 - 0
marshal_test.go

@@ -7,6 +7,7 @@ import (
 	"strings"
 	"testing"
 	"time"
+	"tux21b.org/v1/gocql/uuid"
 )
 
 var marshalTests = []struct {
@@ -49,6 +50,11 @@ var marshalTests = []struct {
 		[]byte(nil),
 		[]byte(nil),
 	},
+	{
+		&TypeInfo{Type: TypeTimeUUID},
+		[]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
+		uuid.FromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0}),
+	},
 	{
 		&TypeInfo{Type: TypeInt},
 		[]byte("\x00\x00\x00\x00"),