Skip to content

Commit 30a5c79

Browse files
committed
internal/encoding/yaml: decode empty inputs as "null"
The YAML 1.2.2 spec says: YAML allows the node content to be omitted in many cases. Nodes with empty content are interpreted as if they were plain scalars with an empty value. Such nodes are commonly resolved to a “null” value. And indeed, multiple of their examples show empty inputs decoding as null, such as --- --- # Empty ... resulting in two "null" values. The old YAML decoder seems to have wanted this behavior for years, but as my TODO on the new decoder explains, it returned a "null" literal alongside an io.EOF error, and the consumer discarded the literal. This resolves the mishandling of empty inputs, which reached io.EOF directly without providing any decoded CUE expression, and also resolves all panics that this caused in cmd/cue. Fixes #1790. Fixes #2714. Fixes #3337. Closes #1807. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Iaa3356c7eee5e84dc51386a057a470138e1ac3ce Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198876 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent cd689ef commit 30a5c79

File tree

5 files changed

+13
-14
lines changed

5 files changed

+13
-14
lines changed

cmd/cue/cmd/testdata/script/encoding_empty.txtar

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ cmp stdout as-cue.stdout
1414
stderr 'unexpected end of JSON input'
1515
! exec cue export jsonl: empty
1616
stderr '^panic: '
17-
! exec cue export yaml: empty
18-
stderr '^panic: '
17+
exec cue export yaml: empty
18+
cmp stdout as-yaml.stdout
1919
exec cue export toml: empty
2020
cmp stdout as-toml.stdout
2121

@@ -27,8 +27,8 @@ cmp stdout as-cue.stdout
2727
stderr 'unexpected end of JSON input'
2828
! exec cue export jsonl: newlines
2929
stderr '^panic: '
30-
! exec cue export yaml: newlines
31-
stderr '^panic: '
30+
exec cue export yaml: newlines
31+
cmp stdout as-yaml.stdout
3232
exec cue export toml: newlines
3333
cmp stdout as-toml.stdout
3434

@@ -41,13 +41,15 @@ cmp stdout as-cue.stdout
4141
stderr 'invalid character ./. looking for beginning of value'
4242
! exec cue export jsonl: slash-comments
4343
stderr 'invalid character ./. looking for beginning of value'
44-
! exec cue export yaml: hash-comments
45-
stderr '^panic: '
44+
exec cue export yaml: hash-comments
45+
cmp stdout as-yaml.stdout
4646
exec cue export toml: hash-comments
4747
cmp stdout as-toml.stdout
4848

4949
-- as-cue.stdout --
5050
{}
51+
-- as-yaml.stdout --
52+
null
5153
-- as-toml.stdout --
5254
{}
5355
-- empty --

cmd/cue/cmd/testdata/script/import_list.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ module: "test.example"
5858
language: version: "v0.9.0"
5959
-- issue2721/empty.yaml --
6060
-- issue2721/empty.cue.golden --
61-
[]
61+
[null]

internal/encoding/yaml/decode.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ func (d *decoder) Decode() (ast.Expr, error) {
120120
// If the input is empty, we produce a single null literal with EOF.
121121
// Note that when the input contains "---", we get an empty document
122122
// with a null scalar value inside instead.
123-
//
124-
// TODO(mvdan): the old decoder seemingly intended to do this,
125-
// but returned a "null" literal with io.EOF, which consumers ignored.
126-
if false && !d.yamlNonEmpty {
123+
if !d.yamlNonEmpty {
127124
return &ast.BasicLit{
128125
Kind: token.NULL,
129126
Value: "null",

internal/encoding/yaml/decode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var unmarshalTests = []struct {
4747
}{
4848
{
4949
"",
50-
"",
50+
"null",
5151
},
5252
{
5353
"{}",
@@ -862,7 +862,7 @@ var decoderTests = []struct {
862862
want string
863863
}{{
864864
"",
865-
"",
865+
"null",
866866
}, {
867867
"a: b",
868868
`a: "b"`,

pkg/encoding/yaml/testdata/gen.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ unmarshalStream: {
7979
}, {
8080
b: 2
8181
}]
82-
empty: []
82+
empty: [null]
8383
nums: [1, 2]
8484
null1: [1, null, 2]
8585
null2: [1, null, 2]

0 commit comments

Comments
 (0)