support nested expressions

This commit is contained in:
Casey Lee 2020-03-12 17:22:33 -07:00
parent 7f88f81bf6
commit c439a6ff14
No known key found for this signature in database
GPG key ID: 1899120ECD0A1784

View file

@ -65,22 +65,34 @@ func (ee *expressionEvaluator) Evaluate(in string) (string, error) {
if err != nil {
return "", err
}
if val.IsNull() || val.IsUndefined() {
return "", nil
}
return val.ToString()
}
func (ee *expressionEvaluator) Interpolate(in string) string {
errList := make([]error, 0)
out := pattern.ReplaceAllStringFunc(in, func(match string) string {
expression := strings.TrimPrefix(strings.TrimSuffix(match, suffix), prefix)
evaluated, err := ee.Evaluate(expression)
if err != nil {
errList = append(errList, err)
out := in
for {
out = pattern.ReplaceAllStringFunc(in, func(match string) string {
expression := strings.TrimPrefix(strings.TrimSuffix(match, suffix), prefix)
evaluated, err := ee.Evaluate(expression)
if err != nil {
errList = append(errList, err)
}
return evaluated
})
if len(errList) > 0 {
logrus.Errorf("Unable to interpolate string '%s' - %v", in, errList)
break
}
return evaluated
})
if len(errList) > 0 {
logrus.Errorf("Unable to interpolate string '%s' - %v", in, errList)
return in
if out == in {
// no replacement occurred, we're done!
break
}
in = out
}
return out
}