I've recently switched to https://github.com/goccy/go-yaml and am very happy with it so far. I've not tried it on your use case.
Regards, Tom On Friday, February 28, 2025 at 12:34:29 AM UTC+1 David Karr wrote: > I wrote some code to load a yaml file and do some work with the resulting > data. I'm using the "gopkg.in/yaml.v2" package for it. This has been > working fine for properly formatted YAML. However, today I discovered that > a slightly misformatted YAML file is being happily loaded by this code, > without throwing any error, but also making sort of odd decisions on what > data to actually load, although seeing what it did I suppose that's > debatable. > > In my suspect yaml file, I have something like this: > > stuff: > keys: > - key1 > - key2 > - key3 > > Note the incorrect indentation for "key2" and "key3". > > When I load this with code like this: > > err = yaml.Unmarshal(configFile, &config) > if err != nil { > log.Fatalf("Failed to parse configuration file: %v", err) > } > > This unexpectedly does NOT fail. However, it produces a "keys" list with > only one entry, with the following value: > > key1 - key2 - key3 > > I can sort of see why it would make that decision. Is the lesson here that > YAML is intended to be easily readable, but not easily writable? I see that > there are some command-line tools for consistent formatting of YAML, but I > need this done in code, but I think I'd rather just fail if the formatting > is inconsistent. Is there any kind of a "strict yaml parser" that will > notice things like this? > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/640a43c6-8f1c-41ca-99f4-efa498164fe0n%40googlegroups.com.