diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2022-01-23 01:29:32 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2022-01-23 01:29:32 +0100 |
commit | 4c8f2c6e5f04afde57c54065182d558dbaa63596 (patch) | |
tree | bfb0c7006565a96eae22d8be898011ae5120b855 | |
parent | d9358f84491879957e1db8de6220c74fe56f1d16 (diff) | |
download | engarde-importer-4c8f2c6e5f04afde57c54065182d558dbaa63596.tar.gz engarde-importer-4c8f2c6e5f04afde57c54065182d558dbaa63596.tar.bz2 engarde-importer-4c8f2c6e5f04afde57c54065182d558dbaa63596.zip |
First round of enguarde logic
Diffstat (limited to '')
-rw-r--r-- | main.go | 115 |
1 files changed, 110 insertions, 5 deletions
@@ -6,21 +6,122 @@ import ( "fmt" "log" "os" + "strings" "github.com/jszwec/csvutil" ) -type Participant struct { +type enguarde interface { + enguarde() (string, error) +} + +type Gender int + +const ( + GenderM = iota + GenderF +) + +func (g Gender) String() string { + switch g { + case GenderM: + return "M" + case GenderF: + return "F" + default: + return fmt.Sprintf("U%d", g) + } +} + +func (g Gender) enguarde() (string, error) { + switch g { + case GenderM: + return "masculin", nil + case GenderF: + return "feminin", nil + default: + return "", fmt.Errorf("unknown gender value '%d'", g) + } +} + +func (g *Gender) UnmarshalCSV(content []byte) error { + c := string(content) + switch c { + case "M": + *g = GenderM + case "F": + *g = GenderF + default: + return fmt.Errorf("unknown gender value '%s'", c) + } + + return nil +} + +type participant struct { LastName string `csv:"lastname"` FirstName string `csv:"firstname"` DateOfBirth string `csv:"dateofbirth"` - Gender string `csv:"gender"` + Gender Gender `csv:"gender"` Nation string `csv:"nation"` Region string `csv:"region"` Club string `csv:"club"` + id uint + club club +} + +type club struct { + name string + id uint } -func parseOphardtInput(fileName string) ([]Participant, error) { +func (p participant) enguarde() (string, error) { + name := strings.ToUpper(p.LastName) + gender, err := p.Gender.enguarde() + if err != nil { + return "", err + } + + return fmt.Sprintf(` +{[classe tireur] [sexe %s] [presence present] [carton_coach non] [status normal] + [medical non] [lateralite droite] [nom " %s "] [prenom " %s "] + [points 1.0] [date_oed "340"] [cle %d] [club1 %d]} +`, gender, name, p.FirstName, p.id, p.club.id), nil +} + +func (c club) enguarde() (string, error) { + return fmt.Sprintf(` +{[classe club] [nom "%s"] [modifie vrai] [date_oed "332"] [cle %d]}`, c.name, c.id), nil +} + +func prepareParticipants(participants []participant) { + clubs := map[string]club{} + var clubId, participantId uint + + for i := range participants { + p := &participants[i] + participantId++ + + p.id = participantId + + pClub := p.Club + if strings.Contains(pClub, ", ") { + pClub = strings.Split(pClub, ", ")[0] + } + + if c, ok := clubs[pClub]; ok { + p.club = c + } else { + clubId++ + + c = club{pClub, clubId} + p.club = c + clubs[p.Club] = c + } + } +} + +func parseOphardtInput(fileName string) ([]participant, error) { f, err := os.Open(fileName) if err != nil { return nil, fmt.Errorf("opening input file '%s': %w", fileName, err) @@ -40,11 +141,12 @@ func parseOphardtInput(fileName string) ([]Participant, error) { } dec.DisallowMissingColumns = true - var participants []Participant + var participants []participant if err = dec.Decode(&participants); err != nil { return nil, fmt.Errorf("decoding file '%s': %w", fileName, err) } + prepareParticipants(participants) return participants, nil } @@ -59,7 +161,10 @@ func run() error { return err } - fmt.Print(p) + x, _ := p[0].enguarde() + y, _ := p[1].enguarde() + + fmt.Print(x, y) return nil } |