Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a helper for roundtripping time.time objects #40

Merged
merged 1 commit into from
Aug 14, 2020
Merged
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
1 change: 1 addition & 0 deletions testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func main() {
types.SimpleTypeTwo{},
types.DeferredContainer{},
types.FixedArrays{},
types.ThingWithSomeTime{},
); err != nil {
panic(err)
}
Expand Down
108 changes: 108 additions & 0 deletions testing/cbor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions testing/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"testing"
"testing/quick"
"time"

"github.com/google/go-cmp/cmp"
cbg "github.com/whyrusleeping/cbor-gen"
Expand Down Expand Up @@ -115,3 +116,34 @@ func TestFixedArrays(t *testing.T) {
recepticle := &FixedArrays{}
testValueRoundtrip(t, zero, recepticle)
}

func TestTimeIsh(t *testing.T) {
val := &ThingWithSomeTime{
When: cbg.CborTime(time.Now()),
Stuff: 1234,
CatName: "hank",
}

buf := new(bytes.Buffer)
if err := val.MarshalCBOR(buf); err != nil {
t.Fatal(err)
}

out := ThingWithSomeTime{}
if err := out.UnmarshalCBOR(buf); err != nil {
t.Fatal(err)
}

if out.When.Time().UnixNano() != val.When.Time().UnixNano() {
t.Fatal("time didnt round trip properly", out.When.Time(), val.When.Time())
}

if out.Stuff != val.Stuff {
t.Fatal("no")
}

if out.CatName != val.CatName {
t.Fatal("no")
}

}
6 changes: 6 additions & 0 deletions testing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ type FixedArrays struct {
Uint64 [20]uint64
}

type ThingWithSomeTime struct {
When cbg.CborTime
Stuff int64
CatName string
}

// Do not add fields to this type.
type NeedScratchForMap struct {
Thing bool
Expand Down
48 changes: 48 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"math"
"time"

cid "github.com/ipfs/go-cid"
)
Expand Down Expand Up @@ -691,3 +692,50 @@ func (ci *CborInt) UnmarshalCBOR(r io.Reader) error {
*ci = CborInt(extraI)
return nil
}

type CborTime time.Time

func (ct *CborTime) MarshalCBOR(w io.Writer) error {
b, err := (*time.Time)(ct).MarshalBinary()
if err != nil {
return err
}

if err := CborWriteHeader(w, MajByteString, uint64(len(b))); err != nil {
return err
}

if _, err := w.Write(b); err != nil {
return err
}

return nil
}

func (ct *CborTime) UnmarshalCBOR(r io.Reader) error {
t, l, err := CborReadHeader(r)
if err != nil {
return err
}

if t != MajByteString {
return fmt.Errorf("CborTime expects to find a byte array (got %d)", t)
}

buf := make([]byte, l)
if _, err := io.ReadFull(r, buf); err != nil {
return err
}

tm := time.Time{}
if err := tm.UnmarshalBinary(buf); err != nil {
return err
}

*ct = (CborTime)(tm)
return nil
}

func (ct CborTime) Time() time.Time {
return (time.Time)(ct)
}