goccy / go-yaml

YAML support for the Go language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get positional (Line / Column) information on a node

pantelis-karamolegkos opened this issue · comments

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

I want to be able to get positional information on a yaml node i.e. the line in its text and its column.
I think this is more or less implemented in the official yaml golang library. Line and Column

Describe the solution you'd like
A clear and concise description of what you want to happen.

e,g, in the following example

package main

import (
	"fmt"
	"log"
	"strings"

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

func main() {
	yml := `
store:
  book:
    - author: john
      price: 10
    - author: ken
      price: 12
	  # comment
  bicycle:
    color: red
    price: 19.95
`
	path, err := yaml.PathString("$.store.book")
	fmt.Printf("%#+v\n", path.String())
	if err != nil {
		log.Fatal(err)
	}
	var authors []interface{}
	if err := path.Read(strings.NewReader(yml), &authors); err != nil {
		log.Fatal(err)
	}
	fmt.Println(authors)
	// [john ken]
}

I want to be able say to get something like path.Line (2) and path.Column (3)

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Great! Thanks for the suggestion 🎉

@nervo I just noticed that in the go playground example, the column value for the .store.book node is 5 while clearly it is in 3rd column; why is that?

@pantelis-karamolegkos the .store.book path represents the VALUE node, it begins where the value begins, aka. right after the first -.

So is there a way for me to get the indentation of the line where .store.book actually resides? a.k.a 2?

@pantelis-karamolegkos navigate through token's tree :)