2018-08-01 15:45:22 +00:00
|
|
|
package hclwrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Body struct {
|
|
|
|
inTree
|
|
|
|
|
|
|
|
items nodeSet
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:44:48 +00:00
|
|
|
func (b *Body) appendItem(c nodeContent) *node {
|
|
|
|
nn := b.children.Append(c)
|
|
|
|
b.items.Add(nn)
|
|
|
|
return nn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Body) appendItemNode(nn *node) *node {
|
|
|
|
nn.assertUnattached()
|
|
|
|
b.children.AppendNode(nn)
|
|
|
|
b.items.Add(nn)
|
|
|
|
return nn
|
2018-08-01 15:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Body) AppendUnstructuredTokens(ts Tokens) {
|
|
|
|
b.inTree.children.Append(ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAttribute returns the attribute from the body that has the given name,
|
|
|
|
// or returns nil if there is currently no matching attribute.
|
|
|
|
func (b *Body) GetAttribute(name string) *Attribute {
|
|
|
|
for n := range b.items {
|
|
|
|
if attr, isAttr := n.content.(*Attribute); isAttr {
|
|
|
|
nameObj := attr.name.content.(*identifier)
|
|
|
|
if nameObj.hasName(name) {
|
|
|
|
// We've found it!
|
|
|
|
return attr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAttributeValue either replaces the expression of an existing attribute
|
|
|
|
// of the given name or adds a new attribute definition to the end of the block.
|
|
|
|
//
|
|
|
|
// The value is given as a cty.Value, and must therefore be a literal. To set
|
|
|
|
// a variable reference or other traversal, use SetAttributeTraversal.
|
|
|
|
//
|
|
|
|
// The return value is the attribute that was either modified in-place or
|
|
|
|
// created.
|
|
|
|
func (b *Body) SetAttributeValue(name string, val cty.Value) *Attribute {
|
2018-08-09 15:44:48 +00:00
|
|
|
attr := b.GetAttribute(name)
|
|
|
|
expr := NewExpressionLiteral(val)
|
|
|
|
if attr != nil {
|
|
|
|
attr.expr = attr.expr.ReplaceWith(expr)
|
|
|
|
} else {
|
|
|
|
attr := newAttribute()
|
|
|
|
attr.init(name, expr)
|
|
|
|
b.appendItem(attr)
|
|
|
|
}
|
|
|
|
return attr
|
2018-08-01 15:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAttributeTraversal either replaces the expression of an existing attribute
|
|
|
|
// of the given name or adds a new attribute definition to the end of the block.
|
|
|
|
//
|
|
|
|
// The new expression is given as a hcl.Traversal, which must be an absolute
|
|
|
|
// traversal. To set a literal value, use SetAttributeValue.
|
|
|
|
//
|
|
|
|
// The return value is the attribute that was either modified in-place or
|
|
|
|
// created.
|
|
|
|
func (b *Body) SetAttributeTraversal(name string, traversal hcl.Traversal) *Attribute {
|
|
|
|
panic("Body.SetAttributeTraversal not yet implemented")
|
|
|
|
}
|