1package common
2
3import (
4 "github.com/vektah/gqlgen/neelance/errors"
5)
6
7type InputValue struct {
8 Name Ident
9 Type Type
10 Default Literal
11 Desc string
12 Loc errors.Location
13 TypeLoc errors.Location
14}
15
16type InputValueList []*InputValue
17
18func (l InputValueList) Get(name string) *InputValue {
19 for _, v := range l {
20 if v.Name.Name == name {
21 return v
22 }
23 }
24 return nil
25}
26
27func ParseInputValue(l *Lexer) *InputValue {
28 p := &InputValue{}
29 p.Loc = l.Location()
30 p.Desc = l.DescComment()
31 p.Name = l.ConsumeIdentWithLoc()
32 l.ConsumeToken(':')
33 p.TypeLoc = l.Location()
34 p.Type = ParseType(l)
35 if l.Peek() == '=' {
36 l.ConsumeToken('=')
37 p.Default = ParseLiteral(l, true)
38 }
39 return p
40}
41
42type Argument struct {
43 Name Ident
44 Value Literal
45}
46
47type ArgumentList []Argument
48
49func (l ArgumentList) Get(name string) (Literal, bool) {
50 for _, arg := range l {
51 if arg.Name.Name == name {
52 return arg.Value, true
53 }
54 }
55 return nil, false
56}
57
58func (l ArgumentList) MustGet(name string) Literal {
59 value, ok := l.Get(name)
60 if !ok {
61 panic("argument not found")
62 }
63 return value
64}
65
66func ParseArguments(l *Lexer) ArgumentList {
67 var args ArgumentList
68 l.ConsumeToken('(')
69 for l.Peek() != ')' {
70 name := l.ConsumeIdentWithLoc()
71 l.ConsumeToken(':')
72 value := ParseLiteral(l, false)
73 args = append(args, Argument{Name: name, Value: value})
74 }
75 l.ConsumeToken(')')
76 return args
77}