diff --git a/bundle/config/interpolation/lookup.go b/bundle/config/interpolation/lookup.go index 733e6790b..93535cdd5 100644 --- a/bundle/config/interpolation/lookup.go +++ b/bundle/config/interpolation/lookup.go @@ -20,16 +20,29 @@ func DefaultLookup(path string, lookup map[string]string) (string, error) { return v, nil } +func pathPrefixMatches(prefix []string, path string) bool { + parts := strings.Split(path, Delimiter) + return len(parts) >= len(prefix) && slices.Compare(prefix, parts[0:len(prefix)]) == 0 +} + // ExcludeLookupsInPath is a lookup function that skips lookups for the specified path. func ExcludeLookupsInPath(exclude ...string) LookupFunction { return func(path string, lookup map[string]string) (string, error) { - parts := strings.Split(path, Delimiter) - - // Skip interpolation of this path. - if len(parts) >= len(exclude) && slices.Compare(exclude, parts[0:len(exclude)]) == 0 { + if pathPrefixMatches(exclude, path) { return fmt.Sprintf("${%s}", path), nil } return DefaultLookup(path, lookup) } } + +// IncludeLookupsInPath is a lookup function that limits lookups to the specified path. +func IncludeLookupsInPath(include ...string) LookupFunction { + return func(path string, lookup map[string]string) (string, error) { + if pathPrefixMatches(include, path) { + return DefaultLookup(path, lookup) + } + + return fmt.Sprintf("${%s}", path), nil + } +} diff --git a/bundle/config/interpolation/lookup_test.go b/bundle/config/interpolation/lookup_test.go index c1521de65..594c5af83 100644 --- a/bundle/config/interpolation/lookup_test.go +++ b/bundle/config/interpolation/lookup_test.go @@ -7,12 +7,14 @@ import ( "github.com/stretchr/testify/require" ) -func TestExcludePath(t *testing.T) { - tmp := struct { - A map[string]string `json:"a"` - B map[string]string `json:"b"` - C map[string]string `json:"c"` - }{ +type interpolationFixture struct { + A map[string]string `json:"a"` + B map[string]string `json:"b"` + C map[string]string `json:"c"` +} + +func fixture() interpolationFixture { + return interpolationFixture{ A: map[string]string{ "x": "1", }, @@ -24,7 +26,10 @@ func TestExcludePath(t *testing.T) { "bx": "${b.x}", }, } +} +func TestExcludePath(t *testing.T) { + tmp := fixture() m := interpolate{ fn: ExcludeLookupsInPath("a"), } @@ -37,3 +42,18 @@ func TestExcludePath(t *testing.T) { assert.Equal(t, "${a.x}", tmp.C["ax"]) assert.Equal(t, "2", tmp.C["bx"]) } + +func TestIncludePath(t *testing.T) { + tmp := fixture() + m := interpolate{ + fn: IncludeLookupsInPath("a"), + } + + err := m.expand(&tmp) + require.NoError(t, err) + + assert.Equal(t, "1", tmp.A["x"]) + assert.Equal(t, "2", tmp.B["x"]) + assert.Equal(t, "1", tmp.C["ax"]) + assert.Equal(t, "${b.x}", tmp.C["bx"]) +}