forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
structtemplater.go
187 lines (162 loc) · 4.29 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
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
package gomplate
import (
"reflect"
"strings"
"github.com/flanksource/commons/context"
"github.com/flanksource/commons/logger"
"github.com/mitchellh/reflectwalk"
"gopkg.in/yaml.v3"
)
type StructTemplater struct {
Context context.Context
Values map[string]interface{}
// IgnoreFields from walking where key is field name and value is field type
IgnoreFields map[string]string
Funcs map[string]any
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.Slice:
switch v.Type().Elem().Kind() {
case reflect.Uint8:
val, err := w.Template(string(v.Bytes()))
if err != nil {
return err
}
v.SetBytes([]byte(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
}
concreteVal := reflect.ValueOf(val.Interface())
switch concreteVal.Kind() {
case reflect.String:
newVal, err := w.Template(concreteVal.String())
if err != nil {
return err
}
newMap.SetMapIndex(newKey, reflect.ValueOf(newVal))
case reflect.Map:
marshalled, err := yaml.Marshal(val.Interface())
if err != nil {
newMap.SetMapIndex(newKey, val)
} else {
templated, err := w.Template(string(marshalled))
if err != nil {
return err
}
var unmarshalled map[string]any
if err := yaml.Unmarshal([]byte(templated), &unmarshalled); err != nil {
newMap.SetMapIndex(newKey, val)
} else {
newMap.SetMapIndex(newKey, reflect.ValueOf(unmarshalled))
}
}
default:
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 {
if w.Context.Logger == nil {
w.Context = newContext()
}
w.Context.Logger.V(7).Infof("walking %s", logger.Pretty(object))
return reflectwalk.Walk(object, w)
}
func (w StructTemplater) Template(val string) (string, error) {
w.Context.Logger.V(8).Infof("templating %s", val)
in := val
if strings.TrimSpace(val) == "" {
return val, nil
}
if w.Funcs == nil {
w.Funcs = make(map[string]any)
}
if w.ValueFunctions {
for k, v := range w.Values {
_v := v
w.Funcs[k] = func() interface{} {
return _v
}
}
}
delimSets := w.DelimSets
// parse go-template headers and override the delim set, as otherwise the header get stripped out
// for the first set of delims and reverts back to the default for the subsequent delimeters
template, err := parseAndStripTemplateHeader(Template{Template: val})
if err != nil {
return "", err
}
val = template.Template
if template.LeftDelim != "" && template.RightDelim != "" {
delimSets = []Delims{{Left: template.LeftDelim, Right: template.RightDelim}}
}
if len(delimSets) == 0 {
delimSets = []Delims{{Left: "{{", Right: "}}"}}
}
for _, delims := range delimSets {
val, err = goTemplate(w.Context, Template{
Template: val,
Functions: w.Funcs,
RightDelim: delims.Right,
LeftDelim: delims.Left,
}, w.Values)
if err != nil {
return val, err
}
}
val = strings.TrimSpace(val)
if strings.TrimSpace(val) != strings.TrimSpace(in) {
w.Context.Logger.V(6).Infof("==> %s", val)
}
return val, nil
}