Skip to content

Commit

Permalink
Rename CastType to AliasedType
Browse files Browse the repository at this point in the history
This field stores a Ref if a type is a builtin that has been aliased. In
most cases if this is set, we want to use this as the type signature
instead of the named type resolved from the schema.
  • Loading branch information
Mathew Byrne committed Jul 24, 2018
1 parent ec928ca commit 7292be7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type Ref struct {
type Type struct {
*NamedType

Modifiers []string
CastType *Ref // the type to cast to when unmarshalling
Modifiers []string
AliasedType *Ref
}

const (
Expand All @@ -47,6 +47,9 @@ func (t Ref) PkgDot() string {
}

func (t Type) Signature() string {
if t.AliasedType != nil {
return strings.Join(t.Modifiers, "") + t.AliasedType.FullName()
}
return strings.Join(t.Modifiers, "") + t.FullName()
}

Expand Down Expand Up @@ -125,11 +128,11 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
}

realResult := result
if t.CastType != nil {
if t.AliasedType != nil {
result = "castTmp"
}

return tpl(`{{- if .t.CastType }}
return tpl(`{{- if .t.AliasedType }}
var castTmp {{.t.FullName}}
{{ end }}
{{- if eq .t.GoType "map[string]interface{}" }}
Expand All @@ -139,8 +142,8 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
{{- else -}}
err = (&{{.result}}).UnmarshalGQL({{.raw}})
{{- end }}
{{- if .t.CastType }}
{{ .realResult }} = {{.t.CastType.FullName}}(castTmp)
{{- if .t.AliasedType }}
{{ .realResult }} = {{.t.AliasedType.FullName}}(castTmp)
{{- end }}`, map[string]interface{}{
"realResult": realResult,
"result": result,
Expand All @@ -150,7 +153,7 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
}

func (t Type) Marshal(val string) string {
if t.CastType != nil {
if t.AliasedType != nil {
val = t.GoType + "(" + val + ")"
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func validateTypeBinding(imports *Imports, field *Field, goType types.Type) erro
field.Type.Modifiers = modifiersFromGoType(goType)
pkg, typ := pkgAndType(goType.String())
imp := imports.findByPath(pkg)
field.CastType = &Ref{GoType: typ, Import: imp}
field.AliasedType = &Ref{GoType: typ, Import: imp}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion example/scalars/generated.go

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

0 comments on commit 7292be7

Please sign in to comment.