Skip to content

Commit

Permalink
use . as join element instead of _
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Nov 28, 2024
1 parent f831649 commit 91cdf05
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
18 changes: 8 additions & 10 deletions tooling/templatize/pkg/ev2/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ package ev2

import (
"fmt"
"reflect"
"strings"

"github.com/Azure/ARO-HCP/tooling/templatize/pkg/config"
)

type PlaceholderGenerator func(key []string, value interface{}) (flattenedKey string, replaceVar string)
type PlaceholderGenerator func(key []string, valueType reflect.Type) (flattenedKey string, replaceVar string)

// NewDunderPlaceholders returns a PlaceholderGenerator function that generates
// placeholder strings by joining the provided key elements with underscores
// and surrounding them with double underscores.
//
// The returned function takes a key (a slice of strings) and an unused interface{} value,
// and returns a flattenedKey and replaceVar, both of which are the generated placeholder string.
//
// Example:
//
// key := []string{"foo", "bar"}
// flattenedKey, replaceVar := NewDunderPlaceholders()(key, nil)
// // flattenedKey and replaceVar will both be "__foo_bar__"
func NewDunderPlaceholders() PlaceholderGenerator {
return func(key []string, _ interface{}) (flattenedKey string, replaceVar string) {
flattenedKey = fmt.Sprintf("__%s__", strings.Join(key, "_"))
return func(key []string, _ reflect.Type) (flattenedKey string, replaceVar string) {
flattenedKey = fmt.Sprintf("__%s__", strings.Join(key, "."))
replaceVar = flattenedKey
return
}
Expand All @@ -40,9 +38,9 @@ func NewDunderPlaceholders() PlaceholderGenerator {
// a flattened key and a replacement variable for bicep parameter usage within EV2.
func NewBicepParamPlaceholders() PlaceholderGenerator {
dunder := NewDunderPlaceholders()
return func(key []string, value interface{}) (flattenedKey string, replaceVar string) {
flattenedKey, replaceVar = dunder(key, value)
if _, ok := value.(string); !ok {
return func(key []string, valueType reflect.Type) (flattenedKey string, replaceVar string) {
flattenedKey, replaceVar = dunder(key, valueType)
if valueType.Kind() != reflect.String {
replaceVar = fmt.Sprintf("any('%s')", replaceVar)
}
return
Expand All @@ -63,7 +61,7 @@ func EV2Mapping(input config.Variables, placeholderGenerator PlaceholderGenerato
}
replaced[key] = replacement
} else {
flattenedKey, replaceVar := placeholderGenerator(nestedKey, value)
flattenedKey, replaceVar := placeholderGenerator(nestedKey, reflect.TypeOf(value))
output[flattenedKey] = strings.Join(nestedKey, ".")
replaced[key] = replaceVar
}
Expand Down
81 changes: 69 additions & 12 deletions tooling/templatize/pkg/ev2/mapping_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ev2

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -34,18 +35,18 @@ func TestMapping(t *testing.T) {
"__key1__": "key1",
"__key2__": "key2",
"__key3__": "key3",
"__parent_nested__": "parent.nested",
"__parent_nestedInt__": "parent.nestedInt",
"__parent_deeper_deepest__": "parent.deeper.deepest",
"__parent.nested__": "parent.nested",
"__parent.nestedInt__": "parent.nestedInt",
"__parent.deeper.deepest__": "parent.deeper.deepest",
},
expectedReplace: map[string]interface{}{
"key1": "__key1__",
"key2": "__key2__",
"key3": "__key3__",
"parent": map[string]interface{}{
"nested": "__parent_nested__",
"nestedInt": "__parent_nestedInt__",
"deeper": map[string]interface{}{"deepest": "__parent_deeper_deepest__"},
"nested": "__parent.nested__",
"nestedInt": "__parent.nestedInt__",
"deeper": map[string]interface{}{"deepest": "__parent.deeper.deepest__"},
},
},
},
Expand All @@ -56,18 +57,18 @@ func TestMapping(t *testing.T) {
"__key1__": "key1",
"__key2__": "key2",
"__key3__": "key3",
"__parent_nested__": "parent.nested",
"__parent_nestedInt__": "parent.nestedInt",
"__parent_deeper_deepest__": "parent.deeper.deepest",
"__parent.nested__": "parent.nested",
"__parent.nestedInt__": "parent.nestedInt",
"__parent.deeper.deepest__": "parent.deeper.deepest",
},
expectedReplace: map[string]interface{}{
"key1": "__key1__",
"key2": "any('__key2__')",
"key3": "any('__key3__')",
"parent": map[string]interface{}{
"nested": "__parent_nested__",
"nestedInt": "any('__parent_nestedInt__')",
"deeper": map[string]interface{}{"deepest": "__parent_deeper_deepest__"},
"nested": "__parent.nested__",
"nestedInt": "any('__parent.nestedInt__')",
"deeper": map[string]interface{}{"deepest": "__parent.deeper.deepest__"},
},
},
},
Expand All @@ -85,3 +86,59 @@ func TestMapping(t *testing.T) {
})
}
}

func TestPlaceholderGenerators(t *testing.T) {
tests := []struct {
name string
generator PlaceholderGenerator
key []string
valueType reflect.Type
expectedFlattened string
expectedReplace string
}{
{
name: "dunder",
generator: NewDunderPlaceholders(),
key: []string{"foo", "bar"},
valueType: nil,
expectedFlattened: "__foo.bar__",
expectedReplace: "__foo.bar__",
},
{
name: "bicep string param",
generator: NewBicepParamPlaceholders(),
key: []string{"foo", "bar"},
valueType: reflect.TypeOf("baz"),
expectedFlattened: "__foo.bar__",
expectedReplace: "__foo.bar__",
},
{
name: "bicep int param",
generator: NewBicepParamPlaceholders(),
key: []string{"foo", "bar"},
valueType: reflect.TypeOf(42),
expectedFlattened: "__foo.bar__",
expectedReplace: "any('__foo.bar__')",
},
{
name: "bicep bool param",
generator: NewBicepParamPlaceholders(),
key: []string{"foo", "bar"},
valueType: reflect.TypeOf(true),
expectedFlattened: "__foo.bar__",
expectedReplace: "any('__foo.bar__')",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
flattened, replace := tc.generator(tc.key, tc.valueType)
if flattened != tc.expectedFlattened {
t.Errorf("got incorrect flattened: %v", flattened)
}
if replace != tc.expectedReplace {
t.Errorf("got incorrect replace: %v", replace)
}
})
}
}
4 changes: 2 additions & 2 deletions tooling/templatize/pkg/ev2/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestScopeBindingVariables(t *testing.T) {
"__regionRG__": "$config(regionRG)",
"__serviceClusterRG__": "$config(serviceClusterRG)",
"__serviceClusterSubscription__": "$config(serviceClusterSubscription)",
"__clusterService_imageTag__": "$config(clusterService.imageTag)",
"__clusterService_replicas__": "$config(clusterService.replicas)",
"__clusterService.imageTag__": "$config(clusterService.imageTag)",
"__clusterService.replicas__": "$config(clusterService.replicas)",
}

if diff := cmp.Diff(expectedVars, vars); diff != "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
param regionRG = '__regionRG__'
param replicas = any('__clusterService_replicas__')
param replicas = any('__clusterService.replicas__')

0 comments on commit 91cdf05

Please sign in to comment.