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 authored and carldunham committed Oct 12, 2021
1 parent acf0422 commit 561a7f5
Show file tree
Hide file tree
Showing 6 changed files with 161 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
11 changes: 11 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ require (
)

require (
github.com/agnivade/levenshtein v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect
github.com/logrusorgru/aurora/v3 v3.0.0 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/opentracing/basictracer-go v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 // indirect
golang.org/x/mod v0.5.0 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 // indirect
)
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ require (
require (
github.com/agnivade/levenshtein v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)

0 comments on commit 561a7f5

Please sign in to comment.