Repository: go-openapi/strfmt
Pull request #59 (2019-10-23 08:37:36)

Methods for fulfil methods from encoding and gob packages
Add methods for Date and DateTime to fulfil interfaces:
- gob.GobEncoder
- gob.GobDecoder
- encoding.BinaryMarshaler
- encoding.BinaryUnmarshaler

During struct serialisation, by using core package "encoding/gob", in case Date or DateTime are used as types for struct fields, error is thrown as result: "type strfmt.Date has no exported fields".

This commits adds those method, by reusing existing ones from time.Time struct.

codecov[bot]: # [Codecov](https://codecov.io/gh/go-openapi/strfmt/pull/59?src=pr&el=h1) Report
> Merging [#59](https://codecov.io/gh/go-openapi/strfmt/pull/59?src=pr&el=desc) into [master](https://codecov.io/gh/go-openapi/strfmt/commit/6faa644e1cdafc07f7b38eb06c1af5f92128f289?src=pr&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `84.61%`.

[![Impacted file tree graph](https://codecov.io/gh/go-openapi/strfmt/pull/59/graphs/tree.svg?width=650&token=1NsRXHf1sd&height=150&src=pr)](https://codecov.io/gh/go-openapi/strfmt/pull/59?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master     #59      +/-   ##
=========================================
+ Coverage   83.17%   83.2%   +0.02%     
=========================================
  Files          10      10              
  Lines        1599    1625      +26     
=========================================
+ Hits         1330    1352      +22     
- Misses        198     200       +2     
- Partials       71      73       +2
```


| [Impacted Files](https://codecov.io/gh/go-openapi/strfmt/pull/59?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [date.go](https://codecov.io/gh/go-openapi/strfmt/pull/59/diff?src=pr&el=tree#diff-ZGF0ZS5nbw==) | `88.88% <84.61%> (-0.82%)` | :arrow_down: |
| [time.go](https://codecov.io/gh/go-openapi/strfmt/pull/59/diff?src=pr&el=tree#diff-dGltZS5nbw==) | `93% <84.61%> (-1.26%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/go-openapi/strfmt/pull/59?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-openapi/strfmt/pull/59?src=pr&el=footer). Last update [6faa644...ff8d330](https://codecov.io/gh/go-openapi/strfmt/pull/59?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/date.go b/date.go
index 2959e65..15029ff 100644
--- a/date.go
+++ b/date.go
@@ -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
+}
diff --git a/time.go b/time.go
index f915125..ee00987 100644
--- a/time.go
+++ b/time.go
@@ -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
+}


Test patch:
diff --git a/date_test.go b/date_test.go
index 6f44ef9..4097e8a 100644
--- a/date_test.go
+++ b/date_test.go
@@ -15,8 +15,10 @@
 package strfmt
 
 import (
+	"bytes"
 	"database/sql"
 	"database/sql/driver"
+	"encoding/gob"
 	"testing"
 	"time"
 
@@ -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())
+}
diff --git a/time_test.go b/time_test.go
index ef453c4..d3012f8 100644
--- a/time_test.go
+++ b/time_test.go
@@ -16,6 +16,7 @@ package strfmt
 
 import (
 	"bytes"
+	"encoding/gob"
 	"testing"
 	"time"
 
@@ -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())
+}