Skip to content
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

Fix bugs relating to optional fields with custom (un)marshalers #116

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ When releasing a new version:

- The `omitempty` option now works correctly for struct- and map-typed variables, matching `encoding/json`, which is to say it never omits structs, and omits empty maps. (#43)
- Generated type-names now abbreviate across multiple components; for example if the path to a type is `(MyOperation, Outer, Outer, Inner, OuterInner)`, it will again be called `MyOperationOuterInner`. (This regressed in a pre-v0.1.0 refactor.) (#109)
- Previously, interface fields with `# @genqlient(pointer: true)` would be unmarshaled to `(*MyInterface)(*<nil>)`, i.e. a pointer to the untyped-nil of the interface type. Now they are unmarshaled as `(*MyInterface)(<nil>)`, i.e. a nil pointer of the pointer-to-interface type, as you would expect.

## v0.1.0

Expand Down
11 changes: 10 additions & 1 deletion generate/marshal.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ func (v *{{.GoName}}) MarshalJSON() ([]byte, error) {
for i, src := range src {
dst := &(*dst)[i]
{{end -}}
{{/* src now has type <GoType>; dst is json.RawMessage */ -}}
{{if $field.GoType.IsPointer -}}
{{/* If you passed a pointer, and it's nil, don't call the
marshaler. This matches json.Marshal's behavior. */ -}}
if src != nil {
{{end -}}
var err error
*dst, err = {{$field.Marshaler $.Generator}}(
{{/* src is a pointer to the struct-field (or field-element, etc.).
{{/* src is the struct-field (or field-element, etc.).
We want to pass a pointer to the type you specified, so if
there's a pointer on the field that's exactly what we want,
and if not we need to take the address. */ -}}
Expand All @@ -41,6 +47,9 @@ func (v *{{.GoName}}) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf(
"Unable to marshal {{$.GoName}}.{{$field.GoName}}: %w", err)
}
{{if $field.GoType.IsPointer -}}
}{{/* end if src != nil */}}
{{end -}}
{{range $i := intRange $field.GoType.SliceDepth -}}
}
{{end -}}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading