-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Treat pointers to nil slices as empty for Patient updates (#71)
- Loading branch information
Showing
5 changed files
with
317 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package elation | ||
|
||
import "encoding/json" | ||
|
||
// NonNullJSONArray ensures that nil slices are marshalled to "[]" instead of "null". It also ensures that empty JSON arrays | ||
// are marshalled to nil slices. | ||
type NonNullJSONArray[T any] []T | ||
|
||
func (nnja NonNullJSONArray[T]) MarshalJSON() ([]byte, error) { | ||
if nnja == nil { | ||
nnja = make([]T, 0) | ||
} | ||
|
||
return json.Marshal([]T(nnja)) | ||
} | ||
|
||
func (nnja *NonNullJSONArray[T]) UnmarshalJSON(data []byte) error { | ||
type alias NonNullJSONArray[T] | ||
var a alias | ||
|
||
err := json.Unmarshal(data, &a) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(a) == 0 { | ||
return nil | ||
} | ||
|
||
*nnja = NonNullJSONArray[T](a) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package elation | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNonNullJSONArray_MarshalJSON(t *testing.T) { | ||
testCases := map[string]struct { | ||
value NonNullJSONArray[string] | ||
expectedBytes []byte | ||
}{ | ||
"nil value": { | ||
value: nil, | ||
expectedBytes: []byte("[]"), | ||
}, | ||
"empty array": { | ||
value: NonNullJSONArray[string]{}, | ||
expectedBytes: []byte("[]"), | ||
}, | ||
"non-empty array": { | ||
value: NonNullJSONArray[string]{"alvin", "simon", "theodore"}, | ||
expectedBytes: []byte(`["alvin","simon","theodore"]`), | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
bytes, err := testCase.value.MarshalJSON() | ||
assert.NoError(err) | ||
|
||
assert.Equal(testCase.expectedBytes, bytes) | ||
}) | ||
} | ||
} | ||
|
||
func TestNonNullJSONArray_UnmarshalJSON(t *testing.T) { | ||
testCases := map[string]struct { | ||
bytes []byte | ||
expectedValue NonNullJSONArray[string] | ||
}{ | ||
"empty array": { | ||
bytes: []byte("[]"), | ||
expectedValue: nil, | ||
}, | ||
"null": { | ||
bytes: []byte("null"), | ||
expectedValue: nil, | ||
}, | ||
"non-empty array": { | ||
bytes: []byte(`["foo", "bar"]`), | ||
expectedValue: NonNullJSONArray[string]{"foo", "bar"}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
var val NonNullJSONArray[string] | ||
err := json.Unmarshal(testCase.bytes, &val) | ||
assert.NoError(err) | ||
|
||
assert.Equal(testCase.expectedValue, val) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.