Browse Source

use struct tags for udt types

Chris Bannister 10 years ago
parent
commit
3ea51ab4bf
2 changed files with 32 additions and 5 deletions
  1. 30 3
      marshal.go
  2. 2 2
      udt_test.go

+ 30 - 3
marshal.go

@@ -13,7 +13,6 @@ import (
 	"net"
 	"reflect"
 	"strconv"
-	"strings"
 	"time"
 
 	"speter.net/go/exp/math/dec/inf"
@@ -1283,9 +1282,23 @@ func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
 		return nil, marshalErrorf("cannot marshal %T into %s", value, info)
 	}
 
+	fields := make(map[string]reflect.Value)
+	t := reflect.TypeOf(value)
+	for i := 0; i < t.NumField(); i++ {
+		sf := t.Field(i)
+
+		if tag := sf.Tag.Get("cql"); tag != "" {
+			fields[tag] = k.Field(i)
+		}
+	}
+
 	var buf []byte
 	for _, e := range udt.Elements {
-		f := k.FieldByName(strings.Title(e.Name))
+		f, ok := fields[e.Name]
+		if !ok {
+			f = k.FieldByName(e.Name)
+		}
+
 		if !f.IsValid() {
 			return nil, marshalErrorf("cannot marshal %T into %s", value, info)
 		} else if f.Kind() == reflect.Ptr {
@@ -1342,6 +1355,16 @@ func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
 		return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
 	}
 
+	fields := make(map[string]reflect.Value)
+	t := k.Type()
+	for i := 0; i < t.NumField(); i++ {
+		sf := t.Field(i)
+
+		if tag := sf.Tag.Get("cql"); tag != "" {
+			fields[tag] = k.Field(i)
+		}
+	}
+
 	udt := info.(UDTTypeInfo)
 
 	for _, e := range udt.Elements {
@@ -1350,7 +1373,11 @@ func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
 
 		var err error
 		if size >= 0 {
-			f := k.FieldByName(strings.Title(e.Name))
+			f, ok := fields[e.Name]
+			if !ok {
+				f = k.FieldByName(e.Name)
+			}
+
 			if !f.IsValid() || !f.CanAddr() {
 				return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
 			}

+ 2 - 2
udt_test.go

@@ -113,8 +113,8 @@ func TestUDT_Reflect(t *testing.T) {
 	}
 
 	type horse struct {
-		Name  string
-		Owner string
+		Name  string `cql:"name"`
+		Owner string `cql:"owner"`
 	}
 
 	insertedHorse := &horse{