-
Notifications
You must be signed in to change notification settings - Fork 121
/
ast_selection.go
181 lines (149 loc) · 5.1 KB
/
ast_selection.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
package ast
import (
"bytes"
"fmt"
"github.com/wundergraph/graphql-go-tools/pkg/internal/unsafebytes"
"github.com/wundergraph/graphql-go-tools/pkg/lexer/position"
)
type SelectionKind int
const (
SelectionKindUnknown SelectionKind = 18 + iota
SelectionKindField
SelectionKindFragmentSpread
SelectionKindInlineFragment
)
type SelectionSet struct {
LBrace position.Position
RBrace position.Position
SelectionRefs []int
}
type Selection struct {
Kind SelectionKind // one of Field, FragmentSpread, InlineFragment
Ref int // reference to the actual selection
}
func (d *Document) CopySelection(ref int) int {
innerRef := -1
switch d.Selections[ref].Kind {
case SelectionKindField:
innerRef = d.CopyField(d.Selections[ref].Ref)
case SelectionKindFragmentSpread:
innerRef = d.CopyFragmentSpread(d.Selections[ref].Ref)
case SelectionKindInlineFragment:
innerRef = d.CopyInlineFragment(d.Selections[ref].Ref)
}
return d.AddSelectionToDocument(Selection{
Kind: d.Selections[ref].Kind,
Ref: innerRef,
})
}
func (d *Document) CopySelectionSet(ref int) int {
refs := d.NewEmptyRefs()
for _, r := range d.SelectionSets[ref].SelectionRefs {
refs = append(refs, d.CopySelection(r))
}
return d.AddSelectionSetToDocument(SelectionSet{
SelectionRefs: refs,
})
}
func (d *Document) PrintSelections(selections []int) (out string) {
out += "["
for i, ref := range selections {
out += fmt.Sprintf("%+v", d.Selections[ref])
if i != len(selections)-1 {
out += ","
}
}
out += "]"
return
}
func (d *Document) SelectionsBeforeField(field int, selectionSet Node) bool {
if selectionSet.Kind != NodeKindSelectionSet {
return false
}
if len(d.SelectionSets[selectionSet.Ref].SelectionRefs) == 1 {
return false
}
for i, j := range d.SelectionSets[selectionSet.Ref].SelectionRefs {
if d.Selections[j].Kind == SelectionKindField && d.Selections[j].Ref == field {
return i != 0
}
}
return false
}
func (d *Document) SelectionsAfter(selectionKind SelectionKind, selectionRef int, selectionSet Node) bool {
if selectionSet.Kind != NodeKindSelectionSet {
return false
}
if len(d.SelectionSets[selectionSet.Ref].SelectionRefs) == 1 {
return false
}
for i, j := range d.SelectionSets[selectionSet.Ref].SelectionRefs {
if d.Selections[j].Kind == selectionKind && d.Selections[j].Ref == selectionRef {
return i != len(d.SelectionSets[selectionSet.Ref].SelectionRefs)-1
}
}
return false
}
func (d *Document) SelectionsAfterField(field int, selectionSet Node) bool {
return d.SelectionsAfter(SelectionKindField, field, selectionSet)
}
func (d *Document) SelectionsAfterInlineFragment(inlineFragment int, selectionSet Node) bool {
return d.SelectionsAfter(SelectionKindInlineFragment, inlineFragment, selectionSet)
}
func (d *Document) SelectionsAfterFragmentSpread(fragmentSpread int, selectionSet Node) bool {
return d.SelectionsAfter(SelectionKindFragmentSpread, fragmentSpread, selectionSet)
}
func (d *Document) AddSelectionSetToDocument(set SelectionSet) int {
d.SelectionSets = append(d.SelectionSets, set)
return len(d.SelectionSets) - 1
}
func (d *Document) AddSelectionSet() Node {
return Node{
Kind: NodeKindSelectionSet,
Ref: d.AddSelectionSetToDocument(SelectionSet{
SelectionRefs: d.Refs[d.NextRefIndex()][:0],
}),
}
}
func (d *Document) AddSelectionToDocument(selection Selection) int {
d.Selections = append(d.Selections, selection)
return len(d.Selections) - 1
}
func (d *Document) AddSelection(set int, selection Selection) {
d.SelectionSets[set].SelectionRefs = append(d.SelectionSets[set].SelectionRefs, d.AddSelectionToDocument(selection))
}
func (d *Document) EmptySelectionSet(ref int) {
d.SelectionSets[ref].SelectionRefs = d.SelectionSets[ref].SelectionRefs[:0]
}
func (d *Document) AppendSelectionSet(ref int, appendRef int) {
d.SelectionSets[ref].SelectionRefs = append(d.SelectionSets[ref].SelectionRefs, d.SelectionSets[appendRef].SelectionRefs...)
}
func (d *Document) ReplaceSelectionOnSelectionSet(ref, replace, with int) {
d.SelectionSets[ref].SelectionRefs = append(d.SelectionSets[ref].SelectionRefs[:replace], append(d.SelectionSets[with].SelectionRefs, d.SelectionSets[ref].SelectionRefs[replace+1:]...)...)
}
func (d *Document) RemoveFromSelectionSet(ref int, index int) {
d.SelectionSets[ref].SelectionRefs = append(d.SelectionSets[ref].SelectionRefs[:index], d.SelectionSets[ref].SelectionRefs[index+1:]...)
}
func (d *Document) SelectionSetHasFieldSelectionWithNameOrAliasBytes(set int, nameOrAlias []byte) bool {
for _, i := range d.SelectionSets[set].SelectionRefs {
if d.Selections[i].Kind != SelectionKindField {
continue
}
field := d.Selections[i].Ref
fieldName := d.FieldNameBytes(field)
if bytes.Equal(fieldName, nameOrAlias) {
return true
}
if !d.FieldAliasIsDefined(field) {
continue
}
fieldAlias := d.FieldAliasBytes(field)
if bytes.Equal(fieldAlias, nameOrAlias) {
return true
}
}
return false
}
func (d *Document) SelectionSetHasFieldSelectionWithNameOrAliasString(set int, nameOrAlias string) bool {
return d.SelectionSetHasFieldSelectionWithNameOrAliasBytes(set, unsafebytes.StringToBytes(nameOrAlias))
}