struct with field name starting with `N` fails to read data
surajssd opened this issue · 1 comments
surajssd commented
Here is a code snippet that I am running:
$ cat non.go
package main
import (
"fmt"
"log"
"github.com/ghodss/yaml"
)
type C struct {
M int `yaml:"M"`
N int `yaml:"N"`
}
func main() {
d := []byte(`
M: 1
N: 2
`)
var c C
err := yaml.Unmarshal(d, &c)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%#v\n", c)
}
Once I run it:
$ go run non.go
main.C{M:1, N:0}
ChrisRx commented
http://yaml.org/type/bool.html
In YAML the key can be a value, and a boolean can represented with (n|N|y|Y). During parsing the type appears to be identifying N as a boolean type so when it compares the tag to the key being parsed it is comparing "false" to "N". I believe you can fix this by quoting your key in the file being parsed:
d := []byte(`
M: 1
"N": 2
`)