internal/yaml: import null value properly
When import this yaml file:
```
a:
b: c
```
We'll get
```
a:
null, b: "c"
```
Because we don't handle null value which take no space correctly. The `ValuePos` of the null value is `Newline` instead of `Blank` and the the `ValuePos` of field `b` is `Blank` instead of `Newline`
This PR properly set `ValuePos` to `NoPos` + `Blank` for null value.
Closes #196
https://github.com/cuelang/cue/pull/196
GitOrigin-RevId: ac9c3cd6bc398b766cd93546163d1cd1bd4a3eee
Change-Id: I0ac5edfa195d8ee6d0cb75394567cc7722911f60
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4260
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/internal/third_party/yaml/decode.go b/internal/third_party/yaml/decode.go
index ffcbdb6..dbafa23 100644
--- a/internal/third_party/yaml/decode.go
+++ b/internal/third_party/yaml/decode.go
@@ -395,6 +395,9 @@
}
func (d *decoder) start(n *node) token.Pos {
+ if n.startPos == n.endPos {
+ return token.NoPos
+ }
return d.pos(n.startPos)
}
@@ -444,7 +447,7 @@
}
if resolved == nil {
return &ast.BasicLit{
- ValuePos: d.start(n),
+ ValuePos: d.start(n).WithRel(token.Blank),
Kind: token.NULL,
Value: "null",
}
@@ -517,7 +520,7 @@
case yaml_NULL_TAG:
return &ast.BasicLit{
- ValuePos: d.start(n),
+ ValuePos: d.start(n).WithRel(token.Blank),
Kind: token.NULL,
Value: "null",
}
diff --git a/internal/third_party/yaml/decode_test.go b/internal/third_party/yaml/decode_test.go
index 813166b..cd7f242 100644
--- a/internal/third_party/yaml/decode_test.go
+++ b/internal/third_party/yaml/decode_test.go
@@ -153,6 +153,11 @@
}, {
"~: null key",
`"~": "null key"`,
+ }, {
+ `empty:
+apple: "newline"`,
+ `empty: null
+apple: "newline"`,
},
// Flow sequence