/
fieldset.go
190 lines (160 loc) · 4.61 KB
/
fieldset.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
package fieldset
import (
"fmt"
"strings"
"github.com/vektah/gqlparser/v2/ast"
"github.com/99designs/gqlgen/codegen"
"github.com/99designs/gqlgen/codegen/templates"
)
// Set represents a FieldSet that is used in federation directives @key and @requires.
// Would be happier to reuse FieldSet parsing from gqlparser, but this suits for now.
type Set []Field
// Field represents a single field in a FieldSet
type Field []string
// New parses a FieldSet string into a TinyFieldSet.
func New(raw string, prefix []string) Set {
if !strings.Contains(raw, "{") {
return parseUnnestedKeyFieldSet(raw, prefix)
}
var (
ret = Set{}
subPrefix = prefix
)
before, during, after := extractSubs(raw)
if before != "" {
befores := New(before, prefix)
if len(befores) > 0 {
subPrefix = befores[len(befores)-1]
ret = append(ret, befores[:len(befores)-1]...)
}
}
if during != "" {
ret = append(ret, New(during, subPrefix)...)
}
if after != "" {
ret = append(ret, New(after, prefix)...)
}
return ret
}
// FieldDefinition looks up a field in the type.
func (f Field) FieldDefinition(schemaType *ast.Definition, schema *ast.Schema) *ast.FieldDefinition {
objType := schemaType
def := objType.Fields.ForName(f[0])
for _, part := range f[1:] {
if objType.Kind != ast.Object {
panic(fmt.Sprintf(`invalid sub-field reference "%s" in %v: `, objType.Name, f))
}
x := def.Type.Name()
objType = schema.Types[x]
if objType == nil {
panic("invalid schema type: " + x)
}
def = objType.Fields.ForName(part)
}
if def == nil {
return nil
}
ret := *def // shallow copy
ret.Name = f.ToGoPrivate()
return &ret
}
// TypeReference looks up the type of a field.
func (f Field) TypeReference(obj *codegen.Object, objects codegen.Objects) *codegen.Field {
var def *codegen.Field
for _, part := range f {
def = fieldByName(obj, part)
if def == nil {
panic("unable to find field " + f[0])
}
obj = objects.ByName(def.TypeReference.Definition.Name)
}
return def
}
// ToGo converts a (possibly nested) field into a proper public Go name.
func (f Field) ToGo() string {
var ret string
for _, field := range f {
ret += templates.ToGo(field)
}
return ret
}
// ToGoPrivate converts a (possibly nested) field into a proper private Go name.
func (f Field) ToGoPrivate() string {
var ret string
for i, field := range f {
if i == 0 {
field = trimArgumentFromFieldName(field)
ret += templates.ToGoPrivate(field)
continue
}
ret += templates.ToGo(field)
}
return ret
}
// Join concatenates the field parts with a string separator between. Useful in templates.
func (f Field) Join(str string) string {
return strings.Join(f, str)
}
// JoinGo concatenates the Go name of field parts with a string separator between. Useful in templates.
func (f Field) JoinGo(str string) string {
strs := []string{}
for _, s := range f {
strs = append(strs, templates.ToGo(s))
}
return strings.Join(strs, str)
}
func (f Field) LastIndex() int {
return len(f) - 1
}
// local functions
// parseUnnestedKeyFieldSet // handles simple case where none of the fields are nested.
func parseUnnestedKeyFieldSet(raw string, prefix []string) Set {
ret := Set{}
for _, s := range strings.Fields(raw) {
next := append(prefix[0:len(prefix):len(prefix)], s) //nolint:gocritic // set cap=len in order to force slice reallocation
ret = append(ret, next)
}
return ret
}
// extractSubs splits out and trims sub-expressions from before, inside, and after "{}".
func extractSubs(str string) (string, string, string) {
start := strings.Index(str, "{")
end := matchingBracketIndex(str, start)
if start < 0 || end < 0 {
panic("invalid key fieldSet: " + str)
}
return trimArgumentFromFieldName(strings.TrimSpace(str[:start])), strings.TrimSpace(str[start+1 : end]), strings.TrimSpace(str[end+1:])
}
// matchingBracketIndex returns the index of the closing bracket, assuming an open bracket at start.
func matchingBracketIndex(str string, start int) int {
if start < 0 || len(str) <= start+1 {
return -1
}
var depth int
for i, c := range str[start+1:] {
switch c {
case '{':
depth++
case '}':
if depth == 0 {
return start + 1 + i
}
depth--
}
}
return -1
}
func fieldByName(obj *codegen.Object, name string) *codegen.Field {
for _, field := range obj.Fields {
field.Name = trimArgumentFromFieldName(field.Name)
if field.Name == name {
return field
}
}
return nil
}
// trimArgumentFromFieldName removes any arguments from the field name.
// It removes any suffixes from the raw string, starting from the argument-open character `(`
func trimArgumentFromFieldName(raw string) string {
return strings.Split(raw, "(")[0]
}