Skip to content

Commit adf905d

Browse files
fix: golang sdk
1 parent 32b4462 commit adf905d

File tree

7 files changed

+222
-68
lines changed

7 files changed

+222
-68
lines changed

src/SDK/Language/Go.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,10 @@ public function getFilters(): array
288288
}),
289289
new TwigFilter('returnType', function (array $method, array $spec, string $namespace, string $generic = 'T') {
290290
return $this->getReturnType($method, $spec, $namespace, $generic);
291+
}),
291292
new TwigFilter('caseEnumKey', function (string $value) {
292293
return $this->toUpperSnakeCase($value);
293-
}),
294+
})
294295
];
295296
}
296297

templates/go/id.go.twig

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package id
22

3+
import (
4+
"time"
5+
"strconv"
6+
"math/rand"
7+
)
8+
39
func Custom(id string) string {
410
return id
511
}
612

713
func Unique() string {
8-
return "unique()"
14+
timestamp := time.Now().UnixNano() / 1000
15+
16+
choices := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}
17+
hexString := strconv.FormatInt(timestamp, 16)
18+
19+
for i := 0; i < 7; i++ {
20+
hexString += choices[rand.Intn(len(choices))]
21+
}
22+
return hexString
923
}

templates/go/query.go.twig

Lines changed: 181 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,242 @@
11
package query
22

33
import (
4-
"fmt"
5-
"reflect"
4+
"encoding/json"
65
)
76

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+
}
817

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,
1231
}
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
2935
}
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)
3144
}
3245

3346
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+
})
3553
}
3654

3755
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+
})
3962
}
4063

4164
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+
})
4371
}
4472

4573
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+
})
4780
}
4881

4982
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+
})
5189
}
5290

5391
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+
})
5598
}
5699

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+
})
59107
}
60108

61109
func IsNull(attribute string) string {
62-
return fmt.Sprintf("isNull(\"%s\")", attribute)
110+
return parseQuery(queryOptions{
111+
Method: "isNull",
112+
Attribute: &attribute,
113+
})
63114
}
64115

65116
func IsNotNull(attribute string) string {
66-
return fmt.Sprintf("isNotNull(\"%s\")", attribute)
117+
return parseQuery(queryOptions{
118+
Method: "isNotNull",
119+
Attribute: &attribute,
120+
})
67121
}
68122

69123
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+
})
71130
}
72131

73132
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+
})
75139
}
76140

77141
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+
})
79148
}
80149

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+
})
90165
}
91166

92167
func OrderAsc(attribute string) string {
93-
return fmt.Sprintf("orderAsc(\"%s\")", attribute)
168+
return parseQuery(queryOptions{
169+
Method: "orderAsc",
170+
Attribute: &attribute,
171+
})
94172
}
95173

96174
func OrderDesc(attribute string) string {
97-
return fmt.Sprintf("orderDesc(\"%s\")", attribute)
98-
}
175+
return parseQuery(queryOptions{
176+
Method: "orderDesc",
177+
Attribute: &attribute,
178+
})}
99179

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+
})
102186
}
103187

104188
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+
})
106194
}
107195

108196
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+
})
110202
}
111203

112204
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+
}

templates/go/role.go.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ func Team(id, role string) string {
3636
func Member(id string) string {
3737
return fmt.Sprintf("member:%s", id)
3838
}
39+
40+
func Label(id string) string {
41+
return fmt.Sprintf("label:%s", id)
42+
}

tests/Go112Test.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class Go112Test extends Base
2121
...Base::FOO_RESPONSES,
2222
...Base::BAR_RESPONSES,
2323
...Base::GENERAL_RESPONSES,
24-
...Base::EXTENDED_GENERAL_RESPONSES,
25-
...Base::LARGE_FILE_RESPONSES,
26-
...Base::UPLOAD_RESPONSE,
24+
...Base::UPLOAD_RESPONSES,
2725
...Base::DOWNLOAD_RESPONSES,
2826
...Base::EXCEPTION_RESPONSES,
2927
...Base::QUERY_HELPER_RESPONSES,

tests/Go118Test.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class Go118Test extends Base
2121
...Base::FOO_RESPONSES,
2222
...Base::BAR_RESPONSES,
2323
...Base::GENERAL_RESPONSES,
24-
...Base::EXTENDED_GENERAL_RESPONSES,
25-
...Base::LARGE_FILE_RESPONSES,
26-
...Base::UPLOAD_RESPONSE,
24+
...Base::UPLOAD_RESPONSES,
2725
...Base::DOWNLOAD_RESPONSES,
2826
...Base::EXCEPTION_RESPONSES,
2927
...Base::QUERY_HELPER_RESPONSES,

0 commit comments

Comments
 (0)