Skip to content

Commit

Permalink
Fix decoding null for NullableString
Browse files Browse the repository at this point in the history
While at it, also fix golangci-lint errors, and add tests to make sure
the NullableTime are able to decode null time, too.
  • Loading branch information
Cuong Manh Le authored and cuonglm committed Aug 15, 2021
1 parent eeaf2b8 commit b2fa552
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
15 changes: 7 additions & 8 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func (ns *NullableString) UnmarshalJSON(b []byte) error {
str := string(b)
// Special case when we encounter `null`, modify it to the empty string
if str == "null" || str == "" {
str = ""
} else {
unquoted, err := strconv.Unquote(str)
if err != nil {
return err
}
*ns = NullableString(unquoted)
*ns = ""
return nil
}
unquoted, err := strconv.Unquote(str)
if err != nil {
return err
}
*ns = NullableString(unquoted)

return nil
}
Expand Down Expand Up @@ -105,7 +105,6 @@ func (nt *NullableTime) UnmarshalJSON(b []byte) error {
return err
}
if ns == "" {
nt = nil
return nil
}

Expand Down
66 changes: 66 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package otils

import (
"encoding/json"
"testing"
"time"
)

func TestNullableString(t *testing.T) {
type testNullableString struct {
S NullableString
}
tests := []struct {
name string
s string
expected NullableString
}{
{"normal", `{"s": "foo"}`, NullableString("foo")},
{"null", `{"s": null}`, NullableString("")},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tn := testNullableString{S: "init"}
if err := json.Unmarshal([]byte(tc.s), &tn); err != nil {
t.Error(err)
}
if tn.S != tc.expected {
t.Errorf("unexpected result, want: %q, got: %q", tc.expected, tn.S)
}
})
}
}

func TestNullableTime(t *testing.T) {
type testNullableTime struct {
T *NullableTime
}
now := NullableTime(time.Now())
tests := []struct {
name string
s string
isNull bool
expected NullableTime
}{
{"null", `{"T": null}`, true, NullableTime(now)},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tn := testNullableTime{T: &now}
if err := json.Unmarshal([]byte(tc.s), &tn); err != nil {
t.Error(err)
}
if tc.isNull && tn.T != nil {
t.Errorf("expected nil, got non-nil")
}
if !tc.isNull && time.Time(*tn.T).UnixNano() != time.Time(tc.expected).UnixNano() {
t.Errorf("unexpected result, want: %v, got: %v", tc.expected, *tn.T)
}
})
}
}
2 changes: 1 addition & 1 deletion otils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func TestCORSMiddleware(t *testing.T) {

for i, tt := range tests {
handler := otils.CORSMiddleware(tt.cors, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello!"))
_, _ = w.Write([]byte("Hello!"))
}))
tst := httptest.NewServer(handler)
res, err := tst.Client().Get(tst.URL)
Expand Down

0 comments on commit b2fa552

Please sign in to comment.