This commit is contained in:
Mihail Vratchanski 2025-06-04 12:36:55 +03:00
parent 9c711684c5
commit 4691c57bad
5 changed files with 3 additions and 34 deletions

View File

@ -207,7 +207,7 @@ yq -P -oy sample.json
}
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.LeadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.")
rootCmd.PersistentFlags().StringVar(&yamlParser, "yaml-parser", "goccy", "YAML parser to use: 'goccy' (default, actively maintained) or 'v3' (legacy gopkg.in/yaml.v3)")
rootCmd.PersistentFlags().StringVar(&yamlParser, "yaml-parser", "v3", "YAML parser to use: 'goccy' (actively maintained) or 'v3' (default, legacy gopkg.in/yaml.v3)")
if err = rootCmd.RegisterFlagCompletionFunc("yaml-parser", cobra.FixedCompletions([]string{"goccy", "v3"}, cobra.ShellCompDirectiveNoFileComp)); err != nil {
panic(err)
}

View File

@ -17,32 +17,19 @@ func (o *CandidateNode) goccyDecodeIntoChild(childNode ast.Node, cm yaml.Comment
}
func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, anchorMap map[string]*CandidateNode) error {
// log.Debugf("UnmarshalYAML %v", node)
// log.Debugf("UnmarshalYAML %v", node.Type().String())
// log.Debugf("UnmarshalYAML Node Value: %v", node.String())
// log.Debugf("UnmarshalYAML Node GetComment: %v", node.GetComment())
if node.GetComment() != nil {
commentMapComments := cm[node.GetPath()]
for _, comment := range node.GetComment().Comments {
// need to use the comment map to find the position :/
// log.Debugf("%v has a comment of [%v]", node.GetPath(), comment.Token.Value)
for _, commentMapComment := range commentMapComments {
commentMapValue := strings.Join(commentMapComment.Texts, "\n")
if commentMapValue == comment.Token.Value {
// log.Debug("found a matching entry in comment map")
// we found the comment in the comment map,
// now we can process the position
switch commentMapComment.Position {
case yaml.CommentHeadPosition:
o.HeadComment = comment.String()
// log.Debug("its a head comment %v", comment.String())
case yaml.CommentLinePosition:
o.LineComment = comment.String()
// log.Debug("its a line comment %v", comment.String())
case yaml.CommentFootPosition:
o.FootComment = comment.String()
// log.Debug("its a foot comment %v", comment.String())
}
}
}
@ -65,7 +52,6 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, an
o.Kind = ScalarNode
o.Tag = "!!bool"
case ast.NullType:
// log.Debugf("its a null type with value %v", node.GetToken().Value)
o.Kind = ScalarNode
o.Tag = "!!null"
o.Value = node.GetToken().Value
@ -82,19 +68,14 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, an
o.Style = DoubleQuotedStyle
}
o.Value = node.(*ast.StringNode).Value
// log.Debugf("string value %v", node.(*ast.StringNode).Value)
case ast.LiteralType:
o.Kind = ScalarNode
o.Tag = "!!str"
o.Style = LiteralStyle
astLiteral := node.(*ast.LiteralNode)
// log.Debugf("astLiteral.Start.Type %v", astLiteral.Start.Type)
if astLiteral.Start.Type == goccyToken.FoldedType {
// log.Debugf("folded Type %v", astLiteral.Start.Type)
o.Style = FoldedStyle
}
// log.Debug("start value: %v ", node.(*ast.LiteralNode).Start.Value)
// log.Debug("start value: %v ", node.(*ast.LiteralNode).Start.Type)
// Preserving the original multiline string value is important for fidelity.
// goccy/go-yaml provides this in astLiteral.Value.Value for literal and folded styles.
o.Value = astLiteral.Value.Value
@ -105,7 +86,6 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, an
}
o.Tag = node.(*ast.TagNode).Start.Value // Tag value includes the '!' or '!!' prefix.
case ast.MappingType:
// log.Debugf("UnmarshalYAML - a mapping node")
o.Kind = MappingNode
o.Tag = "!!map"
@ -120,11 +100,9 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, an
}
}
if mappingNode.FootComment != nil {
// log.Debugf("mapping node has a foot comment of: %v", mappingNode.FootComment)
o.FootComment = mappingNode.FootComment.String()
}
case ast.MappingValueType:
// log.Debugf("UnmarshalYAML - a mapping node")
o.Kind = MappingNode
o.Tag = "!!map"
mappingValueNode := node.(*ast.MappingValueNode)
@ -133,7 +111,6 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, an
return err
}
case ast.SequenceType:
// log.Debugf("UnmarshalYAML - a sequence node")
o.Kind = SequenceNode
o.Tag = "!!seq"
sequenceNode := node.(*ast.SequenceNode)
@ -158,7 +135,6 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, an
o.Content[i] = valueNode
}
case ast.AnchorType:
// log.Debugf("UnmarshalYAML - an anchor node")
anchorNode := node.(*ast.AnchorNode)
err := o.UnmarshalGoccyYAML(anchorNode.Value, cm, anchorMap)
if err != nil {
@ -168,16 +144,14 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, an
anchorMap[o.Anchor] = o
case ast.AliasType:
// log.Debugf("UnmarshalYAML - an alias node")
aliasNode := node.(*ast.AliasNode)
o.Kind = AliasNode
o.Value = aliasNode.Value.String()
o.Alias = anchorMap[o.Value]
case ast.MergeKeyType:
// log.Debugf("UnmarshalYAML - a merge key")
o.Kind = ScalarNode
o.Tag = "!!merge" // note - I should be able to get rid of this.
o.Tag = "!!merge"
o.Value = "<<"
default:
@ -224,8 +198,6 @@ func (o *CandidateNode) MarshalGoccyYAML() (interface{}, error) {
return o.Value, nil
case ScalarNode:
// log.Debug("MarshalGoccyYAML - scalar: %v", o.Value)
// Handle different scalar types based on tag for correct marshalling.
switch o.Tag {
case "!!int":

View File

@ -197,6 +197,5 @@ func (dec *goccyYamlDecoder) Decode() (*CandidateNode, error) {
func (dec *goccyYamlDecoder) blankNodeWithComment() *CandidateNode {
node := createScalarNode(nil, "") // Create an empty scalar node.
node.LeadingContent = dec.leadingContent
// dec.leadingContent = "" // Clear after use? Not strictly necessary here as Decode will clear it next if node is returned.
return node
}

View File

@ -29,7 +29,6 @@ func (ye *goccyYamlEncoder) CanHandleAliases() bool {
func (ye *goccyYamlEncoder) PrintDocumentSeparator(writer io.Writer) error {
if ye.prefs.PrintDocSeparators {
// log.Debug("writing doc sep") // Commented out
if err := writeString(writer, "---\n"); err != nil {
return err
}
@ -96,7 +95,6 @@ func (ye *goccyYamlEncoder) PrintTrailingComment(writer io.Writer, comment strin
}
func (ye *goccyYamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
// log.Debug("goccyYamlEncoder - going to print %v", NodeToString(node)) // Commented out
if node.Kind == ScalarNode && ye.prefs.UnwrapScalar {
valueToPrint := node.Value
if node.LeadingContent == "" || valueToPrint != "" {

View File

@ -18,7 +18,7 @@ func NewDefaultYamlPreferences() YamlPreferences {
PrintDocSeparators: true,
UnwrapScalar: true,
EvaluateTogether: false,
UseGoccyParser: true, // Default to goccy parser (actively maintained)
UseGoccyParser: false,
}
}