hclwrite: Split Attribute and Block into their own source files

These will grow to have significant logic of their own, so hiding them at
the end of the Body file makes things harder to read.
This commit is contained in:
Martin Atkins 2018-11-03 08:43:10 -07:00
parent 3e4b7e0eb2
commit 04d1413613
5 changed files with 65 additions and 55 deletions

48
hclwrite/ast_attribute.go Normal file
View File

@ -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)
}

12
hclwrite/ast_block.go Normal file
View File

@ -0,0 +1,12 @@
package hclwrite
type Block struct {
inTree
leadComments *node
typeName *node
labels nodeSet
open *node
body *node
close *node
}

View File

@ -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
}

View File

@ -215,6 +215,7 @@ func TestBodyGetAttribute(t *testing.T) {
})
}
}
func TestBodySetAttributeValue(t *testing.T) {
tests := []struct {
src string

View File

@ -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