From f76498f2cfe0236529faea6ee99adcfd0e285537 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Thu, 23 May 2024 16:38:42 -0400 Subject: [PATCH 1/2] hclwrite: Add (*Attribute).SetName() method Reference: https://github.com/hashicorp/hcl/pull/340 Reference: https://github.com/hashicorp/hcl/issues/680 Similar to `(*hclwrite.Block).SetType()`, this new method enables in-place renaming of an attribute. --- hclwrite/ast_attribute.go | 7 +++ hclwrite/ast_attribute_test.go | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 hclwrite/ast_attribute_test.go diff --git a/hclwrite/ast_attribute.go b/hclwrite/ast_attribute.go index 3edc68c0..70ca8275 100644 --- a/hclwrite/ast_attribute.go +++ b/hclwrite/ast_attribute.go @@ -49,3 +49,10 @@ func (a *Attribute) init(name string, expr *Expression) { func (a *Attribute) Expr() *Expression { return a.expr.content.(*Expression) } + +// SetName updates the name of the attribute to a given name. +func (a *Attribute) SetName(name string) { + nameTok := newIdentToken(name) + nameObj := newIdentifier(nameTok) + a.name.ReplaceWith(nameObj) +} diff --git a/hclwrite/ast_attribute_test.go b/hclwrite/ast_attribute_test.go new file mode 100644 index 00000000..35631748 --- /dev/null +++ b/hclwrite/ast_attribute_test.go @@ -0,0 +1,78 @@ +package hclwrite + +import ( + "fmt" + "reflect" + "testing" + + "github.com/davecgh/go-spew/spew" + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" +) + +func TestAttributeSetName(t *testing.T) { + t.Parallel() + + tests := []struct { + src string + oldName string + newName string + want Tokens + }{ + { + "old = 123", + "old", + "new", + Tokens{ + { + Type: hclsyntax.TokenIdent, + Bytes: []byte(`new`), + SpacesBefore: 0, + }, + { + Type: hclsyntax.TokenEqual, + Bytes: []byte{'='}, + SpacesBefore: 1, + }, + { + Type: hclsyntax.TokenNumberLit, + Bytes: []byte(`123`), + SpacesBefore: 1, + }, + { + Type: hclsyntax.TokenEOF, + Bytes: []byte{}, + SpacesBefore: 0, + }, + }, + }, + } + + for _, test := range tests { + test := test + + t.Run(fmt.Sprintf("%s %s in %s", test.oldName, test.newName, test.src), func(t *testing.T) { + t.Parallel() + + f, diags := ParseConfig([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1}) + + if len(diags) != 0 { + for _, diag := range diags { + t.Logf("- %s", diag.Error()) + } + t.Fatalf("unexpected diagnostics") + } + + attr := f.Body().GetAttribute(test.oldName) + attr.SetName(test.newName) + got := f.BuildTokens(nil) + format(got) + + if !reflect.DeepEqual(got, test.want) { + diff := cmp.Diff(test.want, got) + t.Errorf("wrong result\ngot: %s\nwant: %s\ndiff:\n%s", spew.Sdump(got), spew.Sdump(test.want), diff) + } + }) + } +} From 6e75e170fcd454ec3685a77c794890a6cf43c942 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Thu, 23 May 2024 16:39:56 -0400 Subject: [PATCH 2/2] hclwrite: Add missing copyright header --- hclwrite/ast_attribute_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hclwrite/ast_attribute_test.go b/hclwrite/ast_attribute_test.go index 35631748..85ea8d7e 100644 --- a/hclwrite/ast_attribute_test.go +++ b/hclwrite/ast_attribute_test.go @@ -1,3 +1,6 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + package hclwrite import (