summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2022-01-25 00:14:41 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2022-01-25 00:14:41 +0100
commit349b66cb520a032fbacb9577ef85dd7e792cceb5 (patch)
treea18171d0d353d1e0122eaaa662392959fc7dba47
parentab5efc3a4320f81a55f21d1f78c222b704feeae8 (diff)
downloadengarde-importer-349b66cb520a032fbacb9577ef85dd7e792cceb5.tar.gz
engarde-importer-349b66cb520a032fbacb9577ef85dd7e792cceb5.tar.bz2
engarde-importer-349b66cb520a032fbacb9577ef85dd7e792cceb5.zip
Slight restructuring
-rw-r--r--input.go89
-rw-r--r--main.go88
2 files changed, 93 insertions, 84 deletions
diff --git a/input.go b/input.go
new file mode 100644
index 0000000..206d99f
--- /dev/null
+++ b/input.go
@@ -0,0 +1,89 @@
+package main
+
+import (
+ "encoding/csv"
+ "fmt"
+ "os"
+ "strings"
+
+ "github.com/jszwec/csvutil"
+)
+
+type Participant struct {
+ LastName string `csv:"lastname"`
+ FirstName string `csv:"firstname"`
+ DateOfBirth string `csv:"dateofbirth"`
+ Gender Gender `csv:"gender"`
+ Nation string `csv:"nation"`
+ Region string `csv:"region"`
+ ClubStr string `csv:"club"`
+ Id uint `csv:"-"`
+ ClubId uint `csv:"-"`
+}
+
+type Club struct {
+ Name string
+ Id uint
+}
+
+func (p Participant) MainClub() string {
+ if strings.Contains(p.ClubStr, ", ") {
+ return strings.Split(p.ClubStr, ", ")[0]
+ }
+ return p.ClubStr
+}
+
+func parseOphardtInput(fileName string) ([]Participant, []Club, error) {
+ f, err := os.Open(fileName)
+ if err != nil {
+ return nil, nil, fmt.Errorf("opening input file '%s': %w", fileName, err)
+ }
+
+ encReader, err := encodedReader(f)
+ if err != nil {
+ return nil, nil, fmt.Errorf("cannot determine encoding of file '%s': %w", fileName, err)
+ }
+
+ csvReader := csv.NewReader(encReader)
+ csvReader.Comma = ';'
+
+ dec, err := csvutil.NewDecoder(csvReader)
+ if err != nil {
+ return nil, nil, fmt.Errorf("reading from file '%s': %w", fileName, err)
+ }
+ dec.DisallowMissingColumns = true
+
+ var participants []Participant
+ if err = dec.Decode(&participants); err != nil {
+ return nil, nil, fmt.Errorf("decoding file '%s': %w", fileName, err)
+ }
+
+ clubs := prepareParticipants(participants)
+ return participants, clubs, nil
+}
+
+func prepareParticipants(participants []Participant) []Club {
+ clubMap := map[string]uint{}
+ var clubs []Club
+ var clubId, participantId uint
+
+ for i := range participants {
+ participantId++
+
+ p := &participants[i]
+ p.Id = participantId
+
+ club := p.MainClub()
+ if cId, ok := clubMap[club]; ok {
+ p.ClubId = cId
+ } else {
+ clubId++
+
+ p.ClubId = clubId
+ clubMap[club] = clubId
+ clubs = append(clubs, Club{club, clubId})
+ }
+ }
+
+ return clubs
+}
diff --git a/main.go b/main.go
index d8adf3a..15833cd 100644
--- a/main.go
+++ b/main.go
@@ -1,97 +1,13 @@
package main
import (
- "encoding/csv"
"errors"
"fmt"
"log"
"os"
- "strings"
"time"
-
- "github.com/jszwec/csvutil"
)
-type Participant struct {
- LastName string `csv:"lastname"`
- FirstName string `csv:"firstname"`
- DateOfBirth string `csv:"dateofbirth"`
- Gender Gender `csv:"gender"`
- Nation string `csv:"nation"`
- Region string `csv:"region"`
- ClubStr string `csv:"club"`
- Id uint `csv:"-"`
- ClubId uint `csv:"-"`
-}
-
-type Club struct {
- Name string
- Id uint
-}
-
-func prepareParticipants(participants []Participant) []Club {
- clubMap := map[string]uint{}
- var clubs []Club
- var clubId, participantId uint
-
- for i := range participants {
- p := &participants[i]
- participantId++
-
- p.Id = participantId
-
- pClub := p.ClubStr
- if strings.Contains(pClub, ", ") {
- pClub = strings.Split(pClub, ", ")[0]
- }
-
- if cId, ok := clubMap[pClub]; ok {
- p.ClubId = cId
- } else {
- clubId++
-
- p.ClubId = clubId
- clubMap[pClub] = clubId
- clubs = append(clubs, Club{pClub, clubId})
- }
- }
-
- return clubs
-}
-
-func parseOphardtInput(fileName string) ([]Participant, []Club, error) {
- f, err := os.Open(fileName)
- if err != nil {
- return nil, nil, fmt.Errorf("opening input file '%s': %w", fileName, err)
- }
-
- encReader, err := encodedReader(f)
- if err != nil {
- return nil, nil, fmt.Errorf("cannot determine encoding of file '%s': %w", fileName, err)
- }
-
- csvReader := csv.NewReader(encReader)
- csvReader.Comma = ';'
-
- dec, err := csvutil.NewDecoder(csvReader)
- if err != nil {
- return nil, nil, fmt.Errorf("reading from file '%s': %w", fileName, err)
- }
- dec.DisallowMissingColumns = true
-
- var participants []Participant
- if err = dec.Decode(&participants); err != nil {
- return nil, nil, fmt.Errorf("decoding file '%s': %w", fileName, err)
- }
-
- clubs := prepareParticipants(participants)
- return participants, clubs, nil
-}
-
-func usage() string {
- return fmt.Sprintf("Usage: %s <input csv> <output dir> <name> <M/F> <S/V> <D/F/S> <dd.mm.yyyy>", os.Args[0])
-}
-
type EngardeConfig struct {
inputFile string
outputDir string
@@ -105,6 +21,10 @@ type EngardeConfig struct {
Clubs []Club
}
+func usage() string {
+ return fmt.Sprintf("Usage: %s <input csv> <output dir> <name> <M/F> <S/V> <D/F/S> <dd.mm.yyyy>", os.Args[0])
+}
+
func parseArgs() (config EngardeConfig, err error) {
config.inputFile = os.Args[1]
config.outputDir = os.Args[2]