From 0b42dc374ad7c3f020f01e3013c89a40d87b5789 Mon Sep 17 00:00:00 2001 From: William Dumont Date: Fri, 31 Jan 2025 11:12:54 +0100 Subject: [PATCH] allow non alphanum strings --- .../controller/node_config_foreach.go | 17 +++++++++++++- .../controller/node_config_foreach_test.go | 13 +++++++++++ .../testdata/foreach_stringer/foreach_6.txtar | 22 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 internal/runtime/testdata/foreach_stringer/foreach_6.txtar diff --git a/internal/runtime/internal/controller/node_config_foreach.go b/internal/runtime/internal/controller/node_config_foreach.go index 1a7f67abf5..b66aaf6bbe 100644 --- a/internal/runtime/internal/controller/node_config_foreach.go +++ b/internal/runtime/internal/controller/node_config_foreach.go @@ -10,6 +10,7 @@ import ( "strings" "sync" "time" + "unicode" "github.com/go-kit/log" "github.com/grafana/alloy/internal/component" @@ -383,7 +384,9 @@ func computeHash(s string) string { func hashObject(obj any) string { //TODO: Test what happens if there is a "true" string and a true bool in the collection. switch v := obj.(type) { - case int, string, bool: + case string: + return replaceNonAlphaNumeric(v) + case int, bool: return fmt.Sprintf("%v", v) case float64: // Dots are not valid characters in Alloy syntax identifiers. @@ -394,6 +397,18 @@ func hashObject(obj any) string { } } +func replaceNonAlphaNumeric(s string) string { + var builder strings.Builder + for _, r := range s { + if unicode.IsLetter(r) || unicode.IsDigit(r) { + builder.WriteRune(r) + } else { + builder.WriteRune('_') + } + } + return builder.String() +} + type NoopRegistry struct{} var _ prometheus.Registerer = NoopRegistry{} diff --git a/internal/runtime/internal/controller/node_config_foreach_test.go b/internal/runtime/internal/controller/node_config_foreach_test.go index 1e7fe8b927..85f7e2e79d 100644 --- a/internal/runtime/internal/controller/node_config_foreach_test.go +++ b/internal/runtime/internal/controller/node_config_foreach_test.go @@ -226,6 +226,19 @@ func TestCreateCustomComponentsCollectionObjectsWithUpdate(t *testing.T) { require.ElementsMatch(t, keys, []string{"foreach_be19d02a2ccb2cbc2c47e90dcad8446a50459577449624176398d1f2aa6cd23a_1", "foreach_1464766cf9c8fd1095d0f7a22abe0632b6a6d44c3eeae65766086350eef3ac33_1"}) } +func TestNonAlphaNumericString(t *testing.T) { + config := `foreach "default" { + collection = ["123./st%4$"] + var = "num" + template { + } + }` + foreachConfigNode := NewForeachConfigNode(getBlockFromConfig(t, config), getComponentGlobals(t), nil) + require.NoError(t, foreachConfigNode.Evaluate(vm.NewScope(make(map[string]interface{})))) + customComponentIds := foreachConfigNode.moduleController.(*ModuleControllerMock).CustomComponents + require.ElementsMatch(t, customComponentIds, []string{"foreach_123__st_4__1"}) +} + func getBlockFromConfig(t *testing.T, config string) *ast.BlockStmt { file, err := parser.ParseFile("", []byte(config)) require.NoError(t, err) diff --git a/internal/runtime/testdata/foreach_stringer/foreach_6.txtar b/internal/runtime/testdata/foreach_stringer/foreach_6.txtar new file mode 100644 index 0000000000..5ce546b9ad --- /dev/null +++ b/internal/runtime/testdata/foreach_stringer/foreach_6.txtar @@ -0,0 +1,22 @@ +A collection containing a string with a dot. + +-- main.alloy -- +foreach "testForeach" { + collection = ["aaa.bbb"] + var = "item" + + template { + testcomponents.stringer "st" { + input_string = item + forward_to = [testcomponents.string_receiver.log.receiver] + } + } +} + +// Receive strings and append them to a log, +// separated by a new line. +testcomponents.string_receiver "log" { +} + +-- expected_debug_info.txt -- +"aaa.bbb" \ No newline at end of file