-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.go
160 lines (141 loc) · 3.05 KB
/
structs.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
log "github.com/sirupsen/logrus"
)
const noEmail = iota
var loggerMap = map[string]log.Level{
"p": log.PanicLevel,
"f": log.FatalLevel,
"e": log.ErrorLevel,
"w": log.WarnLevel,
"i": log.InfoLevel,
"d": log.DebugLevel,
}
type fieldIndices struct {
parentName int
studentName int
parentEmail int
parentEmailAlt int
grade int
room int
teacher int
primaryPhone int
streetAddress int
city int
zip int
parentType int
}
type recordImportError struct {
cause int
msg string
}
func (err *recordImportError) Error() string {
return fmt.Sprintf("[%v] - %s", err.cause, err.msg)
}
type roomMap map[string][][]string
func (r roomMap) add(key string, value []string) {
_, ok := r[key]
if !ok {
r[key] = make([][]string, 0, 20)
}
r[key] = append(r[key], value)
}
func (r roomMap) peek(key string) ([][]string, bool) {
slice, ok := r[key]
if !ok || len(slice) == 0 {
return make([][]string, 0), false
}
return r[key], true
}
type name struct {
first string
last string
}
func (n *name) String() string {
return n.last + ", " + n.first
}
type student struct {
name name
teacher string
room string
grade string
parents []*parent
}
func (stu *student) String() string {
resp := stu.name.String() + ", " + stu.teacher + ", " + stu.room + ", " + stu.grade + ", parents: ["
for i, par := range stu.parents {
if i > 0 {
resp += ", "
}
resp += (*par).String()
}
resp += "]"
return resp
}
func (stu student) key() string {
data := []byte(stu.name.String() + stu.teacher + stu.room + stu.grade)
sum := md5.Sum(data)
key := hex.EncodeToString(sum[:md5.Size])
return key
}
func (stu *student) gradeVal() string {
if stu.grade == "0" {
return "K"
}
return stu.grade
}
type studentsByName []*student
func (s studentsByName) Len() int { return len(s) }
func (s studentsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s studentsByName) Less(i, j int) bool {
iName := s[i].name.last + s[i].name.first
jName := s[j].name.last + s[j].name.first
return iName < jName
}
type address struct {
street string
city string
zip string
}
func (a *address) String() string {
return a.street + ", " + a.city + ", CA " + a.zip
}
type parent struct {
name name
address address
primaryPhone string
parentType string
students []*student
email string
meta []*recordImportError
}
func (par *parent) String() string {
resp := par.parentType + ": " + par.name.String() + ", " + par.address.String() + ", " + par.email + ", " + par.primaryPhone
if len(par.meta) > 0 {
resp += ", meta: ["
for i, err := range par.meta {
_, msg := msgFromImportError(err)
if i > 0 {
resp += ", "
}
resp += "ERROR: " + msg
}
resp += "]"
}
return resp
}
func (par *parent) hasEmailError() bool {
hasError := false
if len(par.meta) > 0 {
for _, err := range par.meta {
errType, _ := msgFromImportError(err)
if errType == noEmail {
hasError = true
}
}
}
return hasError
}