Skip to content

Commit

Permalink
internal/encoding/yaml: add underlying error to "cannot decode V as T"
Browse files Browse the repository at this point in the history
This can be useful context that would otherwise be lost.
Some of these errors may be a bit repetitive, such as repeating
the input YAML string, but that's fine.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ia95c25566288a4f1b7403a7b1fec1fe1e13ae92e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195045
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed May 23, 2024
1 parent a6a7002 commit 2adcaa6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions internal/encoding/yaml/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,10 @@ func (d *decoder) scalar(yn *yaml.Node) (ast.Expr, error) {
// We make the assumption that any valid YAML integer literal will be a valid
// CUE integer literal as well, with the only exception of octal numbers above.
// Note that `!!int 123.456` is not allowed.
if err := literal.ParseNum(value, &info); err != nil || !info.IsInt() {
return nil, d.posErrorf(yn, "cannot decode %q as %s", value, yn.ShortTag())
if err := literal.ParseNum(value, &info); err != nil {
return nil, d.posErrorf(yn, "cannot decode %q as %s: %v", value, tag, err)
} else if !info.IsInt() {
return nil, d.posErrorf(yn, "cannot decode %q as %s: not a literal number", value, tag)
}
return d.makeNum(yn, value, token.INT), nil

Expand All @@ -563,7 +565,7 @@ func (d *decoder) scalar(yn *yaml.Node) (ast.Expr, error) {
// CUE float literal as well, with the only exception of Inf/NaN above.
// Note that `!!float 123` is allowed.
if err := literal.ParseNum(value, &info); err != nil {
return nil, d.posErrorf(yn, "cannot decode %q as %s", value, yn.ShortTag())
return nil, d.posErrorf(yn, "cannot decode %q as %s: %v", value, tag, err)
}
// If the decoded YAML scalar was explicitly or implicitly a float,
// and the scalar literal looks like an integer,
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/yaml/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,9 @@ func TestUnmarshalNaN(t *testing.T) {
var unmarshalErrorTests = []struct {
data, error string
}{
{"\nv: !!float 'error'", `test.yaml:2: cannot decode "error" as !!float`},
{"\nv: !!int 'error'", `test.yaml:2: cannot decode "error" as !!int`},
{"\nv: !!int 123.456", `test.yaml:2: cannot decode "123.456" as !!int`},
{"\nv: !!float 'error'", `test.yaml:2: cannot decode "error" as !!float: illegal number start "error"`},
{"\nv: !!int 'error'", `test.yaml:2: cannot decode "error" as !!int: illegal number start "error"`},
{"\nv: !!int 123.456", `test.yaml:2: cannot decode "123.456" as !!int: not a literal number`},
{"v: [A,", "test.yaml:1: did not find expected node content"},
{"v:\n- [A,", "test.yaml:2: did not find expected node content"},
{"a:\n- b: *,", "test.yaml:2: did not find expected alphabetic or numeric character"},
Expand Down

0 comments on commit 2adcaa6

Please sign in to comment.