Skip to content

Commit eba3435

Browse files
committed
yaml gotchas
1 parent 829c0e7 commit eba3435

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
Roughly based on JSON patch: https://tools.ietf.org/html/rfc6902.
44

5-
[Usage examples](docs/examples.md)
5+
- [Usage examples](docs/examples.md)
6+
- [Go YAML gotchas](docs/go-yaml.md)

docs/go-yaml.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Go YAML
2+
3+
Tests: [patch/yaml_compat_test.go](patch/yaml_compat_test.go)
4+
5+
- Use `!!str ""` instead of `""`

patch/yaml_compat_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package patch_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
"gopkg.in/yaml.v2"
7+
8+
. "github.com/cppforlife/go-patch/patch"
9+
)
10+
11+
var _ = Describe("YAML compatibility", func() {
12+
Describe("empty string", func() {
13+
It("[WORKAROUND] works serializing empty strings", func() {
14+
str := `
15+
- type: replace
16+
path: /instance_groups/name=cloud_controller/instances
17+
value: !!str ""
18+
`
19+
20+
var opDefs []OpDefinition
21+
22+
err := yaml.Unmarshal([]byte(str), &opDefs)
23+
Expect(err).ToNot(HaveOccurred())
24+
25+
val := opDefs[0].Value
26+
Expect((*val).(string)).To(Equal(""))
27+
})
28+
29+
It("[PORBLEM] does not works serializing empty strings", func() {
30+
str := `
31+
- type: replace
32+
path: /instance_groups/name=cloud_controller/instances
33+
value: ""
34+
`
35+
36+
var opDefs []OpDefinition
37+
38+
err := yaml.Unmarshal([]byte(str), &opDefs)
39+
Expect(err).To(HaveOccurred())
40+
Expect(err.Error()).To(ContainSubstring("cannot unmarshal !!str"))
41+
})
42+
})
43+
})

0 commit comments

Comments
 (0)