-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for binding with a custom marshal/unmarshal function
This is useful if you want to bind to a type you don't control (or use for other things) but need different serialization than its default. This is a feature gqlgen has and we've found it very useful. For example, in webapp we want to bind `DateTime` to `time.Time`, but its default serialization is not compatible with Python, so currently we have to bind to a wrapper type and cast all over the place, which is exactly the sort of boilerplate genqlient is supposed to avoid. For unmarshaling, the implementation basically just follows the existing support for abstract types; instead of calling our own generated helper, we now call your specified function. This required some refactoring to abstract the handling of custom unmarshalers generally from abstract types specifically, and to wire in not only the unmarshaler-name but also the `generator` (in order to compute the right import alias). For marshaling, I had to implement all that stuff over again; it's mostly parallel to unmarshaling (and I made a few minor changes to unmarshaling to make the two more parallel). Luckily, after #103 I at least only had to do it once, rather than implementing the same functionality for arguments and for input-type fields. It was still quite a bit of code; I didn't try to be quite as completionist about the tests as with unmarshal but still had to add a few. Issue: #38 Test plan: make check Reviewers: marksandstrom, steve, adam, jvoll, miguel, mahtab
- Loading branch information
1 parent
d344208
commit 30bce20
Showing
42 changed files
with
1,893 additions
and
455 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
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
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
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,52 @@ | ||
{{/* See unmarshal.go.tmpl for more on how this works; this is mostly just | ||
parallel (and simplified -- we don't need to handle embedding). */}} | ||
|
||
func (v *{{.GoName}}) MarshalJSON() ([]byte, error) { | ||
{{/* We do the two passes in the opposite order of unmarshal: first, we | ||
marshal the special fields, then we assign those to the wrapper struct | ||
and finish marshaling the whole object. But first we set up the | ||
object for the second part, so we can assign to it as we go. */}} | ||
var fullObject struct{ | ||
*{{.GoName}} | ||
{{range .Fields -}} | ||
{{if .NeedsMarshaler -}} | ||
{{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"` | ||
{{end -}} | ||
{{end -}} | ||
{{ref "github.com/Khan/genqlient/graphql.NoUnmarshalJSON"}} | ||
} | ||
fullObject.{{.GoName}} = v | ||
|
||
{{range $field := .Fields -}} | ||
{{if $field.NeedsMarshaler -}} | ||
{ | ||
{{/* Here dst is the json.RawMessage, and src is the Go type */}} | ||
dst := &fullObject.{{$field.GoName}} | ||
src := v.{{$field.GoName}} | ||
{{range $i := intRange $field.GoType.SliceDepth -}} | ||
*dst = make( | ||
{{repeat (sub $field.GoType.SliceDepth $i) "[]"}}{{ref "encoding/json.RawMessage"}}, | ||
len(src)) | ||
for i, src := range src { | ||
dst := &(*dst)[i] | ||
{{end -}} | ||
var err error | ||
*dst, err = {{$field.Marshaler $.Generator}}( | ||
{{/* src is a pointer to 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. */ -}} | ||
{{if not $field.GoType.IsPointer}}&{{end}}src) | ||
if err != nil { | ||
return nil, fmt.Errorf( | ||
"Unable to marshal {{$.GoName}}.{{$field.GoName}}: %w", err) | ||
} | ||
{{range $i := intRange $field.GoType.SliceDepth -}} | ||
} | ||
{{end -}} | ||
} | ||
{{end -}} | ||
{{end}} | ||
|
||
return {{ref "encoding/json.Marshal"}}(&fullObject) | ||
} |
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,6 @@ | ||
query CustomMarshal($date: Date!) { | ||
usersBornOn(date: $date) { | ||
id | ||
birthdate | ||
} | ||
} |
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,8 @@ | ||
query CustomMarshalSlice( | ||
$datesss: [[[Date!]!]!]!, | ||
# @genqlient(pointer: true) | ||
$datesssp: [[[Date!]!]!]!, | ||
) { | ||
acceptsListOfListOfListsOfDates(datesss: $datesss) | ||
withPointer: acceptsListOfListOfListsOfDates(datesss: $datesssp) | ||
} |
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.