-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformer_test.go
56 lines (46 loc) · 1.22 KB
/
transformer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package amoeba
import (
"fmt"
"testing"
)
type User struct {
ID uint64
Name string
}
type Role struct {
ID uint64
Name string
}
type Permission struct {
ID uint64
Name string
}
type UserTransformer struct {
Tran
}
type RoleTransformer struct {
Tran
}
type PermissionTransformer struct {
Tran
}
func (user *UserTransformer) RegisterIncluder() {
user.Include("roles", func(transformer Transformer, entity interface{}, includeStr string, args ...interface{}) interface{} {
roles := []Role{{1, "admin"}, {2, "editor"}}
return user.Collection(roles, "role", includeStr, args...)
})
}
func (role *RoleTransformer) RegisterIncluder() {
role.Include("permissions", func(transformer Transformer, entity interface{}, includeStr string, args ...interface{}) interface{} {
permission := []Permission{{1, "update-user"}, {2, "delete-user"}}
return role.Collection(permission, "permission", includeStr, args...)
})
}
func TestCollection(t *testing.T) {
users := []User{{1, "william"}}
RegisterTransformer("user", &UserTransformer{})
RegisterTransformer("role", &RoleTransformer{})
RegisterTransformer("permission", &PermissionTransformer{})
res := Collection(users, "user", "roles.permissions")
fmt.Println(res)
}