Skip to content

Commit

Permalink
add custom NullTime. upgrade go -> 1.18 (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: alexey.sergey <alexey.sergey@softswiss.com>
  • Loading branch information
asechiho and alexey.sergey authored May 10, 2022
1 parent e4acc2f commit 4017925
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mikekonan/go-types

go 1.14
go 1.18

require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
Expand Down
5 changes: 5 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,11 @@ components:
example: 2006-01-02
type: string
x-go-type: github.com/mikekonan/go-types/time.Date
NullTime:
example: 2020-12-08T16:38:09.70516+03:00
type: string
nullable: true
x-go-type: github.com/mikekonan/go-types/time.Time
Timezone:
example: Europe/Minsk
type: string
Expand Down
5 changes: 0 additions & 5 deletions time/date.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package time

import (
"database/sql/driver"
"encoding/json"
"encoding/xml"
"gopkg.in/guregu/null.v4"
Expand Down Expand Up @@ -48,10 +47,6 @@ func (d Date) MarshalXML(decoder *xml.Encoder, element xml.StartElement) error {
return decoder.EncodeElement(d.format(), element)
}

func (d Date) Value() (value driver.Value, err error) {
return null.Time(d).Value()
}

func (d Date) String() string {
var str = d.format()
if str != nil {
Expand Down
42 changes: 15 additions & 27 deletions time/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,18 @@ var unmarshalTestCases = []unmarshalType{
}

func TestUnmarshalJSON(t *testing.T) {
testUnmarshal(t,
func(bytes []byte, d *dt) error {
return json.Unmarshal(bytes, d)
}, func(u unmarshalType) []byte {
return u.testJSON
})
testUnmarshal(t, json.Unmarshal, func(u unmarshalType) []byte {
return u.testJSON
})
}

func TestUnmarshalXML(t *testing.T) {
testUnmarshal(t,
func(bytes []byte, d *dt) error {
return xml.Unmarshal(bytes, d)
}, func(u unmarshalType) []byte {
return u.testXML
})
testUnmarshal(t, xml.Unmarshal, func(u unmarshalType) []byte {
return u.testXML
})
}

func testUnmarshal(t *testing.T, unmarshalFunc func([]byte, *dt) error, valueFunc func(unmarshalType) []byte) {
func testUnmarshal(t *testing.T, unmarshalFunc func([]byte, any) error, valueFunc func(unmarshalType) []byte) {
for _, testCase := range unmarshalTestCases {
var d dt
actualErr := unmarshalFunc(valueFunc(testCase), &d)
Expand Down Expand Up @@ -114,31 +108,25 @@ var marshalTestCases = []marshalType{
}

func TestMarshalJSON(t *testing.T) {
testMarshal(t, "json",
func(d dt) ([]byte, error) {
return json.Marshal(d)
}, func(m marshalType) []byte {
return m.expectedJSON
})
testMarshal(t, "json", json.Marshal, func(m marshalType) []byte {
return m.expectedJSON
})
}

func TestMarshalXML(t *testing.T) {
testMarshal(t, "xml",
func(d dt) ([]byte, error) {
return xml.Marshal(d)
}, func(m marshalType) []byte {
return m.expectedXML
})
testMarshal(t, "xml", xml.Marshal, func(m marshalType) []byte {
return m.expectedXML
})
}

func testMarshal(t *testing.T, tp string, marshalFunc func(dt) ([]byte, error), valueFunc func(marshalType) []byte) {
func testMarshal(t *testing.T, tp string, marshalFunc func(any) ([]byte, error), valueFunc func(marshalType) []byte) {
for _, testCase := range marshalTestCases {
actual, actualErr := marshalFunc(testCase.testData)
if actualErr != nil {
t.Errorf(`%s`, actualErr)
}
if string(actual) != string(valueFunc(testCase)) {
t.Errorf("Actual %s does not equal expected. Actual: '%s', expected: '%s'", tp, actual, testCase.expectedXML)
t.Errorf("Actual %s does not equal expected. Actual: '%s', expected: '%s'", tp, actual, valueFunc(testCase))
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions time/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ components:
example: 2006-01-02
type: string
x-go-type: github.com/mikekonan/go-types/time.Date
NullTime:
example: "2020-12-08T16:38:09.70516+03:00"
type: string
nullable: true
x-go-type: github.com/mikekonan/go-types/time.Time
77 changes: 77 additions & 0 deletions time/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package time

import (
"encoding/json"
"encoding/xml"
"fmt"
"gopkg.in/guregu/null.v4"
"time"
)

type Time null.Time

func NullTime(t time.Time) Time {
return Time(null.TimeFrom(t))
}

func (t *Time) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}

return t.parseTimeFromString(str)
}

func (t Time) MarshalJSON() ([]byte, error) {
return json.Marshal(t.format())
}

func (t *Time) UnmarshalXML(decoder *xml.Decoder, element xml.StartElement) error {
var str string
err := decoder.DecodeElement(&str, &element)
if err != nil {
return err
}

return t.parseTimeFromString(str)
}

func (t Time) MarshalXML(decoder *xml.Encoder, element xml.StartElement) error {
return decoder.EncodeElement(t.format(), element)
}

func (t Time) String() string {
var str = t.format()
if str != nil {
return *str
}
return ""
}

func (t Time) format() *string {
if t.Valid {
var str = t.Time.Format(time.RFC3339)
return &str
}

return nil
}

func (t *Time) parseTimeFromString(value string) (err error) {
if value == "" {
return nil
}

var newTime time.Time
if newTime, err = time.Parse(time.RFC3339, value); err != nil {
if errTime, ok := err.(*time.ParseError); ok {
return fmt.Errorf("cannot parse time '%s' invalid format '%s'", errTime.Value, errTime.Layout)
}
return err
}

*t = NullTime(newTime)
return nil
}
Loading

0 comments on commit 4017925

Please sign in to comment.