-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathlitetpl.go
277 lines (232 loc) · 6.69 KB
/
litetpl.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
273
274
275
276
277
package textutil
import (
"fmt"
"io"
"regexp"
"strings"
"text/template"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/basefn"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/strutil"
)
// LTemplateOptFn lite template option func
type LTemplateOptFn func(opt *LiteTemplateOpt)
// LiteTemplateOpt template options for LiteTemplate
type LiteTemplateOpt struct {
// func name alias map. eg: {"up_first": "upFirst"}
nameMp structs.Aliases
Funcs template.FuncMap
Left, Right string
ParseDef bool
ParseEnv bool
}
// SetVarFmt custom sets the variable format in template
func (o *LiteTemplateOpt) SetVarFmt(varFmt string) {
o.Left, o.Right = strutil.TrimCut(varFmt, ",")
}
// LiteTemplate implement a simple text template engine.
//
// - support parse template vars
// - support access multi-level map field. eg: {{ user.name }}
// - support parse default value
// - support parse env vars
// - support custom pipeline func handle. eg: {{ name | upper }} {{ name | def:guest }}
//
// NOTE: not support control flow, eg: if/else/for/with
type LiteTemplate struct {
LiteTemplateOpt
vr VarReplacer
// template func map. refer the text/template
//
// Func allow return 1 or 2 values, if return 2 values, the second value is error.
fxs map[string]*reflects.FuncX
}
// NewLiteTemplate instance
func NewLiteTemplate(opFns ...LTemplateOptFn) *LiteTemplate {
st := &LiteTemplate{
fxs: make(map[string]*reflects.FuncX),
// with default options
LiteTemplateOpt: LiteTemplateOpt{
Left: "{{",
Right: "}}",
ParseDef: true,
ParseEnv: true,
},
}
st.vr.RenderFn = st.renderVars
for _, fn := range opFns {
fn(&st.LiteTemplateOpt)
}
st.Init()
return st
}
// Init LiteTemplate
func (t *LiteTemplate) Init() {
if t.vr.init {
return
}
// init var replacer
t.vr.init = true
t.initReplacer(&t.vr)
// add built-in funcs
t.AddFuncs(builtInFuncs)
t.nameMp.AddAliasMap(map[string]string{
"up_first": "upFirst",
"lc_first": "lcFirst",
"def": "default",
})
// add custom funcs
if len(t.Funcs) > 0 {
t.AddFuncs(t.Funcs)
}
}
func (t *LiteTemplate) initReplacer(vr *VarReplacer) {
vr.flatSubs = true
vr.parseDef = t.ParseDef
vr.parseEnv = t.ParseEnv
vr.Left, vr.Right = t.Left, t.Right
basefn.PanicIf(vr.Right == "", "var format right chars is required")
vr.lLen, vr.rLen = len(vr.Left), len(vr.Right)
rightLast := string(vr.Right[vr.rLen-1]) // 排除匹配,防止匹配到类似 "{} adb ddf {var}"
// eg: \{(?s:([^\}]+?))\}
// (?s:...) - 让 "." 匹配换行
// (?s:(.+?)) - 第二个 "?" 非贪婪匹配
pattern := regexp.QuoteMeta(vr.Left) + `(?s:([^` + regexp.QuoteMeta(rightLast) + `]+?))` + regexp.QuoteMeta(vr.Right)
vr.varReg = regexp.MustCompile(pattern)
}
// AddFuncs add custom template functions
func (t *LiteTemplate) AddFuncs(fns map[string]any) {
for name, fn := range fns {
t.fxs[name] = reflects.NewFunc(fn)
}
}
// RenderString render template string with vars
func (t *LiteTemplate) RenderString(s string, vars map[string]any) string {
return t.vr.Replace(s, vars)
}
// RenderFile render template file with vars
func (t *LiteTemplate) RenderFile(filePath string, vars map[string]any) (string, error) {
// read file contents
s, err := fsutil.ReadStringOrErr(filePath)
if err != nil {
return "", err
}
return t.vr.Replace(s, vars), nil
}
// RenderWrite render template string with vars, and write result to writer
func (t *LiteTemplate) RenderWrite(wr io.Writer, s string, vars map[string]any) error {
s = t.vr.Replace(s, vars)
_, err := io.WriteString(wr, s)
return err
}
func (t *LiteTemplate) renderVars(s string, varMap map[string]string) string {
return t.vr.varReg.ReplaceAllStringFunc(s, func(sub string) string {
// var name or pipe expression.
name := strings.TrimSpace(sub[t.vr.lLen : len(sub)-t.vr.rLen])
name = strings.TrimLeft(name, "$.")
var defVal string
var pipes []string
if strings.ContainsRune(name, '|') {
pipes = strutil.Split(name, "|")
// compatible default value. eg: {{ name | inhere }}
if len(pipes) == 2 && !strings.ContainsRune(pipes[1], ':') && !t.isFunc(pipes[1]) {
name, defVal = pipes[0], pipes[1]
pipes = nil // clear pipes
} else { // collect pipe functions
name, pipes = pipes[0], pipes[1:]
}
}
if val, ok := varMap[name]; ok {
if len(pipes) > 0 {
var err error
val, err = t.applyPipes(val, pipes)
if err != nil {
return fmt.Sprintf("Render var %q error: %v", name, err)
}
}
return val
}
// var not found
if len(defVal) > 0 {
return defVal
}
if t.vr.NotFound != nil {
if val, ok := t.vr.NotFound(name); ok {
return val
}
}
// check is default func. eg: {{ name | def:guest }}
if len(pipes) == 1 && strings.ContainsRune(pipes[0], ':') {
fName, argVal := strutil.TrimCut(pipes[0], ":")
if t.isDefaultFunc(fName) {
return argVal
}
}
t.vr.missVars = append(t.vr.missVars, name)
return sub
})
}
func (t *LiteTemplate) applyPipes(val any, pipes []string) (string, error) {
var err error
// pipe expr: "trim|upper|substr:1,2"
// =>
// pipes: ["trim", "upper", "substr:1,2"]
for _, name := range pipes {
args := []any{val}
// has custom args. eg: "substr:1,2"
if strings.ContainsRune(name, ':') {
var argStr string
name, argStr = strutil.TrimCut(name, ":")
if otherArgs := parseArgStr(argStr); len(otherArgs) > 0 {
args = append(args, otherArgs...)
}
}
name = t.nameMp.ResolveAlias(name)
// call pipe func
if fx, ok := t.fxs[name]; ok {
val, err = fx.Call2(args...)
if err != nil {
return "", err
}
} else {
return "", fmt.Errorf("template func %q not found", name)
}
}
return strutil.ToString(val)
}
func (t *LiteTemplate) isFunc(name string) bool {
_, ok := t.fxs[name]
if !ok {
// check name alias
return t.nameMp.HasAlias(name)
}
return ok
}
func (t *LiteTemplate) isDefaultFunc(name string) bool {
return name == "default" || name == "def"
}
var stdTpl = NewLiteTemplate()
// RenderFile render template file with vars
func RenderFile(filePath string, vars map[string]any) (string, error) {
return stdTpl.RenderFile(filePath, vars)
}
// RenderString render str template string or file.
func RenderString(input string, data map[string]any) string {
return stdTpl.RenderString(input, data)
}
// RenderWrite render template string with vars, and write result to writer
func RenderWrite(wr io.Writer, s string, vars map[string]any) error {
return stdTpl.RenderWrite(wr, s, vars)
}
func parseArgStr(argStr string) (ss []any) {
if argStr == "" { // no arg
return
}
if len(argStr) == 1 { // one char
return []any{argStr}
}
return arrutil.StringsToAnys(strutil.Split(argStr, ","))
}