Skip to content

hclwrite: add read and write API for ObjectConsExpr - #811

Open
bbasata wants to merge 7 commits into
bbasata/parse-object-construct-expressionsfrom
bbasata/rewrite-objects
Open

hclwrite: add read and write API for ObjectConsExpr#811
bbasata wants to merge 7 commits into
bbasata/parse-object-construct-expressionsfrom
bbasata/rewrite-objects

Conversation

@bbasata

@bbasata bbasata commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Description

Rewrite object values.

config := []byte(`a = {
    hat = "derby", (cat) = "calico" }` + "\n")

bowler := hclwrite.TokensForValue(cty.StringVal(`bowler`))

file, _ := ParseConfig(config, "<inline>", hcl.Pos{})
attr := file.Body().GetAttribute("a")

expr := attr.Expr().AsObjectConsExpr()
key, value := expr.SetItemRaw("hat", bowler)

fmt.Printf("key: ")
key.BuildTokens(nil).WriteTo(os.Stdout)
fmt.Println()
fmt.Println("value: ")
value.BuildTokens(nil).WriteTo(os.Stdout)
fmt.Println()

Related Issue

#810

@bbasata

bbasata commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased onto 2994071. The new commits belonging to this PR are b5b53ef and 321afff.

@bbasata

bbasata commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased onto 2994071. The new commits belonging to this PR are b5b53ef and 321afff.

@mildwonkey: I'm marking this one for review, with no expectation that it's quite ready to merge yet. It's simply the next one in the sequence that we're collaborating on.

I've exercised this in a client application to a high degree of confidence. Now, to turn "the change that works" into "the right change."

@bbasata
bbasata marked this pull request as ready for review August 4, 2026 22:38
@bbasata
bbasata requested a review from a team as a code owner August 4, 2026 22:38
Comment thread hclwrite/ast_object_cons_expr.go Outdated
@bbasata

bbasata commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author

@mildwonkey: I'm marking this one for review, with no expectation that it's quite ready to merge yet. It's simply the next one in the sequence that we're collaborating on.

ℹ️ This new test will see some changes in its tree. This is a consequence of parsing the object-construct item key as an expression, so it now has a richer sub-tree.

TestParse/a_=_{___hat_=_{__style_=_"derby",__color_=_(variable)___},___cat_=_"calico"_}

Comment thread hclwrite/parser.go Outdated
@bbasata
bbasata force-pushed the bbasata/rewrite-objects branch from e3972e2 to 0d7feb5 Compare August 4, 2026 23:46
@bbasata
bbasata changed the base branch from main to bbasata/parse-object-construct-expressions August 4, 2026 23:47
@bbasata bbasata changed the title hclwrite: add ObjectConsExpr.SetItemRaw() and ObjectConsExpr.ItemFor() hclwrite: add ObjectConsExpr.SetItemRaw() and ObjectConsExpr.ItemFor() Aug 4, 2026
@bbasata
bbasata force-pushed the bbasata/rewrite-objects branch from 0d7feb5 to 88dae3b Compare August 5, 2026 05:08
@bbasata bbasata changed the title hclwrite: add ObjectConsExpr.SetItemRaw() and ObjectConsExpr.ItemFor() hclwrite: add read and write API for ObjectConsExpr Aug 5, 2026
@bbasata
bbasata force-pushed the bbasata/rewrite-objects branch from 2356309 to 02f89ac Compare August 5, 2026 05:40
@bbasata
bbasata force-pushed the bbasata/rewrite-objects branch from f7242e8 to 923c509 Compare August 5, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants