Skip to content

Commit

Permalink
fields as slice
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Mar 19, 2017
1 parent 6075990 commit 615afd6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
12 changes: 6 additions & 6 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func makeScalarExec(t *schema.Scalar, resolverType reflect.Type) (iExec, error)
return &scalarExec{}, nil
}

func (b *execBuilder) makeObjectExec(typeName string, fields map[string]*schema.Field, possibleTypes []*schema.Object, nonNull bool, resolverType reflect.Type) (*objectExec, error) {
func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, possibleTypes []*schema.Object, nonNull bool, resolverType reflect.Type) (*objectExec, error) {
if !nonNull {
if resolverType.Kind() != reflect.Ptr && resolverType.Kind() != reflect.Interface {
return nil, fmt.Errorf("%s is not a pointer or interface", resolverType)
Expand All @@ -210,22 +210,22 @@ func (b *execBuilder) makeObjectExec(typeName string, fields map[string]*schema.

methodHasReceiver := resolverType.Kind() != reflect.Interface
fieldExecs := make(map[string]*fieldExec)
for name, f := range fields {
methodIndex := findMethod(resolverType, name)
for _, f := range fields {
methodIndex := findMethod(resolverType, f.Name)
if methodIndex == -1 {
hint := ""
if findMethod(reflect.PtrTo(resolverType), name) != -1 {
if findMethod(reflect.PtrTo(resolverType), f.Name) != -1 {
hint = " (hint: the method exists on the pointer type)"
}
return nil, fmt.Errorf("%s does not resolve %q: missing method for field %q%s", resolverType, typeName, name, hint)
return nil, fmt.Errorf("%s does not resolve %q: missing method for field %q%s", resolverType, typeName, f.Name, hint)
}

m := resolverType.Method(methodIndex)
fe, err := b.makeFieldExec(typeName, f, m, methodIndex, methodHasReceiver)
if err != nil {
return nil, fmt.Errorf("%s\n\treturned by (%s).%s", err, resolverType, m.Name)
}
fieldExecs[name] = fe
fieldExecs[f.Name] = fe
}

typeAssertions := make(map[string]*typeAssertExec)
Expand Down
31 changes: 19 additions & 12 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ type Scalar struct {
type Object struct {
Name string
Interfaces []*Interface
Fields map[string]*Field
FieldOrder []string
Fields FieldList
Desc string

interfaceNames []string
Expand All @@ -49,8 +48,7 @@ type Object struct {
type Interface struct {
Name string
PossibleTypes []*Object
Fields map[string]*Field
FieldOrder []string
Fields FieldList
Desc string
}

Expand Down Expand Up @@ -80,6 +78,17 @@ type InputObject struct {
common.InputMap
}

type FieldList []*Field

func (l FieldList) Get(name string) *Field {
for _, f := range l {
if f.Name == name {
return f
}
}
return nil
}

type Directive struct {
Name string
Desc string
Expand Down Expand Up @@ -349,7 +358,7 @@ func parseObjectDecl(l *lexer.Lexer) *Object {
}
}
l.ConsumeToken('{')
o.Fields, o.FieldOrder = parseFields(l)
o.Fields = parseFields(l)
l.ConsumeToken('}')
return o
}
Expand All @@ -358,7 +367,7 @@ func parseInterfaceDecl(l *lexer.Lexer) *Interface {
i := &Interface{}
i.Name = l.ConsumeIdent()
l.ConsumeToken('{')
i.Fields, i.FieldOrder = parseFields(l)
i.Fields = parseFields(l)
l.ConsumeToken('}')
return i
}
Expand Down Expand Up @@ -430,9 +439,8 @@ func parseDirectiveDecl(l *lexer.Lexer) *Directive {
return d
}

func parseFields(l *lexer.Lexer) (map[string]*Field, []string) {
fields := make(map[string]*Field)
var fieldOrder []string
func parseFields(l *lexer.Lexer) FieldList {
var fields FieldList
for l.Peek() != '}' {
f := &Field{}
f.Desc = l.DescComment()
Expand All @@ -450,8 +458,7 @@ func parseFields(l *lexer.Lexer) (map[string]*Field, []string) {
l.ConsumeToken(':')
f.Type = common.ParseType(l)
f.Directives = common.ParseDirectives(l)
fields[f.Name] = f
fieldOrder = append(fieldOrder, f.Name)
fields = append(fields, f)
}
return fields, fieldOrder
return fields
}
8 changes: 2 additions & 6 deletions introspection/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,18 @@ func (r *Type) Description() *string {
}

func (r *Type) Fields(args *struct{ IncludeDeprecated bool }) *[]*Field {
var fields map[string]*schema.Field
var fieldOrder []string
var fields schema.FieldList
switch t := r.typ.(type) {
case *schema.Object:
fields = t.Fields
fieldOrder = t.FieldOrder
case *schema.Interface:
fields = t.Fields
fieldOrder = t.FieldOrder
default:
return nil
}

var l []*Field
for _, name := range fieldOrder {
f := fields[name]
for _, f := range fields {
if _, ok := f.Directives["deprecated"]; !ok || args.IncludeDeprecated {
l = append(l, &Field{f})
}
Expand Down

0 comments on commit 615afd6

Please sign in to comment.