-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.go
192 lines (152 loc) · 4.77 KB
/
render.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package template
import (
"reflect"
"github.com/CloudyKit/jet/v6"
"github.com/gin-gonic/gin"
"github.com/shoppehub/fastapi/collection"
"github.com/shoppehub/fastapi/crud"
"github.com/shoppehub/fastapi/session"
"github.com/shoppehub/sjet"
"github.com/shoppehub/sjet/context"
"github.com/shoppehub/sjet/engine"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
)
var ENGINE *engine.TemplateEngine
func InitEngine(resource *crud.Resource) {
if ENGINE == nil {
ENGINE = sjet.CreateWithMem()
InitAPIFunc(resource)
}
}
// 根据名称进行匹配
func Render(collection collection.Collection, fnName string, c *gin.Context) (map[string]interface{}, error) {
fun := collection.Functions[fnName]
templateContext := context.InitTemplateContext(ENGINE, c)
_, err := sjet.RenderMemTemplate(ENGINE, templateContext, c, fnName, fun.Template)
if err != nil {
logrus.Error(err)
return nil, err
}
return *templateContext.Context, nil
}
func InitAPIFunc(resource *crud.Resource) {
sjet.RegCustomFunc("query", func(c *gin.Context) jet.Func {
return queryFunc(resource)
})
sjet.RegCustomFunc("findOne", func(c *gin.Context) jet.Func {
return findOneFunc(resource)
})
sjet.RegCustomFunc("findOption", func(c *gin.Context) jet.Func {
return findOptionFunc(resource)
})
sjet.RegCustomFunc("save", func(c *gin.Context) jet.Func {
return saveFunc(resource)
})
sjet.RegCustomFunc("sort", func(c *gin.Context) jet.Func {
return sortFunc()
})
sjet.RegCustomFunc("getCollectionFields", func(c *gin.Context) jet.Func {
return getCollectionFieldsFunc(resource)
})
sjet.RegCustomFunc("login", func(c *gin.Context) jet.Func {
return loginFunc(resource, c)
})
sjet.RegCustomFunc("logout", func(c *gin.Context) jet.Func {
return logoutFunc(resource, c)
})
}
func findOptionFunc(resource *crud.Resource) jet.Func {
return func(a jet.Arguments) reflect.Value {
d := crud.FindOptions{}
collectionName := a.Get(0).Interface().(string)
d.CollectionName = &collectionName
if a.NumOfArguments() > 2 {
curPage := a.Get(1).Interface()
k := a.Get(1).Kind()
if k == reflect.Int {
d.CurPage = int64(curPage.(int))
} else if k == reflect.Int64 {
d.CurPage = curPage.(int64)
} else {
v := curPage.(float64)
d.CurPage = int64(v)
}
pageSize := a.Get(2).Interface()
k2 := a.Get(2).Kind()
if k2 == reflect.Int {
d.PageSize = int64(pageSize.(int))
} else if k2 == reflect.Int64 {
d.PageSize = pageSize.(int64)
} else {
v := pageSize.(float64)
d.PageSize = int64(v)
}
}
m := reflect.ValueOf(d)
return m
}
}
func queryFunc(resource *crud.Resource) jet.Func {
return func(a jet.Arguments) reflect.Value {
typeName := reflect.ValueOf(a.Get(0).Interface()).Type().Name()
findOption := a.Get(1).Interface().(crud.FindOptions)
if typeName == "M" {
if findOption.CurPage == 0 && findOption.PageSize == 0 {
r := resource.FindWithoutPaging(a.Get(0).Interface().(bson.M), findOption)
return reflect.ValueOf(r)
} else {
r := resource.Find(a.Get(0).Interface().(bson.M), findOption)
return reflect.ValueOf(r)
}
}
if findOption.CurPage == 0 && findOption.PageSize == 0 {
r, _ := resource.QueryWithoutPaging(a.Get(0).Interface().([]bson.D), findOption)
return reflect.ValueOf(r)
} else {
r := resource.Query(a.Get(0).Interface().([]bson.D), findOption)
return reflect.ValueOf(r)
}
}
}
func findOneFunc(resource *crud.Resource) jet.Func {
return func(a jet.Arguments) reflect.Value {
filter := a.Get(0).Interface().(bson.M)
findOption := a.Get(1).Interface().(crud.FindOptions)
var result map[string]interface{}
resource.FindOne(filter, &result, crud.FindOneOptions{
CollectionName: findOption.CollectionName,
})
return reflect.ValueOf(result)
}
}
func saveFunc(resource *crud.Resource) jet.Func {
return func(a jet.Arguments) reflect.Value {
collectionName := a.Get(0).Interface().(string)
data := a.Get(1).Interface()
result, _ := resource.SaveOrUpdateOne(data, &crud.UpdateOption{
CollectionName: &collectionName,
})
return reflect.ValueOf(result)
}
}
func getCollectionFieldsFunc(resource *crud.Resource) jet.Func {
return func(a jet.Arguments) reflect.Value {
collectionName := a.Get(0).Interface().(string)
fieldMap := collection.FindOneCollection(resource, collectionName)
return reflect.ValueOf(fieldMap)
}
}
func loginFunc(resource *crud.Resource, c *gin.Context) jet.Func {
return func(a jet.Arguments) reflect.Value {
uid := a.Get(0).Interface().(string)
session.NewUserSession(resource, uid, c.Request, c.Writer)
return reflect.ValueOf("")
}
}
func logoutFunc(resource *crud.Resource, c *gin.Context) jet.Func {
return func(a jet.Arguments) reflect.Value {
session.RemoveUserSession(resource, c.Request)
return reflect.ValueOf("")
}
}