diff --git a/gen/elem.go b/gen/elem.go index 565e694..2a339ed 100644 --- a/gen/elem.go +++ b/gen/elem.go @@ -91,10 +91,11 @@ const ( Int32 Int64 Bool - Intf // interface{} - Time // time.Time - Ext // extension - Error // error + Intf // interface{} + Time // time.Time + Ext // extension + Error // error + Duration // time.Duration IDENT // IDENT means an unrecognized identifier ) @@ -126,6 +127,7 @@ var primitives = map[string]Primitive{ "time.Time": Time, "msgp.Extension": Ext, "error": Error, + "time.Duration": Duration, } // types built into the library @@ -757,11 +759,14 @@ func (s *BaseElem) FromBase() string { // BaseName returns the string form of the // base type (e.g. Float64, Ident, etc) func (s *BaseElem) BaseName() string { - // time is a special case; + // time and duration are special cases; // we strip the package prefix if s.Value == Time { return "Time" } + if s.Value == Duration { + return "Duration" + } return s.Value.String() } @@ -841,7 +846,8 @@ func (s *BaseElem) ZeroExpr() string { Int8, Int16, Int32, - Int64: + Int64, + Duration: return "0" case Bool: return "false" @@ -933,6 +939,8 @@ func (k Primitive) String() string { return "Intf" case Time: return "time.Time" + case Duration: + return "time.Duration" case Ext: return "Extension" case IDENT: diff --git a/msgp/read_bytes.go b/msgp/read_bytes.go index bc1467c..1d6a308 100644 --- a/msgp/read_bytes.go +++ b/msgp/read_bytes.go @@ -387,6 +387,16 @@ func ReadBoolBytes(b []byte) (bool, []byte, error) { } } +// ReadDurationBytes tries to read a time.Duration +// from 'b' and return the value and the remaining bytes. +// Possible errors: +// - ErrShortBytes (too few bytes) +// - TypeError (not a int) +func ReadDurationBytes(b []byte) (d time.Duration, o []byte, err error) { + i, o, err := ReadInt64Bytes(b) + return time.Duration(i), o, err +} + // ReadInt64Bytes tries to read an int64 // from 'b' and return the value and the remaining bytes. // Possible errors: diff --git a/msgp/size.go b/msgp/size.go index ce2f8b1..e3a613b 100644 --- a/msgp/size.go +++ b/msgp/size.go @@ -25,9 +25,10 @@ const ( Complex64Size = 10 Complex128Size = 18 - TimeSize = 15 - BoolSize = 1 - NilSize = 1 + DurationSize = Int64Size + TimeSize = 15 + BoolSize = 1 + NilSize = 1 MapHeaderSize = 5 ArrayHeaderSize = 5 diff --git a/msgp/write_bytes.go b/msgp/write_bytes.go index 24bc20c..b96715c 100644 --- a/msgp/write_bytes.go +++ b/msgp/write_bytes.go @@ -75,6 +75,11 @@ func AppendFloat32(b []byte, f float32) []byte { return o } +// AppendDuration appends a time.Duration to the slice +func AppendDuration(b []byte, d time.Duration) []byte { + return AppendInt64(b, int64(d)) +} + // AppendInt64 appends an int64 to the slice func AppendInt64(b []byte, i int64) []byte { if i >= 0 {