Просмотр исходного кода

Fix detection of string keyed maps (#507)

Parker Zhou 6 лет назад
Родитель
Сommit
c8b7dadae5
2 измененных файлов с 13 добавлено и 3 удалено
  1. 1 1
      decode.go
  2. 12 2
      decode_test.go

+ 1 - 1
decode.go

@@ -795,7 +795,7 @@ func isStringMap(n *Node) bool {
 		return false
 	}
 	l := len(n.Content)
-	for i := 0; i < l; i++ {
+	for i := 0; i < l; i += 2 {
 		if n.Content[i].ShortTag() != strTag {
 			return false
 		}

+ 12 - 2
decode_test.go

@@ -18,6 +18,7 @@ package yaml_test
 import (
 	"bytes"
 	"errors"
+	"fmt"
 	"io"
 	"math"
 	"reflect"
@@ -942,7 +943,7 @@ var unmarshalErrorTests = []struct {
 	{"value: -", "yaml: block sequence entries are not allowed in this context"},
 	{"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
 	{"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
-	{"{{.}}", `yaml: invalid map key: map\[interface\ \{\}\]interface \{\}\{".":interface \{\}\(nil\)\}`},
+	{"{{.}}", `yaml: invalid map key: map\[string]interface \{\}\{".":interface \{\}\(nil\)\}`},
 	{"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`},
 	{"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
 	{
@@ -1102,7 +1103,7 @@ func (s *S) TestUnmarshalerWholeDocument(c *C) {
 	obj := &obsoleteUnmarshalerType{}
 	err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
 	c.Assert(err, IsNil)
-	value, ok := obj.value.(map[interface{}]interface{})
+	value, ok := obj.value.(map[string]interface{})
 	c.Assert(ok, Equals, true, Commentf("value: %#v", obj.value))
 	c.Assert(value["_"], DeepEquals, unmarshalerTests[0].value)
 }
@@ -1395,6 +1396,11 @@ func (s *S) TestMerge(c *C) {
 		"label": "center/big",
 	}
 
+	wantStringMap := make(map[string]interface{})
+	for k, v := range want {
+		wantStringMap[fmt.Sprintf("%v", k)] = v
+	}
+
 	var m map[interface{}]interface{}
 	err := yaml.Unmarshal([]byte(mergeTests), &m)
 	c.Assert(err, IsNil)
@@ -1402,6 +1408,10 @@ func (s *S) TestMerge(c *C) {
 		if name == "anchors" {
 			continue
 		}
+		if name == "plain" {
+			c.Assert(test, DeepEquals, wantStringMap, Commentf("test %q failed", name))
+			continue
+		}
 		c.Assert(test, DeepEquals, want, Commentf("test %q failed", name))
 	}
 }