Skip to content

Commit

Permalink
Remove unused special cast case for string enums
Browse files Browse the repository at this point in the history
  • Loading branch information
lwc committed Aug 12, 2020
1 parent 196954b commit 8996066
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 205 deletions.
41 changes: 5 additions & 36 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func (b *Binder) PointerTo(ref *TypeReference) *TypeReference {
newRef := &TypeReference{
GO: types.NewPointer(ref.GO),
GQL: ref.GQL,
CastType: ref.CastType,
Definition: ref.Definition,
Unmarshaler: ref.Unmarshaler,
Marshaler: ref.Marshaler,
Expand All @@ -168,7 +167,6 @@ type TypeReference struct {
GQL *ast.Type
GO types.Type
Target types.Type
CastType types.Type // Before calling marshalling functions cast from/to this base type
Marshaler *types.Func // When using external marshalling functions this will point to the Marshal function
Unmarshaler *types.Func // When using external marshalling functions this will point to the Unmarshal function
IsMarshaler bool // Does the type implement graphql.Marshaler and graphql.Unmarshaler
Expand All @@ -180,7 +178,6 @@ func (ref *TypeReference) Elem() *TypeReference {
GO: p.Elem(),
Target: ref.Target,
GQL: ref.GQL,
CastType: ref.CastType,
Definition: ref.Definition,
Unmarshaler: ref.Unmarshaler,
Marshaler: ref.Marshaler,
Expand All @@ -193,7 +190,6 @@ func (ref *TypeReference) Elem() *TypeReference {
GO: ref.GO.(*types.Slice).Elem(),
Target: ref.Target,
GQL: ref.GQL.Elem,
CastType: ref.CastType,
Definition: ref.Definition,
Unmarshaler: ref.Unmarshaler,
Marshaler: ref.Marshaler,
Expand Down Expand Up @@ -349,27 +345,16 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret
return nil, err
}

if fun, isFunc := obj.(*types.Func); isFunc {
fun, isFunc := obj.(*types.Func)
switch {
case isFunc:
ref.GO = fun.Type().(*types.Signature).Params().At(0).Type()
ref.Marshaler = fun
ref.Unmarshaler = types.NewFunc(0, fun.Pkg(), "Unmarshal"+typeName, nil)
} else if hasMethod(obj.Type(), "MarshalGQL") && hasMethod(obj.Type(), "UnmarshalGQL") {
case hasMethod(obj.Type(), "MarshalGQL") && hasMethod(obj.Type(), "UnmarshalGQL"):
ref.GO = obj.Type()
ref.IsMarshaler = true
} else if underlying := basicUnderlying(obj.Type()); def.IsLeafType() && underlying != nil && underlying.Kind() == types.String {
// Special case for named types wrapping strings. Used by default enum implementations.

ref.GO = obj.Type()
ref.CastType = underlying

underlyingRef, err := b.TypeReference(&ast.Type{NamedType: "String"}, nil)
if err != nil {
return nil, err
}

ref.Marshaler = underlyingRef.Marshaler
ref.Unmarshaler = underlyingRef.Unmarshaler
} else {
default:
ref.GO = obj.Type()
}

Expand Down Expand Up @@ -446,19 +431,3 @@ func hasMethod(it types.Type, name string) bool {
}
return false
}

func basicUnderlying(it types.Type) *types.Basic {
if ptr, isPtr := it.(*types.Pointer); isPtr {
it = ptr.Elem()
}
namedType, ok := it.(*types.Named)
if !ok {
return nil
}

if basic, ok := namedType.Underlying().(*types.Basic); ok {
return basic
}

return nil
}
121 changes: 4 additions & 117 deletions codegen/testserver/generated.go

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

4 changes: 0 additions & 4 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,6 @@ func (r *queryResolver) ScalarSlice(ctx context.Context) ([]byte, error) {
panic("not implemented")
}

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

func (r *queryResolver) OptionalUnion(ctx context.Context) (TestUnion, error) {
panic("not implemented")
}
Expand Down
4 changes: 0 additions & 4 deletions codegen/testserver/stub.go

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

9 changes: 0 additions & 9 deletions codegen/testserver/typefallback.graphql

This file was deleted.

28 changes: 0 additions & 28 deletions codegen/testserver/typefallback_test.go

This file was deleted.

22 changes: 21 additions & 1 deletion codegen/testserver/wrapped_type.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
package testserver

import "github.com/99designs/gqlgen/codegen/testserver/otherpkg"
import (
"fmt"
"io"
"strconv"

"github.com/99designs/gqlgen/codegen/testserver/otherpkg"
"github.com/99designs/gqlgen/graphql"
)

type WrappedScalar otherpkg.Scalar
type WrappedStruct otherpkg.Struct
type WrappedMap otherpkg.Map
type WrappedSlice otherpkg.Slice

func (e *WrappedScalar) UnmarshalGQL(v interface{}) error {
s, err := graphql.UnmarshalString(v)
if err != nil {
return err
}
*e = WrappedScalar(s)
return nil
}

func (e WrappedScalar) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(string(e)))
}
9 changes: 3 additions & 6 deletions codegen/type.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
return res, nil
{{- else }}
{{- if $type.Unmarshaler }}
{{- if $type.CastType }}
tmp, err := {{ $type.Unmarshaler | call }}(v)
return {{ $type.GO | ref }}(tmp), graphql.WrapErrorWithInputPath(ctx, err)
{{- else if and $type.IsTargetNilable (not $type.IsNilable) }}
{{- if and $type.IsTargetNilable (not $type.IsNilable) }}
tmp, err := {{ $type.Unmarshaler | call }}(v)
res := *tmp
return res, graphql.WrapErrorWithInputPath(ctx, err)
Expand Down Expand Up @@ -137,15 +134,15 @@
in := v
{{- end }}
{{- if $type.GQL.NonNull }}
res := {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}(in){{else}}in{{- end }})
res := {{ $type.Marshaler | call }}(in)
if res == graphql.Null {
if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
ec.Errorf(ctx, "must not be null")
}
}
return res
{{- else }}
return {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}(in){{else}}in{{- end }})
return {{ $type.Marshaler | call }}(in)
{{- end }}
{{- else }}
return ec._{{$type.Definition.Name}}(ctx, sel, {{ if not $type.IsNilable}}&{{end}} v)
Expand Down

0 comments on commit 8996066

Please sign in to comment.