Skip to content

Commit

Permalink
Merge pull request #59 from Ompluscator/encoding-interfaces
Browse files Browse the repository at this point in the history
Methods for fulfil methods from encoding and gob packages
  • Loading branch information
casualjim authored Oct 23, 2019
2 parents 6faa644 + 10cbedc commit 489e930
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
29 changes: 29 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,32 @@ func (d *Date) DeepCopy() *Date {
d.DeepCopyInto(out)
return out
}

// GobEncode implements the gob.GobEncoder interface.
func (d Date) GobEncode() ([]byte, error) {
return d.MarshalBinary()
}

// GobDecode implements the gob.GobDecoder interface.
func (d *Date) GobDecode(data []byte) error {
return d.UnmarshalBinary(data)
}

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (d Date) MarshalBinary() ([]byte, error) {
return time.Time(d).MarshalBinary()
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (d *Date) UnmarshalBinary(data []byte) error {
var original time.Time

err := original.UnmarshalBinary(data)
if err != nil {
return err
}

*d = Date(original)

return nil
}
21 changes: 21 additions & 0 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package strfmt

import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/gob"
"testing"
"time"

Expand Down Expand Up @@ -139,3 +141,22 @@ func TestDeepCopyDate(t *testing.T) {
out3 := inNil.DeepCopy()
assert.Nil(t, out3)
}

func TestGobEncodingDate(t *testing.T) {
now := time.Now()

b := bytes.Buffer{}
enc := gob.NewEncoder(&b)
err := enc.Encode(Date(now))
assert.NoError(t, err)
assert.NotEmpty(t, b.Bytes())

var result Date

dec := gob.NewDecoder(&b)
err = dec.Decode(&result)
assert.NoError(t, err)
assert.Equal(t, now.Year(), time.Time(result).Year())
assert.Equal(t, now.Month(), time.Time(result).Month())
assert.Equal(t, now.Day(), time.Time(result).Day())
}
29 changes: 29 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,32 @@ func (t *DateTime) DeepCopy() *DateTime {
t.DeepCopyInto(out)
return out
}

// GobEncode implements the gob.GobEncoder interface.
func (t DateTime) GobEncode() ([]byte, error) {
return t.MarshalBinary()
}

// GobDecode implements the gob.GobDecoder interface.
func (t *DateTime) GobDecode(data []byte) error {
return t.UnmarshalBinary(data)
}

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (t DateTime) MarshalBinary() ([]byte, error) {
return time.Time(t).MarshalBinary()
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (t *DateTime) UnmarshalBinary(data []byte) error {
var original time.Time

err := original.UnmarshalBinary(data)
if err != nil {
return err
}

*t = DateTime(original)

return nil
}
23 changes: 23 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package strfmt

import (
"bytes"
"encoding/gob"
"testing"
"time"

Expand Down Expand Up @@ -252,3 +253,25 @@ func TestDeepCopyDateTime(t *testing.T) {
out3 := inNil.DeepCopy()
assert.Nil(t, out3)
}

func TestGobEncodingDateTime(t *testing.T) {
now := time.Now()

b := bytes.Buffer{}
enc := gob.NewEncoder(&b)
err := enc.Encode(DateTime(now))
assert.NoError(t, err)
assert.NotEmpty(t, b.Bytes())

var result DateTime

dec := gob.NewDecoder(&b)
err = dec.Decode(&result)
assert.NoError(t, err)
assert.Equal(t, now.Year(), time.Time(result).Year())
assert.Equal(t, now.Month(), time.Time(result).Month())
assert.Equal(t, now.Day(), time.Time(result).Day())
assert.Equal(t, now.Hour(), time.Time(result).Hour())
assert.Equal(t, now.Minute(), time.Time(result).Minute())
assert.Equal(t, now.Second(), time.Time(result).Second())
}

0 comments on commit 489e930

Please sign in to comment.