Skip to content

Commit

Permalink
refactor directives
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Mar 12, 2017
1 parent faf5384 commit 8f5605a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 55 deletions.
24 changes: 24 additions & 0 deletions internal/common/directive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package common

import (
"github.com/neelance/graphql-go/internal/lexer"
)

type Directive struct {
Name string
Args map[string]interface{}
}

func ParseDirectives(l *lexer.Lexer) map[string]*Directive {
directives := make(map[string]*Directive)
for l.Peek() == '@' {
l.ConsumeToken('@')
name := l.ConsumeIdent()
var args map[string]interface{}
if l.Peek() == '(' {
args = ParseArguments(l)
}
directives[name] = &Directive{Name: name, Args: args}
}
return directives
}
13 changes: 13 additions & 0 deletions internal/common/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ func ParseInputValue(l *lexer.Lexer) *InputValue {

type Variable string

func ParseArguments(l *lexer.Lexer) map[string]interface{} {
args := make(map[string]interface{})
l.ConsumeToken('(')
for l.Peek() != ')' {
name := l.ConsumeIdent()
l.ConsumeToken(':')
value := ParseValue(l, false)
args[name] = value
}
l.ConsumeToken(')')
return args
}

func ParseValue(l *lexer.Lexer, constOnly bool) interface{} {
switch l.Peek() {
case '$':
Expand Down
6 changes: 3 additions & 3 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,10 @@ type typeAssertExec struct {
typeExec iExec
}

func skipByDirective(r *request, d map[string]*query.Directive) bool {
func skipByDirective(r *request, d map[string]*common.Directive) bool {
if skip, ok := d["skip"]; ok {
p := valuePacker{valueType: boolType}
v, err := p.pack(r, r.resolveVar(skip.Arguments["if"]))
v, err := p.pack(r, r.resolveVar(skip.Args["if"]))
if err != nil {
r.addError(errors.Errorf("%s", err))
}
Expand All @@ -685,7 +685,7 @@ func skipByDirective(r *request, d map[string]*query.Directive) bool {

if include, ok := d["include"]; ok {
p := valuePacker{valueType: boolType}
v, err := p.pack(r, r.resolveVar(include.Arguments["if"]))
v, err := p.pack(r, r.resolveVar(include.Args["if"]))
if err != nil {
r.addError(errors.Errorf("%s", err))
}
Expand Down
62 changes: 10 additions & 52 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,18 @@ type Field struct {
Alias string
Name string
Arguments map[string]interface{}
Directives map[string]*Directive
Directives map[string]*common.Directive
SelSet *SelectionSet
}

type Directive struct {
Name string
Arguments map[string]interface{}
}

type FragmentSpread struct {
Name string
Directives map[string]*Directive
Directives map[string]*common.Directive
}

type InlineFragment struct {
Fragment
Directives map[string]*Directive
Directives map[string]*common.Directive
}

func (Field) isSelection() {}
Expand Down Expand Up @@ -180,77 +175,40 @@ func parseSelection(l *lexer.Lexer) Selection {
}

func parseField(l *lexer.Lexer) *Field {
f := &Field{
Directives: make(map[string]*Directive),
}
f := &Field{}
f.Alias = l.ConsumeIdent()
f.Name = f.Alias
if l.Peek() == ':' {
l.ConsumeToken(':')
f.Name = l.ConsumeIdent()
}
if l.Peek() == '(' {
f.Arguments = parseArguments(l)
}
for l.Peek() == '@' {
d := parseDirective(l)
f.Directives[d.Name] = d
f.Arguments = common.ParseArguments(l)
}
f.Directives = common.ParseDirectives(l)
if l.Peek() == '{' {
f.SelSet = parseSelectionSet(l)
}
return f
}

func parseArguments(l *lexer.Lexer) map[string]interface{} {
args := make(map[string]interface{})
l.ConsumeToken('(')
for l.Peek() != ')' {
name := l.ConsumeIdent()
l.ConsumeToken(':')
value := common.ParseValue(l, false)
args[name] = value
}
l.ConsumeToken(')')
return args
}

func parseDirective(l *lexer.Lexer) *Directive {
d := &Directive{}
l.ConsumeToken('@')
d.Name = l.ConsumeIdent()
if l.Peek() == '(' {
d.Arguments = parseArguments(l)
}
return d
}

func parseSpread(l *lexer.Lexer) Selection {
l.ConsumeToken('.')
l.ConsumeToken('.')
l.ConsumeToken('.')
ident := l.ConsumeIdent()

if ident == "on" {
f := &InlineFragment{
Directives: make(map[string]*Directive),
}
f := &InlineFragment{}
f.On = l.ConsumeIdent()
for l.Peek() == '@' {
d := parseDirective(l)
f.Directives[d.Name] = d
}
f.Directives = common.ParseDirectives(l)
f.SelSet = parseSelectionSet(l)
return f
}

fs := &FragmentSpread{
Directives: make(map[string]*Directive),
Name: ident,
}
for l.Peek() == '@' {
d := parseDirective(l)
fs.Directives[d.Name] = d
Name: ident,
}
fs.Directives = common.ParseDirectives(l)
return fs
}

0 comments on commit 8f5605a

Please sign in to comment.