/
structtemplater.go
134 lines (116 loc) · 2.85 KB
/
structtemplater.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
package ktemplate
import (
"reflect"
"strings"
"text/template"
"github.com/mitchellh/reflectwalk"
"k8s.io/client-go/kubernetes"
)
type StructTemplater struct {
Values map[string]interface{}
Clientset *kubernetes.Clientset
functions *Functions
// IgnoreFields from walking where key is field name and value is field type
IgnoreFields map[string]string
Funcs template.FuncMap
DelimSets []Delims
// If specified create a function for each value so that is can be accessed via {{ value }} in addition to {{ .value }}
ValueFunctions bool
RequiredTag string
}
type Delims struct {
Left, Right string
}
// this func is required to fulfil the reflectwalk.StructWalker interface
func (w StructTemplater) Struct(reflect.Value) error {
return nil
}
func (w StructTemplater) StructField(f reflect.StructField, v reflect.Value) error {
if !v.CanSet() {
return nil
}
for key, value := range w.IgnoreFields {
if key == f.Name && value == f.Type.String() {
return reflectwalk.SkipEntry
}
}
if w.RequiredTag != "" && f.Tag.Get(w.RequiredTag) != "true" {
return reflectwalk.SkipEntry
}
switch v.Kind() {
case reflect.String:
val, err := w.Template(v.String())
if err != nil {
return err
}
v.SetString(val)
case reflect.Map:
if len(v.MapKeys()) != 0 {
newMap := reflect.MakeMap(v.Type())
for _, key := range v.MapKeys() {
val := v.MapIndex(key)
newKey, err := w.templateKey(key)
if err != nil {
return err
}
if val.Kind() == reflect.String {
newVal, err := w.Template(val.String())
if err != nil {
return err
}
newMap.SetMapIndex(newKey, reflect.ValueOf(newVal))
} else {
newMap.SetMapIndex(newKey, val)
}
}
v.Set(newMap)
}
}
return nil
}
func (w StructTemplater) templateKey(v reflect.Value) (reflect.Value, error) {
if v.Kind() == reflect.String {
key, err := w.Template(v.String())
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(key), nil
}
return v, nil
}
func (w StructTemplater) Walk(object interface{}) error {
return reflectwalk.Walk(object, w)
}
func (w StructTemplater) Template(val string) (string, error) {
if strings.TrimSpace(val) == "" {
return val, nil
}
if w.functions == nil {
w.functions = NewFunctions(w.Clientset)
w.functions.Custom = w.Funcs
if w.ValueFunctions {
if w.functions.Custom == nil {
w.functions.Custom = make(template.FuncMap)
}
for k, v := range w.Values {
_v := v
w.functions.Custom[k] = func() interface{} {
return _v
}
}
}
}
if len(w.DelimSets) == 0 {
w.DelimSets = []Delims{{Left: "{{", Right: "}}"}}
}
var err error
for _, delims := range w.DelimSets {
w.functions.LeftDelim = delims.Left
w.functions.RightDelim = delims.Right
val, err = w.functions.Template(val, w.Values)
if err != nil {
return val, err
}
}
return val, nil
}