-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
272 lines (251 loc) · 5.53 KB
/
helpers.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
package tmplnator
import (
"bytes"
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"reflect"
"strings"
"time"
uuid "github.com/satori/go.uuid"
)
func newFuncMap(f *FileInfo) map[string]interface{} {
return map[string]interface{}{
"file": f.SetFilename,
"path": f.SetFullpath,
"mode": f.SetMode,
"dir_mode": f.SetDirmode,
"source": f.Source,
"timestamp": timestamp,
"to_json": toJSON,
"from_json": fromJSON,
"get": get,
"eql": reflect.DeepEqual,
"exists": exists,
"has_key": hasKey,
"def": def,
"default": def,
"url": parseURL,
"sha": hash,
"downcase": downcase,
"upcase": upcase,
"titleize": titleize,
"strip": trimSpace,
"split": split,
"replace": replace,
"trim": trim,
"trim_suffix": trimSuffix,
"fields": fields,
"has_suffix": hasSuffix,
"contains": contains,
"join": join,
"uuid": uuid.NewV4,
"truncate": truncate,
}
}
func parseURL(rawurl string) (u *url.URL, e error) { u, e = url.Parse(rawurl); return }
func timestamp() string { return time.Now().String() }
func hasSuffix(a, b interface{}) bool { return ssb(strings.HasSuffix, a, b) }
func contains(a, b interface{}) bool { return ssb(strings.Contains, a, b) }
func trim(a, b interface{}) interface{} { return ssm(strings.Trim, a, b) }
func trimSuffix(a, b interface{}) interface{} { return ssm(strings.TrimSuffix, a, b) }
func downcase(s interface{}) interface{} { return sm(strings.ToLower, s) }
func upcase(s interface{}) interface{} { return sm(strings.ToUpper, s) }
func titleize(s interface{}) interface{} { return sm(strings.Title, s) }
func trimSpace(s interface{}) interface{} { return sm(strings.TrimSpace, s) }
func truncate(i int, s interface{}) interface{} {
switch s := s.(type) {
case string:
if i >= 0 && i <= len(s) {
return s[:i]
}
case []byte:
if i >= 0 && i <= len(s) {
return s[:i]
}
}
return s
}
func fromJSON(d interface{}) (j map[string]interface{}, e error) {
switch d := d.(type) {
case string:
e = json.Unmarshal([]byte(d), &j)
case []byte:
e = json.Unmarshal(d, &j)
default:
return nil, errors.New("from_json only accepts a string or []byte")
}
return
}
func toJSON(i interface{}) (string, error) {
b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(i); err != nil {
return "", err
}
return strings.TrimSpace(b.String()), nil
}
func concat(a interface{}, bs ...interface{}) string {
switch a := a.(type) {
default:
return ""
case string:
b := new(bytes.Buffer)
b.WriteString(a)
for _, s := range bs {
if s, ok := s.(string); ok {
b.WriteString(s)
}
}
return b.String()
}
}
func exists(p interface{}) (bool, error) {
if p, ok := p.(string); ok {
if _, e := os.Stat(p); e != nil {
if os.IsNotExist(e) {
return false, nil
}
return false, e
}
return true, nil
}
return false, nil
}
func hasKey(m, k interface{}) bool {
defer func() {
if r := recover(); r != nil {
return
}
}()
ma, ke := reflect.ValueOf(m), reflect.ValueOf(k)
if !ma.IsValid() || ma.IsNil() || ma.Kind() != reflect.Map {
return false
}
if v := ma.MapIndex(ke); !v.IsValid() {
return false
}
return true
}
func def(a, b interface{}) interface{} {
switch a := a.(type) {
case string:
if a == "" {
return b
}
default:
if a == nil {
return b
}
}
return a
}
func get(s, i interface{}) interface{} {
a, b := reflect.ValueOf(s), reflect.ValueOf(i)
switch a.Kind() {
default:
return ""
case reflect.Array:
return a.Index(int(b.Int())).Interface()
case reflect.String:
return a.Index(int(b.Int())).Interface()
case reflect.Slice:
return a.Index(int(b.Int())).Interface()
case reflect.Map:
return a.MapIndex(b).Interface()
}
}
func hash(d interface{}) (string, error) {
if d, ok := d.(string); ok {
h := sha1.New()
io.WriteString(h, d)
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
return "", errors.New("sha only accepts a string")
}
func fields(s interface{}) interface{} {
if s == nil {
return ""
}
switch s := s.(type) {
default:
return s
case string:
return strings.Fields(s)
}
}
func split(a, b interface{}) interface{} {
if a == nil {
return ""
}
switch a := a.(type) {
default:
return a
case string:
if b, ok := b.(string); ok {
return strings.Split(a, b)
}
return a
}
}
func join(a, b interface{}) (interface{}, error) {
switch a := a.(type) {
default:
return "", errors.New("strings.Join requires a string slice")
case []string:
if b, ok := b.(string); ok {
return strings.Join(a, b), nil
}
return "", errors.New("The separator for strings.Join must be a string")
}
}
func replace(s interface{}, a, b string, n int) interface{} {
switch s := s.(type) {
default:
return s
case string:
return strings.Replace(s, a, b, n)
}
}
func sm(fn func(string) string, s interface{}) interface{} {
if s == nil {
return ""
}
switch s := s.(type) {
default:
return s
case string:
return fn(s)
}
}
func ssm(fn func(string, string) string, a, b interface{}) interface{} {
if a == nil {
return ""
}
switch a := a.(type) {
default:
return a
case string:
if b, ok := b.(string); ok {
return fn(a, b)
}
return a
}
}
func ssb(fn func(string, string) bool, a, b interface{}) bool {
if a == nil || b == nil {
return false
}
switch a := a.(type) {
default:
return false
case string:
if b, ok := b.(string); ok {
return fn(a, b)
}
return false
}
}