-
Notifications
You must be signed in to change notification settings - Fork 40
feat!: Support nullable fields by default #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
elliotcourant
wants to merge
6
commits into
Oudwins:master
Choose a base branch
from
elliotcourant:fix/set-nil
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−3
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
367575b
feat: Support nullable fields via opt in ptr method
elliotcourant fb6f07e
chore: Fixing things
elliotcourant 7abf0ff
chore: Tweaks
elliotcourant e6133d4
chore: Greptile feedback
elliotcourant 7372055
chore: Tweaks
elliotcourant 83d1538
chore: Cleanup from code review
elliotcourant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,177 @@ | ||
| package zog | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| p "github.com/Oudwins/zog/pkgs/internals" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func nullableStrPtr(s string) *string { return &s } | ||
|
|
||
| func TestPtrNullable_AbsentKeyPreservesExistingValue(t *testing.T) { | ||
| type Req struct { | ||
| Tag *string | ||
| } | ||
| schema := Struct(Shape{ | ||
| "tag": Ptr(String()), | ||
| }) | ||
| out := Req{Tag: nullableStrPtr("keep-me")} | ||
| errs := schema.Parse(map[string]any{}, &out) | ||
| assert.Empty(t, errs) | ||
| assert.NotNil(t, out.Tag) | ||
| assert.Equal(t, "keep-me", *out.Tag) | ||
| } | ||
|
|
||
| func TestPtrNullable_ExplicitNullClearsPointer(t *testing.T) { | ||
| type Req struct { | ||
| Tag *string | ||
| } | ||
| schema := Struct(Shape{ | ||
| "tag": Ptr(String()), | ||
| }) | ||
| out := Req{Tag: nullableStrPtr("clear-me")} | ||
| errs := schema.Parse(map[string]any{"tag": nil}, &out) | ||
| assert.Empty(t, errs) | ||
| assert.Nil(t, out.Tag) | ||
| } | ||
|
|
||
| func TestPtrNullable_ConcreteValueOverwrites(t *testing.T) { | ||
| type Req struct { | ||
| Tag *string | ||
| } | ||
| schema := Struct(Shape{ | ||
| "tag": Ptr(String()), | ||
| }) | ||
| var out Req | ||
| errs := schema.Parse(map[string]any{"tag": "v"}, &out) | ||
| assert.Empty(t, errs) | ||
| assert.NotNil(t, out.Tag) | ||
| assert.Equal(t, "v", *out.Tag) | ||
| } | ||
|
|
||
| func TestPtrNullable_NestedStructPointer_NullClearsOuter(t *testing.T) { | ||
| type Inner struct { | ||
| V int | ||
| } | ||
| type Outer struct { | ||
| Inner *Inner | ||
| } | ||
| schema := Struct(Shape{ | ||
| "inner": Ptr(Struct(Shape{ | ||
| "v": Int(), | ||
| })), | ||
| }) | ||
| out := Outer{Inner: &Inner{V: 42}} | ||
| errs := schema.Parse(map[string]any{"inner": nil}, &out) | ||
| assert.Empty(t, errs) | ||
| assert.Nil(t, out.Inner) | ||
| } | ||
|
|
||
| func TestPtrNullable_NestedBareStruct_NullBehavesAsEmpty(t *testing.T) { | ||
| type Inner struct { | ||
| V *int | ||
| } | ||
| type Outer struct { | ||
| Inner Inner | ||
| } | ||
| schema := Struct(Shape{ | ||
| "inner": Struct(Shape{ | ||
| "v": Ptr(Int()), | ||
| }), | ||
| }) | ||
| v := 7 | ||
| out := Outer{Inner: Inner{V: &v}} | ||
| errs := schema.Parse(map[string]any{"inner": nil}, &out) | ||
| assert.Empty(t, errs) | ||
| assert.NotNil(t, out.Inner.V) | ||
| assert.Equal(t, 7, *out.Inner.V) | ||
| } | ||
|
|
||
| func TestPtrNullable_DoublePointer_OuterNullable(t *testing.T) { | ||
| type Req struct { | ||
| V **string | ||
| } | ||
| schema := Struct(Shape{ | ||
| "v": Ptr(Ptr(String())), | ||
| }) | ||
| inner := "keep" | ||
| outer := &inner | ||
| out := Req{V: &outer} | ||
| errs := schema.Parse(map[string]any{"v": nil}, &out) | ||
| assert.Empty(t, errs) | ||
| assert.Nil(t, out.V) | ||
| } | ||
|
|
||
| func TestPtrNullable_SliceOfNullablePtr(t *testing.T) { | ||
| schema := Slice(Ptr(String())) | ||
| var out []*string | ||
| errs := schema.Parse([]any{"a", nil, "c"}, &out) | ||
| assert.Empty(t, errs) | ||
| assert.Len(t, out, 3) | ||
| assert.NotNil(t, out[0]) | ||
| assert.Equal(t, "a", *out[0]) | ||
| assert.Nil(t, out[1]) | ||
| assert.NotNil(t, out[2]) | ||
| assert.Equal(t, "c", *out[2]) | ||
| } | ||
|
|
||
| func TestPtrNullable_TopLevelBareNilDoesNotClear(t *testing.T) { | ||
| schema := Ptr(String()) | ||
| dest := nullableStrPtr("keep") | ||
| errs := schema.Parse(nil, &dest) | ||
| assert.Empty(t, errs) | ||
| assert.NotNil(t, dest) | ||
| assert.Equal(t, "keep", *dest) | ||
| } | ||
|
|
||
| func TestPtrNullable_IssueValueIsNotSentinel(t *testing.T) { | ||
| type Req struct { | ||
| Tag *string | ||
| } | ||
| schema := Struct(Shape{ | ||
| "tag": Ptr(String()).NotNil(), | ||
| }) | ||
| var out Req | ||
| errs := schema.Parse(map[string]any{"tag": nil}, &out) | ||
| assert.NotEmpty(t, errs) | ||
| assert.False(t, p.IsExplicitNull(errs[0].Value), "issue value must not be the internal sentinel") | ||
| } | ||
|
|
||
| func TestPtrNullable_MapDataProvider_EmitsSentinelForExplicitNull(t *testing.T) { | ||
| m := map[string]any{"present_null": nil, "present_val": "hello"} | ||
| dp := p.NewSafeMapDataProvider(m) | ||
| assert.Nil(t, dp.Get("absent_key")) | ||
| assert.True(t, p.IsExplicitNull(dp.Get("present_null"))) | ||
| assert.Equal(t, "hello", dp.Get("present_val")) | ||
| } | ||
|
|
||
| func TestPtrNullable_MapDataProvider_TypedMap_NoSentinel(t *testing.T) { | ||
| m := map[string]string{"empty": ""} | ||
| dp := p.NewSafeMapDataProvider(m) | ||
| assert.Equal(t, "", dp.Get("empty")) | ||
| assert.Nil(t, dp.Get("missing")) | ||
| assert.False(t, p.IsExplicitNull(dp.Get("empty"))) | ||
| assert.False(t, p.IsExplicitNull(dp.Get("missing"))) | ||
| } | ||
|
|
||
| func TestPtrNullable_TypedNilInMapAnyValue_EmitsSentinel(t *testing.T) { | ||
| var typedNil *string | ||
| dp := p.NewSafeMapDataProvider(map[string]any{"tag": typedNil}) | ||
| assert.True(t, p.IsExplicitNull(dp.Get("tag"))) | ||
| } | ||
|
|
||
| func TestPtrNullable_TypedNilInMapAnyValue_ClearsPointer(t *testing.T) { | ||
| type Req struct { | ||
| Tag *string | ||
| } | ||
| schema := Struct(Shape{ | ||
| "tag": Ptr(String()), | ||
| }) | ||
| var typedNil *string | ||
| input := map[string]any{"tag": typedNil} | ||
| out := Req{Tag: nullableStrPtr("preexisting")} | ||
| errs := schema.Parse(input, &out) | ||
| assert.Empty(t, errs) | ||
| assert.Nil(t, out.Tag) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure this is right?
if
var userPtr *User
errs := userPtrSchema.Parse(data, &userPtr)
We should be setting the ptr to nil to keep the behaviour consistent. So
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or am I missing something?