-
Notifications
You must be signed in to change notification settings - Fork 41
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
Populating strings with values that could be parsed as booleans mutates the result #74
Comments
Good find. The underlying yaml library does the right thing, which likely means a bug in config code somewhere.
|
It looks like at the core is the way yaml library handles package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
func main() {
var i interface{}
err := yaml.Unmarshal([]byte(`yes`), &i)
if err != nil {
log.Panic(err)
}
fmt.Println("interface{} foo", i)
}
// Output: interface{} foo true The line in question: https://github.com/uber-go/config/blob/master/yaml.go#L51 |
YAML 1.1 says:
Related lines in yaml library: {true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}}, yaml_BOOL_TAG = "tag:yaml.org,2002:bool" // The tag !!bool with the values: true and false. So I think this can be work around by adding an explicit package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
func main() {
var i interface{}
err := yaml.Unmarshal([]byte(`!!str yes`), &i)
if err != nil {
log.Panic(err)
}
fmt.Println("interface{} foo", i)
}
// Output: interface{} foo yes |
As @xuyang2 points out, the YAML specification outlines a number of different ways to encode Boolean values. The way these strings are treated depends on the structure they're unmarshalled into - unmarshalling into a string keeps the YAML as-is, while unmarshalling into These special representations of bools cause particular trouble for this package because we need to merge multiple YAML files into a single file. Post-v1.1, we do this decoding into In principle, we could fix this by writing a YAML parser from scratch and merging ASTs without unmarshalling. I doubt that we'll ever spend the time required to actually do this. |
Documented this more clearly in #94. I'm going to leave this open in case someone comes up with a nice fix (or feels like writing a fully-compliant YAML parser that lets us merge without unmarshalling). |
This prints
true
, notyes
as I'd expect.The text was updated successfully, but these errors were encountered: