From caa4029fc6665d7e90ac2723ab22c8f0b9a24fd4 Mon Sep 17 00:00:00 2001 From: AlObolenski <83572914+AlObolenski@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:11:39 +0200 Subject: [PATCH] fallback to raw values in object attributes --- hcledit_test.go | 14 ++++++++++++++ internal/handler/read.go | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/hcledit_test.go b/hcledit_test.go index 4b27ba1..8b77338 100644 --- a/hcledit_test.go +++ b/hcledit_test.go @@ -461,6 +461,20 @@ attribute = local.var }, }, + "fallback to absolute variable name in object": { + input: ` +object = { + attribute = local.var +} +`, + options: []hcledit.Option{hcledit.WithReadFallbackToRawString()}, + expectErr: true, + query: "object.attribute", + want: map[string]interface{}{ + "object.attribute": "local.var", + }, + }, + "fallback to uninterpolated string": { input: ` attribute = "some-${local.var}" diff --git a/internal/handler/read.go b/internal/handler/read.go index f9761b9..ee759be 100644 --- a/internal/handler/read.go +++ b/internal/handler/read.go @@ -36,8 +36,9 @@ func (h *readHandler) HandleBody(body *hclwrite.Body, name string, keyTrail []st func (h *readHandler) HandleObject(object *ast.Object, name string, keyTrail []string) error { buf := object.GetObjectAttribute(name).BuildTokens().Bytes() - value, err := parse(buf, name, h.fallbackToRawString) - if err != nil { + fallback := h.fallbackToRawString + value, err := parse(buf, name, fallback) + if err != nil && !fallback { return err } h.results[strings.Join(keyTrail, ".")] = value