...
1
2
3
4 package graphb
5
6 import (
7 "strings"
8
9 "github.com/pkg/errors"
10 )
11
12
13 func StringFromChan(c <-chan string) string {
14 var strs []string
15 for str := range c {
16 strs = append(strs, str)
17 }
18 return strings.Join(strs, "")
19 }
20
21
22
23
24
25
26
27
28 func NewField(name string, options ...FieldOptionInterface) *Field {
29 f := &Field{Name: name}
30 for _, op := range options {
31 if err := op.runFieldOption(f); err != nil {
32 f.E = errors.WithStack(err)
33 return f
34 }
35 }
36 return f
37 }
38
39
40 type FieldOptionInterface interface {
41 runFieldOption(f *Field) error
42 }
43
44
45 type FieldOption func(field *Field) error
46
47 func (fco FieldOption) runFieldOption(f *Field) error {
48 return fco(f)
49 }
50
51
52
53 func OfFields(name ...string) FieldOption {
54 return func(f *Field) error {
55 f.setFields(Fields(name...))
56 return nil
57 }
58 }
59
60 func OfAlias(alias string) FieldOption {
61 return func(f *Field) error {
62 f.Alias = alias
63 return errors.WithStack(f.checkAlias())
64 }
65 }
66
67
68 func OfArguments(arguments ...Argument) FieldOption {
69 return func(f *Field) error {
70 f.Arguments = arguments
71 return nil
72 }
73 }
74
75
76
77
78
79
80
81
82
83 func NewQuery(Type operationType, options ...QueryOptionInterface) *Query {
84
85 q := &Query{Type: Type}
86
87 for _, op := range options {
88 if err := op.runQueryOption(q); err != nil {
89 q.E = errors.WithStack(err)
90 return q
91 }
92 }
93 return q
94 }
95
96
97 type QueryOptionInterface interface {
98 runQueryOption(q *Query) error
99 }
100
101
102 type QueryOption func(query *Query) error
103
104 func (qo QueryOption) runQueryOption(query *Query) error {
105 return qo(query)
106 }
107
108
109 func OfName(name string) QueryOption {
110 return func(query *Query) error {
111 query.Name = name
112 if err := query.checkName(); err != nil {
113 return errors.WithStack(err)
114 }
115 return nil
116 }
117 }
118
119
120
121
122 type fieldContainer interface {
123 getFields() []*Field
124 setFields([]*Field)
125 }
126
127
128
129
130
131 type FieldContainerOption func(fc fieldContainer) error
132
133 func (fco FieldContainerOption) runQueryOption(q *Query) error {
134 return fco(q)
135 }
136
137 func (fco FieldContainerOption) runFieldOption(f *Field) error {
138 return fco(f)
139 }
140
141
142
143 func OfField(name string, options ...FieldOptionInterface) FieldContainerOption {
144 return func(fc fieldContainer) error {
145 f := NewField(name, options...)
146 if f.E != nil {
147 return errors.WithStack(f.E)
148 }
149 fc.setFields(append(fc.getFields(), f))
150 return nil
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 func Fields(args ...string) []*Field {
169 fields := make([]*Field, len(args))
170 for i, name := range args {
171 fields[i] = &Field{
172 Name: name,
173 }
174 }
175 return fields
176 }
177
View as plain text