浏览代码

Fix bitshit order in udt length calculation

Nimi Wariboko Jr 10 年之前
父节点
当前提交
1bc2997a1e
共有 2 个文件被更改,包括 19 次插入13 次删除
  1. 9 9
      marshal.go
  2. 10 4
      udt_test.go

+ 9 - 9
marshal.go

@@ -1252,9 +1252,9 @@ func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
 			}
 
 			n := len(data)
-			buf = append(buf, byte(n<<24),
-				byte(n<<16),
-				byte(n<<8),
+			buf = append(buf, byte(n>>24),
+				byte(n>>16),
+				byte(n>>8),
 				byte(n))
 
 			buf = append(buf, data...)
@@ -1275,9 +1275,9 @@ func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
 			}
 
 			n := len(data)
-			buf = append(buf, byte(n<<24),
-				byte(n<<16),
-				byte(n<<8),
+			buf = append(buf, byte(n>>24),
+				byte(n>>16),
+				byte(n>>8),
 				byte(n))
 
 			buf = append(buf, data...)
@@ -1327,9 +1327,9 @@ func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
 		}
 
 		n := len(data)
-		buf = append(buf, byte(n<<24),
-			byte(n<<16),
-			byte(n<<8),
+		buf = append(buf, byte(n>>24),
+			byte(n>>16),
+			byte(n>>8),
 			byte(n))
 
 		buf = append(buf, data...)

+ 10 - 4
udt_test.go

@@ -8,8 +8,9 @@ import (
 )
 
 type position struct {
-	Lat int
-	Lon int
+	Lat     int    `cql:"lat"`
+	Lon     int    `cql:"lon"`
+	Padding string `json:"padding"`
 }
 
 // NOTE: due to current implementation details it is not currently possible to use
@@ -20,6 +21,8 @@ func (p position) MarshalUDT(name string, info TypeInfo) ([]byte, error) {
 		return Marshal(info, p.Lat)
 	case "lon":
 		return Marshal(info, p.Lon)
+	case "padding":
+		return Marshal(info, p.Padding)
 	default:
 		return nil, fmt.Errorf("unknown column for position: %q", name)
 	}
@@ -31,6 +34,8 @@ func (p *position) UnmarshalUDT(name string, info TypeInfo, data []byte) error {
 		return Unmarshal(info, data, &p.Lat)
 	case "lon":
 		return Unmarshal(info, data, &p.Lon)
+	case "padding":
+		return Unmarshal(info, data, &p.Padding)
 	default:
 		return fmt.Errorf("unknown column for position: %q", name)
 	}
@@ -46,7 +51,8 @@ func TestUDT_Marshaler(t *testing.T) {
 
 	err := createTable(session, `CREATE TYPE position(
 		lat int,
-		lon int);`)
+		lon int,
+		padding text);`)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -67,7 +73,7 @@ func TestUDT_Marshaler(t *testing.T) {
 		expLon = 2
 	)
 
-	err = session.Query("INSERT INTO houses(id, name, loc) VALUES(?, ?, ?)", 1, "test", &position{expLat, expLon}).Exec()
+	err = session.Query("INSERT INTO houses(id, name, loc) VALUES(?, ?, ?)", 1, "test", &position{expLat, expLon, fmt.Sprintf("%X", make([]byte, 300))}).Exec()
 	if err != nil {
 		t.Fatal(err)
 	}