38 lines
921 B
Cheetah
38 lines
921 B
Cheetah
func (v *{{.Type}}) UnmarshalJSON(b []byte) error {
|
|
var firstPass struct{
|
|
*{{.Type}}
|
|
{{range .Fields -}}
|
|
{{.GoName}} {{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"`
|
|
{{end}}
|
|
}
|
|
firstPass.{{.Type}} = v
|
|
|
|
err := {{ref "encoding/json.Unmarshal"}}(b, &firstPass)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
{{range .Fields -}}
|
|
var tn struct { TypeName string `json:"__typename"` }
|
|
err = {{ref "encoding/json.Unmarshal"}}(firstPass.{{.GoName}}, &tn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch tn.TypeName {
|
|
{{with $field := .}}
|
|
{{range $field.ConcreteTypes}}
|
|
case "{{.GraphQLName}}":
|
|
{{/* TODO: handle repeated fields! */}}
|
|
v.{{$field.GoName}} = {{.GoName}}{}
|
|
err = {{ref "encoding/json.Unmarshal"}}(
|
|
firstPass.{{$field.GoName}}, &v.{{$field.GoName}})
|
|
{{end}}
|
|
{{end}}
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
{{end}}
|
|
return nil
|
|
}
|