Skip to content

Commit

Permalink
Fix input array processing
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 21, 2018
1 parent 4880497 commit 9817629
Show file tree
Hide file tree
Showing 16 changed files with 348 additions and 1,153 deletions.
2 changes: 1 addition & 1 deletion codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Bind(schema *schema.Schema, userTypes map[string]string, destDir string) (*
NoErr: true,
GoMethodName: "ec.introspectType",
Args: []FieldArgument{
{GQLName: "name", Type: &Type{namedTypes["String"], []string{}, ""}},
{GQLName: "name", Type: &Type{namedTypes["String"], []string{}, ""}, Object: &Object{}},
},
Object: q,
})
Expand Down
3 changes: 2 additions & 1 deletion codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type Field struct {
type FieldArgument struct {
*Type

GQLName string // The name of the argument in graphql
GQLName string // The name of the argument in graphql
Object *Object // A link back to the parent object
}

type Objects []*Object
Expand Down
1 change: 1 addition & 0 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func buildObject(types NamedTypes, typ *schema.Object) *Object {
args = append(args, FieldArgument{
GQLName: arg.Name.Name,
Type: types.getType(arg.Type),
Object: obj,
})
}

Expand Down
10 changes: 7 additions & 3 deletions codegen/templates/args.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
}
{{- else}}
if tmp, ok := field.Args[{{$arg.GQLName|quote}}]; ok {
{{$arg.Unmarshal "tmp2" "tmp" }}
var err error
{{$arg.Unmarshal (print "arg" $i) "tmp" }}
if err != nil {
badArgs = true
{{- if $arg.Object.Stream }}
return nil
{{- else }}
return graphql.Null
{{- end }}
}
arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2
}
{{- end}}
{{- end -}}
6 changes: 3 additions & 3 deletions 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/input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
switch k {
{{- range $field := .Fields }}
case {{$field.GQLName|quote}}:
{{$field.Unmarshal "val" "v" }}
var err error
{{ $field.Unmarshal (print "it." $field.GoVarName) "v" }}
if err != nil {
return it, err
}
it.{{$field.GoVarName}} = {{if $field.Type.IsPtr}}&{{end}}val
{{- end }}
}
}
Expand Down
8 changes: 0 additions & 8 deletions codegen/templates/object.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ func (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection{{
switch field.Name {
{{- range $field := $object.Fields }}
case "{{$field.GQLName}}":
badArgs := false
{{- template "args.gotpl" $field.Args }}
if badArgs {
return nil
}

{{- if $field.GoVarName }}
results := it.{{$field.GoVarName}}
Expand Down Expand Up @@ -73,11 +69,7 @@ func (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection{{
out.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})
{{- range $field := $object.Fields }}
case "{{$field.GQLName}}":
badArgs := false
{{- template "args.gotpl" $field.Args }}
if badArgs {
continue
}

{{- if $field.IsConcurrent }}
ec.wg.Add(1)
Expand Down
72 changes: 55 additions & 17 deletions codegen/type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codegen

import (
"strconv"
"strings"
)

Expand Down Expand Up @@ -34,10 +35,10 @@ const (
)

func (t Ref) FullName() string {
return t.pkgDot() + t.GoType
return t.PkgDot() + t.GoType
}

func (t Ref) pkgDot() string {
func (t Ref) PkgDot() string {
if t.Import == nil || t.Import.Name == "" {
return ""
}
Expand Down Expand Up @@ -70,25 +71,62 @@ func (t NamedType) IsMarshaled() bool {
}

func (t Type) Unmarshal(result, raw string) string {
realResult := result
if t.CastType != "" {
result = "castTmp"
}
ret := tpl(`var {{.result}} {{.type}}
err := (&{{.result}}).UnmarshalGQL({{.raw}})`, map[string]interface{}{
"result": result,
"raw": raw,
"type": t.FullName(),
})
return t.unmarshal(result, raw, t.Modifiers, 1)
}

if t.Marshaler != nil {
ret = result + ", err := " + t.Marshaler.pkgDot() + "Unmarshal" + t.Marshaler.GoType + "(" + raw + ")"
func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) string {
switch {
case len(remainingMods) > 0 && remainingMods[0] == modPtr:
ptr := "ptr" + strconv.Itoa(depth)
return tpl(`var {{.ptr}} {{.t.FullName}}
{{.next}}
{{.result}} = &{{.ptr -}}
`, map[string]interface{}{
"ptr": ptr,
"t": t,
"result": result,
"next": t.unmarshal(ptr, raw, remainingMods[1:], depth+1),
})

case len(remainingMods) > 0 && remainingMods[0] == modList:
var rawIf = "rawIf" + strconv.Itoa(depth)
var index = "idx" + strconv.Itoa(depth)

return tpl(`{{.rawSlice}} := {{.raw}}.([]interface{})
{{.result}} = make({{.type}}, len({{.rawSlice}}))
for {{.index}} := range {{.rawSlice}} {
{{ .next }}
}`, map[string]interface{}{
"raw": raw,
"rawSlice": rawIf,
"index": index,
"result": result,
"type": strings.Join(remainingMods, "") + t.NamedType.FullName(),
"next": t.unmarshal(result+"["+index+"]", rawIf+"["+index+"]", remainingMods[1:], depth+1),
})
}

realResult := result
if t.CastType != "" {
ret += "\n" + realResult + " := " + t.CastType + "(castTmp)"
result = "castTmp"
}
return ret

return tpl(`{{- if .t.CastType }}
var castTmp {{.t.FullName}}
{{- end }}
{{- if .t.Marshaler }}
{{ .result }}, err = {{ .t.Marshaler.PkgDot }}Unmarshal{{.t.Marshaler.GoType}}({{.raw}})
{{- else }}
err = (&{{.result}}).UnmarshalGQL({{.raw}})
{{- end }}
{{- if .t.CastType }}
{{ .realResult }} = {{.t.CastType}}(castTmp)
{{- end }}`, map[string]interface{}{
"realResult": realResult,
"result": result,
"raw": raw,
"t": t,
})
}

func (t Type) Marshal(result, val string) string {
Expand All @@ -97,7 +135,7 @@ func (t Type) Marshal(result, val string) string {
}

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

return result + " = " + val
Expand Down
Loading

0 comments on commit 9817629

Please sign in to comment.