-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
54 lines (47 loc) · 970 Bytes
/
request.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
package monday
import (
"fmt"
"strings"
)
type Query struct {
Object string
Where Where
Select Queries
Wrap bool
}
type Queries []Query
func (q Query) String() string {
qParts := []string{q.Object}
if len(q.Where) > 0 {
qParts = append(qParts, q.Where.String())
}
if len(q.Select) == 0 {
return WrapQuery(strings.Join(qParts, " "), q.Wrap)
}
subfields := []string{}
for _, subfield := range q.Select {
subfields = append(subfields, subfield.String())
}
if len(subfields) > 0 {
qParts = append(qParts,
"{"+strings.Join(subfields, " ")+"}")
}
return WrapQuery(strings.Join(qParts, " "), q.Wrap)
}
type Where map[string]string
func (w Where) String() string {
if len(w) == 0 {
return ""
}
parts := []string{}
for k, v := range w {
parts = append(parts, k+":"+v)
}
return "(" + strings.Join(parts, ",") + ")"
}
func WrapQuery(gql string, wrap bool) string {
if wrap {
return fmt.Sprintf("query {%s}", gql)
}
return gql
}