Skip to content

Commit

Permalink
added support for ints and floats in query
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 20, 2016
1 parent 0f85412 commit c12a8ad
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
19 changes: 15 additions & 4 deletions internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,24 @@ func (l *Lexer) ConsumeKeyword(keyword string) {
l.Consume()
}

func (l *Lexer) ConsumeInt() int32 {
text := l.sc.TokenText()
l.ConsumeToken(scanner.Int)
value, _ := strconv.Atoi(text)
return int32(value)
}

func (l *Lexer) ConsumeFloat() float64 {
text := l.sc.TokenText()
l.ConsumeToken(scanner.Int)
value, _ := strconv.ParseFloat(text, 64)
return value
}

func (l *Lexer) ConsumeString() string {
text := l.sc.TokenText()
l.ConsumeToken(scanner.String)
value, err := strconv.Unquote(text)
if err != nil {
l.SyntaxError(err.Error())
}
value, _ := strconv.Unquote(text)
return value
}

Expand Down
10 changes: 9 additions & 1 deletion internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (Literal) isValue() {}

func Parse(queryString string) (doc *Document, err *errors.GraphQLError) {
sc := &scanner.Scanner{
Mode: scanner.ScanIdents | scanner.ScanFloats | scanner.ScanStrings,
Mode: scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings,
}
sc.Init(strings.NewReader(queryString))

Expand Down Expand Up @@ -296,6 +296,14 @@ func parseValue(l *lexer.Lexer) Value {
return &Variable{
Name: l.ConsumeIdent(),
}
case scanner.Int:
return &Literal{
Value: l.ConsumeInt(),
}
case scanner.Float:
return &Literal{
Value: l.ConsumeFloat(),
}
case scanner.String:
return &Literal{
Value: l.ConsumeString(),
Expand Down
2 changes: 1 addition & 1 deletion internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Parameter struct {

func Parse(schemaString string) (s *Schema, err *errors.GraphQLError) {
sc := &scanner.Scanner{
Mode: scanner.ScanIdents | scanner.ScanFloats | scanner.ScanStrings,
Mode: scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings,
}
sc.Init(strings.NewReader(schemaString))

Expand Down

0 comments on commit c12a8ad

Please sign in to comment.