-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "newtime" directive to use official messagepack time format (#378)
This adds `msgp:newtime` file directive that will encode all time fields using the -1 extension as defined in the [(revised) messagepack spec](https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type) ReadTime/ReadTimeBytes will now support both types natively, and will accept either as input. Extensions should remain unaffected. Fixes #300
- Loading branch information
Showing
15 changed files
with
446 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package _generated | ||
|
||
import "time" | ||
|
||
//go:generate msgp -v | ||
|
||
//msgp:newtime | ||
|
||
type NewTime struct { | ||
T time.Time | ||
Array []time.Time | ||
Map map[string]time.Time | ||
} | ||
|
||
func (t1 NewTime) Equal(t2 NewTime) bool { | ||
if !t1.T.Equal(t2.T) { | ||
return false | ||
} | ||
if len(t1.Array) != len(t2.Array) { | ||
return false | ||
} | ||
for i := range t1.Array { | ||
if !t1.Array[i].Equal(t2.Array[i]) { | ||
return false | ||
} | ||
} | ||
if len(t1.Map) != len(t2.Map) { | ||
return false | ||
} | ||
for k, v := range t1.Map { | ||
if !t2.Map[k].Equal(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package _generated | ||
|
||
import ( | ||
"bytes" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/tinylib/msgp/msgp" | ||
) | ||
|
||
func TestNewTime(t *testing.T) { | ||
value := NewTime{ | ||
T: time.Now().UTC(), | ||
Array: []time.Time{time.Now().UTC(), time.Now().UTC()}, | ||
Map: map[string]time.Time{ | ||
"a": time.Now().UTC(), | ||
}, | ||
} | ||
encoded, err := value.MarshalMsg(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
checkExtMinusOne(t, encoded) | ||
var got NewTime | ||
_, err = got.UnmarshalMsg(encoded) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !value.Equal(got) { | ||
t.Errorf("UnmarshalMsg got %v want %v", value, got) | ||
} | ||
|
||
var buf bytes.Buffer | ||
w := msgp.NewWriter(&buf) | ||
err = value.EncodeMsg(w) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
w.Flush() | ||
checkExtMinusOne(t, buf.Bytes()) | ||
|
||
got = NewTime{} | ||
r := msgp.NewReader(&buf) | ||
err = got.DecodeMsg(r) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !value.Equal(got) { | ||
t.Errorf("DecodeMsg got %v want %v", value, got) | ||
} | ||
} | ||
|
||
func checkExtMinusOne(t *testing.T, b []byte) { | ||
r := msgp.NewReader(bytes.NewBuffer(b)) | ||
_, err := r.ReadMapHeader() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
key, err := r.ReadMapKey(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for !bytes.Equal(key, []byte("T")) { | ||
key, err = r.ReadMapKey(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
n, _, err := r.ReadExtensionRaw() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != -1 { | ||
t.Fatalf("got %v want -1", n) | ||
} | ||
t.Log("Was -1 extension") | ||
} | ||
|
||
func TestNewTimeRandom(t *testing.T) { | ||
rng := rand.New(rand.NewSource(0)) | ||
runs := int(1e6) | ||
if testing.Short() { | ||
runs = 1e4 | ||
} | ||
for i := 0; i < runs; i++ { | ||
nanos := rng.Int63n(999999999 + 1) | ||
secs := rng.Uint64() | ||
// Tweak the distribution, so we get more than average number of | ||
// length 4 and 8 timestamps. | ||
if rng.Intn(5) == 0 { | ||
secs %= uint64(time.Now().Unix()) | ||
if rng.Intn(2) == 0 { | ||
nanos = 0 | ||
} | ||
} | ||
|
||
value := NewTime{ | ||
T: time.Unix(int64(secs), nanos), | ||
} | ||
encoded, err := value.MarshalMsg(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var got NewTime | ||
_, err = got.UnmarshalMsg(encoded) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !value.Equal(got) { | ||
t.Fatalf("UnmarshalMsg got %v want %v", value, got) | ||
} | ||
var buf bytes.Buffer | ||
w := msgp.NewWriter(&buf) | ||
err = value.EncodeMsg(w) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
w.Flush() | ||
got = NewTime{} | ||
r := msgp.NewReader(&buf) | ||
err = got.DecodeMsg(r) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !value.Equal(got) { | ||
t.Fatalf("DecodeMsg got %v want %v", value, got) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.