forked from theplant/resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders_test.go
115 lines (94 loc) · 3.28 KB
/
providers_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package resources_test
import (
"fmt"
"testing"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/theplant/resources"
)
func TestMergeWithCurry(t *testing.T) {
user := &User{gorm.Model{ID: 1}}
model := &Resource{Model: gorm.Model{ID: 2}, UserID: user.ID}
context := &gin.Context{}
userProvider := resources.CurryUserProvider(func(handler resources.UserHandler, ctx *gin.Context) {
if ctx != context {
t.Fatal("provider called with unknown context")
}
handler(ctx, user)
})
modelProvider := resources.CurryModelProvider(func(handler resources.ModelHandler, ctx *gin.Context) {
if ctx != context {
t.Fatal("provider called with unknown context")
}
handler(ctx, model)
})
called := false
handler := func(ctx *gin.Context, u resources.User, m resources.DBModel) {
called = true
if user != u || model != m {
t.Fatal("handler called with different parameters")
}
}
resources.Merge(userProvider, modelProvider)(handler)(context)
if !called {
t.Fatal("handler never called")
}
}
func exampleProvider() {
CurriedPreProcessModelUser := resources.CurryUserModelProcessor(func(accepter resources.UserModelHandler, ctx *gin.Context, user resources.User, model resources.DBModel) {
// check model and user
fmt.Println("Curried Pre-process model + user")
accepter(ctx, user, model)
})
ProvideAuthUser := resources.CurryUserProvider(func(handler resources.UserHandler, ctx *gin.Context) {
var u resources.User // comes from somewhere
fmt.Println("Provide user")
handler(ctx, u)
})
ProvideModel := resources.CurryModelProvider(func(handler resources.ModelHandler, ctx *gin.Context) {
var model resources.DBModel // comes from somewhere
fmt.Println("Provide model")
handler(ctx, model)
})
PreProcessUser := resources.CurryUserProcessor(func(accepter resources.UserHandler, ctx *gin.Context, user resources.User) {
// check user, then maybe call accepter
fmt.Println("Pre-process user")
accepter(ctx, user)
})
PreProcessModel := resources.CurryModelProcessor(func(accepter resources.ModelHandler, ctx *gin.Context, model resources.DBModel) {
// check model, then maybe call accepter
fmt.Println("Pre-process model")
accepter(ctx, model)
})
PreProcessModelUser := resources.CurryUserModelProcessor(func(accepter resources.UserModelHandler, ctx *gin.Context, user resources.User, model resources.DBModel) {
// check model and user
fmt.Println("Pre-process model + user")
accepter(ctx, user, model)
})
AcceptModel := func(ctx *gin.Context, model resources.DBModel) {
// do something with model
fmt.Println("Accept model")
}
AcceptUserModel := func(ctx *gin.Context, user resources.User, model resources.DBModel) {
// do something with model
fmt.Println("Accept user + model")
}
chain := CurriedPreProcessModelUser(PreProcessModelUser(resources.Merge(PreProcessUser(ProvideAuthUser), PreProcessModel(ProvideModel))))
chain(AcceptUserModel)(nil)
resources.DiscardUser(chain)(AcceptModel)(nil)
// Output:
// Provide model
// Pre-process model
// Provide user
// Pre-process user
// Pre-process model + user
// Curried Pre-process model + user
// Accept user + model
// Provide model
// Pre-process model
// Provide user
// Pre-process user
// Pre-process model + user
// Curried Pre-process model + user
// Accept model
}