-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: thxCode <[email protected]>
- Loading branch information
Showing
8 changed files
with
707 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package hclsyntax | ||
|
||
import "github.com/hashicorp/hcl/v2" | ||
|
||
func (t Token) AsHCLToken() hcl.Token { | ||
return hcl.Token{ | ||
Type: rune(t.Type), | ||
Bytes: t.Bytes, | ||
} | ||
} | ||
|
||
func (ts Tokens) AsHCLTokens() hcl.Tokens { | ||
ret := make(hcl.Tokens, len(ts)) | ||
for i, t := range ts { | ||
ret[i] = t.AsHCLToken() | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package hclwrite | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
// AppendHCLBody appends an existing hcl.Body (which must not be already attached | ||
// to a body) to the end of the receiving body, | ||
// | ||
// If the given hcl.Body is not *hclsyntax.Body, this method is a no-op. | ||
func (b *Body) AppendHCLBody(body hcl.Body) { | ||
if body == nil { | ||
return | ||
} | ||
|
||
bd, ok := body.(*hclsyntax.Body) | ||
if !ok { | ||
return | ||
} | ||
|
||
attrs := make([]*hclsyntax.Attribute, 0, len(bd.Attributes)) | ||
{ | ||
for k := range bd.Attributes { | ||
attrs = append(attrs, bd.Attributes[k]) | ||
} | ||
|
||
sort.Slice(attrs, func(i, j int) bool { | ||
if attrs[i].SrcRange.Filename == attrs[j].SrcRange.Filename { | ||
return attrs[i].SrcRange.Start.Line < attrs[j].SrcRange.Start.Line | ||
} | ||
|
||
return attrs[i].SrcRange.Filename < attrs[j].SrcRange.Filename | ||
}) | ||
} | ||
for i := range attrs { | ||
b.SetAttributeRaw(attrs[i].Name, TokensForExpression(attrs[i].Expr)) | ||
} | ||
|
||
for i := range bd.Blocks { | ||
wb := b.AppendNewBlock(bd.Blocks[i].Type, bd.Blocks[i].Labels).Body() | ||
wb.AppendHCLBody(bd.Blocks[i].Body) | ||
} | ||
} |
Oops, something went wrong.