소스 검색

rename UDTMarshaler/UDTUnmarshaler methods. Improve docs

Chris Bannister 10 년 전
부모
커밋
c3ed1a1366
2개의 변경된 파일28개의 추가작업 그리고 20개의 파일을 삭제
  1. 15 7
      marshal.go
  2. 13 13
      udt_test.go

+ 15 - 7
marshal.go

@@ -1203,13 +1203,22 @@ func unmarshalTuple(info TypeInfo, data []byte, value interface{}) error {
 }
 
 // UDTMarshaler is an interface which should be implemented by users wishing to
-// handle encoding UDT types to sent to Cassandra.
+// handle encoding UDT types to sent to Cassandra. Note: due to current implentations
+// methods defined for this interface must be value receivers not pointer receivers.
 type UDTMarshaler interface {
-	EncodeUDTField(name string, info TypeInfo) ([]byte, error)
+	// MarshalUDT will be called for each field in the the UDT returned by Cassandra,
+	// the implementor should marshal the type to return by for example calling
+	// Marshal.
+	MarshalUDT(name string, info TypeInfo) ([]byte, error)
 }
 
+// UDTUnmarshaler should be implemented by users wanting to implement custom
+// UDT unmarshaling.
 type UDTUnmarshaler interface {
-	DecodeUDTField(name string, info TypeInfo, data []byte) error
+	// UnmarshalUDT will be called for each field in the UDT return by Cassandra,
+	// the implementor should unmarshal the data into the value of their chosing,
+	// for example by calling Unmarshal.
+	UnmarshalUDT(name string, info TypeInfo, data []byte) error
 }
 
 func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
@@ -1219,7 +1228,7 @@ func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
 	case UDTMarshaler:
 		var buf []byte
 		for _, e := range udt.Elements {
-			data, err := v.EncodeUDTField(e.Name, e.Type)
+			data, err := v.MarshalUDT(e.Name, e.Type)
 			if err != nil {
 				return nil, err
 			}
@@ -1275,11 +1284,10 @@ func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
 
 			var err error
 			if size < 0 {
-				err = v.DecodeUDTField(e.Name, e.Type, nil)
+				err = v.UnmarshalUDT(e.Name, e.Type, nil)
 			} else {
-				err = v.DecodeUDTField(e.Name, e.Type, data[:size])
+				err = v.UnmarshalUDT(e.Name, e.Type, data[:size])
 				data = data[size:]
-
 			}
 
 			if err != nil {

+ 13 - 13
udt_test.go

@@ -8,35 +8,35 @@ import (
 )
 
 type position struct {
-	lat int
-	lon int
+	Lat int
+	Lon int
 }
 
 // NOTE: due to current implementation details it is not currently possible to use
 // a pointer receiver type for the UDTMarshaler interface to handle UDT's
-func (p position) EncodeUDTField(name string, info TypeInfo) ([]byte, error) {
+func (p position) MarshalUDT(name string, info TypeInfo) ([]byte, error) {
 	switch name {
 	case "lat":
-		return Marshal(info, p.lat)
+		return Marshal(info, p.Lat)
 	case "lon":
-		return Marshal(info, p.lon)
+		return Marshal(info, p.Lon)
 	default:
 		return nil, fmt.Errorf("unknown column for position: %q", name)
 	}
 }
 
-func (p *position) DecodeUDTField(name string, info TypeInfo, data []byte) error {
+func (p *position) UnmarshalUDT(name string, info TypeInfo, data []byte) error {
 	switch name {
 	case "lat":
-		return Unmarshal(info, data, &p.lat)
+		return Unmarshal(info, data, &p.Lat)
 	case "lon":
-		return Unmarshal(info, data, &p.lon)
+		return Unmarshal(info, data, &p.Lon)
 	default:
 		return fmt.Errorf("unknown column for position: %q", name)
 	}
 }
 
-func TestUDT(t *testing.T) {
+func TestUDT_Marshaler(t *testing.T) {
 	if *flagProto < protoVersion3 {
 		t.Skip("UDT are only available on protocol >= 3")
 	}
@@ -79,10 +79,10 @@ func TestUDT(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if pos.lat != expLat {
-		t.Errorf("expeceted lat to be be %d got %d", expLat, pos.lat)
+	if pos.Lat != expLat {
+		t.Errorf("expeceted lat to be be %d got %d", expLat, pos.Lat)
 	}
-	if pos.lon != expLon {
-		t.Errorf("expeceted lon to be be %d got %d", expLon, pos.lon)
+	if pos.Lon != expLon {
+		t.Errorf("expeceted lon to be be %d got %d", expLon, pos.Lon)
 	}
 }