Fixing formatting

This commit is contained in:
Mike Farah 2025-12-08 09:42:48 +11:00
parent 6c4d1bd066
commit 42f2436e1f
2 changed files with 73 additions and 72 deletions

View File

@ -184,89 +184,89 @@ func (he *hclEncoder) encodeNode(body *hclwrite.Body, node *CandidateNode) error
return err return err
} }
continue continue
} else { }
// Render as attribute: key = value
// Check the style to determine how to encode strings // Render as attribute: key = value
if valueNode.Kind == ScalarNode && valueNode.Tag == "!!str" { // Check the style to determine how to encode strings
if valueNode.Style&LiteralStyle != 0 { if valueNode.Kind == ScalarNode && valueNode.Tag == "!!str" {
tokens, err := tokensForRawHCLExpr(valueNode.Value) if valueNode.Style&LiteralStyle != 0 {
if err != nil { tokens, err := tokensForRawHCLExpr(valueNode.Value)
return err if err != nil {
} return err
body.SetAttributeRaw(key, tokens)
continue
} }
// Check style: DoubleQuotedStyle means template, no style could be unquoted or regular body.SetAttributeRaw(key, tokens)
// To distinguish unquoted from regular, we check if the value is a valid identifier continue
if valueNode.Style&DoubleQuotedStyle != 0 && strings.Contains(valueNode.Value, "${") { }
// Template string - use raw tokens to preserve ${} syntax // Check style: DoubleQuotedStyle means template, no style could be unquoted or regular
tokens := hclwrite.Tokens{ // To distinguish unquoted from regular, we check if the value is a valid identifier
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}}, if valueNode.Style&DoubleQuotedStyle != 0 && strings.Contains(valueNode.Value, "${") {
} // Template string - use raw tokens to preserve ${} syntax
// Parse the string and add tokens tokens := hclwrite.Tokens{
for i := 0; i < len(valueNode.Value); i++ { {Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}},
if i < len(valueNode.Value)-1 && valueNode.Value[i] == '$' && valueNode.Value[i+1] == '{' { }
// Start of template interpolation // Parse the string and add tokens
tokens = append(tokens, &hclwrite.Token{ for i := 0; i < len(valueNode.Value); i++ {
Type: hclsyntax.TokenTemplateInterp, if i < len(valueNode.Value)-1 && valueNode.Value[i] == '$' && valueNode.Value[i+1] == '{' {
Bytes: []byte("${"), // Start of template interpolation
}) tokens = append(tokens, &hclwrite.Token{
i++ // skip the '{' Type: hclsyntax.TokenTemplateInterp,
// Find the matching '}' Bytes: []byte("${"),
start := i + 1 })
depth := 1 i++ // skip the '{'
for i++; i < len(valueNode.Value) && depth > 0; i++ { // Find the matching '}'
switch valueNode.Value[i] { start := i + 1
case '{': depth := 1
depth++ for i++; i < len(valueNode.Value) && depth > 0; i++ {
case '}': switch valueNode.Value[i] {
depth-- case '{':
} depth++
case '}':
depth--
} }
i-- // back up to the '}'
interpExpr := valueNode.Value[start:i]
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenIdent,
Bytes: []byte(interpExpr),
})
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenTemplateSeqEnd,
Bytes: []byte("}"),
})
} else {
// Regular character
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenQuotedLit,
Bytes: []byte{valueNode.Value[i]},
})
} }
i-- // back up to the '}'
interpExpr := valueNode.Value[start:i]
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenIdent,
Bytes: []byte(interpExpr),
})
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenTemplateSeqEnd,
Bytes: []byte("}"),
})
} else {
// Regular character
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenQuotedLit,
Bytes: []byte{valueNode.Value[i]},
})
} }
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}})
body.SetAttributeRaw(key, tokens)
} else if isValidHCLIdentifier(valueNode.Value) && valueNode.Style == 0 {
// Could be unquoted identifier - but only if it came from HCL originally
// For safety, only use traversal if style is explicitly 0 (not set)
// This avoids treating strings from YAML as unquoted
traversal := hcl.Traversal{
hcl.TraverseRoot{Name: valueNode.Value},
}
body.SetAttributeTraversal(key, traversal)
} else {
// Regular quoted string - use cty.Value
ctyValue, err := nodeToCtyValue(valueNode)
if err != nil {
return err
}
body.SetAttributeValue(key, ctyValue)
} }
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}})
body.SetAttributeRaw(key, tokens)
} else if isValidHCLIdentifier(valueNode.Value) && valueNode.Style == 0 {
// Could be unquoted identifier - but only if it came from HCL originally
// For safety, only use traversal if style is explicitly 0 (not set)
// This avoids treating strings from YAML as unquoted
traversal := hcl.Traversal{
hcl.TraverseRoot{Name: valueNode.Value},
}
body.SetAttributeTraversal(key, traversal)
} else { } else {
// Non-string value - use cty.Value // Regular quoted string - use cty.Value
ctyValue, err := nodeToCtyValue(valueNode) ctyValue, err := nodeToCtyValue(valueNode)
if err != nil { if err != nil {
return err return err
} }
body.SetAttributeValue(key, ctyValue) body.SetAttributeValue(key, ctyValue)
} }
} else {
// Non-string value - use cty.Value
ctyValue, err := nodeToCtyValue(valueNode)
if err != nil {
return err
}
body.SetAttributeValue(key, ctyValue)
} }
} }
return nil return nil

View File

@ -275,3 +275,4 @@ nohcl
zclconf zclconf
cty cty
go-cty go-cty
unlabeled