1
1
package query
2
2
3
3
import (
4
- "fmt"
5
- "reflect"
4
+ "encoding/json"
6
5
)
7
6
7
+ func toArray(val interface{}) []interface{} {
8
+ switch v := val.(type) {
9
+ case nil:
10
+ return nil
11
+ case []interface{}:
12
+ return v
13
+ default:
14
+ return []interface{}{val}
15
+ }
16
+ }
8
17
9
- func parseValue(value interface{}) string {
10
- if val, ok := value.(string); ok {
11
- return fmt.Sprintf("\"%s\"", val)
18
+ type queryOptions struct {
19
+ Method string
20
+ Attribute *string
21
+ Values *[]interface{}
22
+ }
23
+
24
+ func parseQuery(options queryOptions) string {
25
+ data := struct {
26
+ Method string `json:"method"`
27
+ Attribute string `json:"attribute,omitempty"`
28
+ Values []interface{} `json:"values,omitempty"`
29
+ }{
30
+ Method: options.Method,
12
31
}
13
- return fmt.Sprintf("%v", value)
14
- }
15
-
16
- func parseQuery(attribute string, method string, value interface{}) string {
17
- stringValue := ""
18
- switch reflect.TypeOf(value).Kind() {
19
- case reflect.Array, reflect.Slice:
20
- arr := reflect.Indirect(reflect.ValueOf(value))
21
- for i := 0; i < arr.Len(); i++ {
22
- stringValue += parseValue(arr.Index(i).Interface())
23
- if i < arr.Len()-1 {
24
- stringValue += ","
25
- }
26
- }
27
- default:
28
- stringValue = parseValue(value)
32
+
33
+ if options.Attribute != nil {
34
+ data.Attribute = *options.Attribute
29
35
}
30
- return fmt.Sprintf("%s(\"%s\", [%s])", method, attribute, stringValue)
36
+
37
+ if options.Values != nil {
38
+ data.Values = *options.Values
39
+ }
40
+
41
+ jsonData, _ := json.Marshal(data)
42
+
43
+ return string(jsonData)
31
44
}
32
45
33
46
func Equal(attribute string, value interface{}) string {
34
- return parseQuery(attribute, "equal", value)
47
+ values := toArray(value)
48
+ return parseQuery(queryOptions{
49
+ Method: "equal",
50
+ Attribute: & attribute,
51
+ Values: & values,
52
+ })
35
53
}
36
54
37
55
func NotEqual(attribute string, value interface{}) string {
38
- return parseQuery(attribute, "notEqual", value)
56
+ values := toArray(value)
57
+ return parseQuery(queryOptions{
58
+ Method: "notEqual",
59
+ Attribute: & attribute,
60
+ Values: & values,
61
+ })
39
62
}
40
63
41
64
func LessThan(attribute string, value interface{}) string {
42
- return parseQuery(attribute, "lessThan", value)
65
+ values := toArray(value)
66
+ return parseQuery(queryOptions{
67
+ Method: "lessThan",
68
+ Attribute: & attribute,
69
+ Values: & values,
70
+ })
43
71
}
44
72
45
73
func LessThanEqual(attribute string, value interface{}) string {
46
- return parseQuery(attribute, "lessThanEqual", value)
74
+ values := toArray(value)
75
+ return parseQuery(queryOptions{
76
+ Method: "lessThanEqual",
77
+ Attribute: & attribute,
78
+ Values: & values,
79
+ })
47
80
}
48
81
49
82
func GreaterThan(attribute string, value interface{}) string {
50
- return parseQuery(attribute, "greaterThan", value)
83
+ values := toArray(value)
84
+ return parseQuery(queryOptions{
85
+ Method: "greaterThan",
86
+ Attribute: & attribute,
87
+ Values: & values,
88
+ })
51
89
}
52
90
53
91
func GreaterThanEqual(attribute string, value interface{}) string {
54
- return parseQuery(attribute, "greaterThanEqual", value)
92
+ values := toArray(value)
93
+ return parseQuery(queryOptions{
94
+ Method: "greaterThanEqual",
95
+ Attribute: & attribute,
96
+ Values: & values,
97
+ })
55
98
}
56
99
57
- func Search(attribute string, value string) string {
58
- return parseQuery(attribute, "search", value)
100
+ func Search(attribute string, value interface{}) string {
101
+ values := toArray(value)
102
+ return parseQuery(queryOptions{
103
+ Method: "search",
104
+ Attribute: & attribute,
105
+ Values: & values,
106
+ })
59
107
}
60
108
61
109
func IsNull(attribute string) string {
62
- return fmt.Sprintf("isNull(\"%s\")", attribute)
110
+ return parseQuery(queryOptions{
111
+ Method: "isNull",
112
+ Attribute: & attribute,
113
+ })
63
114
}
64
115
65
116
func IsNotNull(attribute string) string {
66
- return fmt.Sprintf("isNotNull(\"%s\")", attribute)
117
+ return parseQuery(queryOptions{
118
+ Method: "isNotNull",
119
+ Attribute: & attribute,
120
+ })
67
121
}
68
122
69
123
func Between(attribute string, start, end interface{}) string {
70
- return parseQuery(attribute, "between", []interface{}{start, end})
124
+ values := []interface{}{start, end}
125
+ return parseQuery(queryOptions{
126
+ Method: "between",
127
+ Attribute: & attribute,
128
+ Values: & values,
129
+ })
71
130
}
72
131
73
132
func StartsWith(attribute string, value interface{}) string {
74
- return parseQuery(attribute, "startsWith", value)
133
+ values := toArray(value)
134
+ return parseQuery(queryOptions{
135
+ Method: "startsWith",
136
+ Attribute: & attribute,
137
+ Values: & values,
138
+ })
75
139
}
76
140
77
141
func EndsWith(attribute string, value interface{}) string {
78
- return parseQuery(attribute, "endsWith", value)
142
+ values := toArray(value)
143
+ return parseQuery(queryOptions{
144
+ Method: "endsWith",
145
+ Attribute: & attribute,
146
+ Values: & values,
147
+ })
79
148
}
80
149
81
- func Select(attributes []string) string {
82
- stringValue := ""
83
- for i, attribute := range attributes {
84
- stringValue += fmt.Sprintf("\"%s\"", attribute)
85
- if i < len(attributes)-1 {
86
- stringValue += ","
87
- }
88
- }
89
- return fmt.Sprintf("select([%s])", stringValue)
150
+ func Contains(attribute string, value interface{}) string {
151
+ values := toArray(value)
152
+ return parseQuery(queryOptions{
153
+ Method: "contains",
154
+ Attribute: & attribute,
155
+ Values: & values,
156
+ })
157
+ }
158
+
159
+ func Select(attributes interface{}) string {
160
+ values := toArray(attributes)
161
+ return parseQuery(queryOptions{
162
+ Method: "select",
163
+ Values: & values,
164
+ })
90
165
}
91
166
92
167
func OrderAsc(attribute string) string {
93
- return fmt.Sprintf("orderAsc(\"%s\")", attribute)
168
+ return parseQuery(queryOptions{
169
+ Method: "orderAsc",
170
+ Attribute: & attribute,
171
+ })
94
172
}
95
173
96
174
func OrderDesc(attribute string) string {
97
- return fmt.Sprintf("orderDesc(\"%s\")", attribute)
98
- }
175
+ return parseQuery(queryOptions{
176
+ Method: "orderDesc",
177
+ Attribute: & attribute,
178
+ })}
99
179
100
- func CursorBefore(documentId string) string {
101
- return fmt.Sprintf("cursorBefore(\"%s\")", documentId)
180
+ func CursorBefore(documentId interface{}) string {
181
+ values := toArray(documentId)
182
+ return parseQuery(queryOptions{
183
+ Method: "cursorBefore",
184
+ Values: & values,
185
+ })
102
186
}
103
187
104
188
func CursorAfter(documentId string) string {
105
- return fmt.Sprintf("cursorAfter(\"%s\")", documentId)
189
+ values := toArray(documentId)
190
+ return parseQuery(queryOptions{
191
+ Method: "cursorAfter",
192
+ Values: & values,
193
+ })
106
194
}
107
195
108
196
func Limit(limit int) string {
109
- return fmt.Sprintf("limit(%d)", limit)
197
+ values := toArray(limit)
198
+ return parseQuery(queryOptions{
199
+ Method: "limit",
200
+ Values: & values,
201
+ })
110
202
}
111
203
112
204
func Offset(offset int) string {
113
- return fmt.Sprintf("offset(%d)", offset)
114
- }
205
+ values := toArray(offset)
206
+ return parseQuery(queryOptions{
207
+ Method: "offset",
208
+ Values: & values,
209
+ })
210
+ }
211
+
212
+ func Or(queries []string) string {
213
+ var parsedQueries []interface{}
214
+ for _, query := range queries {
215
+ var q interface{}
216
+ if err := json.Unmarshal([]byte(query), & q); err != nil {
217
+ // Handle error, possibly log it or return an empty result
218
+ continue
219
+ }
220
+ parsedQueries = append(parsedQueries, q)
221
+ }
222
+ return parseQuery(queryOptions{
223
+ Method: "or",
224
+ Values: & parsedQueries,
225
+ })
226
+ }
227
+
228
+ func And(queries []string) string {
229
+ var parsedQueries []interface{}
230
+ for _, query := range queries {
231
+ var q interface{}
232
+ if err := json.Unmarshal([]byte(query), & q); err != nil {
233
+ // Handle error, possibly log it or return an empty result
234
+ continue
235
+ }
236
+ parsedQueries = append(parsedQueries, q)
237
+ }
238
+ return parseQuery(queryOptions{
239
+ Method: "and",
240
+ Values: & parsedQueries,
241
+ })
242
+ }
0 commit comments