Skip to content

Commit

Permalink
allow non alphanum strings
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum committed Jan 31, 2025
1 parent 86435e4 commit 0b42dc3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
17 changes: 16 additions & 1 deletion internal/runtime/internal/controller/node_config_foreach.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"sync"
"time"
"unicode"

"github.com/go-kit/log"
"github.com/grafana/alloy/internal/component"
Expand Down Expand Up @@ -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.
Expand All @@ -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{}
Expand Down
13 changes: 13 additions & 0 deletions internal/runtime/internal/controller/node_config_foreach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions internal/runtime/testdata/foreach_stringer/foreach_6.txtar
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 0b42dc3

Please sign in to comment.