Skip to content

Commit

Permalink
Add UnmarshalYAML function to SerializableDate
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Dionisi committed Nov 26, 2024
1 parent 099dcf2 commit 1e7e5bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/sanity-io/litter v1.5.5
github.com/spf13/cobra v1.8.0
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 changes: 21 additions & 10 deletions pkg/types/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strings"
"time"

"gopkg.in/yaml.v3"
)

var ErrDateNotJSONString = errors.New("cannot parse non-string value as a date")
Expand All @@ -13,12 +15,24 @@ type SerializableDate struct {
time.Time
}

func (date SerializableDate) MarshalJSON() ([]byte, error) {
return []byte("\"" + date.Format(time.DateOnly) + "\""), nil
func (d *SerializableDate) MarshalJSON() ([]byte, error) {
return []byte("\"" + d.Format(time.DateOnly) + "\""), nil
}

func (d *SerializableDate) parse(date string) error {
parsedDate, err := time.Parse(time.DateOnly, date)
if err != nil {
return fmt.Errorf("unable to parse date from JSON: %w", err)
}

d.Time = parsedDate

return nil
}

func (date *SerializableDate) UnmarshalJSON(data []byte) error {
func (d *SerializableDate) UnmarshalJSON(data []byte) error {
stringifiedData := string(data)

if stringifiedData == "null" {
return nil
}
Expand All @@ -29,12 +43,9 @@ func (date *SerializableDate) UnmarshalJSON(data []byte) error {

dataWithoutQuotes := stringifiedData[1 : len(stringifiedData)-1]

parsedDate, err := time.Parse(time.DateOnly, dataWithoutQuotes)
if err != nil {
return fmt.Errorf("unable to parse date from JSON: %w", err)
}

date.Time = parsedDate
return d.parse(dataWithoutQuotes)
}

return nil
func (d *SerializableDate) UnmarshalYAML(value *yaml.Node) error {
return d.parse(value.Value)
}

0 comments on commit 1e7e5bb

Please sign in to comment.