hclwrite: Rename Body.AppendBlock to AppendNewBlock
Since this function implicitly creates a new body, this name is more appropriate and leaves the name "AppendBlock" open for a later method to append an _existing_ block, such as when moving a block from one file to another.
This commit is contained in:
parent
379d277e2b
commit
fd915f557d
@ -85,26 +85,23 @@ func (b *Body) SetAttributeTraversal(name string, traversal hcl.Traversal) *Attr
|
|||||||
panic("Body.SetAttributeTraversal not yet implemented")
|
panic("Body.SetAttributeTraversal not yet implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendBlock appends a new nested block to the end of the receiving body.
|
// AppendNewBlock appends a new nested block to the end of the receiving body
|
||||||
//
|
// with the given type name and labels.
|
||||||
// If blankLine is set, an additional empty line is added before the block
|
func (b *Body) AppendNewBlock(typeName string, labels []string) *Block {
|
||||||
// for separation. Usual HCL style suggests that we group together blocks of
|
|
||||||
// the same type without intervening blank lines and then put blank lines
|
|
||||||
// between blocks of different types. In some languages, some different block
|
|
||||||
// types may be conceptually related and so may still be grouped together.
|
|
||||||
// It is the caller's responsibility to respect the usual conventions of the
|
|
||||||
// language being generated.
|
|
||||||
func (b *Body) AppendBlock(typeName string, labels []string, blankLine bool) *Block {
|
|
||||||
block := newBlock()
|
block := newBlock()
|
||||||
block.init(typeName, labels)
|
block.init(typeName, labels)
|
||||||
if blankLine {
|
|
||||||
b.AppendUnstructuredTokens(Tokens{
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenNewline,
|
|
||||||
Bytes: []byte{'\n'},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
b.appendItem(block)
|
b.appendItem(block)
|
||||||
return block
|
return block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppendNewline appends a newline token to th end of the receiving body,
|
||||||
|
// which generally serves as a separator between different sets of body
|
||||||
|
// contents.
|
||||||
|
func (b *Body) AppendNewline() {
|
||||||
|
b.AppendUnstructuredTokens(Tokens{
|
||||||
|
{
|
||||||
|
Type: hclsyntax.TokenNewline,
|
||||||
|
Bytes: []byte{'\n'},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -419,14 +419,12 @@ func TestBodyAppendBlock(t *testing.T) {
|
|||||||
src string
|
src string
|
||||||
blockType string
|
blockType string
|
||||||
labels []string
|
labels []string
|
||||||
blank bool
|
|
||||||
want Tokens
|
want Tokens
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"",
|
"",
|
||||||
"foo",
|
"foo",
|
||||||
nil,
|
nil,
|
||||||
false,
|
|
||||||
Tokens{
|
Tokens{
|
||||||
{
|
{
|
||||||
Type: hclsyntax.TokenIdent,
|
Type: hclsyntax.TokenIdent,
|
||||||
@ -464,7 +462,6 @@ func TestBodyAppendBlock(t *testing.T) {
|
|||||||
"",
|
"",
|
||||||
"foo",
|
"foo",
|
||||||
[]string{"bar"},
|
[]string{"bar"},
|
||||||
false,
|
|
||||||
Tokens{
|
Tokens{
|
||||||
{
|
{
|
||||||
Type: hclsyntax.TokenIdent,
|
Type: hclsyntax.TokenIdent,
|
||||||
@ -517,7 +514,6 @@ func TestBodyAppendBlock(t *testing.T) {
|
|||||||
"",
|
"",
|
||||||
"foo",
|
"foo",
|
||||||
[]string{"bar", "baz"},
|
[]string{"bar", "baz"},
|
||||||
false,
|
|
||||||
Tokens{
|
Tokens{
|
||||||
{
|
{
|
||||||
Type: hclsyntax.TokenIdent,
|
Type: hclsyntax.TokenIdent,
|
||||||
@ -585,7 +581,6 @@ func TestBodyAppendBlock(t *testing.T) {
|
|||||||
"bar {}\n",
|
"bar {}\n",
|
||||||
"foo",
|
"foo",
|
||||||
nil,
|
nil,
|
||||||
false,
|
|
||||||
Tokens{
|
Tokens{
|
||||||
{
|
{
|
||||||
Type: hclsyntax.TokenIdent,
|
Type: hclsyntax.TokenIdent,
|
||||||
@ -639,69 +634,6 @@ func TestBodyAppendBlock(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"bar_blank_after {}\n",
|
|
||||||
"foo",
|
|
||||||
nil,
|
|
||||||
true,
|
|
||||||
Tokens{
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenIdent,
|
|
||||||
Bytes: []byte(`bar_blank_after`),
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenOBrace,
|
|
||||||
Bytes: []byte{'{'},
|
|
||||||
SpacesBefore: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenCBrace,
|
|
||||||
Bytes: []byte{'}'},
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenNewline,
|
|
||||||
Bytes: []byte{'\n'},
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenNewline,
|
|
||||||
Bytes: []byte{'\n'},
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenIdent,
|
|
||||||
Bytes: []byte(`foo`),
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenOBrace,
|
|
||||||
Bytes: []byte{'{'},
|
|
||||||
SpacesBefore: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenNewline,
|
|
||||||
Bytes: []byte{'\n'},
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenCBrace,
|
|
||||||
Bytes: []byte{'}'},
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenNewline,
|
|
||||||
Bytes: []byte{'\n'},
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: hclsyntax.TokenEOF,
|
|
||||||
Bytes: []byte{},
|
|
||||||
SpacesBefore: 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@ -714,7 +646,7 @@ func TestBodyAppendBlock(t *testing.T) {
|
|||||||
t.Fatalf("unexpected diagnostics")
|
t.Fatalf("unexpected diagnostics")
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Body().AppendBlock(test.blockType, test.labels, test.blank)
|
f.Body().AppendNewBlock(test.blockType, test.labels)
|
||||||
got := f.BuildTokens(nil)
|
got := f.BuildTokens(nil)
|
||||||
format(got)
|
format(got)
|
||||||
if !reflect.DeepEqual(got, test.want) {
|
if !reflect.DeepEqual(got, test.want) {
|
||||||
|
Loading…
Reference in New Issue
Block a user