Przeglądaj źródła

Merge pull request #884 from jqin/master

Fix and test for unmarshal date type
Chris Bannister 8 lat temu
rodzic
commit
ec709fa434
2 zmienionych plików z 21 dodań i 0 usunięć
  1. 6 0
      marshal.go
  2. 15 0
      marshal_test.go

+ 6 - 0
marshal.go

@@ -1164,7 +1164,13 @@ func marshalDate(info TypeInfo, value interface{}) ([]byte, error) {
 
 func unmarshalDate(info TypeInfo, data []byte, value interface{}) error {
 	switch v := value.(type) {
+	case Unmarshaler:
+		return v.UnmarshalCQL(info, data)
 	case *time.Time:
+		if len(data) == 0 {
+			*v = time.Time{}
+			return nil
+		}
 		var origin uint32 = 1 << 31
 		var current uint32 = binary.BigEndian.Uint32(data)
 		timestamp := (int64(current) - int64(origin)) * 86400000

+ 15 - 0
marshal_test.go

@@ -1092,3 +1092,18 @@ func TestUnmarshalInetCopyBytes(t *testing.T) {
 		t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
 	}
 }
+
+func TestUnmarshalDate(t *testing.T) {
+	data := []uint8{0x80, 0x0, 0x43, 0x31}
+	var date time.Time
+	if err := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &date); err != nil {
+		t.Fatal(err)
+	}
+
+	expectedDate := "2017-02-04"
+	formattedDate := date.Format("2006-01-02")
+	if expectedDate != formattedDate {
+		t.Errorf("marshalTest: expected %x (%v), got %x (%v)", expectedDate, formattedDate)
+		return
+	}
+}