forked from aymerick/raymond
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhelper.go
572 lines (462 loc) · 14.1 KB
/
helper.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package raymond
import (
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"sync"
)
// Options represents the options argument provided to helpers and context functions.
type Options struct {
// evaluation visitor
eval *evalVisitor
// params
params []interface{}
hash map[string]interface{}
}
var (
// helpers stores all globally registered helpers
helpers = make(map[string]reflect.Value)
paramHelpers = make(map[string]paramHelperFunc)
// protects global helpers
helpersMutex sync.RWMutex
// protects global param helpers
paramHelpersMutex sync.RWMutex
)
func init() {
// Register builtin helpers.
RegisterHelper("if", ifHelper)
RegisterHelper("unless", unlessHelper)
RegisterHelper("with", withHelper)
RegisterHelper("each", eachHelper)
RegisterHelper("log", logHelper)
RegisterHelper("lookup", lookupHelper)
RegisterHelper("equal", equalHelper)
RegisterHelper("ifGt", ifGtHelper)
RegisterHelper("ifLt", ifLtHelper)
RegisterHelper("ifEq", ifEqHelper)
RegisterHelper("ifMatchesRegexStr", ifMatchesRegexStr)
RegisterHelper("pluralize", pluralizeHelper)
// Register builtin param helpers.
RegisterParamHelper("length", lengthParamHelper)
}
// RegisterHelper registers a global helper. That helper will be available to all templates.
func RegisterHelper(name string, helper interface{}) {
helpersMutex.Lock()
defer helpersMutex.Unlock()
if helpers[name] != zero {
panic(fmt.Errorf("Helper already registered: %s", name))
}
val := reflect.ValueOf(helper)
ensureValidHelper(name, val)
helpers[name] = val
}
// RegisterHelpers registers several global helpers. Those helpers will be available to all templates.
func RegisterHelpers(helpers map[string]interface{}) {
for name, helper := range helpers {
RegisterHelper(name, helper)
}
}
// RemoveHelper unregisters a global helper
func RemoveHelper(name string) {
helpersMutex.Lock()
defer helpersMutex.Unlock()
delete(helpers, name)
}
// RemoveAllHelpers unregisters all global helpers
func RemoveAllHelpers() {
helpersMutex.Lock()
defer helpersMutex.Unlock()
helpers = make(map[string]reflect.Value)
}
// ensureValidHelper panics if given helper is not valid
func ensureValidHelper(name string, funcValue reflect.Value) {
if funcValue.Kind() != reflect.Func {
panic(fmt.Errorf("Helper must be a function: %s", name))
}
funcType := funcValue.Type()
if funcType.NumOut() != 1 {
panic(fmt.Errorf("Helper function must return a string or a SafeString: %s", name))
}
// @todo Check if first returned value is a string, SafeString or interface{} ?
}
// findHelper finds a globally registered helper
func findHelper(name string) reflect.Value {
helpersMutex.RLock()
defer helpersMutex.RUnlock()
return helpers[name]
}
// newOptions instanciates a new Options
func newOptions(eval *evalVisitor, params []interface{}, hash map[string]interface{}) *Options {
return &Options{
eval: eval,
params: params,
hash: hash,
}
}
// newEmptyOptions instanciates a new empty Options
func newEmptyOptions(eval *evalVisitor) *Options {
return &Options{
eval: eval,
hash: make(map[string]interface{}),
}
}
//
// Context Values
//
// Value returns field value from current context.
func (options *Options) Value(name string) interface{} {
value := options.eval.evalField(options.eval.curCtx(), name, false)
if !value.IsValid() {
return nil
}
return value.Interface()
}
// ValueStr returns string representation of field value from current context.
func (options *Options) ValueStr(name string) string {
return Str(options.Value(name))
}
// Ctx returns current evaluation context.
func (options *Options) Ctx() interface{} {
return options.eval.curCtx().Interface()
}
//
// Hash Arguments
//
// HashProp returns hash property.
func (options *Options) HashProp(name string) interface{} {
return options.hash[name]
}
// HashStr returns string representation of hash property.
func (options *Options) HashStr(name string) string {
return Str(options.hash[name])
}
// Hash returns entire hash.
func (options *Options) Hash() map[string]interface{} {
return options.hash
}
//
// Parameters
//
// Param returns parameter at given position.
func (options *Options) Param(pos int) interface{} {
if len(options.params) > pos {
return options.params[pos]
}
return nil
}
// ParamStr returns string representation of parameter at given position.
func (options *Options) ParamStr(pos int) string {
return Str(options.Param(pos))
}
// Params returns all parameters.
func (options *Options) Params() []interface{} {
return options.params
}
//
// Private data
//
// Data returns private data value.
func (options *Options) Data(name string) interface{} {
return options.eval.dataFrame.Get(name)
}
// DataStr returns string representation of private data value.
func (options *Options) DataStr(name string) string {
return Str(options.eval.dataFrame.Get(name))
}
// DataFrame returns current private data frame.
func (options *Options) DataFrame() *DataFrame {
return options.eval.dataFrame
}
// NewDataFrame instanciates a new data frame that is a copy of current evaluation data frame.
//
// Parent of returned data frame is set to current evaluation data frame.
func (options *Options) NewDataFrame() *DataFrame {
return options.eval.dataFrame.Copy()
}
// newIterDataFrame instanciates a new data frame and set iteration specific vars
func (options *Options) newIterDataFrame(length int, i int, key interface{}) *DataFrame {
return options.eval.dataFrame.newIterDataFrame(length, i, key)
}
//
// Evaluation
//
// evalBlock evaluates block with given context, private data and iteration key
func (options *Options) evalBlock(ctx interface{}, data *DataFrame, key interface{}) string {
result := ""
if block := options.eval.curBlock(); (block != nil) && (block.Program != nil) {
result = options.eval.evalProgram(block.Program, ctx, data, key)
}
return result
}
// Fn evaluates block with current evaluation context.
func (options *Options) Fn() string {
return options.evalBlock(nil, nil, nil)
}
// FnCtxData evaluates block with given context and private data frame.
func (options *Options) FnCtxData(ctx interface{}, data *DataFrame) string {
return options.evalBlock(ctx, data, nil)
}
// FnWith evaluates block with given context.
func (options *Options) FnWith(ctx interface{}) string {
return options.evalBlock(ctx, nil, nil)
}
// FnData evaluates block with given private data frame.
func (options *Options) FnData(data *DataFrame) string {
return options.evalBlock(nil, data, nil)
}
// Inverse evaluates "else block".
func (options *Options) Inverse() string {
result := ""
if block := options.eval.curBlock(); (block != nil) && (block.Inverse != nil) {
result, _ = block.Inverse.Accept(options.eval).(string)
}
return result
}
// Eval evaluates field for given context.
func (options *Options) Eval(ctx interface{}, field string) interface{} {
if ctx == nil {
return nil
}
if field == "" {
return nil
}
val := options.eval.evalField(reflect.ValueOf(ctx), field, false)
if !val.IsValid() {
return nil
}
return val.Interface()
}
//
// Misc
//
// isIncludableZero returns true if 'includeZero' option is set and first param is the number 0
func (options *Options) isIncludableZero() bool {
b, ok := options.HashProp("includeZero").(bool)
if ok && b {
nb, ok := options.Param(0).(int)
if ok && nb == 0 {
return true
}
}
return false
}
//
// Builtin helpers
//
// #if block helper
func ifHelper(conditional interface{}, options *Options) interface{} {
if options.isIncludableZero() || IsTrue(conditional) {
return options.Fn()
}
return options.Inverse()
}
func ifGtHelper(a, b interface{}, options *Options) interface{} {
var aFloat, bFloat float64
var err error
if aFloat, err = floatValue(a); err != nil {
log.WithError(err).Errorf("failed to convert value to float '%v'", a)
return options.Inverse()
}
if bFloat, err = floatValue(b); err != nil {
log.WithError(err).Errorf("failed to convert value to float '%v'", b)
return options.Inverse()
}
if aFloat > bFloat {
return options.Fn()
}
// Evaluate possible else condition.
return options.Inverse()
}
func ifLtHelper(a, b interface{}, options *Options) interface{} {
var aFloat, bFloat float64
var err error
if aFloat, err = floatValue(a); err != nil {
log.WithError(err).Errorf("failed to convert value to float '%v'", a)
return options.Inverse()
}
if bFloat, err = floatValue(b); err != nil {
log.WithError(err).Errorf("failed to convert value to float '%v'", b)
return options.Inverse()
}
if aFloat < bFloat {
return options.Fn()
}
// Evaluate possible else condition.
return options.Inverse()
}
func ifEqHelper(a, b interface{}, options *Options) interface{} {
var aFloat, bFloat float64
var err error
if aFloat, err = floatValue(a); err != nil {
log.WithError(err).Errorf("failed to convert value to float '%v'", a)
return options.Inverse()
}
if bFloat, err = floatValue(b); err != nil {
log.WithError(err).Errorf("failed to convert value to float '%v'", b)
return options.Inverse()
}
if aFloat == bFloat {
return options.Fn()
}
// Evaluate possible else condition.
return options.Inverse()
}
// ifMatchesRegexStr is helper function which does a regex match, where a is the expression to compile and
// b is the string to match against.
func ifMatchesRegexStr(a, b interface{}, options *Options) interface{} {
exp := Str(a)
match := Str(b)
re, err := regexp.Compile(exp)
if err != nil {
log.WithError(err).Errorf("failed to compile regex '%v'", a)
return options.Inverse()
}
if re.MatchString(match) {
return options.Fn()
}
return options.Inverse()
}
func pluralizeHelper(count, plural, singular interface{}) interface{} {
if c, err := floatValue(count); err != nil || c <= 1 {
return singular
}
return plural
}
// #unless block helper
func unlessHelper(conditional interface{}, options *Options) interface{} {
if options.isIncludableZero() || IsTrue(conditional) {
return options.Inverse()
}
return options.Fn()
}
// #with block helper
func withHelper(context interface{}, options *Options) interface{} {
if IsTrue(context) {
return options.FnWith(context)
}
return options.Inverse()
}
// #each block helper
func eachHelper(context interface{}, options *Options) interface{} {
if !IsTrue(context) {
return options.Inverse()
}
result := ""
val := reflect.ValueOf(context)
switch val.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < val.Len(); i++ {
// computes private data
data := options.newIterDataFrame(val.Len(), i, nil)
// evaluates block
result += options.evalBlock(val.Index(i).Interface(), data, i)
}
case reflect.Map:
// note: a go hash is not ordered, so result may vary, this behaviour differs from the JS implementation
keys := val.MapKeys()
for i := 0; i < len(keys); i++ {
key := keys[i].Interface()
ctx := val.MapIndex(keys[i]).Interface()
// computes private data
data := options.newIterDataFrame(len(keys), i, key)
// evaluates block
result += options.evalBlock(ctx, data, key)
}
case reflect.Struct:
var exportedFields []int
// collect exported fields only
for i := 0; i < val.NumField(); i++ {
if tField := val.Type().Field(i); tField.PkgPath == "" {
exportedFields = append(exportedFields, i)
}
}
for i, fieldIndex := range exportedFields {
key := val.Type().Field(fieldIndex).Name
ctx := val.Field(fieldIndex).Interface()
// computes private data
data := options.newIterDataFrame(len(exportedFields), i, key)
// evaluates block
result += options.evalBlock(ctx, data, key)
}
}
return result
}
// #log helper
func logHelper(message string) interface{} {
log.Print(message)
return ""
}
// #lookup helper
func lookupHelper(obj interface{}, field string, options *Options) interface{} {
return Str(options.Eval(obj, field))
}
// #equal helper
// Ref: https://github.com/aymerick/raymond/issues/7
func equalHelper(a interface{}, b interface{}, options *Options) interface{} {
if Str(a) == Str(b) {
return options.Fn()
}
return ""
}
// floatValue attempts to convert value into a float64 and returns an error if it fails.
func floatValue(value interface{}) (result float64, err error) {
val := reflect.ValueOf(value)
switch val.Kind() {
case reflect.Bool:
result = 0
if val.Bool() {
result = 1
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
result = float64(val.Int())
case reflect.Float32, reflect.Float64:
result = val.Float()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
result = float64(val.Uint())
case reflect.String:
result, err = strconv.ParseFloat(val.String(), 64)
default:
err = errors.New(fmt.Sprintf("uable to convert type '%s' to float64", val.Kind().String()))
}
return
}
// A paramHelperFunc is a function that will mutate the input by performing some kind of
// operation on it. Such as getting the length of a string, slice, or map.
type paramHelperFunc func(value reflect.Value) reflect.Value
// RegisterParamHelper registers a global param helper. That helper will be available to all templates.
func RegisterParamHelper(name string, helper paramHelperFunc) {
paramHelpersMutex.Lock()
defer paramHelpersMutex.Unlock()
if _, ok := paramHelpers[name]; ok {
panic(fmt.Errorf("Param helper already registered: %s", name))
}
paramHelpers[name] = helper
}
// RemoveParamHelper unregisters a global param helper
func RemoveParamHelper(name string) {
paramHelpersMutex.Lock()
defer paramHelpersMutex.Unlock()
delete(paramHelpers, name)
}
// findParamHelper finds a globally registered param helper
func findParamHelper(name string) paramHelperFunc {
paramHelpersMutex.RLock()
defer paramHelpersMutex.RUnlock()
return paramHelpers[name]
}
// lengthParamHelper is a helper func to return the length of the value passed. It
// will only return the length if the value is an array, slice, map, or string. Otherwise,
// it returns zero value.
// e.g. foo == "foo" -> foo.length -> 3
func lengthParamHelper(ctx reflect.Value) reflect.Value {
if ctx == zero {
return ctx
}
switch ctx.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
return reflect.ValueOf(ctx.Len())
}
return zero
}