Skip to content
This repository has been archived by the owner on Jun 12, 2020. It is now read-only.

Zero time marshal/unmarshal #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/gossie/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions src/gossie/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}