43 lines
759 B
Go
43 lines
759 B
Go
|
package parser
|
||
|
|
||
|
import (
|
||
|
"encoding/csv"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
|
||
|
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
||
|
)
|
||
|
|
||
|
func ParseCSV(path string) ([]*model.Birthday, error) {
|
||
|
f, err := os.Open(path)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
reader := csv.NewReader(f)
|
||
|
|
||
|
birthdays := []*model.Birthday{}
|
||
|
lineno := 0
|
||
|
for {
|
||
|
lineno++
|
||
|
record, err := reader.Read()
|
||
|
if errors.Is(err, io.EOF) {
|
||
|
break
|
||
|
}
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error reading lineno %d: %w", lineno, err)
|
||
|
}
|
||
|
|
||
|
birthday, err := model.NewBirthdayFromRecord(record)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error parsing record for line %d into birthday: %w", lineno, err)
|
||
|
}
|
||
|
birthdays = append(birthdays, birthday)
|
||
|
}
|
||
|
|
||
|
return birthdays, nil
|
||
|
}
|