2021-10-07 01:40:49 +05:30
|
|
|
package expressions
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2021-10-07 05:35:32 +05:30
|
|
|
var unresolvedVariablesRegex = regexp.MustCompile(`(?:%7[B|b]|\{){2}([^}]+)(?:%7[D|d]|\}){2}["'\)\}]*`)
|
2021-10-07 01:40:49 +05:30
|
|
|
|
|
|
|
|
// ContainsUnresolvedVariables returns an error with variable names if the passed
|
|
|
|
|
// input contains unresolved {{<pattern-here>}} variables.
|
2021-11-18 14:52:11 +01:00
|
|
|
func ContainsUnresolvedVariables(items ...string) error {
|
|
|
|
|
for _, data := range items {
|
|
|
|
|
matches := unresolvedVariablesRegex.FindAllStringSubmatch(data, -1)
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
|
return nil
|
2021-10-07 01:40:49 +05:30
|
|
|
}
|
2021-11-18 14:52:11 +01:00
|
|
|
errorString := &strings.Builder{}
|
|
|
|
|
errorString.WriteString("unresolved variables found: ")
|
|
|
|
|
|
|
|
|
|
for i, match := range matches {
|
|
|
|
|
if len(match) < 2 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
errorString.WriteString(match[1])
|
|
|
|
|
if i != len(matches)-1 {
|
|
|
|
|
errorString.WriteString(",")
|
|
|
|
|
}
|
2021-10-07 01:40:49 +05:30
|
|
|
}
|
2021-11-18 14:52:11 +01:00
|
|
|
errorMessage := errorString.String()
|
|
|
|
|
return errors.New(errorMessage)
|
2021-10-07 01:40:49 +05:30
|
|
|
}
|
2021-10-07 12:36:27 +02:00
|
|
|
|
2021-11-18 14:52:11 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
2021-10-07 12:36:27 +02:00
|
|
|
|
2021-11-18 14:52:11 +01:00
|
|
|
func ContainsVariablesWithNames(names map[string]interface{}, items ...string) error {
|
|
|
|
|
for _, data := range items {
|
|
|
|
|
matches := unresolvedVariablesRegex.FindAllStringSubmatch(data, -1)
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
|
return nil
|
2021-10-07 12:36:27 +02:00
|
|
|
}
|
2021-11-18 14:52:11 +01:00
|
|
|
errorString := &strings.Builder{}
|
|
|
|
|
errorString.WriteString("unresolved variables with values found: ")
|
|
|
|
|
|
|
|
|
|
for i, match := range matches {
|
|
|
|
|
if len(match) < 2 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
matchName := match[1]
|
|
|
|
|
if _, ok := names[matchName]; !ok {
|
|
|
|
|
errorString.WriteString(matchName)
|
|
|
|
|
if i != len(matches)-1 {
|
|
|
|
|
errorString.WriteString(",")
|
|
|
|
|
}
|
2021-10-07 12:36:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-18 14:52:11 +01:00
|
|
|
errorMessage := errorString.String()
|
|
|
|
|
return errors.New(errorMessage)
|
2021-10-07 12:36:27 +02:00
|
|
|
}
|
2021-11-18 14:52:11 +01:00
|
|
|
|
|
|
|
|
return nil
|
2021-10-07 12:36:27 +02:00
|
|
|
}
|