From e58b6870c3691067fe21562626e6958a4d3bf293 Mon Sep 17 00:00:00 2001 From: Dave Gardner Date: Thu, 8 Aug 2013 10:46:24 +0100 Subject: [PATCH 1/2] Adding support for marshaling/unmarshaling zero time, by storing as empty bytes --- src/gossie/types.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/gossie/types.go b/src/gossie/types.go index bffca9c..45ad14c 100644 --- a/src/gossie/types.go +++ b/src/gossie/types.go @@ -229,7 +229,12 @@ func marshalInt(value int64, size int, typeDesc TypeDesc) ([]byte, error) { } func marshalTime(value time.Time, typeDesc TypeDesc) ([]byte, error) { + if value.IsZero() { + return []byte{}, nil + } + switch typeDesc { + // following Java conventions Cassandra standarizes this as millis case LongType, BytesType, DateType: valueI := value.UnixNano() / 1e6 @@ -421,6 +426,12 @@ func unmarshalInt64(b []byte, typeDesc TypeDesc, value *int64) error { } func unmarshalTime(b []byte, typeDesc TypeDesc, value *time.Time) error { + + if len(b) == 0 { + *value = *new(time.Time) + return nil + } + switch typeDesc { case LongType, BytesType, DateType: if len(b) != 8 { From f3e3a890ad73bf5ca17db51afc7ca3782d9acd29 Mon Sep 17 00:00:00 2001 From: Dave Gardner Date: Thu, 8 Aug 2013 11:00:11 +0100 Subject: [PATCH 2/2] Adding a test for zero time --- src/gossie/types_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/gossie/types_test.go b/src/gossie/types_test.go index 2a48f90..4578afd 100644 --- a/src/gossie/types_test.go +++ b/src/gossie/types_test.go @@ -374,3 +374,22 @@ func TestMarshalTime(t *testing.T) { errorMarshal(t, v, BooleanType) errorMarshal(t, v, DoubleType) } + +func TestMarshalUnmarshalZeroTime(t *testing.T) { + zeroT := *new(time.Time) + b, err := marshalTime(zeroT, LongType) + if err != nil { + t.Fatalf("Failed to marshal time: %v", err) + } + // now get back to where we were + unmarshaledT := new(time.Time) + err = unmarshalTime(b, LongType, unmarshaledT) + if err != nil { + t.Fatalf("Failed to unmarshal time: %v", err) + } + + // must still be zero time + if !unmarshaledT.IsZero() { + t.Fatalf("Unmarshaled time is not zero time: %#v", unmarshaledT) + } +}