Skip to content

Commit 8612b0d

Browse files
committed
Organize into packages and fix tests
1 parent 071f049 commit 8612b0d

File tree

12 files changed

+181
-162
lines changed

12 files changed

+181
-162
lines changed

src/SDK/Language/Go.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,32 @@ public function getFiles(): array
8585
],
8686
[
8787
'scope' => 'default',
88-
'destination' => '{{ spec.title | caseLower}}/client.go',
88+
'destination' => 'client/client.go',
8989
'template' => 'go/client.go.twig',
9090
],
9191
[
9292
'scope' => 'default',
93-
'destination' => '{{ spec.title | caseLower}}/inputFile.go',
93+
'destination' => 'file/inputFile.go',
9494
'template' => 'go/inputFile.go.twig',
9595
],
9696
[
9797
'scope' => 'default',
98-
'destination' => '{{ spec.title | caseLower}}/query.go',
98+
'destination' => 'query/query.go',
9999
'template' => 'go/query.go.twig',
100100
],
101101
[
102102
'scope' => 'default',
103-
'destination' => '{{ spec.title | caseLower}}/permission.go',
103+
'destination' => 'permission/permission.go',
104104
'template' => 'go/permission.go.twig',
105105
],
106106
[
107107
'scope' => 'default',
108-
'destination' => '{{ spec.title | caseLower}}/role.go',
108+
'destination' => 'role/role.go',
109109
'template' => 'go/role.go.twig',
110110
],
111111
[
112112
'scope' => 'default',
113-
'destination' => '{{ spec.title | caseLower}}/id.go',
113+
'destination' => 'id/id.go',
114114
'template' => 'go/id.go.twig',
115115
],
116116
[
@@ -125,7 +125,7 @@ public function getFiles(): array
125125
],
126126
[
127127
'scope' => 'definition',
128-
'destination' => '{{ spec.title | caseLower}}models/{{ definition.name | caseLower }}.go',
128+
'destination' => 'models/{{ definition.name | caseLower }}.go',
129129
'template' => 'go/models/model.go.twig',
130130
],
131131
];
@@ -144,7 +144,7 @@ public function getTypeName(array $parameter): string
144144
case self::TYPE_NUMBER:
145145
return 'float64';
146146
case self::TYPE_FILE:
147-
return 'appwrite.InputFile';
147+
return 'file.InputFile';
148148
case self::TYPE_STRING:
149149
return 'string';
150150
case self::TYPE_BOOLEAN:
@@ -273,10 +273,11 @@ public function getFilters(): array
273273
{
274274
return [
275275
new TwigFilter('godocComment', function ($value, $indent=0) {
276+
$value = trim($value);
276277
$value = explode("\n", $value);
277278
$indent = \str_repeat(' ', $indent);
278279
foreach ($value as $key => $line) {
279-
$value[$key] = "// " . wordwrap($line, 75, "\n" . $indent . "// ");
280+
$value[$key] = "// " . wordwrap(trim($line), 75, "\n" . $indent . "// ");
280281
}
281282
return implode("\n" . $indent, $value);
282283
}, ['is_safe' => ['html']]),
@@ -301,7 +302,7 @@ protected function getReturnType(array $method, array $spec, string $namespace,
301302
return 'bool';
302303
}
303304
if ($method['type'] === 'location') {
304-
return '[]byte';
305+
return 'string';
305306
}
306307

307308
if (
@@ -314,7 +315,6 @@ protected function getReturnType(array $method, array $spec, string $namespace,
314315

315316
$ret = ucfirst($method['responseModel']);
316317

317-
318-
return $namespace . 'Model.' . $ret;
318+
return 'models.' . $ret;
319319
}
320320
}

templates/go/base/params.twig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% if method.parameters.all|filter(v => not v.required)|length > 0 %}
2-
options := &{{(method.name ~ "Options") | caseUcfirst}}{}
2+
options := {{(method.name ~ "Options") | caseUcfirst}}{}.New()
33
for _, opt := range optionalSetters {
44
opt(options)
55
}
@@ -9,7 +9,9 @@
99
{% if parameter.required %}
1010
params["{{ parameter.name }}"] = {{ parameter.name | caseUcfirst }}
1111
{% else %}
12-
params["{{ parameter.name }}"] = options.{{ parameter.name | caseUcfirst }}
12+
if options.enabledSetters["{{ parameter.name | caseUcfirst}}"] {
13+
params["{{ parameter.name }}"] = options.{{ parameter.name | caseUcfirst }}
14+
}
1315
{% endif %}
1416
{% endfor %}
1517
headers := map[string]interface{}{

templates/go/base/requests/file.twig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@
1616
if err != nil {
1717
return nil, err
1818
}
19+
var parsed {{ method | returnType(spec, spec.title | caseLower) }}
20+
if strings.HasPrefix(resp.Type, "application/json") {
21+
err = json.Unmarshal([]byte(resp.Result.(string)), &parsed)
22+
if err != nil {
23+
return nil, err
24+
}
25+
return &parsed, nil
26+
}
1927
parsed, ok := resp.Result.({{ method | returnType(spec, spec.title | caseLower) }})
2028
if !ok {
21-
return nil, errors.New("Unexpected response type")
29+
return nil, errors.New("unexpected response type")
2230
}
23-
return &parsed, nil
31+
return &parsed, nil

templates/go/client.go.twig

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package {{ spec.title | caseLower }}
1+
package client
22

33
import (
44
"bytes"
@@ -17,6 +17,7 @@ import (
1717
"strconv"
1818
"strings"
1919
"time"
20+
"github.com/{{sdk.gitUserName}}/sdk-for-go/file"
2021
)
2122

2223
const (
@@ -149,9 +150,9 @@ func isFileUpload(headers map[string]interface{}) bool {
149150
}
150151

151152
func (clt *Client) FileUpload(url string, headers map[string]interface{}, params map[string]interface{}, paramName string, uploadId string) (*ClientResponse, error) {
152-
inputFile, ok := params[paramName].(InputFile)
153+
inputFile, ok := params[paramName].(file.InputFile)
153154
if !ok {
154-
msg := fmt.Sprintf("invalid input file. params[%s] must be of type InputFile", paramName)
155+
msg := fmt.Sprintf("invalid input file. params[%s] must be of type file.InputFile", paramName)
155156
return nil, errors.New(msg)
156157
}
157158

@@ -208,7 +209,13 @@ func (clt *Client) FileUpload(url string, headers map[string]interface{}, params
208209
return nil, err
209210
}
210211

211-
uploadId, _ = result.Result.(map[string]interface{})["$id"].(string)
212+
var parsed map[string]interface{}
213+
if strings.HasPrefix(result.Type, "application/json") {
214+
err = json.Unmarshal([]byte(result.Result.(string)), &parsed)
215+
if err == nil {
216+
uploadId, _ = parsed["$id"].(string)
217+
}
218+
}
212219
}
213220
return result, nil
214221
}
@@ -243,7 +250,7 @@ func (clt *Client) Call(method string, path string, headers map[string]interface
243250
var body bytes.Buffer
244251
writer := multipart.NewWriter(&body)
245252
for key, val := range params {
246-
if file, ok := val.(InputFile); ok {
253+
if file, ok := val.(file.InputFile); ok {
247254
fileName := file.Name
248255
fileData := file.Data
249256
fw, err := writer.CreateFormFile(key, fileName)

templates/go/id.go.twig

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
package {{ spec.title | caseLower }}
1+
package id
22

3-
type ID struct {}
4-
5-
func NewID() *ID {
6-
return &ID{}
7-
}
8-
9-
func (i *ID) Custom(id string) string {
3+
func Custom(id string) string {
104
return id
115
}
126

13-
func (id *ID) Unique() string {
7+
func Unique() string {
148
return "unique()"
15-
}
9+
}

templates/go/inputFile.go.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package {{ spec.title | caseLower }}
1+
package file
22

33
type InputFile struct {
44
Name string

templates/go/models/model.go.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package {{ spec.title | caseLower }}Model
1+
package models
22

33

44
{{ ((definition.description | caseUcfirst) ~ " Model") | godocComment }}

templates/go/permission.go.twig

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
1-
package {{ spec.title | caseLower }}
1+
package permission
22

33
import "fmt"
44

5-
type Permission struct {}
6-
7-
func NewPermission() *Permission {
8-
return &Permission{}
9-
}
10-
11-
func (p *Permission) Read(role string) string {
5+
func Read(role string) string {
126
return fmt.Sprintf("read(\"%s\")", role)
137
}
148

15-
func (p *Permission) Write(role string) string {
9+
func Write(role string) string {
1610
return fmt.Sprintf("write(\"%s\")", role)
1711
}
1812

19-
func (p *Permission) Create(role string) string {
13+
func Create(role string) string {
2014
return fmt.Sprintf("create(\"%s\")", role)
2115
}
2216

23-
func (p *Permission) Update(role string) string {
17+
func Update(role string) string {
2418
return fmt.Sprintf("update(\"%s\")", role)
2519
}
2620

27-
func (p *Permission) Delete(role string) string {
21+
func Delete(role string) string {
2822
return fmt.Sprintf("delete(\"%s\")", role)
2923
}

templates/go/query.go.twig

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,84 @@
1-
package {{ spec.title | caseLower }}
1+
package query
22

33
import (
44
"fmt"
55
"reflect"
66
)
77

8-
type Query struct {}
98

10-
func NewQuery() *Query {
11-
return &Query{}
12-
}
13-
14-
func (q *Query) parseValue(value interface{}) string {
9+
func parseValue(value interface{}) string {
1510
if val, ok := value.(string); ok {
1611
return fmt.Sprintf("\"%s\"", val)
1712
}
18-
return toString(value)
13+
return fmt.Sprintf("%v", value)
1914
}
2015

21-
func (q *Query) parseQuery(attribute string, method string, value interface{}) string {
16+
func parseQuery(attribute string, method string, value interface{}) string {
2217
stringValue := ""
2318
switch reflect.TypeOf(value).Kind() {
2419
case reflect.Array, reflect.Slice:
2520
arr := reflect.Indirect(reflect.ValueOf(value))
2621
for i := 0; i < arr.Len(); i++ {
27-
stringValue += q.parseValue(arr.Index(i).Interface())
22+
stringValue += parseValue(arr.Index(i).Interface())
2823
if i < arr.Len()-1 {
2924
stringValue += ","
3025
}
3126
}
3227
default:
33-
stringValue = q.parseValue(value)
28+
stringValue = parseValue(value)
3429
}
3530
return fmt.Sprintf("%s(\"%s\", [%s])", method, attribute, stringValue)
3631
}
3732

38-
func (q *Query) Equal(attribute string, value interface{}) string {
39-
return q.parseQuery(attribute, "equal", value)
33+
func Equal(attribute string, value interface{}) string {
34+
return parseQuery(attribute, "equal", value)
4035
}
4136

42-
func (q *Query) NotEqual(attribute string, value interface{}) string {
43-
return q.parseQuery(attribute, "notEqual", value)
37+
func NotEqual(attribute string, value interface{}) string {
38+
return parseQuery(attribute, "notEqual", value)
4439
}
4540

46-
func (q *Query) LessThan(attribute string, value interface{}) string {
47-
return q.parseQuery(attribute, "lessThan", value)
41+
func LessThan(attribute string, value interface{}) string {
42+
return parseQuery(attribute, "lessThan", value)
4843
}
4944

50-
func (q *Query) LessThanEqual(attribute string, value interface{}) string {
51-
return q.parseQuery(attribute, "lessThanEqual", value)
45+
func LessThanEqual(attribute string, value interface{}) string {
46+
return parseQuery(attribute, "lessThanEqual", value)
5247
}
5348

54-
func (q *Query) GreaterThan(attribute string, value interface{}) string {
55-
return q.parseQuery(attribute, "greaterThan", value)
49+
func GreaterThan(attribute string, value interface{}) string {
50+
return parseQuery(attribute, "greaterThan", value)
5651
}
5752

58-
func (q *Query) GreaterThanEqual(attribute string, value interface{}) string {
59-
return q.parseQuery(attribute, "greaterThanEqual", value)
53+
func GreaterThanEqual(attribute string, value interface{}) string {
54+
return parseQuery(attribute, "greaterThanEqual", value)
6055
}
6156

62-
func (q *Query) Search(attribute string, value string) string {
63-
return q.parseQuery(attribute, "search", value)
57+
func Search(attribute string, value string) string {
58+
return parseQuery(attribute, "search", value)
6459
}
6560

66-
func (q *Query) IsNull(attribute string) string {
61+
func IsNull(attribute string) string {
6762
return fmt.Sprintf("isNull(\"%s\")", attribute)
6863
}
6964

70-
func (q *Query) IsNotNull(attribute string) string {
65+
func IsNotNull(attribute string) string {
7166
return fmt.Sprintf("isNotNull(\"%s\")", attribute)
7267
}
7368

74-
func (q *Query) Between(attribute string, start, end interface{}) string {
75-
return q.parseQuery(attribute, "between", []interface{}{start, end})
69+
func Between(attribute string, start, end interface{}) string {
70+
return parseQuery(attribute, "between", []interface{}{start, end})
7671
}
7772

78-
func (q *Query) StartsWith(attribute string, value interface{}) string {
79-
return q.parseQuery(attribute, "startsWith", value)
73+
func StartsWith(attribute string, value interface{}) string {
74+
return parseQuery(attribute, "startsWith", value)
8075
}
8176

82-
func (q *Query) EndsWith(attribute string, value interface{}) string {
83-
return q.parseQuery(attribute, "endsWith", value)
77+
func EndsWith(attribute string, value interface{}) string {
78+
return parseQuery(attribute, "endsWith", value)
8479
}
8580

86-
func (q *Query) Select(attributes []string) string {
81+
func Select(attributes []string) string {
8782
stringValue := ""
8883
for i, attribute := range attributes {
8984
stringValue += fmt.Sprintf("\"%s\"", attribute)
@@ -94,26 +89,26 @@ func (q *Query) Select(attributes []string) string {
9489
return fmt.Sprintf("select([%s])", stringValue)
9590
}
9691

97-
func (q *Query) OrderAsc(attribute string) string {
92+
func OrderAsc(attribute string) string {
9893
return fmt.Sprintf("orderAsc(\"%s\")", attribute)
9994
}
10095

101-
func (q *Query) OrderDesc(attribute string) string {
96+
func OrderDesc(attribute string) string {
10297
return fmt.Sprintf("orderDesc(\"%s\")", attribute)
10398
}
10499

105-
func (q *Query) CursorBefore(documentId string) string {
100+
func CursorBefore(documentId string) string {
106101
return fmt.Sprintf("cursorBefore(\"%s\")", documentId)
107102
}
108103

109-
func (q *Query) CursorAfter(documentId string) string {
104+
func CursorAfter(documentId string) string {
110105
return fmt.Sprintf("cursorAfter(\"%s\")", documentId)
111106
}
112107

113-
func (q *Query) Limit(limit int) string {
108+
func Limit(limit int) string {
114109
return fmt.Sprintf("limit(%d)", limit)
115110
}
116111

117-
func (q *Query) Offset(offset int) string {
112+
func Offset(offset int) string {
118113
return fmt.Sprintf("offset(%d)", offset)
119114
}

0 commit comments

Comments
 (0)