func (s *S) TestUnmarshalErrors(c *C) { for _, item := range unmarshalErrorTests { var value interface{} err := goyaml.Unmarshal([]byte(item.data), &value) c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value)) } }
func (s *S) TestUnmarshal(c *C) { for i, item := range unmarshalTests { t := reflect.ValueOf(item.value).Type() var value interface{} switch t.Kind() { case reflect.Map: value = reflect.MakeMap(t).Interface() case reflect.String: t := reflect.ValueOf(item.value).Type() v := reflect.New(t) value = v.Interface() default: pt := reflect.ValueOf(item.value).Type() pv := reflect.New(pt.Elem()) value = pv.Interface() } err := goyaml.Unmarshal([]byte(item.data), value) c.Assert(err, IsNil, Commentf("Item #%d", i)) if t.Kind() == reflect.String { c.Assert(*value.(*string), Equals, item.value, Commentf("Item #%d", i)) } else { c.Assert(value, DeepEquals, item.value, Commentf("Item #%d", i)) } } }
func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) { obj := &typeWithSetter{} err := goyaml.Unmarshal([]byte(setterTests[0].data), obj) c.Assert(err, IsNil) c.Assert(obj.tag, Equals, setterTests[0].tag) value, ok := obj.value.(map[interface{}]interface{}) c.Assert(ok, Equals, true) c.Assert(value["_"], DeepEquals, setterTests[0].value) }
func (s *S) TestUnmarshalWithSetter(c *C) { for _, item := range setterTests { obj := &typeWithSetterField{} err := goyaml.Unmarshal([]byte(item.data), obj) c.Assert(err, IsNil) c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value)) c.Assert(obj.Field.tag, Equals, item.tag) c.Assert(obj.Field.value, DeepEquals, item.value) } }
// Read detects and parses the front matter data, and returns the // remaining contents. If no front matter is found, the entire // file contents are returned. For details on the frontmatter // parameter, please see the launchpad.net/goyaml package. func Read(data []byte, frontmatter interface{}) (content []byte, err error) { r := bytes.NewBuffer(data) // eat away starting whitespace var ch rune = ' ' for unicode.IsSpace(ch) { ch, _, err = r.ReadRune() if err != nil { // file is just whitespace return []byte{}, nil } } r.UnreadRune() // check if first line is --- line, err := r.ReadString('\n') if err != nil && err != io.EOF { return nil, err } if strings.TrimSpace(line) != "---" { // no front matter, just content return data, nil } yamlStart := len(data) - r.Len() yamlEnd := yamlStart for { line, err = r.ReadString('\n') if err != nil { if err == io.EOF { return data, nil } return nil, err } if strings.TrimSpace(line) == "---" { yamlEnd = len(data) - r.Len() break } } err = goyaml.Unmarshal(data[yamlStart:yamlEnd], frontmatter) if err != nil { return nil, err } content = data[yamlEnd:] err = nil return }
func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { setterResult[2] = false setterResult[4] = false defer func() { delete(setterResult, 2) delete(setterResult, 4) }() m := map[string]*typeWithSetter{} data := "{abc: 1, def: 2, ghi: 3, jkl: 4}" err := goyaml.Unmarshal([]byte(data), m) c.Assert(err, IsNil) c.Assert(m["abc"], NotNil) c.Assert(m["def"], IsNil) c.Assert(m["ghi"], NotNil) c.Assert(m["jkl"], IsNil) c.Assert(m["abc"].value, Equals, 1) c.Assert(m["ghi"].value, Equals, 3) }
func (s *S) TestUnmarshalNaN(c *C) { value := map[string]interface{}{} err := goyaml.Unmarshal([]byte("notanum: .NaN"), &value) c.Assert(err, IsNil) c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true) }