From 90adc1bf48313c80a02550887a17ce0a5cfab315 Mon Sep 17 00:00:00 2001 From: Manuel Carbajal Date: Wed, 12 Jun 2024 17:28:42 -0600 Subject: [PATCH 1/3] chore: add idea folder to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cde0123..57d3d6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist/ +.idea From 603d29dbbf1ce85728e4c5a03e15fec84bd3cb02 Mon Sep 17 00:00:00 2001 From: Manuel Carbajal Date: Wed, 12 Jun 2024 17:29:07 -0600 Subject: [PATCH 2/3] fix: add native null to be handle in a proper way --- json.go | 19 +++++++++++++------ json_test.go | 11 +++++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/json.go b/json.go index a0629a7..a3595dc 100644 --- a/json.go +++ b/json.go @@ -4,17 +4,24 @@ import ( "encoding/json" "reflect" "strconv" + "strings" ) func (c *Optional[T]) UnmarshalJSON(data []byte) error { - unquoted, err := strconv.Unquote(string(data)) - if err != nil { - return err + var ( + err error + asString = string(data) + ) + if strings.HasPrefix(asString, "\"") { + asString, err = strconv.Unquote(asString) + if err != nil { + return err + } } - if unquoted == "null" { - unquoted = "" + if asString == "null" { + asString = "" } - c.isValidValue = getIsValidDataBool(unquoted) + c.isValidValue = getIsValidDataBool(asString) if err := json.Unmarshal(data, &c.value); err != nil { return err } diff --git a/json_test.go b/json_test.go index 2c4c497..a994f23 100644 --- a/json_test.go +++ b/json_test.go @@ -35,16 +35,19 @@ var _ = Describe("Json", func() { }) When("is as null", func() { - It("creates an empty optional", func() { + DescribeTable("creates an empty optional", func(jsonData []byte) { var ( - jsonData = []byte(`"null"`) - holder = goption.Empty[string]() + holder = goption.Empty[string]() ) err := holder.UnmarshalJSON(jsonData) Expect(err).ToNot(HaveOccurred()) Expect(holder.IsPresent()).To(BeFalse()) - }) + }, + Entry("null as string", []byte(`"null"`)), + Entry("native null", []byte(`null`)), + ) + }) When("is zero", func() { From dd7948923ed4ddf530f9f7e186928f49274ef8e4 Mon Sep 17 00:00:00 2001 From: Manuel Carbajal Date: Wed, 12 Jun 2024 17:29:25 -0600 Subject: [PATCH 3/3] chore: change var name --- echo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/echo.go b/echo.go index a3f43ca..ea7b34b 100644 --- a/echo.go +++ b/echo.go @@ -4,7 +4,7 @@ import ( "fmt" ) -func (cb *Optional[T]) UnmarshalText(text []byte) error { - return cb.UnmarshalJSON([]byte(fmt.Sprintf("\"%s\"", text))) +func (c *Optional[T]) UnmarshalText(text []byte) error { + return c.UnmarshalJSON([]byte(fmt.Sprintf("\"%s\"", text))) }