Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/encoding/yaml: add the new Decoder
This implementation builds directly on top of gopkg.in/yaml.v3's Decoder as opposed to internal/third_party/yaml.Decoder, which forked go-yaml. There are a number of differences between the old and new decoders. Starting with the disadvantages or minor regressions per decode_test.go: * yaml.v3's Node only has each node's start position, lacking any end or closing token position. As such, empty lists or objects are always decoded into a single line even when the YAML was multi-line. * yaml.v3's Node does not record precise comment positions, so we must assume that any HeadComment lines immediately precede the YAML node. In some cases this isn't true, and we remove an empty line. * yaml.v3's errors do not always contain line information, such as the "unknown anchor" errors already covered in our test suite. There are a number of advantages or improvements, though: * We no longer lose inline or foot/trailing comments in some cases, although we continue to lose comments inside empty objects and lists. * Floating point scalars which are valid CUE integral literals are now decoded as `number & N` rather than `float & N`, as the latter simply fails, e.g. `float & 123` is an error. * The test case using simply `v: ! test` works as expected now. It should also be noted that an empty YAML input or document decodes as a nil ast.Expr in both the old and new decoder, even though the old decoder seemingly intended to decode empty input as a CUE null literal. It returned that null literal alongside an io.EOF error, which consumers such as internal/encoding and pkg/encoding/yaml ignored as they stopped looking at ast.Expr result values at the first error. Add a TODO to consider switching the behavior for empty inputs to produce a single "null" value instead as a separate change. And for now, continue with the simple design of never returning io.EOF alongside any non-nil valid result, to not make changes elsewhere. The new decoder is disabled by default and can be enabled via CUE_EXPERIMENT=yamlv3decoder - a follow-up CL will enable it by default. Updates #3027. Signed-off-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: I538725c5157924639d7084228d59e3f4e58c6d23 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1191897 Reviewed-by: Roger Peppe <rogpeppe@gmail.com> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
- Loading branch information