-
Notifications
You must be signed in to change notification settings - Fork 0
/
student.go
78 lines (62 loc) · 1.11 KB
/
student.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"encoding/csv"
"io"
"strconv"
"strings"
)
type Student struct {
id string
name string
group int
}
func trimLeftChar(s string) string {
for i := range s {
if i > 0 {
return s[i:]
}
}
return s[:0]
}
// OrgDefinedId, Username, Group
func NewStudent(csvRow []string) *Student {
s := new(Student)
s.id = csvRow[0]
s.name = csvRow[1]
// Remove # from id
if s.id[0] == '#' {
s.id = trimLeftChar(s.id)
}
// Remove # from name
if s.name[0] == '#' {
s.name = trimLeftChar(s.name)
}
// Parse group number: Group # => #
groupStr := strings.Split(csvRow[2], " ")[1]
group, err := strconv.Atoi(groupStr)
if err != nil {
s.group = -1
} else {
s.group = group
}
return s
}
func getStudentsFromCsv(file io.Reader) []Student {
reader := csv.NewReader(file)
// Getting rid of the header row
// TODO: throw error if incorrect format
_, err := reader.Read()
if err == io.EOF {
return nil
}
var students []Student
for {
row, err := reader.Read()
if err == io.EOF {
break
}
s := NewStudent(row)
students = append(students, *s)
}
return students
}