Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix (v3): Marshal yaml 1.1 bool strings as explicit strings #490

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,9 @@ func (d *decoder) scalar(n *Node, out reflect.Value) bool {
case string:
// This offers some compatibility with the 1.1 spec (https://yaml.org/type/bool.html).
// It only works if explicitly attempting to unmarshal into a typed bool value.
switch resolved {
case "y", "Y", "yes", "Yes", "YES", "on", "On", "ON":
out.SetBool(true)
return true
case "n", "N", "no", "No", "NO", "off", "Off", "OFF":
out.SetBool(false)
val, found := boolMap[resolved]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this still decode y as true?

if found {
out.SetBool(val)
return true
}
}
Expand Down
5 changes: 5 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ func (e *encoder) stringv(tag string, in reflect.Value) {
// there's no need to quote it.
rtag, _ := resolve("", s)
canUsePlain = rtag == strTag && !isBase60Float(s)

// Check to see if this is a v1.1 yaml bool string
// if it is, require quotes to preserve backwards compatibility
_, is11BoolString := boolMap[s]
canUsePlain = !is11BoolString && canUsePlain
}
// Note: it's possible for user code to emit invalid YAML
// if they explicitly specify a tag and a string containing
Expand Down
33 changes: 33 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,39 @@ var marshalTests = []struct {
map[string]string{"a": "\tB\n\tC\n"},
"a: |\n \tB\n \tC\n",
},
// yaml 1.1 bool strings should be marshalled with quotes to preserve compatibility
{
map[string]string{"a": "y", "b": "n"},
"a: \"y\"\nb: \"n\"\n",
},
{
map[string]string{"a": "Y", "b": "N"},
"a: \"Y\"\nb: \"N\"\n",
},
{
map[string]string{"a": "yes", "b": "no"},
"a: \"yes\"\nb: \"no\"\n",
},
{
map[string]string{"a": "Yes", "b": "No"},
"a: \"Yes\"\nb: \"No\"\n",
},
{
map[string]string{"a": "YES", "b": "NO"},
"a: \"YES\"\nb: \"NO\"\n",
},
{
map[string]string{"a": "on", "b": "off"},
"a: \"on\"\nb: \"off\"\n",
},
{
map[string]string{"a": "On", "b": "Off"},
"a: \"On\"\nb: \"Off\"\n",
},
{
map[string]string{"a": "ON", "b": "OFF"},
"a: \"ON\"\nb: \"OFF\"\n",
},
}

func (s *S) TestMarshal(c *C) {
Expand Down
21 changes: 21 additions & 0 deletions resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type resolveMapItem struct {

var resolveTable = make([]byte, 256)
var resolveMap = make(map[string]resolveMapItem)
var boolMap = make(map[string]bool)

func init() {
t := resolveTable
Expand Down Expand Up @@ -65,6 +66,26 @@ func init() {
m[s] = resolveMapItem{item.v, item.tag}
}
}

// the remaining bool values from the 1.1 spec (https://yaml.org/type/bool.html)
boolMap = map[string]bool{
"y": true,
"Y": true,
"yes": true,
"Yes": true,
"YES": true,
"on": true,
"On": true,
"ON": true,
"n": false,
"N": false,
"no": false,
"No": false,
"NO": false,
"off": false,
"Off": false,
"OFF": false,
}
}

const (
Expand Down