Skip to content

Commit

Permalink
Rename core types to have clearer meanings
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Jan 5, 2019
1 parent f10fc64 commit 34b8787
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 131 deletions.
16 changes: 8 additions & 8 deletions codegen/directive_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func (cfg *Config) buildDirectives(types NamedTypes) (map[string]*Directive, err
var args []FieldArgument
for _, arg := range dir.Arguments {
newArg := FieldArgument{
GQLName: arg.Name,
Type: types.getType(arg.Type),
GoVarName: sanitizeArgName(arg.Name),
GQLName: arg.Name,
TypeReference: types.getType(arg.Type),
GoVarName: sanitizeArgName(arg.Name),
}

if !newArg.Type.IsInput && !newArg.Type.IsScalar {
if !newArg.TypeReference.IsInput && !newArg.TypeReference.IsScalar {
return nil, errors.Errorf("%s cannot be used as argument of directive %s(%s) only input and scalar types are allowed", arg.Type, dir.Name, arg.Name)
}

Expand Down Expand Up @@ -69,10 +69,10 @@ func (cfg *Config) getDirectives(list ast.DirectiveList) ([]*Directive, error) {
value = argValue
}
args = append(args, FieldArgument{
GQLName: a.GQLName,
Value: value,
GoVarName: a.GoVarName,
Type: a.Type,
GQLName: a.GQLName,
Value: value,
GoVarName: a.GoVarName,
TypeReference: a.TypeReference,
})
}
dirs[i] = &Directive{
Expand Down
2 changes: 1 addition & 1 deletion codegen/enum.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package codegen

type Enum struct {
*NamedType
*TypeDefinition
Description string
Values []EnumValue
}
Expand Down
6 changes: 3 additions & 3 deletions codegen/enum_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func (cfg *Config) buildEnums(types NamedTypes) []Enum {
}

enum := Enum{
NamedType: namedType,
Values: values,
Description: typ.Description,
TypeDefinition: namedType,
Values: values,
Description: typ.Description,
}
enum.GoType = templates.ToCamel(enum.GQLType)
enums = append(enums, enum)
Expand Down
16 changes: 8 additions & 8 deletions codegen/input_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (cfg *Config) buildInputs(namedTypes NamedTypes, prog *loader.Program) (Obj
}

func (cfg *Config) buildInput(types NamedTypes, typ *ast.Definition) (*Object, error) {
obj := &Object{NamedType: types[typ.Name]}
obj := &Object{TypeDefinition: types[typ.Name]}
typeEntry, entryExists := cfg.Models[typ.Name]

for _, field := range typ.Fields {
Expand All @@ -53,10 +53,10 @@ func (cfg *Config) buildInput(types NamedTypes, typ *ast.Definition) (*Object, e
return nil, err
}
newField := Field{
GQLName: field.Name,
Type: types.getType(field.Type),
Object: obj,
Directives: dirs,
GQLName: field.Name,
TypeReference: types.getType(field.Type),
Object: obj,
Directives: dirs,
}

if entryExists {
Expand All @@ -73,7 +73,7 @@ func (cfg *Config) buildInput(types NamedTypes, typ *ast.Definition) (*Object, e
}
}

if !newField.Type.IsInput && !newField.Type.IsScalar {
if !newField.TypeReference.IsInput && !newField.TypeReference.IsScalar {
return nil, errors.Errorf("%s cannot be used as a field of %s. only input and scalar types are allowed", newField.GQLType, obj.GQLType)
}

Expand All @@ -91,7 +91,7 @@ func (cfg *Config) buildInput(types NamedTypes, typ *ast.Definition) (*Object, e

// if user has implemented an UnmarshalGQL method on the input type manually, use it
// otherwise we will generate one.
func buildInputMarshaler(typ *ast.Definition, def types.Object) *Ref {
func buildInputMarshaler(typ *ast.Definition, def types.Object) *TypeImplementation {
switch def := def.(type) {
case *types.TypeName:
namedType := def.Type().(*types.Named)
Expand All @@ -103,5 +103,5 @@ func buildInputMarshaler(typ *ast.Definition, def types.Object) *Ref {
}
}

return &Ref{GoType: typ.Name}
return &TypeImplementation{GoType: typ.Name}
}
4 changes: 2 additions & 2 deletions codegen/interface.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package codegen

type Interface struct {
*NamedType
*TypeDefinition

Implementors []InterfaceImplementor
}

type InterfaceImplementor struct {
ValueReceiver bool

*NamedType
*TypeDefinition
}
8 changes: 4 additions & 4 deletions codegen/interface_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func (cfg *Config) buildInterfaces(types NamedTypes, prog *loader.Program) []*In
}

func (cfg *Config) buildInterface(types NamedTypes, typ *ast.Definition, prog *loader.Program) *Interface {
i := &Interface{NamedType: types[typ.Name]}
i := &Interface{TypeDefinition: types[typ.Name]}

for _, implementor := range cfg.schema.GetPossibleTypes(typ) {
t := types[implementor.Name]

i.Implementors = append(i.Implementors, InterfaceImplementor{
NamedType: t,
ValueReceiver: cfg.isValueReceiver(types[typ.Name], t, prog),
TypeDefinition: t,
ValueReceiver: cfg.isValueReceiver(types[typ.Name], t, prog),
})
}

return i
}

func (cfg *Config) isValueReceiver(intf *NamedType, implementor *NamedType, prog *loader.Program) bool {
func (cfg *Config) isValueReceiver(intf *TypeDefinition, implementor *TypeDefinition, prog *loader.Program) bool {
interfaceType, err := findGoInterface(prog, intf.Package, intf.GoType)
if interfaceType == nil || err != nil {
return true
Expand Down
6 changes: 3 additions & 3 deletions codegen/model.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package codegen

type Model struct {
*NamedType
*TypeDefinition
Description string
Fields []ModelField
Implements []*NamedType
Implements []*TypeDefinition
}

type ModelField struct {
*Type
*TypeReference
GQLName string
GoFieldName string
GoFKName string
Expand Down
16 changes: 8 additions & 8 deletions codegen/models_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program) ([]Model,

func (cfg *Config) obj2Model(obj *Object) Model {
model := Model{
NamedType: obj.NamedType,
Implements: obj.Implements,
Fields: []ModelField{},
TypeDefinition: obj.TypeDefinition,
Implements: obj.Implements,
Fields: []ModelField{},
}

model.GoType = ucFirst(obj.GQLType)
model.Marshaler = &Ref{GoType: obj.GoType}
model.Marshaler = &TypeImplementation{GoType: obj.GoType}

for i := range obj.Fields {
field := &obj.Fields[i]
mf := ModelField{Type: field.Type, GQLName: field.GQLName}
mf := ModelField{TypeReference: field.TypeReference, GQLName: field.GQLName}

if field.GoFieldName != "" {
mf.GoFieldName = field.GoFieldName
Expand All @@ -80,12 +80,12 @@ func (cfg *Config) obj2Model(obj *Object) Model {

func int2Model(obj *Interface) Model {
model := Model{
NamedType: obj.NamedType,
Fields: []ModelField{},
TypeDefinition: obj.TypeDefinition,
Fields: []ModelField{},
}

model.GoType = ucFirst(obj.GQLType)
model.Marshaler = &Ref{GoType: obj.GoType}
model.Marshaler = &TypeImplementation{GoType: obj.GoType}

return model
}
12 changes: 6 additions & 6 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ const (
)

type Object struct {
*NamedType
*TypeDefinition

Fields []Field
Satisfies []string
Implements []*NamedType
ResolverInterface *Ref
Implements []*TypeDefinition
ResolverInterface *TypeImplementation
Root bool
DisableConcurrency bool
Stream bool
Directives []*Directive
}

type Field struct {
*Type
*TypeReference
Description string // Description of a field
GQLName string // The name of the field in graphql
GoFieldType GoFieldType // The field type in go, if any
Expand All @@ -49,7 +49,7 @@ type Field struct {
}

type FieldArgument struct {
*Type
*TypeReference

GQLName string // The name of the argument in graphql
GoVarName string // The name of the var in go
Expand Down Expand Up @@ -248,7 +248,7 @@ func (f *Field) CallArgs() string {

// should be in the template, but its recursive and has a bunch of args
func (f *Field) WriteJson() string {
return f.doWriteJson("res", f.Type.Modifiers, f.ASTType, false, 1)
return f.doWriteJson("res", f.TypeReference.Modifiers, f.ASTType, false, 1)
}

func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Type, isPtr bool, depth int) string {
Expand Down
24 changes: 12 additions & 12 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func sanitizeArgName(name string) string {
}

func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition) (*Object, error) {
obj := &Object{NamedType: types[typ.Name]}
obj := &Object{TypeDefinition: types[typ.Name]}
typeEntry, entryExists := cfg.Models[typ.Name]

obj.ResolverInterface = &Ref{GoType: obj.GQLType + "Resolver"}
obj.ResolverInterface = &TypeImplementation{GoType: obj.GQLType + "Resolver"}

if typ == cfg.schema.Query {
obj.Root = true
Expand All @@ -110,7 +110,7 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition) (*Object,
for _, field := range typ.Fields {
if typ == cfg.schema.Query && field.Name == "__type" {
obj.Fields = append(obj.Fields, Field{
Type: &Type{types["__Schema"], []string{modPtr}, ast.NamedType("__Schema", nil), nil},
TypeReference: &TypeReference{types["__Schema"], []string{modPtr}, ast.NamedType("__Schema", nil), nil},
GQLName: "__schema",
GoFieldType: GoFieldMethod,
GoReceiverName: "ec",
Expand All @@ -122,13 +122,13 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition) (*Object,
}
if typ == cfg.schema.Query && field.Name == "__schema" {
obj.Fields = append(obj.Fields, Field{
Type: &Type{types["__Type"], []string{modPtr}, ast.NamedType("__Schema", nil), nil},
TypeReference: &TypeReference{types["__Type"], []string{modPtr}, ast.NamedType("__Schema", nil), nil},
GQLName: "__type",
GoFieldType: GoFieldMethod,
GoReceiverName: "ec",
GoFieldName: "introspectType",
Args: []FieldArgument{
{GQLName: "name", Type: &Type{types["String"], []string{}, ast.NamedType("String", nil), nil}, Object: &Object{}},
{GQLName: "name", TypeReference: &TypeReference{types["String"], []string{}, ast.NamedType("String", nil), nil}, Object: &Object{}},
},
Object: obj,
})
Expand All @@ -151,14 +151,14 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition) (*Object,
return nil, err
}
newArg := FieldArgument{
GQLName: arg.Name,
Type: types.getType(arg.Type),
Object: obj,
GoVarName: sanitizeArgName(arg.Name),
Directives: dirs,
GQLName: arg.Name,
TypeReference: types.getType(arg.Type),
Object: obj,
GoVarName: sanitizeArgName(arg.Name),
Directives: dirs,
}

if !newArg.Type.IsInput && !newArg.Type.IsScalar {
if !newArg.TypeReference.IsInput && !newArg.TypeReference.IsScalar {
return nil, errors.Errorf("%s cannot be used as argument of %s.%s. only input and scalar types are allowed", arg.Type, obj.GQLType, field.Name)
}

Expand All @@ -174,7 +174,7 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition) (*Object,

obj.Fields = append(obj.Fields, Field{
GQLName: field.Name,
Type: types.getType(field.Type),
TypeReference: types.getType(field.Type),
Args: args,
Object: obj,
GoFieldName: goName,
Expand Down
28 changes: 14 additions & 14 deletions codegen/type_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"golang.org/x/tools/go/loader"
)

// namedTypeFromSchema objects for every graphql type, including scalars. There should only be one instance of Type for each thing
// namedTypeFromSchema objects for every graphql type, including scalars. There should only be one instance of TypeReference for each thing
func (cfg *Config) buildNamedTypes() NamedTypes {
types := map[string]*NamedType{}
types := map[string]*TypeDefinition{}
for _, schemaType := range cfg.schema.Types {
t := namedTypeFromSchema(schemaType)

Expand All @@ -37,7 +37,7 @@ func (cfg *Config) bindTypes(namedTypes NamedTypes, destDir string, prog *loader
switch def := def.(type) {
case *types.Func:
sig := def.Type().(*types.Signature)
cpy := t.Ref
cpy := t.TypeImplementation
t.Marshaler = &cpy

t.Package, t.GoType = pkgAndType(sig.Params().At(0).Type().String())
Expand All @@ -47,20 +47,20 @@ func (cfg *Config) bindTypes(namedTypes NamedTypes, destDir string, prog *loader

// namedTypeFromSchema objects for every graphql type, including primitives.
// don't recurse into object fields or interfaces yet, lets make sure we have collected everything first.
func namedTypeFromSchema(schemaType *ast.Definition) *NamedType {
func namedTypeFromSchema(schemaType *ast.Definition) *TypeDefinition {
switch schemaType.Kind {
case ast.Scalar, ast.Enum:
return &NamedType{GQLType: schemaType.Name, IsScalar: true}
return &TypeDefinition{GQLType: schemaType.Name, IsScalar: true}
case ast.Interface, ast.Union:
return &NamedType{GQLType: schemaType.Name, IsInterface: true}
return &TypeDefinition{GQLType: schemaType.Name, IsInterface: true}
case ast.InputObject:
return &NamedType{GQLType: schemaType.Name, IsInput: true}
return &TypeDefinition{GQLType: schemaType.Name, IsInput: true}
default:
return &NamedType{GQLType: schemaType.Name}
return &TypeDefinition{GQLType: schemaType.Name}
}
}

// take a string in the form github.com/package/blah.Type and split it into package and type
// take a string in the form github.com/package/blah.TypeReference and split it into package and type
func pkgAndType(name string) (string, string) {
parts := strings.Split(name, ".")
if len(parts) == 1 {
Expand All @@ -70,7 +70,7 @@ func pkgAndType(name string) (string, string) {
return normalizeVendor(strings.Join(parts[:len(parts)-1], ".")), parts[len(parts)-1]
}

func (n NamedTypes) getType(t *ast.Type) *Type {
func (n NamedTypes) getType(t *ast.Type) *TypeReference {
orig := t
var modifiers []string
for {
Expand All @@ -84,10 +84,10 @@ func (n NamedTypes) getType(t *ast.Type) *Type {
if n[t.NamedType] == nil {
panic("missing type " + t.NamedType)
}
res := &Type{
NamedType: n[t.NamedType],
Modifiers: modifiers,
ASTType: orig,
res := &TypeReference{
TypeDefinition: n[t.NamedType],
Modifiers: modifiers,
ASTType: orig,
}

if res.IsInterface {
Expand Down
Loading

0 comments on commit 34b8787

Please sign in to comment.