diff --git a/hclwrite/ast_attribute.go b/hclwrite/ast_attribute.go new file mode 100644 index 0000000..975fa74 --- /dev/null +++ b/hclwrite/ast_attribute.go @@ -0,0 +1,48 @@ +package hclwrite + +import ( + "github.com/hashicorp/hcl2/hcl/hclsyntax" +) + +type Attribute struct { + inTree + + leadComments *node + name *node + expr *node + lineComments *node +} + +func newAttribute() *Attribute { + return &Attribute{ + inTree: newInTree(), + } +} + +func (a *Attribute) init(name string, expr *Expression) { + expr.assertUnattached() + + nameTok := newIdentToken(name) + nameObj := newIdentifier(nameTok) + a.leadComments = a.children.Append(newComments(nil)) + a.name = a.children.Append(nameObj) + a.children.AppendUnstructuredTokens(Tokens{ + { + Type: hclsyntax.TokenEqual, + Bytes: []byte{'='}, + }, + }) + a.expr = a.children.Append(expr) + a.expr.list = a.children + a.lineComments = a.children.Append(newComments(nil)) + a.children.AppendUnstructuredTokens(Tokens{ + { + Type: hclsyntax.TokenNewline, + Bytes: []byte{'\n'}, + }, + }) +} + +func (a *Attribute) Expr() *Expression { + return a.expr.content.(*Expression) +} diff --git a/hclwrite/ast_block.go b/hclwrite/ast_block.go new file mode 100644 index 0000000..ba6fa17 --- /dev/null +++ b/hclwrite/ast_block.go @@ -0,0 +1,12 @@ +package hclwrite + +type Block struct { + inTree + + leadComments *node + typeName *node + labels nodeSet + open *node + body *node + close *node +} diff --git a/hclwrite/ast_body.go b/hclwrite/ast_body.go index d0d79fd..a3cacd7 100644 --- a/hclwrite/ast_body.go +++ b/hclwrite/ast_body.go @@ -2,7 +2,6 @@ package hclwrite import ( "github.com/hashicorp/hcl2/hcl" - "github.com/hashicorp/hcl2/hcl/hclsyntax" "github.com/zclconf/go-cty/cty" ) @@ -77,57 +76,3 @@ func (b *Body) SetAttributeValue(name string, val cty.Value) *Attribute { func (b *Body) SetAttributeTraversal(name string, traversal hcl.Traversal) *Attribute { panic("Body.SetAttributeTraversal not yet implemented") } - -type Attribute struct { - inTree - - leadComments *node - name *node - expr *node - lineComments *node -} - -func newAttribute() *Attribute { - return &Attribute{ - inTree: newInTree(), - } -} - -func (a *Attribute) init(name string, expr *Expression) { - expr.assertUnattached() - - nameTok := newIdentToken(name) - nameObj := newIdentifier(nameTok) - a.leadComments = a.children.Append(newComments(nil)) - a.name = a.children.Append(nameObj) - a.children.AppendUnstructuredTokens(Tokens{ - { - Type: hclsyntax.TokenEqual, - Bytes: []byte{'='}, - }, - }) - a.expr = a.children.Append(expr) - a.expr.list = a.children - a.lineComments = a.children.Append(newComments(nil)) - a.children.AppendUnstructuredTokens(Tokens{ - { - Type: hclsyntax.TokenNewline, - Bytes: []byte{'\n'}, - }, - }) -} - -func (a *Attribute) Expr() *Expression { - return a.expr.content.(*Expression) -} - -type Block struct { - inTree - - leadComments *node - typeName *node - labels nodeSet - open *node - body *node - close *node -} diff --git a/hclwrite/ast_body_test.go b/hclwrite/ast_body_test.go index de7abb7..c47d203 100644 --- a/hclwrite/ast_body_test.go +++ b/hclwrite/ast_body_test.go @@ -215,6 +215,7 @@ func TestBodyGetAttribute(t *testing.T) { }) } } + func TestBodySetAttributeValue(t *testing.T) { tests := []struct { src string diff --git a/hclwrite/doc.go b/hclwrite/doc.go index 6f4109d..56d5b77 100644 --- a/hclwrite/doc.go +++ b/hclwrite/doc.go @@ -4,4 +4,8 @@ // It operates at a different level of abstraction than the main HCL parser // and AST, since details such as the placement of comments and newlines // are preserved when unchanged. +// +// The hclwrite API follows a similar principle to XML/HTML DOM, allowing nodes +// to be read out, created and inserted, etc. Nodes represent syntax constructs +// rather than semantic concepts. package hclwrite