Skip to content

Commit

Permalink
Bug fix: Missing csv header when neither of SelectColumns, ExpectHead…
Browse files Browse the repository at this point in the history
…er, AssumeHeader is called on csvplus.File object.
  • Loading branch information
maxim2266 committed Oct 17, 2017
1 parent 6f49b32 commit 95be18a
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions csvplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,9 @@ type File struct {
// csv.Reader settings.
func FromFile(name string) *File {
return &File{
name: name,
delimiter: ',',
name: name,
delimiter: ',',
headerFromFile: true,
}
}

Expand Down Expand Up @@ -1029,14 +1030,16 @@ func (s *File) Iterate(fn RowFunc) error {
reader.FieldsPerRecord = s.numFields

// header
header := s.header
var header map[string]int

if s.headerFromFile {
if header, err = s.makeHeader(reader); err != nil {
return s.mapError(err, lineNo)
}

lineNo++
} else {
header = s.header
}

// iteration
Expand Down Expand Up @@ -1079,6 +1082,21 @@ func (s *File) makeHeader(reader *csv.Reader) (map[string]int, error) {
return nil, err
}

if len(line) == 0 {
return nil, errors.New("Empty header")
}

if len(s.header) == 0 { // the header is not specified - get it from the first line
header := make(map[string]int, len(line))

for i, name := range line {
header[name] = i
}

return header, nil
}

// check and update the specified header
header := make(map[string]int, len(s.header))

// fix column indices
Expand Down

0 comments on commit 95be18a

Please sign in to comment.