-
Notifications
You must be signed in to change notification settings - Fork 65
/
interpolator.go
110 lines (91 loc) · 2.99 KB
/
interpolator.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
// Copyright 2021 VMware
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package templates
import (
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
"github.com/valyala/fasttemplate"
)
type TemplateExecutor func(template, startTag, endTag string, f fasttemplate.TagFunc) (string, error)
//counterfeiter:generate . evaluator
type evaluator interface {
EvaluateJsonPath(path string, obj interface{}) (interface{}, error)
}
//counterfeiter:generate . tagInterpolator
type tagInterpolator interface {
InterpolateTag(w io.Writer, tag string) (int, error)
Evaluate(tag string) (interface{}, error)
}
func isSingleTag(template string) bool {
return strings.HasPrefix(template, `$(`) &&
strings.HasSuffix(template, `)$`) &&
strings.Count(template, `$(`) == 1
}
// InterpolateLeafNode merges the context variables anywhere a $(<<jsonPath>>)$ tag is found
// It validates that the jsonPath refers to objects within the context
func InterpolateLeafNode(executor TemplateExecutor, template []byte, tagInterpolator tagInterpolator) (interface{}, error) {
input := string(template)
if isSingleTag(input) {
jsonPathExpr := strings.TrimPrefix(strings.TrimSuffix(input, `)$`), `$(`)
result, err := tagInterpolator.Evaluate(jsonPathExpr)
if err != nil {
return nil, fmt.Errorf("evaluate tag %s: %w", input, err)
}
return result, nil
}
stringResult, err := executor(input, `$(`, `)$`, tagInterpolator.InterpolateTag)
if err != nil {
return nil, fmt.Errorf("interpolate tag: %w", err)
}
return stringResult, nil
}
type StandardTagInterpolator struct {
Context JsonPathContext
Evaluator evaluator
}
//counterfeiter:generate io.Writer
func (t StandardTagInterpolator) Evaluate(tag string) (interface{}, error) {
return t.Evaluator.EvaluateJsonPath(tag, t.Context)
}
func (t StandardTagInterpolator) InterpolateTag(w io.Writer, tag string) (int, error) {
var (
val interface{}
err error
writeLen int
jsonValue []byte
)
val, err = t.Evaluator.EvaluateJsonPath(tag, t.Context)
if err != nil {
return 0, fmt.Errorf("evaluate jsonpath: %w", err)
}
if val == nil {
return 0, fmt.Errorf("tag must not point to nil value: %s", tag)
}
if reflect.TypeOf(val).Kind() == reflect.String {
writeLen, err = w.Write([]byte(fmt.Sprintf("%s", val)))
} else {
jsonValue, err = json.Marshal(val)
if err != nil {
return 0, fmt.Errorf("json marshal: %w", err)
}
writeLen, err = w.Write(jsonValue)
}
if err != nil {
return 0, fmt.Errorf("writer write: %w", err)
}
return writeLen, nil
}