Lots of things, mainly models and CSV parser
This commit is contained in:
parent
d63d1a4513
commit
901b42d801
4 changed files with 147 additions and 1 deletions
42
parser/csv_parser.go
Normal file
42
parser/csv_parser.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue