Unbreak goyaml after interface mutability fix in reflect.

R=rog
CC=
https://codereview.appspot.com/5784060
This commit is contained in:
Gustavo Niemeyer 2012-03-08 19:28:38 -03:00
Родитель 54c736c86c 722ff6b958
Коммит 5515099aac
3 изменённых файлов: 20 добавлений и 9 удалений

1
.lbox Normal file
Просмотреть файл

@ -0,0 +1 @@
propose -cr -for=lp:goyaml

Просмотреть файл

@ -375,11 +375,11 @@ func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
if set := d.setter("!!seq", &out, &good); set != nil { if set := d.setter("!!seq", &out, &good); set != nil {
defer set() defer set()
} }
var iface reflect.Value
if out.Kind() == reflect.Interface { if out.Kind() == reflect.Interface {
// No type hints. Will have to use a generic sequence. // No type hints. Will have to use a generic sequence.
iface := out iface = out
out = settableValueOf(make([]interface{}, 0)) out = settableValueOf(make([]interface{}, 0))
iface.Set(out)
} }
if out.Kind() != reflect.Slice { if out.Kind() != reflect.Slice {
@ -394,6 +394,9 @@ func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
out.Set(reflect.Append(out, e)) out.Set(reflect.Append(out, e))
} }
} }
if iface.IsValid() {
iface.Set(out)
}
return true return true
} }

Просмотреть файл

@ -41,7 +41,7 @@ var unmarshalTests = []struct {
{"fixed: 685_230.15", map[string]interface{}{"fixed": 685230.15}}, {"fixed: 685_230.15", map[string]interface{}{"fixed": 685230.15}},
//{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported
{"neginf: -.inf", map[string]interface{}{"neginf": math.Inf(-1)}}, {"neginf: -.inf", map[string]interface{}{"neginf": math.Inf(-1)}},
{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails.
{"fixed: 685_230.15", map[string]float64{"fixed": 685230.15}}, {"fixed: 685_230.15", map[string]float64{"fixed": 685230.15}},
// Bools from spec // Bools from spec
@ -136,11 +136,18 @@ func (s *S) TestUnmarshal(c *C) {
value = pv.Interface() value = pv.Interface()
} }
err := goyaml.Unmarshal([]byte(item.data), value) err := goyaml.Unmarshal([]byte(item.data), value)
c.Assert(err, IsNil, Bug("Item #%d", i)) c.Assert(err, IsNil, Commentf("Item #%d", i))
c.Assert(value, Equals, item.value) c.Assert(value, DeepEquals, item.value)
} }
} }
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)
}
var unmarshalErrorTests = []struct { var unmarshalErrorTests = []struct {
data, error string data, error string
}{ }{
@ -155,7 +162,7 @@ func (s *S) TestUnmarshalErrors(c *C) {
for _, item := range unmarshalErrorTests { for _, item := range unmarshalErrorTests {
var value interface{} var value interface{}
err := goyaml.Unmarshal([]byte(item.data), &value) err := goyaml.Unmarshal([]byte(item.data), &value)
c.Assert(err, ErrorMatches, item.error, Bug("Partial unmarshal: %#v", value)) c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
} }
} }
@ -198,9 +205,9 @@ func (s *S) TestUnmarshalWithSetter(c *C) {
err := goyaml.Unmarshal([]byte(item.data), obj) err := goyaml.Unmarshal([]byte(item.data), obj)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(obj.Field, NotNil, c.Assert(obj.Field, NotNil,
Bug("Pointer not initialized (%#v)", item.value)) Commentf("Pointer not initialized (%#v)", item.value))
c.Assert(obj.Field.tag, Equals, item.tag) c.Assert(obj.Field.tag, Equals, item.tag)
c.Assert(obj.Field.value, Equals, item.value) c.Assert(obj.Field.value, DeepEquals, item.value)
} }
} }
@ -211,7 +218,7 @@ func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) {
c.Assert(obj.tag, Equals, setterTests[0].tag) c.Assert(obj.tag, Equals, setterTests[0].tag)
value, ok := obj.value.(map[interface{}]interface{}) value, ok := obj.value.(map[interface{}]interface{})
c.Assert(ok, Equals, true) c.Assert(ok, Equals, true)
c.Assert(value["_"], Equals, setterTests[0].value) c.Assert(value["_"], DeepEquals, setterTests[0].value)
} }
func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {