Skip to content

Commit dbbbb7d

Browse files
committed
Add id, roles and permission helpers with tests
1 parent b9d621f commit dbbbb7d

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

src/SDK/Language/Go.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ public function getFiles(): array
9898
'destination' => '{{ spec.title | caseLower}}/query.go',
9999
'template' => 'go/query.go.twig',
100100
],
101+
[
102+
'scope' => 'default',
103+
'destination' => '{{ spec.title | caseLower}}/permission.go',
104+
'template' => 'go/permission.go.twig',
105+
],
106+
[
107+
'scope' => 'default',
108+
'destination' => '{{ spec.title | caseLower}}/role.go',
109+
'template' => 'go/role.go.twig',
110+
],
111+
[
112+
'scope' => 'default',
113+
'destination' => '{{ spec.title | caseLower}}/id.go',
114+
'template' => 'go/id.go.twig',
115+
],
101116
[
102117
'scope' => 'service',
103118
'destination' => '{{ spec.title | caseLower}}/{{service.name | caseDash}}.go',

templates/go/id.go.twig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package {{ spec.title | caseLower }}
2+
3+
type ID struct {}
4+
5+
func NewID() *ID {
6+
return &ID{}
7+
}
8+
9+
func (i *ID) Custom(id string) string {
10+
return id
11+
}
12+
13+
func (id *ID) Unique() string {
14+
return "unique()"
15+
}

templates/go/permission.go.twig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package {{ spec.title | caseLower }}
2+
3+
import "fmt"
4+
5+
type Permission struct {}
6+
7+
func NewPermission() *Permission {
8+
return &Permission{}
9+
}
10+
11+
func (p *Permission) Read(role string) string {
12+
return fmt.Sprintf("read(\"%s\")", role)
13+
}
14+
15+
func (p *Permission) Write(role string) string {
16+
return fmt.Sprintf("write(\"%s\")", role)
17+
}
18+
19+
func (p *Permission) Create(role string) string {
20+
return fmt.Sprintf("create(\"%s\")", role)
21+
}
22+
23+
func (p *Permission) Update(role string) string {
24+
return fmt.Sprintf("update(\"%s\")", role)
25+
}
26+
27+
func (p *Permission) Delete(role string) string {
28+
return fmt.Sprintf("delete(\"%s\")", role)
29+
}

templates/go/role.go.twig

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package {{ spec.title | caseLower }}
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Role struct {}
8+
9+
func NewRole() *Role {
10+
return &Role{}
11+
}
12+
13+
func (r *Role) Any() string {
14+
return "any"
15+
}
16+
17+
func (r *Role) User(id, status string) string {
18+
if status != "" {
19+
return fmt.Sprintf("user:%s/%s", id, status)
20+
}
21+
return fmt.Sprintf("user:%s", id)
22+
}
23+
24+
func (r *Role) Users(status string) string{
25+
if status != "" {
26+
return fmt.Sprintf("users/%s", status)
27+
}
28+
return "users"
29+
}
30+
31+
func (r *Role) Guests() string {
32+
return "guests"
33+
}
34+
35+
func (r *Role) Team(id, role string) string {
36+
if role != "" {
37+
return fmt.Sprintf("team:%s/%s", id, role)
38+
}
39+
return fmt.Sprintf("team:%s", id)
40+
}
41+
42+
func (r *Role) Member(id string) string {
43+
return fmt.Sprintf("member:%s", id)
44+
}

tests/Go112Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ class Go112Test extends Base
2525
...Base::LARGE_FILE_RESPONSES,
2626
...Base::EXCEPTION_RESPONSES,
2727
...Base::QUERY_HELPER_RESPONSES,
28+
...Base::PERMISSION_HELPER_RESPONSES,
29+
...Base::ID_HELPER_RESPONSES,
2830
];
2931
}

tests/Go118Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ class Go118Test extends Base
2525
...Base::LARGE_FILE_RESPONSES,
2626
...Base::EXCEPTION_RESPONSES,
2727
...Base::QUERY_HELPER_RESPONSES,
28+
...Base::PERMISSION_HELPER_RESPONSES,
29+
...Base::ID_HELPER_RESPONSES,
2830
];
2931
}

tests/languages/go/tests.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ func testGeneralService(client appwrite.Client, stringInArray []interface{}) {
129129
// Test Queries
130130
testQueries()
131131

132+
// Test Permission Helpers
133+
testPermissionHelpers()
134+
135+
// Test Id Helpers
136+
testIdHelpers()
137+
132138
// Final test
133139
response, err = general.Headers()
134140
if err != nil {
@@ -193,3 +199,22 @@ func testQueries() {
193199
fmt.Println(query.Limit(50))
194200
fmt.Println(query.Offset(20))
195201
}
202+
203+
func testPermissionHelpers() {
204+
permission := appwrite.NewPermission()
205+
roles := appwrite.NewRole()
206+
fmt.Println(permission.Read(roles.Any()))
207+
fmt.Println(permission.Write(roles.User(appwrite.NewID().Custom("userid"), "")))
208+
fmt.Println(permission.Create(roles.Users("")))
209+
fmt.Println(permission.Update(roles.Guests()))
210+
fmt.Println(permission.Delete(roles.Team("teamId", "owner")))
211+
fmt.Println(permission.Delete(roles.Team("teamId", "")))
212+
fmt.Println(permission.Create(roles.Member("memberId")))
213+
fmt.Println(permission.Update(roles.Users("verified")))
214+
fmt.Println(permission.Update(roles.User(appwrite.NewID().Custom("userid"), "unverified")))
215+
}
216+
217+
func testIdHelpers() {
218+
fmt.Println(appwrite.NewID().Unique())
219+
fmt.Println(appwrite.NewID().Custom("custom_id"))
220+
}

0 commit comments

Comments
 (0)