forked from wundergraph/graphql-go-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astimport.go
229 lines (186 loc) · 6.88 KB
/
astimport.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Package astimport can be used to import Nodes manually into an AST.
//
// This is useful when an AST should be created manually.
package astimport
import (
"fmt"
"github.com/matthewmcneely/graphql-go-tools/v2/pkg/ast"
)
// Importer imports Nodes into an existing AST.
// Always use NewImporter() to create a new Importer.
type Importer struct {
}
func (i *Importer) ImportDirective(ref int, from, to *ast.Document) int {
name := string(from.Input.ByteSlice(from.Directives[ref].Name))
args := i.ImportArguments(from.Directives[ref].Arguments.Refs, from, to)
return to.AddDirective(ast.Directive{
Name: to.Input.AppendInputString(name),
HasArguments: len(args) != 0,
Arguments: ast.ArgumentList{
Refs: args,
},
})
}
func (i *Importer) ImportDirectiveWithRename(ref int, renameTo string, from, to *ast.Document) int {
args := i.ImportArguments(from.Directives[ref].Arguments.Refs, from, to)
return to.AddDirective(ast.Directive{
Name: to.Input.AppendInputString(renameTo),
HasArguments: len(args) != 0,
Arguments: ast.ArgumentList{
Refs: args,
},
})
}
func (i *Importer) ImportType(ref int, from, to *ast.Document) int {
astType := ast.Type{
TypeKind: from.Types[ref].TypeKind,
OfType: -1,
}
if astType.TypeKind == ast.TypeKindNamed {
astType.Name = to.Input.AppendInputBytes(from.TypeNameBytes(ref))
}
if from.Types[ref].OfType != -1 {
astType.OfType = i.ImportType(from.Types[ref].OfType, from, to)
}
to.Types = append(to.Types, astType)
return len(to.Types) - 1
}
func (i *Importer) ImportTypeWithRename(ref int, from, to *ast.Document, renameTo string) int {
astType := ast.Type{
TypeKind: from.Types[ref].TypeKind,
OfType: -1,
}
if astType.TypeKind == ast.TypeKindNamed {
astType.Name = to.Input.AppendInputString(renameTo)
}
if from.Types[ref].OfType != -1 {
astType.OfType = i.ImportTypeWithRename(from.Types[ref].OfType, from, to, renameTo)
}
to.Types = append(to.Types, astType)
return len(to.Types) - 1
}
func (i *Importer) ImportValue(fromValue ast.Value, from, to *ast.Document) (value ast.Value) {
value.Kind = fromValue.Kind
switch fromValue.Kind {
case ast.ValueKindFloat:
value.Ref = to.ImportFloatValue(
from.FloatValueRaw(fromValue.Ref),
from.FloatValueIsNegative(fromValue.Ref))
case ast.ValueKindInteger:
value.Ref = to.ImportIntValue(
from.IntValueRaw(fromValue.Ref),
from.IntValueIsNegative(fromValue.Ref))
case ast.ValueKindBoolean:
value.Ref = fromValue.Ref
case ast.ValueKindString:
value.Ref = to.ImportStringValue(
from.StringValueContentBytes(fromValue.Ref),
from.StringValueIsBlockString(fromValue.Ref))
case ast.ValueKindNull:
// empty case
case ast.ValueKindEnum:
value.Ref = to.ImportEnumValue(from.EnumValueNameBytes(fromValue.Ref))
case ast.ValueKindVariable:
value.Ref = to.ImportVariableValue(from.VariableValueNameBytes(fromValue.Ref))
case ast.ValueKindList:
value.Ref = to.ImportListValue(i.ImportListValues(fromValue.Ref, from, to))
case ast.ValueKindObject:
value.Ref = to.ImportObjectValue(i.ImportObjectFields(fromValue.Ref, from, to))
default:
value.Kind = ast.ValueKindUnknown
fmt.Printf("astimport.Importer.ImportValue: not implemented for ValueKind: %s\n", fromValue.Kind)
}
return
}
func (i *Importer) ImportObjectFields(ref int, from, to *ast.Document) (refs []int) {
objValue := from.ObjectValues[ref]
for _, fieldRef := range objValue.Refs {
objectField := from.ObjectFields[fieldRef]
refs = append(refs, to.ImportObjectField(
from.ObjectFieldNameBytes(fieldRef),
i.ImportValue(objectField.Value, from, to)))
}
return
}
func (i *Importer) ImportListValues(ref int, from, to *ast.Document) (refs []int) {
listValue := from.ListValues[ref]
for _, valueRef := range listValue.Refs {
value := i.ImportValue(from.Values[valueRef], from, to)
refs = append(refs, to.AddValue(value))
}
return
}
func (i *Importer) ImportArgument(ref int, from, to *ast.Document) int {
arg := ast.Argument{
Name: to.Input.AppendInputBytes(from.ArgumentNameBytes(ref)),
Value: i.ImportValue(from.ArgumentValue(ref), from, to),
}
to.Arguments = append(to.Arguments, arg)
return len(to.Arguments) - 1
}
func (i *Importer) ImportArguments(refs []int, from, to *ast.Document) []int {
args := make([]int, len(refs))
for j, k := range refs {
args[j] = i.ImportArgument(k, from, to)
}
return args
}
func (i *Importer) ImportVariableDefinition(ref int, from, to *ast.Document) int {
variableDefinition := ast.VariableDefinition{
VariableValue: i.ImportValue(from.VariableDefinitions[ref].VariableValue, from, to),
Type: i.ImportType(from.VariableDefinitions[ref].Type, from, to),
DefaultValue: ast.DefaultValue{
IsDefined: from.VariableDefinitions[ref].DefaultValue.IsDefined,
},
// HasDirectives: false, //TODO: implement import directives
// Directives: ast.DirectiveList{},
}
if from.VariableDefinitions[ref].DefaultValue.IsDefined {
variableDefinition.DefaultValue.Value = i.ImportValue(from.VariableDefinitions[ref].DefaultValue.Value, from, to)
}
to.VariableDefinitions = append(to.VariableDefinitions, variableDefinition)
return len(to.VariableDefinitions) - 1
}
func (i *Importer) ImportVariableDefinitionWithRename(ref int, from, to *ast.Document, renameTo string) int {
variableDefinition := ast.VariableDefinition{
VariableValue: i.ImportValue(from.VariableDefinitions[ref].VariableValue, from, to),
Type: i.ImportTypeWithRename(from.VariableDefinitions[ref].Type, from, to, renameTo),
DefaultValue: ast.DefaultValue{
IsDefined: from.VariableDefinitions[ref].DefaultValue.IsDefined,
},
// HasDirectives: false, //TODO: implement import directives
// Directives: ast.DirectiveList{},
}
if from.VariableDefinitions[ref].DefaultValue.IsDefined {
variableDefinition.DefaultValue.Value = i.ImportValue(from.VariableDefinitions[ref].DefaultValue.Value, from, to)
}
to.VariableDefinitions = append(to.VariableDefinitions, variableDefinition)
return len(to.VariableDefinitions) - 1
}
func (i *Importer) ImportVariableDefinitions(refs []int, from, to *ast.Document) []int {
definitions := make([]int, len(refs))
for j, k := range refs {
definitions[j] = i.ImportVariableDefinition(k, from, to)
}
return definitions
}
func (i *Importer) ImportField(ref int, from, to *ast.Document) int {
field := ast.Field{
Alias: ast.Alias{
IsDefined: from.FieldAliasIsDefined(ref),
},
Name: to.Input.AppendInputBytes(from.FieldNameBytes(ref)),
HasArguments: from.FieldHasArguments(ref),
// HasDirectives: from.FieldHasDirectives(ref), // HasDirectives: false, //TODO: implement import directives
SelectionSet: -1,
HasSelections: false,
}
if field.Alias.IsDefined {
field.Alias.Name = to.Input.AppendInputBytes(from.FieldAliasBytes(ref))
}
if field.HasArguments {
field.Arguments.Refs = i.ImportArguments(from.FieldArguments(ref), from, to)
}
to.Fields = append(to.Fields, field)
return len(to.Fields) - 1
}