-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_map.go
73 lines (62 loc) · 1.38 KB
/
func_map.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
package main
import (
"encoding/json"
"hash/fnv"
"log"
"strconv"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"gopkg.in/yaml.v2"
)
func funcMap() template.FuncMap {
f := sprig.TxtFuncMap()
f["baseConvert"] = baseConvert
f["fnv64sum"] = fnv64sum
f["fromJson"] = fromJson
f["fromYaml"] = fromYaml
f["toYaml"] = toYaml
f["trimLeft"] = trimLeft
f["trimRight"] = trimRight
return f
}
func baseConvert(from, to int, s string) string {
i, err := strconv.ParseInt(s, from, 64)
if err != nil {
log.Printf("warning: cannot parse integer base %d from string %q: %v", from, s, err)
return "0"
}
return strconv.FormatInt(i, to)
}
func fnv64sum(s string) string {
return string(fnv.New64().Sum([]byte(s)))
}
func fromJson(p string) interface{} {
var v interface{}
err := json.Unmarshal([]byte(p), &v)
if err != nil {
log.Printf("cannot unmarshal JSON: %v (in %q)", err, p)
}
return v
}
func fromYaml(p string) interface{} {
var v interface{}
err := yaml.Unmarshal([]byte(p), &v)
if err != nil {
log.Printf("cannot unmarshal YAML: %v (in %q)", err, p)
}
return v
}
func toYaml(v interface{}) string {
d, err := yaml.Marshal(v)
if err != nil {
log.Printf("cannot marshal YAML: %v", err)
}
return string(d)
}
func trimLeft(cut, s string) string {
return strings.TrimLeft(s, cut)
}
func trimRight(cut, s string) string {
return strings.TrimRight(s, cut)
}