-
Notifications
You must be signed in to change notification settings - Fork 118
/
ast_argument.go
196 lines (174 loc) · 4.5 KB
/
ast_argument.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package ast
import (
"bytes"
"io"
"github.com/wundergraph/graphql-go-tools/v2/pkg/internal/unsafebytes"
"github.com/wundergraph/graphql-go-tools/v2/pkg/lexer/literal"
"github.com/wundergraph/graphql-go-tools/v2/pkg/lexer/position"
)
type ArgumentList struct {
LPAREN position.Position
Refs []int // Argument
RPAREN position.Position
}
type Argument struct {
Name ByteSliceReference // e.g. foo
Colon position.Position // :
Value Value // e.g. 100 or "Bar"
Position position.Position
PrintBeforeValue []byte
PrintAfterValue []byte
}
func (d *Document) CopyArgument(ref int) int {
return d.AddArgument(Argument{
Name: d.copyByteSliceReference(d.Arguments[ref].Name),
Value: Value{
Kind: d.Arguments[ref].Value.Kind,
Ref: d.copyValueRef(d.Arguments[ref].Value.Kind, d.Arguments[ref].Value.Ref),
},
})
}
func (d *Document) CopyArgumentList(list ArgumentList) ArgumentList {
refs := d.NewEmptyRefs()
for _, r := range list.Refs {
refs = append(refs, d.CopyArgument(r))
}
return ArgumentList{Refs: refs}
}
func (d *Document) PrintArgument(ref int, w io.Writer) error {
_, err := w.Write(d.Input.ByteSlice(d.Arguments[ref].Name))
if err != nil {
return err
}
_, err = w.Write(literal.COLON)
if err != nil {
return err
}
_, err = w.Write(literal.SPACE)
if err != nil {
return err
}
if d.Arguments[ref].PrintBeforeValue != nil {
_, err = w.Write(d.Arguments[ref].PrintBeforeValue)
if err != nil {
return err
}
}
err = d.PrintValue(d.Arguments[ref].Value, w)
if err != nil {
return err
}
if d.Arguments[ref].PrintAfterValue != nil {
_, err = w.Write(d.Arguments[ref].PrintAfterValue)
if err != nil {
return err
}
}
return nil
}
func (d *Document) PrintArguments(refs []int, w io.Writer) (err error) {
_, err = w.Write(literal.LPAREN)
if err != nil {
return
}
for i, j := range refs {
err = d.PrintArgument(j, w)
if err != nil {
return
}
if i != len(refs)-1 {
_, err = w.Write(literal.COMMA)
if err != nil {
return
}
_, err = w.Write(literal.SPACE)
if err != nil {
return
}
}
}
_, err = w.Write(literal.RPAREN)
return
}
func (d *Document) ArgumentNameBytes(ref int) ByteSlice {
return d.Input.ByteSlice(d.Arguments[ref].Name)
}
func (d *Document) ArgumentNameString(ref int) string {
return unsafebytes.BytesToString(d.ArgumentNameBytes(ref))
}
func (d *Document) ArgumentValue(ref int) Value {
return d.Arguments[ref].Value
}
func (d *Document) ArgumentsAreEqual(left, right int) bool {
return bytes.Equal(d.ArgumentNameBytes(left), d.ArgumentNameBytes(right)) &&
d.ValuesAreEqual(d.ArgumentValue(left), d.ArgumentValue(right))
}
func (d *Document) ArgumentSetsAreEquals(left, right []int) bool {
if len(left) != len(right) {
return false
}
for i := 0; i < len(left); i++ {
leftArgument, rightArgument := left[i], right[i]
if !d.ArgumentsAreEqual(leftArgument, rightArgument) {
return false
}
}
return true
}
func (d *Document) ArgumentsBefore(ancestor Node, argument int) []int {
switch ancestor.Kind {
case NodeKindField:
for i, j := range d.Fields[ancestor.Ref].Arguments.Refs {
if argument == j {
return d.Fields[ancestor.Ref].Arguments.Refs[:i]
}
}
case NodeKindDirective:
for i, j := range d.Directives[ancestor.Ref].Arguments.Refs {
if argument == j {
return d.Directives[ancestor.Ref].Arguments.Refs[:i]
}
}
}
return nil
}
func (d *Document) ArgumentsAfter(ancestor Node, argument int) []int {
switch ancestor.Kind {
case NodeKindField:
for i, j := range d.Fields[ancestor.Ref].Arguments.Refs {
if argument == j {
return d.Fields[ancestor.Ref].Arguments.Refs[i+1:]
}
}
case NodeKindDirective:
for i, j := range d.Directives[ancestor.Ref].Arguments.Refs {
if argument == j {
return d.Directives[ancestor.Ref].Arguments.Refs[i+1:]
}
}
}
return nil
}
func (d *Document) AddArgument(argument Argument) (ref int) {
d.Arguments = append(d.Arguments, argument)
return len(d.Arguments) - 1
}
func (d *Document) ImportArgument(name string, value Value) (ref int) {
arg := Argument{
Name: d.Input.AppendInputString(name),
Value: value,
}
return d.AddArgument(arg)
}
func (d *Document) ImportVariableValueArgument(argName, variableName ByteSlice) (variableValueRef, argRef int) {
variableValueRef = d.ImportVariableValue(variableName)
arg := Argument{
Name: d.Input.AppendInputBytes(argName),
Value: Value{
Kind: ValueKindVariable,
Ref: variableValueRef,
},
}
argRef = d.AddArgument(arg)
return
}