-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
73 lines (56 loc) · 1.11 KB
/
field.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
package graphqb
import "strings"
type Field struct {
Name string
Alias string
Arguments []*Argument
Fields []*Field
Err error
}
func NewField(name string) *Field {
return &Field{Name: name}
}
func (f *Field) Stringify() string {
var sb strings.Builder
if len(f.Alias) > 0 {
sb.WriteString(f.Alias + qColumn)
}
sb.WriteString(f.Name)
if len(f.Arguments) > 0 {
sb.WriteString(qLP)
for k, argument := range f.Arguments {
if k != 0 {
sb.WriteString(qComma)
}
sb.WriteString(argument.Stringify())
}
sb.WriteString(qRP)
}
if len(f.Fields) > 0 {
sb.WriteString(qLB)
for k, field := range f.Fields {
if k != 0 {
sb.WriteString(qComma)
}
sb.WriteString(field.Stringify())
}
sb.WriteString(qRB)
}
return sb.String()
}
func (f *Field) SetArguments(args ...*Argument) *Field {
f.Arguments = args
return f
}
func (f *Field) SetFields(fs ...*Field) *Field {
f.Fields = fs
return f
}
func (f *Field) AddField(fs *Field) *Field {
f.Fields = append(f.Fields, fs)
return f
}
func (f *Field) SetAlias(alias string) *Field {
f.Alias = alias
return f
}