better list filling

master
Jordan Orelli 10 years ago
parent 7817994f07
commit 5c970f1c63

@ -28,7 +28,7 @@ func (l List) fillValue(v reflect.Value) error {
}
default:
sv := reflect.ValueOf(item)
if !dv.Type().AssignableTo(sv.Type()) {
if !sv.Type().AssignableTo(dv.Type()) {
return fmt.Errorf("unable to assign element %d: source type %v is not assignable to destination type %v", idx, sv.Type(), dv.Type())
}
dv.Set(sv)

@ -15,6 +15,7 @@ func TestFillList(t *testing.T) {
]
numbers: [1 1 2 3 5 8 13]
names: @cool_names
misc: [one; 2.1 {x: 5.0 y: 1.0}]
`)
if err != nil {
t.Error(err)
@ -25,9 +26,10 @@ func TestFillList(t *testing.T) {
Label string `name: label; required: true`
}
var config struct {
Servers []server `name: servers`
Numbers []int `name: numbers`
Names []string `name: names`
Servers []server `name: servers`
Numbers []int `name: numbers`
Names []string `name: names`
Misc []interface{} `name: misc`
}
if err := doc.Fill(&config); err != nil {
t.Error(err)
@ -46,4 +48,8 @@ func TestFillList(t *testing.T) {
if !reflect.DeepEqual(config.Names, []string{"larry", "curly", "moe"}) {
t.Errorf("bad names: %v", config.Names)
}
misc := []interface{}{"one", 2.1, map[string]interface{}{"x": 5.0, "y": 1.0}}
if !reflect.DeepEqual(config.Misc, misc) {
t.Errorf("bad misc: %v", config.Misc)
}
}

@ -149,6 +149,10 @@ func (o *Object) fillValue(dv reflect.Value) error {
}
dv = dv.Elem()
default:
if reflect.TypeOf(o.items).AssignableTo(dv.Type()) {
dv.Set(reflect.ValueOf(o.items))
return nil
}
return fmt.Errorf("moon object can only fillValue to a struct value, saw %v (%v)", dv.Type(), dv.Kind())
}

Loading…
Cancel
Save