Skip to content

Commit

Permalink
feat(codegen): handle (v, ok) methods
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Jan 27, 2021
1 parent 997da42 commit 478c3f0
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 1 deletion.
3 changes: 3 additions & 0 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Field struct {
Args []*FieldArgument // A list of arguments to be passed to this field
MethodHasContext bool // If this is bound to a go method, does the method also take a context
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
VOkFunc bool // If this is bound to a go method, is it of shape (interface{}, bool)
Object *Object // A link back to the parent object
Default interface{} // The default value
Stream bool // does this field return a channel?
Expand Down Expand Up @@ -152,6 +153,8 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
sig := target.Type().(*types.Signature)
if sig.Results().Len() == 1 {
f.NoErr = true
} else if s := sig.Results(); s.Len() == 2 && s.At(1).Type().String() == "bool" {
f.VOkFunc = true
} else if sig.Results().Len() != 2 {
return fmt.Errorf("method has wrong number of args")
}
Expand Down
8 changes: 7 additions & 1 deletion codegen/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
return nil, fmt.Errorf("unexpected type %T for field %s", v, {{ .Name | quote}})
}
{{- else if .IsMethod -}}
{{- if .NoErr -}}
{{- if .VOkFunc -}}
v, ok := {{.GoReceiverName}}.{{.GoFieldName}}({{ .CallArgs }})
if !ok {
return nil, nil
}
return v, nil
{{- else if .NoErr -}}
return {{.GoReceiverName}}.{{.GoFieldName}}({{ .CallArgs }}), nil
{{- else -}}
return {{.GoReceiverName}}.{{.GoFieldName}}({{ .CallArgs }})
Expand Down
261 changes: 261 additions & 0 deletions codegen/testserver/generated.go

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

8 changes: 8 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ func (r *queryResolver) OptionalUnion(ctx context.Context) (TestUnion, error) {
panic("not implemented")
}

func (r *queryResolver) VOkCaseValue(ctx context.Context) (*VOkCaseValue, error) {
panic("not implemented")
}

func (r *queryResolver) VOkCaseNil(ctx context.Context) (*VOkCaseNil, error) {
panic("not implemented")
}

func (r *queryResolver) ValidType(ctx context.Context) (*ValidType, error) {
panic("not implemented")
}
Expand Down

0 comments on commit 478c3f0

Please sign in to comment.