Skip to content

Commit

Permalink
feat: adjust hcl v2
Browse files Browse the repository at this point in the history
Signed-off-by: thxCode <[email protected]>
  • Loading branch information
thxCode committed Feb 3, 2024
1 parent 00c4c93 commit 8afd776
Show file tree
Hide file tree
Showing 8 changed files with 707 additions and 1 deletion.
6 changes: 6 additions & 0 deletions staging/hcl/doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Copyright (c) 2024 Seal, Inc.
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// Package hcl borrows from the https://github.com/hashicorp/hcl/tree/v2.19.1.
//
// This package adds some modelling types and general utility functions,
// it is not exactly the same with the original implementation but works well for us.

// Package hcl contains the main modelling types and general utility functions
// for HCL.
//
Expand Down
9 changes: 8 additions & 1 deletion staging/hcl/hclsyntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"unicode/utf8"

"github.com/apparentlymart/go-textseg/v15/textseg"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"

"github.com/hashicorp/hcl/v2"
)

type parser struct {
Expand Down Expand Up @@ -244,6 +245,7 @@ func (p *parser) finishParsingBodyAttribute(ident Token, singleLine bool) (Node,
panic("finishParsingBodyAttribute called with next not equals")
}

var startIndex = p.NextIndex
var endRange hcl.Range

expr, diags := p.ParseExpression()
Expand Down Expand Up @@ -291,6 +293,7 @@ func (p *parser) finishParsingBodyAttribute(ident Token, singleLine bool) (Node,
SrcRange: hcl.RangeBetween(ident.Range, endRange),
NameRange: ident.Range,
EqualsRange: eqTok.Range,
Tokens: p.Tokens[startIndex:p.NextIndex],
}, diags
}

Expand All @@ -300,6 +303,7 @@ func (p *parser) finishParsingBodyBlock(ident Token) (Node, hcl.Diagnostics) {
var labels []string
var labelRanges []hcl.Range

var startIndex int
var oBrace Token

Token:
Expand All @@ -309,6 +313,7 @@ Token:
switch tok.Type {

case TokenOBrace:
startIndex = p.NextIndex
oBrace = p.Read()
break Token

Expand Down Expand Up @@ -371,6 +376,7 @@ Token:
LabelRanges: labelRanges,
OpenBraceRange: ident.Range, // placeholder
CloseBraceRange: ident.Range, // placeholder
Tokens: p.Tokens[startIndex:p.NextIndex],
}, diags
}
}
Expand Down Expand Up @@ -472,6 +478,7 @@ Token:
LabelRanges: labelRanges,
OpenBraceRange: oBrace.Range,
CloseBraceRange: cBraceRange,
Tokens: p.Tokens[startIndex:p.NextIndex],
}, diags
}

Expand Down
4 changes: 4 additions & 0 deletions staging/hcl/hclsyntax/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (b *Block) AsHCLBlock() *hcl.Block {
DefRange: b.DefRange(),
TypeRange: b.TypeRange,
LabelRanges: b.LabelRanges,
Tokens: b.Tokens.AsHCLTokens(),
}
}

Expand Down Expand Up @@ -320,6 +321,7 @@ type Attribute struct {
SrcRange hcl.Range
NameRange hcl.Range
EqualsRange hcl.Range
Tokens Tokens
}

func (a *Attribute) walkChildNodes(w internalWalkFunc) {
Expand All @@ -341,6 +343,7 @@ func (a *Attribute) AsHCLAttribute() *hcl.Attribute {

Range: a.SrcRange,
NameRange: a.NameRange,
Tokens: a.Tokens.AsHCLTokens(),
}
}

Expand Down Expand Up @@ -377,6 +380,7 @@ type Block struct {
LabelRanges []hcl.Range
OpenBraceRange hcl.Range
CloseBraceRange hcl.Range
Tokens Tokens
}

func (b *Block) walkChildNodes(w internalWalkFunc) {
Expand Down
18 changes: 18 additions & 0 deletions staging/hcl/hclsyntax/token_ext.go
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
}
46 changes: 46 additions & 0 deletions staging/hcl/hclwrite/ast_body_ext.go
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)
}
}
Loading

0 comments on commit 8afd776

Please sign in to comment.