Skip to content

Commit

Permalink
Add support for encoding time.Duration (#19)
Browse files Browse the repository at this point in the history
Manually cherry-picked changes from upstream
repo https://github.com/tinylib/msgp the following commits:
77976d5b6d, 5467d2f6f6, 618077c952
66ba8975dc, fd0c04e0f5, 222902d7cb
  • Loading branch information
iansuvak authored Oct 12, 2022
1 parent d06a11d commit 26c81f3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
20 changes: 14 additions & 6 deletions gen/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -841,7 +846,8 @@ func (s *BaseElem) ZeroExpr() string {
Int8,
Int16,
Int32,
Int64:
Int64,
Duration:
return "0"
case Bool:
return "false"
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions msgp/read_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions msgp/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions msgp/write_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 26c81f3

Please sign in to comment.