forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate.go
More file actions
520 lines (447 loc) · 14.9 KB
/
Copy pathtemplate.go
File metadata and controls
520 lines (447 loc) · 14.9 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package gomplate
import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
gotemplate "text/template"
"time"
commonsContext "github.com/flanksource/commons/context"
"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/properties"
_ "github.com/flanksource/gomplate/v3/js"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/patrickmn/go-cache"
"github.com/robertkrimen/otto"
"github.com/robertkrimen/otto/registry"
_ "github.com/robertkrimen/otto/underscore"
"github.com/samber/oops"
"google.golang.org/protobuf/types/known/structpb"
)
var funcMap gotemplate.FuncMap
var (
// keep the cache period low as lots of anonymous functions can pile up the cache.
goTemplateCache = cache.New(time.Hour, time.Hour)
celExpressionCache = cache.New(time.Hour, time.Hour)
)
func init() {
funcMap = CreateFuncs(context.Background())
}
type Template struct {
Template string `yaml:"template,omitempty" json:"template,omitempty"` // Go template
JSONPath string `yaml:"jsonPath,omitempty" json:"jsonPath,omitempty"`
Expression string `yaml:"expr,omitempty" json:"expr,omitempty"` // A cel-go expression
Javascript string `yaml:"javascript,omitempty" json:"javascript,omitempty"`
RightDelim string `yaml:"-" json:"-"`
LeftDelim string `yaml:"-" json:"-"`
// DelimSets, when non-empty, runs the gotemplate over the input once per
// delimiter pair, feeding the output of each pass into the next. Useful for
// inputs that mix delimiter styles (e.g. {{ }} and $( )).
// A header (# gotemplate: left-delim=… right-delim=…) overrides DelimSets to
// a single pass. Falls back to LeftDelim/RightDelim if both are set, otherwise
// to the default {{ }}.
DelimSets []Delims `yaml:"-" json:"-"`
// ValueFunctions, when true, exposes each key in the environment as a
// zero-arg function in addition to dot-access. Enables {{ foo }} alongside
// the standard {{ .foo }}.
ValueFunctions bool `yaml:"-" json:"-"`
// Pass in additional cel-env options like functions
// that aren't simple enough to be included in Functions
CelEnvs []cel.EnvOption `yaml:"-" json:"-"`
// A map of functions that are accessible to cel expressions
// and go templates.
// NOTE: For cel expressions, the functions must be of type func() any.
// If any other function type is used, an error will be returned.
// Opt to CelEnvs for those cases.
Functions map[string]any `yaml:"-" json:"-"`
// CacheKey, when non-empty, is used as the program-cache key for this
// template, bypassing the IsCacheable() heuristic. The caller asserts that
// any two templates sharing this key may share the compiled cel.Program
// (same expression semantics, same env shape, same function semantics).
CacheKey string `yaml:"-" json:"-"`
// CacheTime controls how long the compiled program/template is retained
// in the cache. Zero means use the cache's default TTL; a positive value
// is an explicit TTL; a negative value means no expiration.
CacheTime time.Duration `yaml:"-" json:"-"`
}
func (t Template) String() string {
if t.Template != "" {
return "gotemplate: " + t.Template
}
if t.Expression != "" {
return "cel: " + t.Expression
}
if t.Javascript != "" {
return "js: " + t.Javascript
}
if t.JSONPath != "" {
return "jsonpath: " + t.JSONPath
}
return ""
}
func (t Template) ShortString() string {
if t.Template != "" {
return "gotemplate: " + short(t.Template)
}
if t.Expression != "" {
return "cel: " + short(t.Expression)
}
if t.Javascript != "" {
return "js: " + short(t.Javascript)
}
if t.JSONPath != "" {
return "jsonpath: " + short(t.JSONPath)
}
return ""
}
func short(v string) string {
v = strings.TrimSpace(v)
if len(v) == 0 {
return ""
}
lines := strings.Split(v, "\n")
if len(lines) == 1 {
return lines[0]
}
return fmt.Sprintf("%s .. %d more lines", lines[0], len(lines)-1)
}
// autoCacheKey derives a cache key from the template fields and the env shape.
// Used when the caller has not supplied an explicit CacheKey.
func (t Template) autoCacheKey(env map[string]any) string {
envVars := make([]string, 0, len(env)+1)
for k := range env {
envVars = append(envVars, k)
}
sort.Slice(envVars, func(i, j int) bool { return envVars[i] < envVars[j] })
return strings.Join(envVars, "-") +
t.RightDelim +
t.LeftDelim +
t.Expression +
t.Javascript +
t.JSONPath +
t.Template
}
// cacheKey returns the key to use for the program/template cache. If the caller
// supplied CacheKey, it is used verbatim; otherwise the auto-derived key is used.
func (t Template) cacheKey(env map[string]any) string {
if t.CacheKey != "" {
return t.CacheKey
}
return t.autoCacheKey(env)
}
func (t Template) IsCacheable() bool {
// An explicit CacheKey is the caller asserting the program is reusable
// regardless of Functions/CelEnvs identity.
if t.CacheKey != "" {
return true
}
// Note: If custom functions are provided then we don't cache the template
// because it's not possible to uniquely identify a function to be used as a cache key.
// Pointers don't work well because different functions, that are behaviourly different,
// but syntatically identical, will have the same pointer value.
//
// Reference: https://pkg.go.dev/reflect#Value.Pointer
// > If v's Kind is Func, the returned pointer is an underlying code pointer,
// > but not necessarily enough to identify a single function uniquely.
// > The only guarantee is that the result is zero if and only if v is a nil func Value.
return len(t.CelEnvs) == 0 && len(t.Functions) == 0
}
func (t Template) IsEmpty() bool {
return t.Template == "" && t.JSONPath == "" && t.Expression == "" && t.Javascript == ""
}
func RunExpression(_environment map[string]any, template Template) (any, error) {
return RunExpressionContext(newContext(), _environment, template)
}
func RunExpressionContext(ctx commonsContext.Context, _environment map[string]any, template Template) (any, error) {
data, err := Serialize(_environment)
if err != nil {
return "", err
}
// Look up the compiled-program cache BEFORE constructing the CEL env options.
// GetCelEnv (notably kubernetes.Library()) is the dominant allocation on the CEL
// path. On the overwhelmingly common cache hit it would be built and then
// immediately discarded, since cel.NewEnv is only needed to compile a new
// program. Build env options only when we actually need to compile.
var prg cel.Program
if template.IsCacheable() {
cached, ok := celExpressionCache.Get(template.cacheKey(_environment))
if ok {
if cachedPrg, ok := cached.(*cel.Program); ok {
prg = *cachedPrg
}
}
}
if prg == nil {
base, err := baseCelEnv()
if err != nil {
return "", err
}
// Only the per-call options are layered on top of the cached base env: the
// heavy, environment-independent libraries already live in base. This keeps
// the dominant CEL setup cost (kubernetes.Library and declaration
// validation) off the compile path.
envOptions := make([]cel.EnvOption, 0, len(typeAdapters)+len(data)+len(template.Functions)+len(template.CelEnvs))
envOptions = append(envOptions, typeAdapters...)
for k := range data {
envOptions = append(envOptions, cel.Variable(k, cel.AnyType))
}
for name, fn := range template.Functions {
_name := name
_fn := fn
envOptions = append(envOptions, cel.Function(_name, cel.Overload(
_name,
nil,
cel.AnyType,
cel.FunctionBinding(func(values ...ref.Val) ref.Val {
ogFunc, ok := _fn.(func() any)
if !ok {
return types.WrapErr(fmt.Errorf("%s is expected to be of type func() any", _name))
}
out := ogFunc()
return types.DefaultTypeAdapter.NativeToValue(out)
}),
)))
}
envOptions = append(envOptions, template.CelEnvs...)
env, err := base.Extend(envOptions...)
if err != nil {
return "", err
}
ast, issues := env.Compile(strings.ReplaceAll(template.Expression, "\n", " "))
if issues != nil && issues.Err() != nil {
return "", oops.With("template", template.Expression).Errorf("issues: %s", issues.String())
}
prg, err = env.Program(ast)
if err != nil {
return "", err
}
if template.IsCacheable() {
celExpressionCache.Set(template.cacheKey(_environment), &prg, template.CacheTime)
}
}
out, _, err := prg.Eval(data)
if err != nil {
return nil, oops.With("template", template.Expression).Wrap(err)
}
if ctx.Logger != nil && out.Value() != template.Expression && properties.On(false, "gomplate.log") {
ctx.Logger.V(4).Infof("templated %s => %v", template.ShortString(), out)
}
return out.Value(), nil
}
func newContext() commonsContext.Context {
return commonsContext.NewContext(context.TODO(),
commonsContext.WithLogger(logger.GetLogger("gomplate")))
}
func RunTemplateBool(environment map[string]any, template Template) (bool, error) {
output, err := RunTemplateContext(newContext(), environment, template)
if err != nil {
return false, err
}
result, err := strconv.ParseBool(output)
if err != nil {
return false, fmt.Errorf("failed to parse template output (%s) as bool: %w", output, err)
}
return result, nil
}
func RunTemplate(environment map[string]any, template Template) (string, error) {
return RunTemplateContext(newContext(), environment, template)
}
func RunTemplateContext(ctx commonsContext.Context, environment map[string]any, template Template) (string, error) {
// javascript
if template.Javascript != "" {
vm := otto.New()
for k, v := range environment {
if err := vm.Set(k, v); err != nil {
return "", fmt.Errorf("error setting %s: %w", k, err)
}
}
out, err := vm.Run(template.Javascript)
if err != nil {
return "", fmt.Errorf("failed to run javascript: %w", err)
}
if s, err := out.ToString(); err != nil {
return "", fmt.Errorf("failed to cast output to string: %w", err)
} else {
return s, nil
}
}
// gotemplate
if template.Template != "" {
return runGoTemplate(ctx, template, environment)
}
// cel-go
if template.Expression != "" {
out, err := RunExpressionContext(ctx, environment, template)
if err != nil {
return "", err
}
if _, ok := out.(structpb.NullValue); ok || out == nil {
return "", nil
}
return fmt.Sprintf("%v", out), nil
}
return "", nil
}
func runGoTemplate(ctx commonsContext.Context, template Template, environment map[string]any) (string, error) {
// Parse the gotemplate header once up-front so we can detect whether the
// header set delimiters (which overrides DelimSets to a single pass).
origLeft, origRight := template.LeftDelim, template.RightDelim
parsed, err := parseAndStripTemplateHeader(template)
if err != nil {
return "", err
}
template = parsed
headerSetDelims := template.LeftDelim != origLeft || template.RightDelim != origRight
if template.ValueFunctions {
funcs := make(map[string]any, len(template.Functions)+len(environment))
for k, v := range template.Functions {
funcs[k] = v
}
for k, v := range environment {
_v := v
funcs[k] = func() any { return _v }
}
template.Functions = funcs
}
var delimSets []Delims
switch {
case headerSetDelims:
delimSets = []Delims{{Left: template.LeftDelim, Right: template.RightDelim}}
case len(template.DelimSets) > 0:
delimSets = template.DelimSets
case template.LeftDelim != "" && template.RightDelim != "":
delimSets = []Delims{{Left: template.LeftDelim, Right: template.RightDelim}}
default:
delimSets = []Delims{{Left: "{{", Right: "}}"}}
}
val := template.Template
for _, d := range delimSets {
pass := template
pass.Template = val
pass.LeftDelim = d.Left
pass.RightDelim = d.Right
pass.DelimSets = nil
out, err := goTemplate(ctx, pass, environment)
if err != nil {
return out, err
}
val = out
}
return val, nil
}
func goTemplate(ctx commonsContext.Context, template Template, environment map[string]any) (string, error) {
var tpl *gotemplate.Template
if template.IsCacheable() {
cached, ok := goTemplateCache.Get(template.cacheKey(nil))
if ok {
if cachedTpl, ok := cached.(*gotemplate.Template); ok {
if ctx.Logger != nil && properties.On(false, "gomplate.log") {
ctx.Logger.V(7).Infof("%s using cached template", template.ShortString())
}
tpl = cachedTpl
}
}
}
if tpl == nil {
template, err := parseAndStripTemplateHeader(template)
if err != nil {
return "", err
}
tpl = gotemplate.New("")
if template.LeftDelim != "" {
tpl = tpl.Delims(template.LeftDelim, template.RightDelim)
}
funcs := make(map[string]any)
for k, v := range funcMap {
funcs[k] = v
}
for k, v := range template.Functions {
funcs[k] = v
}
tpl, err = tpl.Funcs(funcs).Parse(template.Template)
if err != nil {
return "", oops.With("template", template.Template).Wrap(err)
}
if template.IsCacheable() {
goTemplateCache.Set(template.cacheKey(nil), tpl, template.CacheTime)
}
}
data, err := Serialize(environment)
if err != nil {
return "", oops.
// With("environment", environment)
Wrapf(err, "error serializing env")
}
var buf bytes.Buffer
if err := tpl.Execute(&buf, data); err != nil {
return "", oops.
With("template", template.Template).
// With("environment", environment).
Wrap(err)
}
out := strings.TrimSpace(buf.String())
if ctx.Logger != nil && out != template.Template && properties.On(false, "gomplate.log") {
ctx.Logger.V(4).Infof("templated %s ==> %s", template.ShortString(), out)
}
return out, nil
}
// LoadSharedLibrary loads a shared library for Otto
func LoadSharedLibrary(source string) error {
source = strings.TrimSpace(source)
data, err := os.ReadFile(source)
if err != nil {
return fmt.Errorf("failed to read shared library %s: %s", source, err)
}
registry.Register(func() string { return string(data) })
return nil
}
func parseAndStripTemplateHeader(template Template) (Template, error) {
header, content := extractHeaderAndContent(template.Template)
if header == "" {
return template, nil
}
template.Template = content
fields := strings.Fields(header)
for _, field := range fields {
split := strings.SplitN(field, "=", 2)
if len(split) != 2 {
return template, fmt.Errorf("invalid header: %s", field)
}
switch split[0] {
case "right-delim":
template.RightDelim = split[1]
case "left-delim":
template.LeftDelim = split[1]
}
}
return template, nil
}
const templateHeaderPrefix = "# gotemplate: "
func extractHeaderAndContent(template string) (string, string) {
scanner := bufio.NewScanner(strings.NewReader(template))
// Loop through headers.
// There could be multiple, we look for the gotemplate header.
for scanner.Scan() {
line := scanner.Text()
if line == "---" {
// Special case for yaml where the header might not start from the first line.
continue
}
// end of headers.
isHeader := strings.HasPrefix(line, "#")
if !isHeader {
break
}
if strings.HasPrefix(line, templateHeaderPrefix) {
header := strings.TrimPrefix(line, templateHeaderPrefix)
return header, strings.Replace(template, fmt.Sprintf("%s\n", line), "", 1)
}
}
return "", template
}