Skip to content

Commit

Permalink
change field marshaler return process that make it easy to insert oth…
Browse files Browse the repository at this point in the history
…er processing
  • Loading branch information
vvakame committed Oct 22, 2018
1 parent ab4752c commit 583f980
Show file tree
Hide file tree
Showing 12 changed files with 4,063 additions and 3,286 deletions.
18 changes: 14 additions & 4 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,15 @@ func (f *Field) CallArgs() string {

// should be in the template, but its recursive and has a bunch of args
func (f *Field) WriteJson() string {
return f.doWriteJson("res", f.Type.Modifiers, f.ASTType, false, 1)
return strings.TrimSpace(
tpl(`
func () graphql.Marshaler {
{{ .next }}
}()
`, map[string]interface{}{
"next": f.doWriteJson("res", f.Type.Modifiers, f.ASTType, false, 1),
}),
)
}

func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Type, isPtr bool, depth int) string {
Expand All @@ -235,7 +243,8 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
{{- end }}
return graphql.Null
}
{{.next }}`, map[string]interface{}{
{{ .next -}}
`, map[string]interface{}{
"val": val,
"nonNull": astType.NonNull,
"next": f.doWriteJson(val, remainingMods[1:], astType, true, depth+1),
Expand Down Expand Up @@ -289,7 +298,8 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
{{- end}}
}
{{ if and .top (not .isScalar) }} wg.Wait() {{ end }}
return {{.arr}}`, map[string]interface{}{
return {{ .arr -}}
`, map[string]interface{}{
"val": val,
"arr": arr,
"index": index,
Expand All @@ -304,7 +314,7 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
if isPtr {
val = "*" + val
}
return f.Marshal(val)
return "return " + f.Marshal(val)

default:
if !isPtr {
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/data.go

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

4 changes: 2 additions & 2 deletions codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
return nil
}
var out graphql.OrderedMap
out.Add(field.Alias, func() graphql.Marshaler { {{ $field.WriteJson }} }())
out.Add(field.Alias, func() graphql.Marshaler { return {{ $field.WriteJson }} }())
return &out
}
}
Expand Down Expand Up @@ -71,6 +71,6 @@
}
res := resTmp.({{$field.Signature}})
rctx.Result = res
{{ $field.WriteJson }}
return {{ $field.WriteJson }}
}
{{ end }}
4 changes: 2 additions & 2 deletions codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func (t Type) Marshal(val string) string {
}

if t.Marshaler != nil {
return "return " + t.Marshaler.PkgDot() + "Marshal" + t.Marshaler.GoType + "(" + val + ")"
return t.Marshaler.PkgDot() + "Marshal" + t.Marshaler.GoType + "(" + val + ")"
}

return "return " + val
return val
}
Loading

0 comments on commit 583f980

Please sign in to comment.