Skip to content

Commit

Permalink
Merge pull request #2 from nrfta/feat/fall-back-date
Browse files Browse the repository at this point in the history
Add datetime fallback to date Unmarshal
  • Loading branch information
josemarluedke authored Sep 15, 2022
2 parents 5bf7d92 + 8d80433 commit bc65921
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 75 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- main
- 'v*'
- "v*"
pull_request: {}

jobs:
Expand All @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
go-version: [1.12.x, 1.13.x]
go-version: [1.18.x, 1.19.x]
os: [ubuntu-latest, macos-latest, windows-latest]

env:
Expand Down
13 changes: 12 additions & 1 deletion date.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ func MarshalDate(t time.Time) graphql.Marshaler {

func UnmarshalDate(v interface{}) (time.Time, error) {
if tmpStr, ok := v.(string); ok {
return time.Parse("2006-01-02", tmpStr)
result, err := time.Parse("2006-01-02", tmpStr)
if err != nil {
result2, err2 := time.Parse(time.RFC3339, tmpStr)

if err2 != nil {
return time.Time{}, err
}

return time.Parse("2006-01-02", result2.Format("2006-01-02"))
}

return result, nil
}
return time.Time{}, errors.InvalidArgument.New("date should be YYYY-MM-DD formatted string")
}
5 changes: 4 additions & 1 deletion date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/nrfta/go-graphql-scalars"
scalars "github.com/nrfta/go-graphql-scalars"
)

type sampleTime struct {
Expand All @@ -27,6 +27,9 @@ var _ = Describe("Unmarshal Date Test", func() {
_, err := scalars.UnmarshalDate(wrongTime)
Expect(err).ToNot(BeNil())
})
It("handles ios 8601 as input", func() {
Expect(scalars.UnmarshalDate("2006-01-02T15:04:05-07:00")).Should(BeAssignableToTypeOf(time.Time{}))
})
})

var (
Expand Down
24 changes: 21 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
module github.com/nrfta/go-graphql-scalars

go 1.13
go 1.18

require (
github.com/99designs/gqlgen v0.11.3
github.com/99designs/gqlgen v0.17.17
github.com/neighborly/go-errors v0.2.0
github.com/onsi/ginkgo v1.12.2
github.com/onsi/gomega v1.10.1
github.com/volatiletech/null/v8 v8.1.0
github.com/volatiletech/null/v8 v8.1.2
)

require (
github.com/friendsofgo/errors v0.9.2 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gofrs/uuid v4.3.0+incompatible // indirect
github.com/nxadm/tail v1.4.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/vektah/gqlparser/v2 v2.5.1 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
github.com/volatiletech/randomize v0.0.1 // indirect
github.com/volatiletech/strmangle v0.0.4 // indirect
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit bc65921

Please sign in to comment.