This repository was archived by the owner on Dec 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.go
More file actions
92 lines (84 loc) · 2.06 KB
/
query.go
File metadata and controls
92 lines (84 loc) · 2.06 KB
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
package monday
import (
"fmt"
"strings"
)
func NewQueryPayload(queries ...Query) Payload {
return Payload{queries: queries}
}
type Query struct {
name string
fields []field
args []argument
}
func (q Query) stringify() string {
fields := make([]string, 0)
for _, field := range q.fields {
fields = append(fields, field.stringify())
}
args := make([]string, 0)
for _, arg := range q.args {
args = append(args, arg.stringify())
}
if len(fields) == 0 {
return ``
}
if len(args) == 0 {
return fmt.Sprintf(`%s{%s}`, q.name, strings.Join(fields, " "))
}
return fmt.Sprintf(`%s(%s){%s}`, q.name, strings.Join(args, ","), strings.Join(fields, " "))
}
type field struct {
field string
value *Query
}
func (f field) stringify() string {
if f.value != nil {
return f.value.stringify()
}
return fmt.Sprint(f.field)
}
type argument struct {
argument string
value interface{}
}
func (a argument) stringify() string {
switch a.argument {
case "column_id", "column_value":
return fmt.Sprintf("%s:%q", a.argument, a.value)
case "ids":
if strs, ok := a.value.([]string); ok {
switch {
case len(strs) == 1:
return fmt.Sprintf("ids:%q", strs[0])
case len(strs) > 1:
return fmt.Sprintf("ids:%v", strings.Replace(fmt.Sprintf("%q", strs), " ", ",", -1))
default:
return ""
}
}
switch ids := a.value.([]int); {
case len(ids) == 1:
return fmt.Sprintf("ids:%v", ids[0])
case len(ids) > 1:
return fmt.Sprintf("ids:%v", strings.Replace(fmt.Sprint(ids), " ", ",", -1))
default:
return ""
}
default:
switch a.value.(type) {
case string:
return fmt.Sprintf("%s:%q", a.argument, a.value)
case BoardsKind:
return fmt.Sprintf("%s:%v", a.argument, a.value.(BoardsKind).kind)
case UsersKind:
return fmt.Sprintf("%s:%v", a.argument, a.value.(UsersKind).kind)
case ColumnsType:
return fmt.Sprintf("%s:%v", a.argument, a.value.(ColumnsType).typ)
case NotificationType:
return fmt.Sprintf("%s:%v", a.argument, a.value.(NotificationType).kind)
default:
return fmt.Sprintf("%s:%v", a.argument, a.value)
}
}
}