-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.go
272 lines (223 loc) · 6.42 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package template
import (
"bytes"
"fmt"
"reflect"
"sort"
"github.com/CloudyKit/jet/v6"
"github.com/shoppehub/fastapi/collection"
"github.com/shoppehub/fastapi/crud"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var views *jet.Set
var fsLoader interface{}
func init() {
fsLoader = NewStringLoader()
httpfsLoader := fsLoader.(jet.Loader)
views = jet.NewSet(
httpfsLoader,
)
}
// 根据名称进行匹配
func Render(resource *crud.Resource, collection collection.Collection, fnName string, body map[string]interface{}) (map[string]interface{}, error) {
fun := collection.Functions[fnName]
loader := fsLoader.(*stringLoader)
if !loader.Exists(fnName) {
loader.templates["/"+fnName] = fun.Template
}
// view, err := views.GetTemplate(fnName)
view, err := views.Parse(fnName, fun.Template)
if err != nil {
logrus.Error(err)
return nil, err
}
var resp bytes.Buffer
result := make(map[string]interface{})
vars := newVars(resource, result)
if fun.Params != nil {
for _, param := range fun.Params {
if body[param.Name] != nil {
vars.Set(param.Name, body[param.Name])
}
}
}
if err = view.Execute(&resp, vars, nil); err != nil {
logrus.Error(err)
return nil, err
}
fmt.Println(resp.String())
// fmt.Println(result)
return result, nil
}
//排序 start
type MapsSort struct {
Key string
MapList []bson.M
Desc bool
}
func (m *MapsSort) Len() int {
return len(m.MapList)
}
func (m *MapsSort) Less(i, j int) bool {
if m.Desc {
return m.MapList[i][m.Key].(float64) > m.MapList[j][m.Key].(float64)
} else {
return m.MapList[i][m.Key].(float64) < m.MapList[j][m.Key].(float64)
}
}
func (m *MapsSort) Swap(i, j int) {
m.MapList[i], m.MapList[j] = m.MapList[j], m.MapList[i]
}
func Sort(key string, maps []bson.M, desc bool) []bson.M {
mapsSort := MapsSort{}
mapsSort.Key = key
mapsSort.MapList = maps
mapsSort.Desc = desc
sort.Sort(&mapsSort)
return mapsSort.MapList
}
// 初始化模板上下文
func newVars(resource *crud.Resource, result map[string]interface{}) jet.VarMap {
vars := make(jet.VarMap)
vars.SetFunc("string", func(a jet.Arguments) reflect.Value {
if !a.Get(0).IsValid() {
return reflect.ValueOf("")
}
name := a.Get(0).Type().Name()
switch name {
case "ObjectID":
oid := a.Get(0).Interface().(primitive.ObjectID)
return reflect.ValueOf(oid.Hex())
case "int":
return reflect.ValueOf(fmt.Sprint(a.Get(0).Interface().(int)))
}
return reflect.ValueOf(a.Get(0).Interface())
})
vars.SetFunc("append", func(a jet.Arguments) reflect.Value {
name := a.Get(0).Type().Name()
if name == "M" {
m := a.Get(0).Interface().(bson.M)
if m[a.Get(1).String()] != nil {
val := append(m[a.Get(1).String()].([]bson.M), a.Get(2).Interface().(bson.M))
m[a.Get(1).String()] = val
} else {
val := []bson.M{a.Get(2).Interface().(bson.M)}
m[a.Get(1).String()] = val
}
return reflect.ValueOf(m)
} else {
m := a.Get(0).Interface().(map[string]interface{})
if m[a.Get(1).String()] != nil {
val := append(m[a.Get(1).String()].([]interface{}), a.Get(2).Interface())
m[a.Get(1).String()] = val
} else {
val := []interface{}{a.Get(2).Interface()}
m[a.Get(1).String()] = val
}
return reflect.ValueOf(m)
}
})
vars.SetFunc("map", func(a jet.Arguments) reflect.Value {
if a.NumOfArguments()%2 > 0 {
return reflect.ValueOf(make(map[string]interface{}))
}
m := reflect.ValueOf(make(map[string]interface{}, a.NumOfArguments()/2))
for i := 0; i < a.NumOfArguments(); i += 2 {
m.SetMapIndex(a.Get(i), a.Get(i+1))
}
return m
})
vars.SetFunc("put", func(a jet.Arguments) reflect.Value {
name := a.Get(0).Type().Name()
if name == "M" {
m := a.Get(0).Interface().(bson.M)
m[a.Get(1).String()] = a.Get(2).Interface()
return reflect.ValueOf(m)
} else {
m := a.Get(0).Interface().(map[string]interface{})
m[a.Get(1).String()] = a.Get(2).Interface()
return reflect.ValueOf(m)
}
})
vars.SetFunc("sort", func(a jet.Arguments) reflect.Value {
m := a.Get(0).Interface().([]bson.M)
return reflect.ValueOf(Sort(a.Get(1).String(), m, a.Get(2).Bool()))
})
vars.SetFunc("d", func(a jet.Arguments) reflect.Value {
d := bson.D{}
for i := 0; i < a.NumOfArguments(); i += 2 {
d = append(d, bson.E{
Key: a.Get(i).String(),
Value: a.Get(i + 1).Interface(),
})
}
m := reflect.ValueOf(d)
return m
})
vars.SetFunc("aggregate", func(a jet.Arguments) reflect.Value {
p := mongo.Pipeline{}
for i := 0; i < a.NumOfArguments(); i++ {
p = append(p, a.Get(i).Interface().(bson.D))
}
m := reflect.ValueOf(p)
return m
})
vars.SetFunc("m", func(a jet.Arguments) reflect.Value {
d := bson.M{}
for i := 0; i < a.NumOfArguments(); i += 2 {
d[a.Get(i).String()] = a.Get(i + 1).Interface()
}
m := reflect.ValueOf(d)
return m
})
vars.SetFunc("findOption", 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()
if reflect.ValueOf(curPage).Kind() == reflect.Int {
d.CurPage = int64(curPage.(int))
} else {
d.CurPage = curPage.(int64)
}
pageSize := a.Get(2).Interface()
if reflect.ValueOf(pageSize).Kind() == reflect.Int {
d.PageSize = int64(pageSize.(int))
} else {
d.PageSize = pageSize.(int64)
}
}
m := reflect.ValueOf(d)
return m
})
vars.SetFunc("query", 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().(mongo.Pipeline), findOption)
return reflect.ValueOf(r)
} else {
r := resource.Query(a.Get(0).Interface().(mongo.Pipeline), findOption)
return reflect.ValueOf(r)
}
})
vars.SetFunc("context", func(a jet.Arguments) reflect.Value {
// r := *result
result[a.Get(0).String()] = a.Get(1).Interface()
return reflect.ValueOf(result)
})
return vars
}