Skip to content

Commit

Permalink
update code generation for pointer-to-pointer updating
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Dunham committed Sep 13, 2021
1 parent c05e63a commit 62fcaf8
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 8 deletions.
10 changes: 10 additions & 0 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ func (t *TypeReference) IsPtr() bool {
return isPtr
}

// fix for https://github.com/golang/go/issues/31103 may make it possible to remove this (may still be useful)
//
func (t *TypeReference) IsPtrToPtr() bool {
if p, isPtr := t.GO.(*types.Pointer); isPtr {
_, isPtr := p.Elem().(*types.Pointer)
return isPtr
}
return false
}

func (t *TypeReference) IsNilable() bool {
return IsNilable(t.GO)
}
Expand Down
123 changes: 117 additions & 6 deletions codegen/testserver/generated.go

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

2 changes: 1 addition & 1 deletion codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func processType(ret map[string]*config.TypeReference, ref *config.TypeReference
}
ret[key] = ref

if ref.IsSlice() || ref.IsPtrToSlice() {
if ref.IsSlice() || ref.IsPtrToSlice() || ref.IsPtrToPtr() {
processType(ret, ref.Elem())
}
}
17 changes: 16 additions & 1 deletion codegen/type.gotpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- range $type := .ReferencedTypes }}
{{ with $type.UnmarshalFunc }}
func (ec *executionContext) {{ . }}(ctx context.Context, v interface{}) ({{ $type.GO | ref }}, error) {
{{- if and $type.IsNilable (not $type.GQL.NonNull) }}
{{- if and $type.IsNilable (not $type.GQL.NonNull) (not $type.IsPtrToPtr) }}
if v == nil { return nil, nil }
{{- end }}
{{- if $type.IsPtrToSlice }}
Expand All @@ -26,6 +26,16 @@
}
}
return res, nil
{{- else if and $type.IsPtrToPtr (not $type.Unmarshaler) (not $type.IsMarshaler) }}
var pres {{ $type.Elem.GO | ref }}
if v != nil {
res, err := ec.{{ $type.Elem.UnmarshalFunc }}(ctx, v)
if err != nil {
return nil, graphql.ErrorOnPath(ctx, err)
}
pres = res
}
return &pres, nil
{{- else }}
{{- if $type.Unmarshaler }}
{{- if $type.CastType }}
Expand Down Expand Up @@ -123,6 +133,11 @@
}
{{ end }}
return ret
{{- else if and $type.IsPtrToPtr (not $type.Unmarshaler) (not $type.IsMarshaler) }}
if v == nil {
return graphql.Null
}
return ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, *v)
{{- else }}
{{- if $type.IsNilable }}
if v == nil {
Expand Down

0 comments on commit 62fcaf8

Please sign in to comment.