-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exposing line numbers in the API #108
Comments
The `linenum` and `colnum` flags were introduced to mark fields that will save the line number and column number of an element. The field should not really map to any field names on the YAML document, since the parser won't really save it. Once line or column number is saved to a field that carries the flags mentioned here, the parser is done with that field. Might fix go-yaml#108
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? |
@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: |
@dprotaso Yes, that does work, and I've managed to implement full line/column number error messages using a |
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:
Full 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)}
} |
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.
The text was updated successfully, but these errors were encountered: