total rewrite to interface handling; not complete but it compiles

This commit is contained in:
Ben Kraft
2020-07-16 13:28:43 -07:00
parent af4a765a32
commit cf7136ca65
33 changed files with 712 additions and 173 deletions
+35
View File
@@ -0,0 +1,35 @@
func (v *{{.Type}}) UnmarshalJSON(b []byte) error {
var firstPass struct{
*{{.Type}}
{{range .Fields -}}
{{.GoName}} json.RawMessage `json:"{{.JSONName}}"`
{{end}}
}
firstPass.{{.Type}} = v
err := json.Unmarshal(b, &typenames)
if err != nil {
return err
}
{{range .Fields -}}
var tn struct { TypeName string `json:"__typename"` }
err = json.Unmarshal(firstPass.{{.GoName}}, &tn)
if err != nil {
return err
}
switch tn.TypeName {
{{with $field := .}}
{{range $field.ConcreteTypes}}
case "{{.GraphQLName}}":
v.{{$field.GoName}} = {{.GoName}}{}
err = json.Unmarshal(
firstPass.{{$field.GoName}}, &v.{{$field.GoName}})
{{end}}
{{end}}
}
if err != nil {
return err
}
{{end}}
}