forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.go
84 lines (77 loc) · 1.55 KB
/
values.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package common
import (
"text/scanner"
"github.com/neelance/graphql-go/internal/lexer"
)
type InputMap struct {
Fields map[string]*InputValue
FieldOrder []string
}
type InputValue struct {
Name string
Type Type
Default interface{}
Desc string
}
func ParseInputValue(l *lexer.Lexer) *InputValue {
p := &InputValue{}
p.Desc = l.DescComment()
p.Name = l.ConsumeIdent()
l.ConsumeToken(':')
p.Type = ParseType(l)
if l.Peek() == '=' {
l.ConsumeToken('=')
p.Default = ParseValue(l, true)
}
return p
}
type Variable string
func ParseValue(l *lexer.Lexer, constOnly bool) interface{} {
switch l.Peek() {
case '$':
if constOnly {
l.SyntaxError("variable not allowed")
panic("unreachable")
}
l.ConsumeToken('$')
return Variable(l.ConsumeIdent())
case scanner.Int:
return l.ConsumeInt()
case scanner.Float:
return l.ConsumeFloat()
case scanner.String:
return l.ConsumeString()
case scanner.Ident:
switch ident := l.ConsumeIdent(); ident {
case "true":
return true
case "false":
return false
case "null":
return nil
default:
return ident
}
case '[':
l.ConsumeToken('[')
var list []interface{}
for l.Peek() != ']' {
list = append(list, ParseValue(l, constOnly))
}
l.ConsumeToken(']')
return list
case '{':
l.ConsumeToken('{')
obj := make(map[string]interface{})
for l.Peek() != '}' {
name := l.ConsumeIdent()
l.ConsumeToken(':')
obj[name] = ParseValue(l, constOnly)
}
l.ConsumeToken('}')
return obj
default:
l.SyntaxError("invalid value")
panic("unreachable")
}
}