Преглед на файлове

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 преди 12 години
родител
ревизия
9fadbef9b9
променени са 2 файла, в които са добавени 17 реда и са изтрити 2 реда
  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)
 		return unmarshalMap(info, data, value)
 	case TypeTimeUUID:
 	case TypeTimeUUID:
 		return unmarshalTimeUUID(info, data, value)
 		return unmarshalTimeUUID(info, data, value)
+	case TypeUUID:
+		return unmarshalUUID(info, data, value)
 	case TypeInet:
 	case TypeInet:
 		return unmarshalInet(info, data, value)
 		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)
 	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) {
 	switch v := value.(type) {
 	case Unmarshaler:
 	case Unmarshaler:
 		return v.UnmarshalCQL(info, data)
 		return v.UnmarshalCQL(info, data)
 	case *uuid.UUID:
 	case *uuid.UUID:
 		*v = uuid.FromBytes(data)
 		*v = uuid.FromBytes(data)
 		return nil
 		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:
 	case *time.Time:
 		id := uuid.FromBytes(data)
 		id := uuid.FromBytes(data)
 		if id.Version() != 1 {
 		if id.Version() != 1 {
@@ -926,8 +934,9 @@ func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
 		}
 		}
 		*v = id.Time()
 		*v = id.Time()
 		return nil
 		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 {
 func unmarshalInet(info *TypeInfo, data []byte, value interface{}) error {

+ 6 - 0
marshal_test.go

@@ -7,6 +7,7 @@ import (
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
+	"tux21b.org/v1/gocql/uuid"
 )
 )
 
 
 var marshalTests = []struct {
 var marshalTests = []struct {
@@ -49,6 +50,11 @@ var marshalTests = []struct {
 		[]byte(nil),
 		[]byte(nil),
 		[]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},
 		&TypeInfo{Type: TypeInt},
 		[]byte("\x00\x00\x00\x00"),
 		[]byte("\x00\x00\x00\x00"),