Skip to content

Commit 954f3d6

Browse files
Merge pull request #916 from appwrite/feat-go-improvements
Feat: Go improvements
2 parents f577698 + 721c6c2 commit 954f3d6

File tree

9 files changed

+204
-161
lines changed

9 files changed

+204
-161
lines changed

src/SDK/Language/Go.php

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ public function getFiles(): array
6363
'destination' => 'go.mod',
6464
'template' => 'go/go.mod.twig',
6565
],
66-
[
67-
'scope' => 'default',
68-
'destination' => 'example/main.go',
69-
'template' => 'go/main.go.twig',
70-
],
7166
[
7267
'scope' => 'default',
7368
'destination' => 'README.md',
@@ -83,6 +78,11 @@ public function getFiles(): array
8378
'destination' => 'LICENSE',
8479
'template' => 'go/LICENSE.twig',
8580
],
81+
[
82+
'scope' => 'default',
83+
'destination' => 'appwrite/appwrite.go',
84+
'template' => 'go/appwrite.go.twig',
85+
],
8686
[
8787
'scope' => 'default',
8888
'destination' => 'client/client.go',
@@ -145,7 +145,9 @@ public function getTypeName(array $parameter, array $spec = []): string
145145
self::TYPE_STRING => 'string',
146146
self::TYPE_BOOLEAN => 'bool',
147147
self::TYPE_OBJECT => 'interface{}',
148-
self::TYPE_ARRAY => '[]interface{}',
148+
self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type']))
149+
? '[]' . $this->getTypeName($parameter['array'])
150+
: '[]string',
149151
default => $parameter['type'],
150152
};
151153
}
@@ -170,8 +172,10 @@ public function getParamDefault(array $param): string
170172
switch ($type) {
171173
case self::TYPE_NUMBER:
172174
case self::TYPE_INTEGER:
175+
$output .= "0";
176+
break;
173177
case self::TYPE_BOOLEAN:
174-
$output .= 'null';
178+
$output .= 'false';
175179
break;
176180
case self::TYPE_STRING:
177181
$output .= '""';
@@ -283,25 +287,34 @@ public function getFilters(): array
283287
}
284288
return implode("\n" . $indent, $value);
285289
}, ['is_safe' => ['html']]),
286-
new TwigFilter('propertyType', function (array $property, array $spec, string $generic = 'interface{}') {
290+
new TwigFilter('propertyType', function (array $property, array $spec, string $generic = 'map[string]interface{}') {
287291
return $this->getPropertyType($property, $spec, $generic);
288292
}),
289-
new TwigFilter('returnType', function (array $method, array $spec, string $namespace, string $generic = 'T') {
293+
new TwigFilter('returnType', function (array $method, array $spec, string $namespace, string $generic = 'map[string]interface{}') {
290294
return $this->getReturnType($method, $spec, $namespace, $generic);
291295
}),
292296
new TwigFilter('caseEnumKey', function (string $value) {
293297
return $this->toUpperSnakeCase($value);
294-
})
298+
}),
295299
];
296300
}
297301

298-
protected function getPropertyType(array $property, array $spec, string $generic = 'interface{}'): string
302+
protected function getPropertyType(array $property, array $spec, string $generic = 'map[string]interface{}'): string
299303
{
300-
$type = $this->getTypeName($property);
304+
if (\array_key_exists('sub_schema', $property)) {
305+
$type = $this->toPascalCase($property['sub_schema']);
306+
307+
if ($property['type'] === 'array') {
308+
$type = '[]' . $type;
309+
}
310+
} else {
311+
$type = $this->getTypeName($property);
312+
}
313+
301314
return $type;
302315
}
303316

304-
protected function getReturnType(array $method, array $spec, string $namespace, string $generic = 'T'): string
317+
protected function getReturnType(array $method, array $spec, string $namespace, string $generic = 'map[string]interface{}'): string
305318
{
306319
if ($method['type'] === 'webAuth') {
307320
return 'bool';
@@ -322,4 +335,27 @@ protected function getReturnType(array $method, array $spec, string $namespace,
322335

323336
return 'models.' . $ret;
324337
}
338+
339+
protected function hasGenericType(?string $model, array $spec): string
340+
{
341+
if (empty($model) || $model === 'any') {
342+
return false;
343+
}
344+
345+
$model = $spec['definitions'][$model];
346+
347+
if ($model['additionalProperties']) {
348+
return true;
349+
}
350+
351+
foreach ($model['properties'] as $property) {
352+
if (!\array_key_exists('sub_schema', $property) || !$property['sub_schema']) {
353+
continue;
354+
}
355+
356+
return $this->hasGenericType($property['sub_schema'], $spec);
357+
}
358+
359+
return false;
360+
}
325361
}

templates/go/README.md.twig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,32 @@ import (
5252
"os"
5353
"time"
5454

55-
"github.com/{{sdk.gitUserName}}/sdk-for-go/{{ sdk.gitRepoName|url_encode }}"
55+
"github.com/{{sdk.gitUserName}}/{{ sdk.gitRepoName|url_encode }}/appwrite"
56+
"github.com/{{sdk.gitUserName}}/{{ sdk.gitRepoName|url_encode }}/id"
5657
)
5758

5859
func main() {
59-
client := {{ sdk.gitRepoName|url_encode }}.NewClient(10 * time.Second)
60-
client.SetEndpoint(os.Getenv("YOUR_ENDPOINT"))
61-
client.SetProject(os.Getenv("YOUR_PROJECT_ID"))
62-
client.SetKey(os.Getenv("YOUR_KEY"))
60+
client := appwrite.NewClient(
61+
appwrite.WithEndpoint(os.Getenv("YOUR_ENDPOINT")),
62+
appwrite.WithProject(os.Getenv("YOUR_PROJECT_ID")),
63+
appwrite.WithKey(os.Getenv("YOUR_KEY")),
64+
)
65+
66+
databases := appwrite.NewDatabase(client)
6367

64-
db := {{ sdk.gitRepoName|url_encode }}.NewDatabase(client)
6568
data := map[string]string{
6669
"hello": "world",
6770
}
68-
var EmptyArray = []interface{}{}
69-
doc, err := db.CreateDocument(
71+
doc, err := databases.CreateDocument(
72+
os.Getenv("DATABASE_ID"),
7073
os.Getenv("COLLECTION_ID"),
74+
id.Unique(),
7175
data,
72-
EmptyArray,
73-
EmptyArray,
74-
"",
75-
"",
76-
"",
7776
)
7877
if err != nil {
7978
log.Printf("Error creating document: %v", err)
8079
}
80+
8181
log.Printf("Created document: %v", doc)
8282
}
8383
```

templates/go/appwrite.go.twig

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package appwrite
2+
3+
import (
4+
"time"
5+
6+
"github.com/{{sdk.gitUserName}}/sdk-for-go/client"
7+
{% for key,service in spec.services %}
8+
"github.com/{{sdk.gitUserName}}/sdk-for-go/{{ service.name | caseLower}}"
9+
{% endfor %}
10+
)
11+
12+
{% for key,service in spec.services %}
13+
func New{{ service.name | caseUcfirst }}(clt client.Client) *{{ service.name | caseLower}}.{{ service.name | caseUcfirst }} {
14+
return {{ service.name | caseLower}}.New(clt)
15+
}
16+
{% endfor %}
17+
18+
// NewClient initializes a new {{ spec.title | caseUcfirst }} client with a given timeout
19+
func NewClient(optionalSetters ...client.ClientOption) client.Client {
20+
return client.New(optionalSetters...)
21+
}
22+
23+
// Helper method to construct NewClient()
24+
func WithEndpoint(endpoint string) client.ClientOption {
25+
return func(clt *client.Client) error {
26+
clt.Endpoint = endpoint
27+
return nil
28+
}
29+
}
30+
31+
// Helper method to construct NewClient()
32+
func WithTimeout(timeout time.Duration) client.ClientOption {
33+
return func(clt *client.Client) error {
34+
httpClient, err := client.GetDefaultClient(timeout)
35+
if err != nil {
36+
return err
37+
}
38+
39+
clt.Timeout = timeout
40+
clt.Client = httpClient
41+
42+
return nil
43+
}
44+
}
45+
46+
// Helper method to construct NewClient()
47+
func WithSelfSigned(status bool) client.ClientOption {
48+
return func(clt *client.Client) error {
49+
clt.SelfSigned = status
50+
return nil
51+
}
52+
}
53+
54+
// Helper method to construct NewClient()
55+
func WithChunkSize(size int64) client.ClientOption {
56+
return func(clt *client.Client) error {
57+
clt.ChunkSize = size
58+
return nil
59+
}
60+
}
61+
62+
{% for header in spec.global.headers %}
63+
// Helper method to construct NewClient()
64+
{% if header.description %}
65+
//
66+
// {{header.description}}
67+
{% endif %}
68+
func With{{header.key | caseUcfirst}}(value string) client.ClientOption {
69+
return func(clt *client.Client) error {
70+
clt.Headers["{{header.name}}"] = value
71+
return nil
72+
}
73+
}
74+
{% endfor %}

0 commit comments

Comments
 (0)