示例#1
0
// Decode decodes the current row into dest struct
func (r *Reader) Decode(dest interface{}) error {
	if r.err != nil {
		return r.err
	}
	if len(r.row) == 0 {
		return ErrEmptyRow
	}

	info, err := structinfo.Extract(dest, "tsv")
	if err != nil {
		return err
	}
	v := reflect.ValueOf(dest)
	for v.Kind() == reflect.Ptr {
		v = v.Elem()
	}
	for _, fi := range info.Fields.Exported {
		if fi.Tag == "-" {
			continue
		}
		f := v.Field(fi.Index)
		if f.CanSet() {
			col := r.getFieldCol(fi.Tag)
			if col >= 0 {
				if err = setVal(r.row[col], f); err != nil {
					return err
				}
			}
		}
	}

	return nil
}
示例#2
0
func TestExtract(t *testing.T) {
	for i, test := range extractTests {
		info, err := structinfo.Extract(test.in, test.tag)
		if !reflect.DeepEqual(info, test.out) {
			t.Errorf("#%d: mismatch\nexpecting:\t%#+v\ngot:\t\t%#+v", i, test.out, info)
		}
		if err != test.err {
			t.Errorf("#%d: error mismatch\nexpecting: %+v\ngot: %+v", i, test.err, err)
		}
	}
}