From 615afd61aacfdc6f408c518e3a8e4c0928d97209 Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Sun, 19 Mar 2017 18:42:28 +0100 Subject: [PATCH] fields as slice --- internal/exec/exec.go | 12 ++++++------ internal/schema/schema.go | 31 +++++++++++++++++++------------ introspection/introspection.go | 8 ++------ 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/internal/exec/exec.go b/internal/exec/exec.go index ea97f7c5f27..2533a8702d5 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -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) @@ -210,14 +210,14 @@ 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) @@ -225,7 +225,7 @@ func (b *execBuilder) makeObjectExec(typeName string, fields map[string]*schema. 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) diff --git a/internal/schema/schema.go b/internal/schema/schema.go index efb0f8933d5..0b6ab7dc8f4 100644 --- a/internal/schema/schema.go +++ b/internal/schema/schema.go @@ -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 @@ -49,8 +48,7 @@ type Object struct { type Interface struct { Name string PossibleTypes []*Object - Fields map[string]*Field - FieldOrder []string + Fields FieldList Desc string } @@ -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 @@ -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 } @@ -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 } @@ -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() @@ -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 } diff --git a/introspection/introspection.go b/introspection/introspection.go index 18e75097c4d..9b89ed7c23a 100644 --- a/introspection/introspection.go +++ b/introspection/introspection.go @@ -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}) }