don't panic if we can't set a field

fix unit tests
master
Jordan Orelli 10 years ago
parent 9a966a37f5
commit c6cbb01fa2

@ -116,6 +116,7 @@ func (d *Doc) Get(path string, dest interface{}) error {
// other type for dest will result in an error. Please see the Parse // other type for dest will result in an error. Please see the Parse
// documentation for a description of how the values will be filled. // documentation for a description of how the values will be filled.
func (d *Doc) Fill(dest interface{}) error { func (d *Doc) Fill(dest interface{}) error {
// dt = destination type
dt := reflect.TypeOf(dest) dt := reflect.TypeOf(dest)
if dt.Kind() != reflect.Ptr { if dt.Kind() != reflect.Ptr {
return fmt.Errorf("destination is of type %v; a pointer type is required", dt) return fmt.Errorf("destination is of type %v; a pointer type is required", dt)
@ -126,11 +127,16 @@ func (d *Doc) Fill(dest interface{}) error {
return fmt.Errorf("unable to gather requirements: %s", err) return fmt.Errorf("unable to gather requirements: %s", err)
} }
// dv = destination value
dv := reflect.ValueOf(dest).Elem() dv := reflect.ValueOf(dest).Elem()
for fname, req := range reqs { for fname, req := range reqs {
// fv = field value
fv := dv.FieldByName(fname) fv := dv.FieldByName(fname)
v, ok := d.items[req.name] v, ok := d.items[req.name]
if ok { if ok {
if !fv.Type().AssignableTo(reflect.TypeOf(v)) {
return fmt.Errorf("unable to assign field %s: source type %v is not assignable to destination type %v", req.name, fv.Type(), reflect.TypeOf(v))
}
fv.Set(reflect.ValueOf(v)) fv.Set(reflect.ValueOf(v))
} else { } else {
if req.required { if req.required {

@ -34,13 +34,13 @@ root:
dur_6 dur_6
value: value:
dur: dur:
18us 18µs
assign: assign:
name: name:
dur_7 dur_7
value: value:
dur: dur:
45us 45µs
assign: assign:
name: name:
dur_8 dur_8

Loading…
Cancel
Save