forked from robfig/soy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.go
178 lines (154 loc) · 3.95 KB
/
funcs.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
package soyhtml
import (
"math"
"math/rand"
"strings"
"github.com/robfig/soy/data"
)
type loopFunc func(s *state, key string) data.Value
var loopFuncs = map[string]loopFunc{
"index": funcIndex,
"isFirst": funcIsFirst,
"isLast": funcIsLast,
}
func funcIndex(s *state, key string) data.Value {
return s.context.lookup(key + "__index")
}
func funcIsFirst(s *state, key string) data.Value {
return data.Bool(s.context.lookup(key+"__index").(data.Int) == 0)
}
func funcIsLast(s *state, key string) data.Value {
return data.Bool(
s.context.lookup(key+"__index").(data.Int) == s.context.lookup(key+"__lastIndex").(data.Int))
}
// Func represents a soy function that may be invoked within a soy template.
type Func struct {
Apply func([]data.Value) data.Value
ValidArgLengths []int
}
// Funcs contains the builtin soy functions.
// Callers may add their own functions to this map as well.
var Funcs = map[string]Func{
"isNonnull": {funcIsNonnull, []int{1}},
"length": {funcLength, []int{1}},
"keys": {funcKeys, []int{1}},
"augmentMap": {funcAugmentMap, []int{2}},
"round": {funcRound, []int{1, 2}},
"floor": {funcFloor, []int{1}},
"ceiling": {funcCeiling, []int{1}},
"min": {funcMin, []int{2}},
"max": {funcMax, []int{2}},
"randomInt": {funcRandomInt, []int{1}},
"strContains": {funcStrContains, []int{2}},
"range": {funcRange, []int{1, 2, 3}},
"hasData": {funcHasData, []int{0}},
}
func funcIsNonnull(v []data.Value) data.Value {
return data.Bool(!(v[0] == data.Null{} || v[0] == data.Undefined{}))
}
func funcLength(v []data.Value) data.Value {
return data.Int(len(v[0].(data.List)))
}
func funcKeys(v []data.Value) data.Value {
var keys data.List
for k, _ := range v[0].(data.Map) {
keys = append(keys, data.String(k))
}
return keys
}
func funcAugmentMap(v []data.Value) data.Value {
var m1 = v[0].(data.Map)
var m2 = v[1].(data.Map)
var result = make(data.Map, len(m1)+len(m2)+4)
for k, v := range m1 {
result[k] = v
}
for k, v := range m2 {
result[k] = v
}
return result
}
func funcRound(v []data.Value) data.Value {
var digitsAfterPt = 0
if len(v) == 2 {
digitsAfterPt = int(v[1].(data.Int))
}
var result = round(toFloat(v[0]), digitsAfterPt)
if digitsAfterPt <= 0 {
return data.Int(result)
}
return data.Float(result)
}
func round(x float64, prec int) float64 {
pow := math.Pow(10, float64(prec))
intermed := x * pow
if intermed < 0.0 {
intermed -= 0.5
} else {
intermed += 0.5
}
return float64(int64(intermed)) / float64(pow)
}
func funcFloor(v []data.Value) data.Value {
if isInt(v[0]) {
return v[0]
}
return data.Int(math.Floor(toFloat(v[0])))
}
func funcCeiling(v []data.Value) data.Value {
if isInt(v[0]) {
return v[0]
}
return data.Int(math.Ceil(toFloat(v[0])))
}
func funcMin(v []data.Value) data.Value {
if isInt(v[0]) && isInt(v[1]) {
if v[0].(data.Int) < v[1].(data.Int) {
return v[0]
}
return v[1]
}
return data.Float(math.Min(toFloat(v[0]), toFloat(v[1])))
}
func funcMax(v []data.Value) data.Value {
if isInt(v[0]) && isInt(v[1]) {
if v[0].(data.Int) > v[1].(data.Int) {
return v[0]
}
return v[1]
}
return data.Float(math.Max(toFloat(v[0]), toFloat(v[1])))
}
func funcRandomInt(v []data.Value) data.Value {
return data.Int(rand.Int63n(int64(v[0].(data.Int))))
}
func funcStrContains(v []data.Value) data.Value {
return data.Bool(strings.Contains(string(v[0].(data.String)), string(v[1].(data.String))))
}
func funcRange(v []data.Value) data.Value {
var (
increment = 1
init = 0
limit int
)
switch len(v) {
case 3:
increment = int(v[2].(data.Int))
fallthrough
case 2:
init = int(v[0].(data.Int))
limit = int(v[1].(data.Int))
case 1:
limit = int(v[0].(data.Int))
}
var indices data.List
var i = 0
for index := init; index < limit; index += increment {
indices = append(indices, data.Int(index))
i++
}
return indices
}
func funcHasData(v []data.Value) data.Value {
return data.Bool(true)
}