Read wraps csv.Reader.Read, creating a map of column name to field value. If the line has fewer columns than Reader.Columns, the map will not contain keys for these columns; thus we can distinguish between missing columns and columns with empty values. If the line has more columns than Reader.Column
()
| 29 | // thus we can distinguish between missing columns and columns with empty values. |
| 30 | // If the line has more columns than Reader.Columns, Reader.Read() ignores them. |
| 31 | func (r *Reader) Read() (record map[string]string, err error) { |
| 32 | var rawRecord []string |
| 33 | rawRecord, err = r.Reader.Read() |
| 34 | length := min(len(rawRecord), len(r.Columns)) |
| 35 | record = make(map[string]string) |
| 36 | for index := 0; index < length; index++ { |
| 37 | column := r.Columns[index] |
| 38 | if _, exists := record[column]; exists { |
| 39 | return nil, fmt.Errorf("Multiple indices with the same name '%s'", column) |
| 40 | } |
| 41 | record[column] = rawRecord[index] |
| 42 | } |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | // ReadAll reads all the remaining records from r. Each record is a map of column name to field value. |
| 47 | func (r *Reader) ReadAll() (records []map[string]string, err error) { |