From 6ec57d47fbbb5c4edd4f99eb9be7fae3be68ca22 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 11 Oct 2023 17:13:00 +0100 Subject: [PATCH] decoder: allow objects in for_each (#333) --- decoder/body_extensions_test.go | 16 +++++++++++++++- decoder/hover_test.go | 2 +- .../schemahelper/attribute_extensions.go | 6 ++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/decoder/body_extensions_test.go b/decoder/body_extensions_test.go index 74085096..a23bcfa3 100644 --- a/decoder/body_extensions_test.go +++ b/decoder/body_extensions_test.go @@ -667,7 +667,7 @@ func TestCompletionAtPos_BodySchema_Extensions_ForEach(t *testing.T) { Value: "A meta-argument that accepts a map or a set of strings, and creates an instance for each item in that map or set.\n\n**Note**: A given block cannot use both `count` and `for_each`.", Kind: lang.MarkdownKind, }, - Detail: "optional, map of any single type or set of string", + Detail: "optional, map of any single type or set of string or object", TextEdit: lang.TextEdit{ Range: hcl.Range{ Filename: "test.tf", @@ -1102,6 +1102,20 @@ for_each = }, Kind: lang.SetCandidateKind, }, + { + Label: "{ }", + Detail: "object", + TextEdit: lang.TextEdit{ + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 2, Column: 12, Byte: 43}, + End: hcl.Pos{Line: 2, Column: 12, Byte: 43}, + }, + NewText: "{\n}", + Snippet: "{\n}", + }, + Kind: lang.ObjectCandidateKind, + }, }), }, } diff --git a/decoder/hover_test.go b/decoder/hover_test.go index 88f6a3cc..351beb8d 100644 --- a/decoder/hover_test.go +++ b/decoder/hover_test.go @@ -1147,7 +1147,7 @@ func TestDecoder_HoverAtPos_extension_for_each(t *testing.T) { hcl.Pos{Line: 2, Column: 5, Byte: 26}, &lang.HoverData{ Content: lang.MarkupContent{ - Value: "**for_each** _optional, map of any single type or set of string_\n\n" + + Value: "**for_each** _optional, map of any single type or set of string or object_\n\n" + "A meta-argument that accepts a map or a set of strings, and creates an instance for each item in that map or set.\n\n" + "**Note**: A given block cannot use both `count` and `for_each`.", Kind: lang.MarkdownKind, diff --git a/decoder/internal/schemahelper/attribute_extensions.go b/decoder/internal/schemahelper/attribute_extensions.go index a1f6c125..2d01e95a 100644 --- a/decoder/internal/schemahelper/attribute_extensions.go +++ b/decoder/internal/schemahelper/attribute_extensions.go @@ -24,6 +24,12 @@ func ForEachAttributeSchema() *schema.AttributeSchema { Constraint: schema.OneOf{ schema.AnyExpression{OfType: cty.Map(cty.DynamicPseudoType)}, schema.AnyExpression{OfType: cty.Set(cty.String)}, + // Objects are still supported for backwards-compatible reasons + // but in general should be avoided here. We add it here because + // otherwise valid configuration would be flagged as invalid. + // We cannot suppress it in completion (yet) or otherwise inform + // the user of this anti-pattern though. + schema.AnyExpression{OfType: cty.EmptyObject}, }, Description: lang.Markdown("A meta-argument that accepts a map or a set of strings, and creates an instance for each item in that map or set.\n\n" + "**Note**: A given block cannot use both `count` and `for_each`."),