Skip to content

Commit

Permalink
Merge pull request #14 from manicar2093/fix/support-custom-types-on-u…
Browse files Browse the repository at this point in the history
…nmarshal

fix: add support custom types on unmarshal
  • Loading branch information
manicar2093 authored Jun 13, 2024
2 parents 2b3fdef + 4962eee commit 91d8ba5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bump_version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ jobs:
with:
distribution: goreleaser
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ4
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
Expand All @@ -35,6 +36,7 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
7 changes: 5 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ func (c *Optional[T]) UnmarshalJSON(data []byte) error {
asString = ""
}
c.isValidValue = getIsValidDataBool(asString)
if err := json.Unmarshal(data, &c.value); err != nil {
return err
if c.isValidValue {
if err := json.Unmarshal(data, &c.value); err != nil {
return err
}
}

return nil
}

Expand Down
12 changes: 12 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goption_test
import (
"encoding/json"
"fmt"
"time"

"github.com/manicar2093/goption"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -48,6 +49,17 @@ var _ = Describe("Json", func() {
Entry("native null", []byte(`null`)),
)

It("handles custom types", func() {
type testType time.Time
var (
holder = goption.Empty[testType]()
)
err := holder.UnmarshalJSON([]byte(`"null"`))

Expect(err).ToNot(HaveOccurred())
Expect(holder.IsPresent()).To(BeFalse())
})

})

When("is zero", func() {
Expand Down

0 comments on commit 91d8ba5

Please sign in to comment.