mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-05 20:15:36 +00:00
Simplifying HCL decoder
This commit is contained in:
parent
fd405749f9
commit
e4bf8a1e0a
@ -64,44 +64,6 @@ func extractLineComment(src []byte, endPos int) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractLeadingComments extracts comments from the very beginning of the file.
|
|
||||||
// It returns the comment text and the byte position of the last character in that leading block.
|
|
||||||
func extractLeadingComments(src []byte) (string, int) {
|
|
||||||
var comments []string
|
|
||||||
i := 0
|
|
||||||
|
|
||||||
// Skip leading whitespace
|
|
||||||
for i < len(src) && (src[i] == ' ' || src[i] == '\t' || src[i] == '\n' || src[i] == '\r') {
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
lastPos := -1
|
|
||||||
|
|
||||||
// Extract comment lines from the start
|
|
||||||
for i < len(src) && src[i] == '#' {
|
|
||||||
lineStart := i
|
|
||||||
// Find end of line
|
|
||||||
for i < len(src) && src[i] != '\n' {
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
lastPos = i - 1
|
|
||||||
comments = append(comments, strings.TrimSpace(string(src[lineStart:i])))
|
|
||||||
// Skip newline
|
|
||||||
if i < len(src) && src[i] == '\n' {
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
// Skip whitespace between comment lines
|
|
||||||
for i < len(src) && (src[i] == ' ' || src[i] == '\t' || src[i] == '\n' || src[i] == '\r') {
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(comments) > 0 {
|
|
||||||
return strings.Join(comments, "\n"), lastPos
|
|
||||||
}
|
|
||||||
return "", -1
|
|
||||||
}
|
|
||||||
|
|
||||||
// extractHeadComment extracts comments before a given start position
|
// extractHeadComment extracts comments before a given start position
|
||||||
func extractHeadComment(src []byte, startPos int) string {
|
func extractHeadComment(src []byte, startPos int) string {
|
||||||
var comments []string
|
var comments []string
|
||||||
@ -174,15 +136,9 @@ func (dec *hclDecoder) Decode() (*CandidateNode, error) {
|
|||||||
|
|
||||||
root := &CandidateNode{Kind: MappingNode}
|
root := &CandidateNode{Kind: MappingNode}
|
||||||
|
|
||||||
// Extract file-level head comments (comments at the very beginning of the file)
|
|
||||||
leadingComment, _ := extractLeadingComments(dec.fileBytes)
|
|
||||||
leadingUsed := false
|
|
||||||
if leadingComment != "" {
|
|
||||||
root.HeadComment = leadingComment
|
|
||||||
}
|
|
||||||
|
|
||||||
// process attributes in declaration order
|
// process attributes in declaration order
|
||||||
body := dec.file.Body.(*hclsyntax.Body)
|
body := dec.file.Body.(*hclsyntax.Body)
|
||||||
|
firstAttr := true
|
||||||
for _, attrWithName := range sortedAttributes(body.Attributes) {
|
for _, attrWithName := range sortedAttributes(body.Attributes) {
|
||||||
keyNode := createStringScalarNode(attrWithName.Name)
|
keyNode := createStringScalarNode(attrWithName.Name)
|
||||||
valNode := convertHclExprToNode(attrWithName.Attr.Expr, dec.fileBytes)
|
valNode := convertHclExprToNode(attrWithName.Attr.Expr, dec.fileBytes)
|
||||||
@ -190,17 +146,11 @@ func (dec *hclDecoder) Decode() (*CandidateNode, error) {
|
|||||||
// Attach comments if any
|
// Attach comments if any
|
||||||
attrRange := attrWithName.Attr.Range()
|
attrRange := attrWithName.Attr.Range()
|
||||||
headComment := extractHeadComment(dec.fileBytes, attrRange.Start.Byte)
|
headComment := extractHeadComment(dec.fileBytes, attrRange.Start.Byte)
|
||||||
if !leadingUsed && leadingComment != "" {
|
if firstAttr && headComment != "" {
|
||||||
// Avoid double-applying the leading file comment to the first attribute
|
// For the first attribute, apply its head comment to the root
|
||||||
switch headComment {
|
root.HeadComment = headComment
|
||||||
case leadingComment:
|
firstAttr = false
|
||||||
headComment = ""
|
} else if headComment != "" {
|
||||||
case "":
|
|
||||||
headComment = leadingComment
|
|
||||||
}
|
|
||||||
leadingUsed = true
|
|
||||||
}
|
|
||||||
if headComment != "" {
|
|
||||||
keyNode.HeadComment = headComment
|
keyNode.HeadComment = headComment
|
||||||
}
|
}
|
||||||
if lineComment := extractLineComment(dec.fileBytes, attrRange.End.Byte); lineComment != "" {
|
if lineComment := extractLineComment(dec.fileBytes, attrRange.End.Byte); lineComment != "" {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user