From 910a7de33fc4d705a5e1997a088d2ad9394ce4ee Mon Sep 17 00:00:00 2001 From: Manuel Carbajal Date: Fri, 24 May 2024 14:39:13 -0600 Subject: [PATCH 1/3] fix: add recognition of empty string in unmarshall json --- json.go | 3 ++- json_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/json.go b/json.go index 2792ed1..fd23a52 100644 --- a/json.go +++ b/json.go @@ -6,7 +6,8 @@ import ( ) func (c *Optional[T]) UnmarshalJSON(data []byte) error { - if string(data) == `"null"` { + asString := string(data) + if asString == `"null"` || asString == `""` { c.isValidValue = false return nil } diff --git a/json_test.go b/json_test.go index 9cb7e3b..2c4c497 100644 --- a/json_test.go +++ b/json_test.go @@ -46,6 +46,19 @@ var _ = Describe("Json", func() { Expect(holder.IsPresent()).To(BeFalse()) }) }) + + When("is zero", func() { + It("creates an empty optional", func() { + var ( + jsonData = []byte(`""`) + holder = goption.Empty[string]() + ) + err := holder.UnmarshalJSON(jsonData) + + Expect(err).ToNot(HaveOccurred()) + Expect(holder.IsPresent()).To(BeFalse()) + }) + }) }) Describe("MarshalJson", func() { From 5f1f20f32b6a03336c9533f71051219cdb3723e3 Mon Sep 17 00:00:00 2001 From: Manuel Carbajal Date: Fri, 24 May 2024 14:44:49 -0600 Subject: [PATCH 2/3] feat: change way to handle null on unmarshall json --- json.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/json.go b/json.go index fd23a52..72b8aff 100644 --- a/json.go +++ b/json.go @@ -3,15 +3,19 @@ package goption import ( "encoding/json" "reflect" + "strconv" ) func (c *Optional[T]) UnmarshalJSON(data []byte) error { - asString := string(data) - if asString == `"null"` || asString == `""` { - c.isValidValue = false - return nil + unquoted, err := strconv.Unquote(string(data)) + if err != nil { + return err + } + if unquoted == "null" { + unquoted = "" } - c.isValidValue = len(data) > 0 + _, isValid := isValidData(unquoted) + c.isValidValue = isValid if err := json.Unmarshal(data, &c.value); err != nil { return err } From 7f4926f2a03aa2360720dbfac73934e403acfec8 Mon Sep 17 00:00:00 2001 From: Manuel Carbajal Date: Fri, 24 May 2024 14:47:52 -0600 Subject: [PATCH 3/3] feat: add util func to get bool from isValidData func --- goption.go | 8 ++++++-- json.go | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/goption.go b/goption.go index 105c776..95439f4 100644 --- a/goption.go +++ b/goption.go @@ -21,8 +21,7 @@ func Empty[T any]() Optional[T] { // Of returns an Optional with the specified present value. It does not matters if value is nil func Of[T any](value T) Optional[T] { - _, isValid := isValidData(value) - return Optional[T]{value: value, isValidValue: isValid} + return Optional[T]{value: value, isValidValue: getIsValidDataBool(value)} } // Get when a value is present returns the value, otherwise throws ErrNoSuchElement. @@ -80,3 +79,8 @@ func isValidData[T any](value T) (reflect.Value, bool) { return val, !val.IsZero() } } + +func getIsValidDataBool[T any](value T) bool { + _, is := isValidData(value) + return is +} diff --git a/json.go b/json.go index 72b8aff..a0629a7 100644 --- a/json.go +++ b/json.go @@ -14,8 +14,7 @@ func (c *Optional[T]) UnmarshalJSON(data []byte) error { if unquoted == "null" { unquoted = "" } - _, isValid := isValidData(unquoted) - c.isValidValue = isValid + c.isValidValue = getIsValidDataBool(unquoted) if err := json.Unmarshal(data, &c.value); err != nil { return err }