Skip to content

Commit

Permalink
refactor: lexer.Ident
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Mar 22, 2017
1 parent 254afa8 commit 9c054f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ func (e *objectExec) execField(ctx context.Context, r *request, f *query.Field,
}

func (e *objectExec) execFragment(ctx context.Context, r *request, frag *query.Fragment, resolver reflect.Value, addResult addResultFn) {
if frag.On != "" && frag.On != e.name {
a, ok := e.typeAssertions[frag.On]
if frag.On.Name != "" && frag.On.Name != e.name {
a, ok := e.typeAssertions[frag.On.Name]
if !ok {
panic(fmt.Errorf("%q does not implement %q", frag.On, e.name)) // TODO proper error handling
}
Expand Down
16 changes: 14 additions & 2 deletions internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type Literal struct {
Text string
}

type Ident struct {
Name string
Loc *errors.Location
}

type Variable string

func New(sc *scanner.Scanner) *Lexer {
Expand Down Expand Up @@ -75,9 +80,16 @@ func (l *Lexer) Consume() {
}

func (l *Lexer) ConsumeIdent() string {
text := l.sc.TokenText()
name := l.sc.TokenText()
l.ConsumeToken(scanner.Ident)
return name
}

func (l *Lexer) ConsumeIdentWithLoc() Ident {
loc := l.Location()
name := l.sc.TokenText()
l.ConsumeToken(scanner.Ident)
return text
return Ident{name, loc}
}

func (l *Lexer) ConsumeKeyword(keyword string) {
Expand Down
10 changes: 3 additions & 7 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ type NamedFragment struct {
}

type Fragment struct {
On string
OnLoc *errors.Location
On lexer.Ident
SelSet *SelectionSet
Directives map[string]common.ArgumentList
}
Expand Down Expand Up @@ -125,7 +124,6 @@ func resolveSelection(doc *Document, sel Selection) (Selection, *errors.QueryErr
}
return &Fragment{
On: frag.On,
OnLoc: frag.OnLoc,
SelSet: frag.SelSet,
Directives: sel.Directives,
}, nil
Expand Down Expand Up @@ -190,8 +188,7 @@ func parseFragment(l *lexer.Lexer) *NamedFragment {
f := &NamedFragment{}
f.Name = l.ConsumeIdent()
l.ConsumeKeyword("on")
f.OnLoc = l.Location()
f.On = l.ConsumeIdent()
f.On = l.ConsumeIdentWithLoc()
f.SelSet = parseSelectionSet(l)
return f
}
Expand Down Expand Up @@ -248,8 +245,7 @@ func parseSpread(l *lexer.Lexer) Selection {
fs.Directives = common.ParseDirectives(l)
return fs
}
f.OnLoc = l.Location()
f.On = l.ConsumeIdent()
f.On = l.ConsumeIdentWithLoc()
}
f.Directives = common.ParseDirectives(l)
f.SelSet = parseSelectionSet(l)
Expand Down
10 changes: 5 additions & 5 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func Validate(s *schema.Schema, q *query.Document) (errs []*errors.QueryError) {

for _, frag := range q.Fragments {
errs = append(errs, validateDirectives(s, frag.Directives)...)
t, ok := s.Types[frag.On]
t, ok := s.Types[frag.On.Name]
if !ok {
continue
}
if !canBeFragment(t) {
errs = append(errs, errors.ErrorfWithLoc(frag.OnLoc, "Fragment %q cannot condition on non composite type %q.", frag.Name, t))
errs = append(errs, errors.ErrorfWithLoc(frag.On.Loc, "Fragment %q cannot condition on non composite type %q.", frag.Name, t))
continue
}
errs = append(errs, validateSelectionSet(s, frag.SelSet, t)...)
Expand Down Expand Up @@ -105,11 +105,11 @@ func validateSelection(s *schema.Schema, sel query.Selection, t common.Type) (er

case *query.Fragment:
errs = append(errs, validateDirectives(s, sel.Directives)...)
if sel.On != "" {
t = s.Types[sel.On]
if sel.On.Name != "" {
t = s.Types[sel.On.Name]
}
if !canBeFragment(t) {
errs = append(errs, errors.ErrorfWithLoc(sel.OnLoc, "Fragment cannot condition on non composite type %q.", t))
errs = append(errs, errors.ErrorfWithLoc(sel.On.Loc, "Fragment cannot condition on non composite type %q.", t))
return
}
errs = append(errs, validateSelectionSet(s, sel.SelSet, t)...)
Expand Down

0 comments on commit 9c054f5

Please sign in to comment.