better list filling

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

@ -28,7 +28,7 @@ func (l List) fillValue(v reflect.Value) error {
} }
default: default:
sv := reflect.ValueOf(item) 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()) 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) dv.Set(sv)

@ -15,6 +15,7 @@ func TestFillList(t *testing.T) {
] ]
numbers: [1 1 2 3 5 8 13] numbers: [1 1 2 3 5 8 13]
names: @cool_names names: @cool_names
misc: [one; 2.1 {x: 5.0 y: 1.0}]
`) `)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -28,6 +29,7 @@ func TestFillList(t *testing.T) {
Servers []server `name: servers` Servers []server `name: servers`
Numbers []int `name: numbers` Numbers []int `name: numbers`
Names []string `name: names` Names []string `name: names`
Misc []interface{} `name: misc`
} }
if err := doc.Fill(&config); err != nil { if err := doc.Fill(&config); err != nil {
t.Error(err) t.Error(err)
@ -46,4 +48,8 @@ func TestFillList(t *testing.T) {
if !reflect.DeepEqual(config.Names, []string{"larry", "curly", "moe"}) { if !reflect.DeepEqual(config.Names, []string{"larry", "curly", "moe"}) {
t.Errorf("bad names: %v", config.Names) 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() dv = dv.Elem()
default: 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()) return fmt.Errorf("moon object can only fillValue to a struct value, saw %v (%v)", dv.Type(), dv.Kind())
} }

Loading…
Cancel
Save