github.com/databricks/cli@v0.203.0/bundle/config/interpolation/lookup.go (about)

     1  package interpolation
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"golang.org/x/exp/slices"
     9  )
    10  
    11  // LookupFunction returns the value to rewrite a path expression to.
    12  type LookupFunction func(path string, depends map[string]string) (string, error)
    13  
    14  // ErrSkipInterpolation can be used to fall through from [LookupFunction].
    15  var ErrSkipInterpolation = errors.New("skip interpolation")
    16  
    17  // DefaultLookup looks up the specified path in the map.
    18  // It returns an error if it doesn't exist.
    19  func DefaultLookup(path string, lookup map[string]string) (string, error) {
    20  	v, ok := lookup[path]
    21  	if !ok {
    22  		return "", fmt.Errorf("expected to find value for path: %s", path)
    23  	}
    24  	return v, nil
    25  }
    26  
    27  func pathPrefixMatches(prefix []string, path string) bool {
    28  	parts := strings.Split(path, Delimiter)
    29  	return len(parts) >= len(prefix) && slices.Compare(prefix, parts[0:len(prefix)]) == 0
    30  }
    31  
    32  // ExcludeLookupsInPath is a lookup function that skips lookups for the specified path.
    33  func ExcludeLookupsInPath(exclude ...string) LookupFunction {
    34  	return func(path string, lookup map[string]string) (string, error) {
    35  		if pathPrefixMatches(exclude, path) {
    36  			return "", ErrSkipInterpolation
    37  		}
    38  
    39  		return DefaultLookup(path, lookup)
    40  	}
    41  }
    42  
    43  // IncludeLookupsInPath is a lookup function that limits lookups to the specified path.
    44  func IncludeLookupsInPath(include ...string) LookupFunction {
    45  	return func(path string, lookup map[string]string) (string, error) {
    46  		if !pathPrefixMatches(include, path) {
    47  			return "", ErrSkipInterpolation
    48  		}
    49  
    50  		return DefaultLookup(path, lookup)
    51  	}
    52  }