From b8f37f4c3f8fcb2528b55143f49cb792e2de04ae Mon Sep 17 00:00:00 2001 From: Ivan Orlov Date: Mon, 16 Dec 2024 19:45:27 +0000 Subject: [PATCH] Document unsupported "bracket-less" collection addressing --- examples/README.md | 4 ++++ pkg/config/expression.go | 8 ++++++++ pkg/config/expression_test.go | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/examples/README.md b/examples/README.md index 30883ce0f9..64364e3fd0 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1884,6 +1884,10 @@ To learn more about how to refer to a module in a blueprint file, please consult Variables can be used to refer both to values defined elsewhere in the blueprint and to the output and structure of other modules. +> [!NOTE] +> "Brackets-less" access to elements of collection is not supported, use brackets. +> E.g. `pink.lime[0].salmon` instead of `pink.lime.0.salmon`. + ### Blueprint expressions Expressions in a blueprint file can refer to deployment variables or the outputs diff --git a/pkg/config/expression.go b/pkg/config/expression.go index 3cfeb096d1..0fb75f71fd 100644 --- a/pkg/config/expression.go +++ b/pkg/config/expression.go @@ -88,6 +88,14 @@ func bpTraversalToTerraform(t hcl.Traversal) (hcl.Traversal, error) { // BlueprintExpressionLiteralToExpression takes a content of `$(...)`-literal and transforms it to `Expression` func BlueprintExpressionLiteralToExpression(s string) (Expression, error) { + // TODO: FIX: this function relies on assumption that + // `epxrToTokens(toExpression(tokenize(X))) == tokenize(X)` + // This is not correct, e.g.: + // ``` + // epxrToTokens(toExpression(tokenize("pink.lime.0.salmon"))) == + // tokenize("pink.lime[0].salmon") != tokenize("pink.lime.0.salmon") + // ``` + // As a result `pink.lime.0.salmon` can not be properly translated. bpExp, diag := hclsyntax.ParseExpression([]byte(s), "", hcl.Pos{}) if diag.HasErrors() { return nil, diag diff --git a/pkg/config/expression_test.go b/pkg/config/expression_test.go index 88a8fa8338..e7a135846e 100644 --- a/pkg/config/expression_test.go +++ b/pkg/config/expression_test.go @@ -85,6 +85,7 @@ func TestParseBpLit(t *testing.T) { {"$(vars.green.sleeve)", "var.green.sleeve", false}, {`$(vars.green["sleeve"])`, `var.green["sleeve"]`, false}, {"$(vars.green.sleeve[3])", "var.green.sleeve[3]", false}, + {"$(vars.green[3].sleeve)", "var.green[3].sleeve", false}, {"$(var.green)", "module.var.green", false}, {"$(box.green)", "module.box.green", false}, @@ -135,6 +136,9 @@ echo "Hello $(vars.project_id)" {"$(vars[3]])", "", true}, // can't index vars {`$(vars["green"])`, "", true}, // can't index module + // TODO: uncomment + // see comment to `BlueprintExpressionLiteralToExpression` + // {"$(pink.lime.0.salmon)", "module.pink.lime[0].salmon", false}, } for _, tc := range tests { t.Run(tc.input, func(t *testing.T) {