Fix case-sensitivity bug in GraphQL parser

This commit is contained in:
Vikram Rangnekar
2020-01-07 23:44:19 -05:00
parent 7a5cf47486
commit c40ff38b05
6 changed files with 53 additions and 34 deletions

View File

@ -29,9 +29,9 @@ type Pos int
// item represents a token or text string returned from the scanner.
type item struct {
_type itemType // The type of this item.
pos Pos // The starting position, in bytes, of this item in the input string.
end Pos // The ending position, in bytes, of this item in the input string.
line uint16 // The line number at the start of this item.
pos Pos // The starting position, in bytes, of this item in the input string.
end Pos // The ending position, in bytes, of this item in the input string.
line uint16 // The line number at the start of this item.
}
// itemType identifies the type of lex items.
@ -147,6 +147,12 @@ func (l *lexer) emit(t itemType) {
l.start = l.pos
}
func (l *lexer) emitL(t itemType) {
s, e := l.current()
lowercase(l.input, s, e)
l.emit(t)
}
// ignore skips over the pending input before this point.
func (l *lexer) ignore() {
for i := l.start; i < l.pos; i++ {
@ -211,7 +217,7 @@ func lex(l *lexer, input []byte) error {
l.run()
if last := l.items[len(l.items)-1]; last._type== itemError {
if last := l.items[len(l.items)-1]; last._type == itemError {
return l.err
}
return nil
@ -295,19 +301,17 @@ func lexName(l *lexer) stateFn {
l.backup()
s, e := l.current()
lowercase(l.input, s, e)
switch {
case equals(l.input, s, e, queryToken):
l.emit(itemQuery)
l.emitL(itemQuery)
case equals(l.input, s, e, mutationToken):
l.emit(itemMutation)
l.emitL(itemMutation)
case equals(l.input, s, e, subscriptionToken):
l.emit(itemSub)
l.emitL(itemSub)
case equals(l.input, s, e, trueToken):
l.emit(itemBoolVal)
l.emitL(itemBoolVal)
case equals(l.input, s, e, falseToken):
l.emit(itemBoolVal)
l.emitL(itemBoolVal)
default:
l.emit(itemName)
}
@ -435,7 +439,7 @@ func lowercase(b []byte, s Pos, e Pos) {
func (i *item) String() string {
var v string
switch i._type{
switch i._type {
case itemEOF:
v = "EOF"
case itemError:

View File

@ -329,14 +329,14 @@ func (p *Parser) parseFields(fields []Field) ([]Field, error) {
func (p *Parser) parseField(f *Field) error {
var err error
f.Name = p.val(p.next())
f.Name = p.vall(p.next())
if p.peek(itemColon) {
p.ignore()
if p.peek(itemName) {
f.Alias = f.Name
f.Name = p.val(p.next())
f.Name = p.vall(p.next())
} else {
return errors.New("expecting an aliased field name")
}
@ -496,6 +496,11 @@ func (p *Parser) val(v item) string {
return b2s(p.input[v.pos:v.end])
}
func (p *Parser) vall(v item) string {
lowercase(p.input, v.pos, v.end)
return b2s(p.input[v.pos:v.end])
}
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}