Skip to content

Path skip commented content in the result of ReadNode. #659

@yudintsevegor

Description

@yudintsevegor

Describe the bug
I don't know if it's expected or not, but the Path function seems to ignore the commented lines for the requested "child" node.

To Reproduce

package main

import (
	"bytes"
	"fmt"

	"github.com/goccy/go-yaml"
)

var content = `
name: test

a:
  - 0
  - 1
b:
  #l: 1
  d: 1
`

type yamlContent struct {
	Name string         `yaml:"name"`
	A    []int          `yaml:"a"`
	B    map[string]int `yaml:"b"`
}

func main(){
	example()
}

func example() {
	var y yamlContent
	err := yaml.Unmarshal([]byte(content), &y)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", y) // {Name:test A:[0 1] B:map[d:1]} - expected.

	path, err := yaml.PathString("$.b")
	if err != nil {
		panic(err)
	}
	node, err := path.ReadNode(bytes.NewReader([]byte(content)))
	if err != nil {
		panic(err)
	}
	fmt.Println("NODE", node.String()) // d: 1 - not expected.
	/*
		#l: 1
		d: 1
	*/
}

Expected behavior
I would expect to have in the node the content that is under the requested path $.b. In the example above:

#l: 1
d: 1

Screenshots
If applicable, add screenshots to help explain your problem.

Version Variables

  • Go version: go1.23.1
  • go-yaml's Version: v1.15.22

Additional context
The reason of doing it is the desire to get the line where d is located. In this use case, it should be 8.

My current approach is to use lexer.Tokenize(content) to get the line for the token that contains the value that I am looking for.
Maybe there is a better way to solve my problem, but still, the bug that is being reported is related to the behavior of the function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions