From c6cbb01fa2a84e373cc3e3eac6b94a31efa6aca5 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 13 Jul 2015 14:02:53 -0400 Subject: [PATCH] don't panic if we can't set a field fix unit tests --- doc.go | 6 ++++++ tests/parse/08.out | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc.go b/doc.go index ab589ee..ace8a06 100644 --- a/doc.go +++ b/doc.go @@ -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 { diff --git a/tests/parse/08.out b/tests/parse/08.out index 50738e3..a9f26c5 100644 --- a/tests/parse/08.out +++ b/tests/parse/08.out @@ -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