-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Omittable can now be serialized as json
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 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,88 @@ | ||
package graphql | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOmittable_MarshalJSON(t *testing.T) { | ||
s := "test" | ||
testCases := []struct { | ||
name string | ||
input any | ||
expectedJSON string | ||
}{ | ||
{ | ||
name: "simple string", | ||
input: struct{ Value Omittable[string] }{Value: OmittableOf("simple string")}, | ||
expectedJSON: `{"Value": "simple string"}`, | ||
}, | ||
{ | ||
name: "string pointer", | ||
input: struct{ Value Omittable[*string] }{Value: OmittableOf(&s)}, | ||
expectedJSON: `{"Value": "test"}`, | ||
}, | ||
{ | ||
name: "omitted integer", | ||
input: struct{ Value Omittable[int] }{}, | ||
expectedJSON: `{"Value": null}`, | ||
}, | ||
{ | ||
name: "omittable omittable", | ||
input: struct{ Value Omittable[Omittable[uint64]] }{Value: OmittableOf(OmittableOf(uint64(42)))}, | ||
expectedJSON: `{"Value": 42}`, | ||
}, | ||
{ | ||
name: "omittable struct", | ||
input: struct { | ||
Value Omittable[struct{ Inner string }] | ||
}{Value: OmittableOf(struct{ Inner string }{})}, | ||
expectedJSON: `{"Value": {"Inner": ""}}`, | ||
}, | ||
{ | ||
name: "omitted struct", | ||
input: struct { | ||
Value Omittable[struct{ Inner string }] | ||
}{}, | ||
expectedJSON: `{"Value": null}`, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
data, err := json.Marshal(tc.input) | ||
require.NoError(t, err) | ||
assert.JSONEq(t, tc.expectedJSON, string(data)) | ||
}) | ||
} | ||
} | ||
|
||
func TestOmittable_UnmarshalJSON(t *testing.T) { | ||
var s struct { | ||
String Omittable[string] | ||
OmittedString Omittable[string] | ||
StringPointer Omittable[*string] | ||
NullInt Omittable[int] | ||
} | ||
|
||
err := json.Unmarshal([]byte(` | ||
{ | ||
"String": "simple string", | ||
"StringPointer": "string pointer", | ||
"NullInt": null | ||
}`), &s) | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, "simple string", s.String.Value()) | ||
assert.True(t, s.String.IsSet()) | ||
assert.False(t, s.OmittedString.IsSet()) | ||
assert.True(t, s.StringPointer.IsSet()) | ||
if assert.NotNil(t, s.StringPointer.Value()) { | ||
assert.EqualValues(t, "string pointer", *s.StringPointer.Value()) | ||
} | ||
assert.True(t, s.NullInt.IsSet()) | ||
assert.Zero(t, s.NullInt.Value()) | ||
} |