I'd like to be able to get line/col numbers for each YAML element. I'm contributing to a project using YAML as input to a compiler, and we want to show position information in compiler error messages.
I'm interested in adding that feature if you'd take pull requests for it.
It looks like go-yaml tracks all the positions internally, but doesn't expose them through the API. The hard part seems to be designing an API to expose positions. I have a few thoughts, but I figured I'd see if there is interest before going too far into the details.
Any chances that this feature lands someday in go-yaml? The YAML decoder seems to know already the line and column number of nodes but this information doesn't get exposed yet.
@clarete @yobert Just out of curiosity, have you found over the time any alternative solutions for reading out the line and column numbers?
Related issues:
https://github.com/go-yaml/yaml/issues/343
https://github.com/go-yaml/yaml/pull/126
@subesokun The upcoming and in-progress yaml.v3 will offer an intermediate representation that will allow that kind of detail to be exposed.
@niemeyer That are awesome news! Will there be any example that demonstrates how these line and column numbers can be read out?
It will be documented and pretty straightforward.
Any chance to have an early peek at v3? I'd like to take it for a spin and give feedback.
I would also love to see this implementation. I'm working on an application for which this feature is critical. I've even considered forking an adding it myself.
Why not push the v3 branch to GitHub? The community is happy to help with your effort!
A very long time has passed since this issue has been opened and since the v3 release was teased.
@niemeyer Is this work continuing at all? If not, would you be open to having other members of the community take over this work?
Seems like this is possible here:
https://godoc.org/gopkg.in/yaml.v3#Node
@dprotaso Yes, that does work, and I've managed to implement full line/column number error messages using a Node
-based lookup system. I believe this issue can be closed.
Yeah, that's sorted in v3 indeed.
With API v3, here's how to do it:
import (
"fmt"
"gopkg.in/yaml.v3"
)
var node yaml.Node
err := yaml.Unmarshal([]byte(testYaml), &node)
Also see:
/node_test.go:2270
, TestNodeRoundtrip in the decode caseFull minimal example:
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
var testYaml = `
a: Easy!
b:
c: 2
d: [3, 4]
`
func main() {
var node yaml.Node
err := yaml.Unmarshal([]byte(testYaml), &node)
if err != nil {
panic(err)
}
fmt.Printf("Result node: %s", node)
// Result node: {%!s(yaml.Kind=1) %!s(yaml.Style=0) %!s(*yaml.Node=<nil>) [%!s(*yaml.Node=&{4 0 !!map <nil> [0xc00009a5a0 0xc00009a640 0xc00009a6e0 0xc00009a780] 2 1})] %!s(int=2) %!s(int=1)}
}
Most helpful comment
I would also love to see this implementation. I'm working on an application for which this feature is critical. I've even considered forking an adding it myself.
Why not push the v3 branch to GitHub? The community is happy to help with your effort!