aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hcl2/hclwrite/ast_block.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hclwrite/ast_block.go')
-rw-r--r--vendor/github.com/hashicorp/hcl2/hclwrite/ast_block.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hclwrite/ast_block.go b/vendor/github.com/hashicorp/hcl2/hclwrite/ast_block.go
new file mode 100644
index 0000000..d5fd32b
--- /dev/null
+++ b/vendor/github.com/hashicorp/hcl2/hclwrite/ast_block.go
@@ -0,0 +1,74 @@
1package hclwrite
2
3import (
4 "github.com/hashicorp/hcl2/hcl/hclsyntax"
5 "github.com/zclconf/go-cty/cty"
6)
7
8type Block struct {
9 inTree
10
11 leadComments *node
12 typeName *node
13 labels nodeSet
14 open *node
15 body *node
16 close *node
17}
18
19func newBlock() *Block {
20 return &Block{
21 inTree: newInTree(),
22 labels: newNodeSet(),
23 }
24}
25
26// NewBlock constructs a new, empty block with the given type name and labels.
27func NewBlock(typeName string, labels []string) *Block {
28 block := newBlock()
29 block.init(typeName, labels)
30 return block
31}
32
33func (b *Block) init(typeName string, labels []string) {
34 nameTok := newIdentToken(typeName)
35 nameObj := newIdentifier(nameTok)
36 b.leadComments = b.children.Append(newComments(nil))
37 b.typeName = b.children.Append(nameObj)
38 for _, label := range labels {
39 labelToks := TokensForValue(cty.StringVal(label))
40 labelObj := newQuoted(labelToks)
41 labelNode := b.children.Append(labelObj)
42 b.labels.Add(labelNode)
43 }
44 b.open = b.children.AppendUnstructuredTokens(Tokens{
45 {
46 Type: hclsyntax.TokenOBrace,
47 Bytes: []byte{'{'},
48 },
49 {
50 Type: hclsyntax.TokenNewline,
51 Bytes: []byte{'\n'},
52 },
53 })
54 body := newBody() // initially totally empty; caller can append to it subsequently
55 b.body = b.children.Append(body)
56 b.close = b.children.AppendUnstructuredTokens(Tokens{
57 {
58 Type: hclsyntax.TokenCBrace,
59 Bytes: []byte{'}'},
60 },
61 {
62 Type: hclsyntax.TokenNewline,
63 Bytes: []byte{'\n'},
64 },
65 })
66}
67
68// Body returns the body that represents the content of the receiving block.
69//
70// Appending to or otherwise modifying this body will make changes to the
71// tokens that are generated between the blocks open and close braces.
72func (b *Block) Body() *Body {
73 return b.body.content.(*Body)
74}