Skip to content

Commit ac6e897

Browse files
committed
Add support for query helpers
1 parent 3283627 commit ac6e897

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

src/SDK/Language/Go.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public function getFiles(): array
9393
'destination' => '{{ spec.title | caseLower}}/inputFile.go',
9494
'template' => 'go/inputFile.go.twig',
9595
],
96+
[
97+
'scope' => 'default',
98+
'destination' => '{{ spec.title | caseLower}}/query.go',
99+
'template' => 'go/query.go.twig',
100+
],
96101
[
97102
'scope' => 'service',
98103
'destination' => '{{ spec.title | caseLower}}/{{service.name | caseDash}}.go',

templates/go/query.go.twig

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package {{ spec.title | caseLower }}
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
type Query struct {}
9+
10+
func NewQuery() *Query {
11+
return &Query{}
12+
}
13+
14+
func (q *Query) parseValue(value interface{}) string {
15+
if val, ok := value.(string); ok {
16+
return fmt.Sprintf("\"%s\"", val)
17+
}
18+
return toString(value)
19+
}
20+
21+
func (q *Query) parseQuery(attribute string, method string, value interface{}) string {
22+
stringValue := ""
23+
switch reflect.TypeOf(value).Kind() {
24+
case reflect.Array, reflect.Slice:
25+
arr := reflect.Indirect(reflect.ValueOf(value))
26+
for i := 0; i < arr.Len(); i++ {
27+
stringValue += q.parseValue(arr.Index(i).Interface())
28+
if i < arr.Len()-1 {
29+
stringValue += ","
30+
}
31+
}
32+
default:
33+
stringValue = q.parseValue(value)
34+
}
35+
return fmt.Sprintf("%s(\"%s\", [%s])", method, attribute, stringValue)
36+
}
37+
38+
func (q *Query) Equal(attribute string, value interface{}) string {
39+
return q.parseQuery(attribute, "equal", value)
40+
}
41+
42+
func (q *Query) NotEqual(attribute string, value interface{}) string {
43+
return q.parseQuery(attribute, "notEqual", value)
44+
}
45+
46+
func (q *Query) LessThan(attribute string, value interface{}) string {
47+
return q.parseQuery(attribute, "lessThan", value)
48+
}
49+
50+
func (q *Query) LessThanEqual(attribute string, value interface{}) string {
51+
return q.parseQuery(attribute, "lessThanEqual", value)
52+
}
53+
54+
func (q *Query) GreaterThan(attribute string, value interface{}) string {
55+
return q.parseQuery(attribute, "greaterThan", value)
56+
}
57+
58+
func (q *Query) GreaterThanEqual(attribute string, value interface{}) string {
59+
return q.parseQuery(attribute, "greaterThanEqual", value)
60+
}
61+
62+
func (q *Query) Search(attribute string, value string) string {
63+
return q.parseQuery(attribute, "search", value)
64+
}
65+
66+
func (q *Query) IsNull(attribute string) string {
67+
return fmt.Sprintf("isNull(\"%s\")", attribute)
68+
}
69+
70+
func (q *Query) IsNotNull(attribute string) string {
71+
return fmt.Sprintf("isNotNull(\"%s\")", attribute)
72+
}
73+
74+
func (q *Query) Between(attribute string, start, end interface{}) string {
75+
return q.parseQuery(attribute, "between", []interface{}{start, end})
76+
}
77+
78+
func (q *Query) StartsWith(attribute string, value interface{}) string {
79+
return q.parseQuery(attribute, "startsWith", value)
80+
}
81+
82+
func (q *Query) EndsWith(attribute string, value interface{}) string {
83+
return q.parseQuery(attribute, "endsWith", value)
84+
}
85+
86+
func (q *Query) Select(attributes []string) string {
87+
stringValue := ""
88+
for i, attribute := range attributes {
89+
stringValue += fmt.Sprintf("\"%s\"", attribute)
90+
if i < len(attributes)-1 {
91+
stringValue += ","
92+
}
93+
}
94+
return fmt.Sprintf("select([%s])", stringValue)
95+
}
96+
97+
func (q *Query) OrderAsc(attribute string) string {
98+
return fmt.Sprintf("orderAsc(\"%s\")", attribute)
99+
}
100+
101+
func (q *Query) OrderDesc(attribute string) string {
102+
return fmt.Sprintf("orderDesc(\"%s\")", attribute)
103+
}
104+
105+
func (q *Query) CursorBefore(documentId string) string {
106+
return fmt.Sprintf("cursorBefore(\"%s\")", documentId)
107+
}
108+
109+
func (q *Query) CursorAfter(documentId string) string {
110+
return fmt.Sprintf("cursorAfter(\"%s\")", documentId)
111+
}
112+
113+
func (q *Query) Limit(limit int) string {
114+
return fmt.Sprintf("limit(%d)", limit)
115+
}
116+
117+
func (q *Query) Offset(offset int) string {
118+
return fmt.Sprintf("offset(%d)", offset)
119+
}

tests/Go112Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ class Go112Test extends Base
2424
...Base::EXTENDED_GENERAL_RESPONSES,
2525
...Base::LARGE_FILE_RESPONSES,
2626
...Base::EXCEPTION_RESPONSES,
27+
...Base::QUERY_HELPER_RESPONSES,
2728
];
2829
}

tests/Go118Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ class Go118Test extends Base
2424
...Base::EXTENDED_GENERAL_RESPONSES,
2525
...Base::LARGE_FILE_RESPONSES,
2626
...Base::EXCEPTION_RESPONSES,
27+
...Base::QUERY_HELPER_RESPONSES,
2728
];
2829
}

tests/languages/go/tests.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func testGeneralService(client appwrite.Client, stringInArray []interface{}) {
126126

127127
general.Empty()
128128

129+
// Test Queries
130+
testQueries()
131+
129132
// Final test
130133
response, err = general.Headers()
131134
if err != nil {
@@ -166,3 +169,27 @@ func testLargeUpload(client appwrite.Client, stringInArray []interface{}) {
166169
}
167170
fmt.Printf("%s\n", response.Result.(map[string]interface{})["result"])
168171
}
172+
173+
func testQueries() {
174+
query := appwrite.NewQuery()
175+
fmt.Println(query.Equal("released", []interface{}{true}))
176+
fmt.Println(query.Equal("title", []string{"Spiderman", "Dr. Strange"}))
177+
fmt.Println(query.NotEqual("title", "Spiderman"))
178+
fmt.Println(query.LessThan("releasedYear", 1990))
179+
fmt.Println(query.GreaterThan("releasedYear", 1990))
180+
fmt.Println(query.Search("name", "john"))
181+
fmt.Println(query.IsNull("name"))
182+
fmt.Println(query.IsNotNull("name"))
183+
fmt.Println(query.Between("age", 50, 100))
184+
fmt.Println(query.Between("age", 50.5, 100.5))
185+
fmt.Println(query.Between("name", "Anna", "Brad"))
186+
fmt.Println(query.StartsWith("name", "Ann"))
187+
fmt.Println(query.EndsWith("name", "nne"))
188+
fmt.Println(query.Select([]string{"name", "age"}))
189+
fmt.Println(query.OrderAsc("title"))
190+
fmt.Println(query.OrderDesc("title"))
191+
fmt.Println(query.CursorAfter("my_movie_id"))
192+
fmt.Println(query.CursorBefore("my_movie_id"))
193+
fmt.Println(query.Limit(50))
194+
fmt.Println(query.Offset(20))
195+
}

0 commit comments

Comments
 (0)