-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_builtin.go
164 lines (151 loc) · 3.5 KB
/
mod_builtin.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
package dyntpl
import (
"math"
)
const (
// Types of round.
round = iota
roundPrec
ceil
ceilPrec
floor
floorPrec
)
// If var is empty, the given default value (first in args) will print instead.
func modDefault(ctx *Ctx, buf *any, val any, args []any) (err error) {
if len(args) == 0 {
err = ErrModNoArgs
return
}
// Check is value is empty.
if EmptyCheck(ctx, val) {
*buf = args[0]
}
return
}
// Shorthand replacement of {% if ... %}{%= ... %}{% endif %} statement.
//
// Example of usage: {%= leftVal|ifThen(val) %}, leftVal should be a boolean.
func modIfThen(_ *Ctx, buf *any, val any, args []any) (err error) {
if len(args) == 0 {
err = ErrModNoArgs
return
}
if b, ok := ConvBool(val); ok {
if b {
*buf = args[0]
}
}
return
}
// Shorthand replacement of {% if ... %}{%= ... %}{% else %}{%= ... %}{% endif %} statement.
//
// Example of usage: {%= leftVal|ifThenElse(valIfTrue, valIfFalse) %}, leftVal should be a boolean.
// valIfTrue and valIfFalse may has arbitrary types or may be a static values.
func modIfThenElse(_ *Ctx, buf *any, val any, args []any) (err error) {
if len(args) < 2 {
err = ErrModPoorArgs
return
}
if b, ok := ConvBool(val); ok {
if b {
*buf = args[0]
} else {
*buf = args[1]
}
}
return
}
// Round float val to integer using rounding half away from zero algorithm.
func modRound(ctx *Ctx, buf *any, val any, args []any) (err error) {
if f, ok := ConvFloat(val); ok {
ctx.BufF = roundHelper(f, round, args)
*buf = &ctx.BufF
}
return
}
// Round to precision, example: pi|roundPrec(3) will print 3.141
func modRoundPrec(ctx *Ctx, buf *any, val any, args []any) (err error) {
if f, ok := ConvFloat(val); ok {
ctx.BufF = roundHelper(f, roundPrec, args)
*buf = &ctx.BufF
}
return
}
// Round to least integer value greater than or equal to val.
func modCeil(ctx *Ctx, buf *any, val any, args []any) (err error) {
if f, ok := ConvFloat(val); ok {
ctx.BufF = roundHelper(f, ceil, args)
*buf = &ctx.BufF
}
return
}
// Ceil round to precision, example: 56.68734|ceilPrec will print 56.688
func modCeilPrec(ctx *Ctx, buf *any, val any, args []any) (err error) {
if f, ok := ConvFloat(val); ok {
ctx.BufF = roundHelper(f, ceilPrec, args)
*buf = &ctx.BufF
}
return
}
// Round to greatest integer value less than or equal to val.
func modFloor(ctx *Ctx, buf *any, val any, args []any) (err error) {
if f, ok := ConvFloat(val); ok {
ctx.BufF = roundHelper(f, floor, args)
*buf = &ctx.BufF
}
return
}
// Float round to precision, example: 20.214999|floorPrec(3) will print 20.214
func modFloorPrec(ctx *Ctx, buf *any, val any, args []any) (err error) {
if f, ok := ConvFloat(val); ok {
ctx.BufF = roundHelper(f, floorPrec, args)
*buf = &ctx.BufF
}
return
}
// Universal internal round helper for round modifiers.
func roundHelper(f float64, mode int, args []any) float64 {
var (
prec int64
ok bool
)
if len(args) > 0 {
if prec, ok = if2int(args[0]); !ok {
return f
}
}
switch mode {
case round:
return math.Round(f)
case roundPrec:
if prec == 0 {
return f
}
p := math.Pow10(int(prec))
return float64(int(f*p)) / p
case ceil:
return math.Ceil(f)
case ceilPrec:
if prec == 0 {
return f
}
p := math.Pow10(int(prec))
x := p * f
return math.Ceil(x) / p
case floor:
return math.Floor(f)
case floorPrec:
if prec == 0 {
return f
}
p := math.Pow10(int(prec))
x := p * f
return math.Floor(x) / p
}
return f
}
func modTestNameOf(_ *Ctx, _ *any, _ any, _ []any) (err error) {
// do nothing
return
}