forked from jiaoge/sjet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
190 lines (162 loc) · 4.47 KB
/
global.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
package function
import (
"fmt"
"math"
"net/url"
"reflect"
"github.com/CloudyKit/jet/v6"
"github.com/shoppehub/sjet/engine"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// 初始化全局函数
func InitGlobalFunc(t *engine.TemplateEngine) {
// 把数字转换为int数组
t.Views.AddGlobalFunc("numArray", numArrayFunc)
// 支持把数据转换为字符串,比如 objectId
t.Views.AddGlobalFunc("string", stringFunc)
t.Views.AddGlobalFunc("oid", oidFunc)
t.Views.AddGlobalFunc("formatUrlPath", formatUrlPathFunc)
t.Views.AddGlobalFunc("map", mapFunc)
t.Views.AddGlobalFunc("put", putFunc)
t.Views.AddGlobalFunc("append", appendFunc)
t.Views.AddGlobalFunc("array", arrayFunc)
t.Views.AddGlobalFunc("aggregate", aggregateFunc)
t.Views.AddGlobalFunc("m", mFunc)
t.Views.AddGlobalFunc("d", dFunc)
t.Views.AddGlobalFunc("ceil", putFunc)
t.Views.AddGlobalFunc("floor", putFunc)
}
func oidFunc(a jet.Arguments) reflect.Value {
if !a.Get(0).IsValid() {
return reflect.ValueOf("")
}
oid, _ := primitive.ObjectIDFromHex(a.Get(0).String())
return reflect.ValueOf(oid)
}
func formatUrlPathFunc(a jet.Arguments) reflect.Value {
if !a.Get(0).IsValid() {
return reflect.ValueOf("")
}
u, _ := url.Parse(a.Get(0).Interface().(string))
return reflect.ValueOf(u.Path)
}
// 把数字转换为int数组
func numArrayFunc(a jet.Arguments) reflect.Value {
var total int
k := a.Get(0).Kind()
switch k {
case reflect.Float64:
total = int(a.Get(0).Float())
default:
total = int(a.Get(0).Int())
}
nums := make([]int64, total)
for i := 0; i < total; i++ {
nums[i] = int64(i + 1)
}
return reflect.ValueOf(nums)
}
func stringFunc(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())
}
func mapFunc(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
}
func putFunc(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)
}
}
func appendFunc(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)
}
}
func ceilFunc(a jet.Arguments) reflect.Value {
value := a.Get(0).Interface()
return reflect.ValueOf(int(math.Ceil(value.(float64))))
}
func floorFunc(a jet.Arguments) reflect.Value {
value := a.Get(0).Interface()
return reflect.ValueOf(int(math.Floor(value.(float64))))
}
func mFunc(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
}
func dFunc(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
}
func aggregateFunc(a jet.Arguments) reflect.Value {
var p []bson.D
for i := 0; i < a.NumOfArguments(); i++ {
p = append(p, a.Get(i).Interface().(bson.D))
}
m := reflect.ValueOf(p)
return m
}
func arrayFunc(a jet.Arguments) reflect.Value {
var p []interface{}
for i := 0; i < a.NumOfArguments(); i++ {
p = append(p, a.Get(i).Interface())
}
m := reflect.ValueOf(p)
return m
}