2022-12-01 08:33:42 +00:00
|
|
|
package interpolation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-12 09:48:52 +00:00
|
|
|
"errors"
|
2022-12-01 08:33:42 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
2023-04-19 23:13:33 +00:00
|
|
|
"sort"
|
2022-12-01 08:33:42 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config/variable"
|
2023-04-19 23:13:33 +00:00
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
"golang.org/x/exp/slices"
|
2022-12-01 08:33:42 +00:00
|
|
|
)
|
|
|
|
|
2022-12-01 21:38:49 +00:00
|
|
|
const Delimiter = "."
|
|
|
|
|
2022-12-01 08:33:42 +00:00
|
|
|
var re = regexp.MustCompile(`\$\{(\w+(\.\w+)*)\}`)
|
|
|
|
|
|
|
|
type stringField struct {
|
2022-12-01 21:38:49 +00:00
|
|
|
path string
|
|
|
|
|
|
|
|
getter
|
|
|
|
setter
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 21:38:49 +00:00
|
|
|
func newStringField(path string, g getter, s setter) *stringField {
|
2022-12-01 08:33:42 +00:00
|
|
|
return &stringField{
|
2022-12-01 21:38:49 +00:00
|
|
|
path: path,
|
2022-12-01 08:33:42 +00:00
|
|
|
|
2022-12-01 21:38:49 +00:00
|
|
|
getter: g,
|
|
|
|
setter: s,
|
|
|
|
}
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringField) dependsOn() []string {
|
|
|
|
var out []string
|
2022-12-01 21:38:49 +00:00
|
|
|
m := re.FindAllStringSubmatch(s.Get(), -1)
|
2022-12-01 08:33:42 +00:00
|
|
|
for i := range m {
|
|
|
|
out = append(out, m[i][1])
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-12-12 09:48:52 +00:00
|
|
|
func (s *stringField) interpolate(fns []LookupFunction, lookup map[string]string) {
|
2022-12-01 21:38:49 +00:00
|
|
|
out := re.ReplaceAllStringFunc(s.Get(), func(s string) string {
|
2022-12-01 08:33:42 +00:00
|
|
|
// Turn the whole match into the submatch.
|
|
|
|
match := re.FindStringSubmatch(s)
|
2022-12-12 09:48:52 +00:00
|
|
|
for _, fn := range fns {
|
|
|
|
v, err := fn(match[1], lookup)
|
|
|
|
if errors.Is(err, ErrSkipInterpolation) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|
2022-12-01 21:38:49 +00:00
|
|
|
|
2022-12-12 09:48:52 +00:00
|
|
|
// No substitution.
|
|
|
|
return s
|
2022-12-01 08:33:42 +00:00
|
|
|
})
|
|
|
|
|
2022-12-01 21:38:49 +00:00
|
|
|
s.Set(out)
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type accumulator struct {
|
2023-04-19 23:13:33 +00:00
|
|
|
// all string fields in the bundle config
|
2022-12-01 08:33:42 +00:00
|
|
|
strings map[string]*stringField
|
2023-04-19 23:13:33 +00:00
|
|
|
|
|
|
|
// contains path -> resolved_string mapping for string fields in the config
|
|
|
|
// The resolved strings will NOT contain any variable references that could
|
|
|
|
// have been resolved, however there might still be references that cannot
|
|
|
|
// be resolved
|
|
|
|
memo map[string]string
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// jsonFieldName returns the name in a field's `json` tag.
|
|
|
|
// Returns the empty string if it isn't set.
|
|
|
|
func jsonFieldName(sf reflect.StructField) string {
|
|
|
|
tag, ok := sf.Tag.Lookup("json")
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
parts := strings.Split(tag, ",")
|
|
|
|
if parts[0] == "-" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return parts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *accumulator) walkStruct(scope []string, rv reflect.Value) {
|
|
|
|
num := rv.NumField()
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
sf := rv.Type().Field(i)
|
|
|
|
f := rv.Field(i)
|
|
|
|
|
|
|
|
// Walk field with the same scope for anonymous (embedded) fields.
|
|
|
|
if sf.Anonymous {
|
|
|
|
a.walk(scope, f, anySetter{f})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip unnamed fields.
|
|
|
|
fieldName := jsonFieldName(rv.Type().Field(i))
|
|
|
|
if fieldName == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
a.walk(append(scope, fieldName), f, anySetter{f})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *accumulator) walk(scope []string, rv reflect.Value, s setter) {
|
|
|
|
// Dereference pointer.
|
|
|
|
if rv.Type().Kind() == reflect.Pointer {
|
|
|
|
// Skip nil pointers.
|
|
|
|
if rv.IsNil() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rv = rv.Elem()
|
|
|
|
s = anySetter{rv}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rv.Type().Kind() {
|
|
|
|
case reflect.String:
|
2022-12-01 21:38:49 +00:00
|
|
|
path := strings.Join(scope, Delimiter)
|
|
|
|
a.strings[path] = newStringField(path, anyGetter{rv}, s)
|
2023-05-15 09:34:05 +00:00
|
|
|
|
|
|
|
// register alias for variable value. `var.foo` would be the alias for
|
|
|
|
// `variables.foo.value`
|
|
|
|
if len(scope) == 3 && scope[0] == "variables" && scope[2] == "value" {
|
|
|
|
aliasPath := strings.Join([]string{variable.VariableReferencePrefix, scope[1]}, Delimiter)
|
|
|
|
a.strings[aliasPath] = a.strings[path]
|
|
|
|
}
|
2022-12-01 08:33:42 +00:00
|
|
|
case reflect.Struct:
|
|
|
|
a.walkStruct(scope, rv)
|
|
|
|
case reflect.Map:
|
|
|
|
if rv.Type().Key().Kind() != reflect.String {
|
|
|
|
panic("only support string keys in map")
|
|
|
|
}
|
|
|
|
keys := rv.MapKeys()
|
|
|
|
for _, key := range keys {
|
|
|
|
a.walk(append(scope, key.String()), rv.MapIndex(key), mapSetter{rv, key})
|
|
|
|
}
|
|
|
|
case reflect.Slice:
|
|
|
|
n := rv.Len()
|
|
|
|
name := scope[len(scope)-1]
|
|
|
|
base := scope[:len(scope)-1]
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
element := rv.Index(i)
|
|
|
|
a.walk(append(base, fmt.Sprintf("%s[%d]", name, i)), element, anySetter{element})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:13:33 +00:00
|
|
|
// walk and gather all string fields in the config
|
2022-12-01 21:38:49 +00:00
|
|
|
func (a *accumulator) start(v any) {
|
2022-12-01 08:33:42 +00:00
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
if rv.Type().Kind() != reflect.Pointer {
|
|
|
|
panic("expect pointer")
|
|
|
|
}
|
|
|
|
rv = rv.Elem()
|
|
|
|
if rv.Type().Kind() != reflect.Struct {
|
|
|
|
panic("expect struct")
|
|
|
|
}
|
|
|
|
|
2022-12-01 21:38:49 +00:00
|
|
|
a.strings = make(map[string]*stringField)
|
2023-04-19 23:13:33 +00:00
|
|
|
a.memo = make(map[string]string)
|
2022-12-01 21:38:49 +00:00
|
|
|
a.walk([]string{}, rv, nilSetter{})
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:13:33 +00:00
|
|
|
// recursively interpolate variables in a depth first manner
|
|
|
|
func (a *accumulator) Resolve(path string, seenPaths []string, fns ...LookupFunction) error {
|
|
|
|
// return early if the path is already resolved
|
|
|
|
if _, ok := a.memo[path]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the string node to resolve
|
|
|
|
field, ok := a.strings[path]
|
|
|
|
if !ok {
|
2023-05-15 09:34:05 +00:00
|
|
|
return fmt.Errorf("could not resolve reference %s", path)
|
2023-04-19 23:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return early if the string field has no variables to interpolate
|
|
|
|
if len(field.dependsOn()) == 0 {
|
|
|
|
a.memo[path] = field.Get()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve all variables refered in the root string field
|
|
|
|
for _, childFieldPath := range field.dependsOn() {
|
|
|
|
// error if there is a loop in variable interpolation
|
|
|
|
if slices.Contains(seenPaths, childFieldPath) {
|
|
|
|
return fmt.Errorf("cycle detected in field resolution: %s", strings.Join(append(seenPaths, childFieldPath), " -> "))
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 23:13:33 +00:00
|
|
|
// recursive resolve variables in the child fields
|
|
|
|
err := a.Resolve(childFieldPath, append(seenPaths, childFieldPath), fns...)
|
2022-12-01 08:33:42 +00:00
|
|
|
if err != nil {
|
2023-04-19 23:13:33 +00:00
|
|
|
return err
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:13:33 +00:00
|
|
|
// interpolate root string once all variable references in it have been resolved
|
|
|
|
field.interpolate(fns, a.memo)
|
|
|
|
|
|
|
|
// record interpolated string in memo
|
|
|
|
a.memo[path] = field.Get()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate all string fields in the config
|
|
|
|
func (a *accumulator) expand(fns ...LookupFunction) error {
|
|
|
|
// sorting paths for stable order of iteration
|
|
|
|
paths := maps.Keys(a.strings)
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
|
|
|
// iterate over paths for all strings fields in the config
|
|
|
|
for _, path := range paths {
|
|
|
|
err := a.Resolve(path, []string{path}, fns...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-12-01 08:33:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-01 21:38:49 +00:00
|
|
|
type interpolate struct {
|
2022-12-12 09:48:52 +00:00
|
|
|
fns []LookupFunction
|
2022-12-01 21:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *interpolate) expand(v any) error {
|
|
|
|
a := accumulator{}
|
|
|
|
a.start(v)
|
2022-12-12 09:48:52 +00:00
|
|
|
return a.expand(m.fns...)
|
2022-12-01 21:38:49 +00:00
|
|
|
}
|
2022-12-01 08:33:42 +00:00
|
|
|
|
2022-12-12 09:48:52 +00:00
|
|
|
func Interpolate(fns ...LookupFunction) bundle.Mutator {
|
|
|
|
return &interpolate{fns: fns}
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *interpolate) Name() string {
|
|
|
|
return "Interpolate"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *interpolate) Apply(_ context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) {
|
2022-12-01 21:38:49 +00:00
|
|
|
return nil, m.expand(&b.Config)
|
2022-12-01 08:33:42 +00:00
|
|
|
}
|