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
// documentation for a description of how the values will be filled.
func (d *Doc) Fill(dest interface{}) error {
// dt = destination type
dt := reflect.TypeOf(dest)
if dt.Kind() != reflect.Ptr {
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)
}
// dv = destination value
dv := reflect.ValueOf(dest).Elem()
for fname, req := range reqs {
// fv = field value
fv := dv.FieldByName(fname)
v, ok := d.items[req.name]
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))
} else {
if req.required {

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

Loading…
Cancel
Save