/
base.go
538 lines (492 loc) · 11.8 KB
/
base.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package populate
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/google/uuid"
"github.com/nsip/sifxml2go/sifxml"
)
// Abort on error.
func Errcheck(err error) {
if err != nil {
log.Fatalf("Error %s", err)
}
}
// Marshal to XML, and print to output.
func PrintXML(record interface{}) error {
output, err := xml.MarshalIndent(record, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
// Marshal to JSON, and print to output.
func PrintJSON(record interface{}) error {
b, err := json.Marshal(record)
if err != nil {
return err
}
var out bytes.Buffer
json.Indent(&out, b, " ", "\t")
out.WriteTo(os.Stdout)
return nil
}
func create_GUID() string {
return strings.ToUpper(uuid.New().String())
}
func randomStringFromSlice(slice []string) string {
return slice[rand.Intn(len(slice))]
}
func this_year() string {
return strconv.Itoa(time.Now().Year())
}
func create_boolean() bool {
r := rand.Float64()
if r < 0.5 {
return true
}
return false
}
func sex_seeded(sex string) string {
r := rand.Float64()
if r < 0.1 {
return "9"
}
if sex == "female" {
return "2"
}
if sex == "male" {
return "1"
}
return "9"
}
func create_salutation(sex string) string {
if sex == "female" {
return randomStringFromSlice([]string{"Mrs", "Dr", "Ms", "Miss"})
} else if sex == "male" {
return randomStringFromSlice([]string{"Mr", "Dr", "Mr", "Mr"})
} else {
return randomStringFromSlice([]string{"Mx", "Dr", "Mx", "Mx"})
}
}
func create_state() string {
return randomStringFromSlice([]string{"ACT", "NT", "QLD", "VIC", "WA", "NSW", "TAS", "SA"})
}
func postcode_seeded(state string) string {
var ret int
switch state {
case "ACT":
ret = rand.Intn(19) + 2600
case "NT":
ret = rand.Intn(100) + 800
case "QLD":
ret = rand.Intn(1000) + 4000
case "NSW":
ret = rand.Intn(600) + 2000
case "VIC":
ret = rand.Intn(1000) + 3000
case "TAS":
ret = rand.Intn(800) + 7000
case "SA":
ret = rand.Intn(800) + 5000
case "WA":
ret = rand.Intn(798) + 6000
}
return fmt.Sprintf("%04d", ret)
}
var sequences map[string]int = make(map[string]int)
func seq_gen(key string) int {
if _, ok := sequences[key]; !ok {
sequences[key] = 0
}
sequences[key] += 1
return sequences[key]
}
func seq_gen_reset(key string) {
sequences[key] = 0
}
var random_sequences map[string]map[int]bool = make(map[string]map[int]bool)
func random_seq_gen(key string, n int) int {
var ok bool
if _, ok = random_sequences[key]; !ok {
random_sequences[key] = make(map[int]bool)
}
ret := rand.Intn(n)
_, ok = random_sequences[key][ret]
if ok {
found := false
for i := 0; i < n; i++ {
if _, ok2 := random_sequences[key][i]; !ok2 {
found = true
}
}
if !found {
log.Fatalf("no available slots remaining in random int sequence %s, max length %d", key, n)
}
}
for ok {
ret = rand.Intn(n)
_, ok = random_sequences[key][ret]
}
random_sequences[key][ret] = true
return ret
}
func random_seq_gen_reset(key string) {
random_sequences[key] = make(map[int]bool)
}
func birth_year(yearlevel_str string) (string, error) {
minyear := time.Now().Year() - 5
gofakeit.Seed(0)
yearlevel := yearlevel2yr(yearlevel_str)
return gofakeit.DateRange(time.Date(minyear-yearlevel-1, time.January, 1, 0, 0, 0, 0, time.UTC),
time.Date(minyear-yearlevel, time.December, 31, 0, 0, 0, 0, time.UTC)).Format("2006-01-02"), nil
}
// assumes probabilities ranked in descending order from 1.0 to 0 (final probability 0), and that cardinality of
// probabilities is same as that of options
func threshold_rand_strings(probabilities []float64, options []string) string {
r := rand.Float64()
if len(probabilities) != len(options) {
log.Fatalf("threshold_rand_strings: The cardinality of %+v is not same as the cardinality of %+v", probabilities, options)
}
for i, prob := range probabilities {
if r > prob {
return options[i]
}
}
return options[len(options)-1]
}
func school_name(schooltype string) string {
gofakeit.Seed(0)
school := randomStringFromSlice([]string{"Academy", "Grammar", "College"})
switch schooltype {
case "Camp":
school = "Camp"
case "Commty":
school = "Community College"
case "EarlyCh":
school = "Childcare Centre"
case "JunPri":
school = "Primary School"
case "Kgarten":
school = "Kindergarten"
case "Kind":
school = "Kindergarten"
case "Lang":
school = "Language School"
case "MCH":
school = "Maternal Child Health Centre"
case "Middle":
school = "Middle School"
case "PreSch":
school = "Preschool"
case "Pri/Sec":
school = "College"
case "Prim":
school = "Primary School"
case "Sec":
school = "Secondary College"
case "Senior":
school = "Secondary College"
case "Spec/P-12":
school = "Special School"
case "Spec/Pri":
school = "Special School"
case "Spec/Sec":
school = "Special School"
case "Special":
school = "Special School"
case "Supp":
school = "Support Centre"
}
return gofakeit.City() + " " + school
}
func create_email(given string, middle string, family string, domain string) string {
return family + "." + given + "." + string((middle)[0]) + "@" + domain
}
func create_school_email_domain(school *sifxml.SchoolInfo) string {
domain := randomStringFromSlice([]string{"mail.vic.edu.au", "people.vic.edu.au", "vic.edu.au", "dashboard.vic.edu.au", "distance.vic.edu.au"})
state := school.AddressList().Last().StateProvince().String()
return strings.Replace(domain, "vic", strings.ToLower(state), 1)
}
func create_commercial_email_domain() string {
gofakeit.Seed(0)
return gofakeit.DomainName()
}
func create_phone_number(state *string) string {
var areacode string
if state == nil {
val := "Other"
state = &val
}
switch *state {
case "ACT":
areacode = "02"
case "NSW":
areacode = "02"
case "VIC":
areacode = "03"
case "TAS":
areacode = "03"
case "QLD":
areacode = "07"
case "WA":
areacode = "08"
case "SA":
areacode = "08"
case "NT":
areacode = "08"
default:
areacode = "04"
}
return fmt.Sprintf("%s%08d", areacode, rand.Intn(100000000))
}
func random_date(from string, to string) string {
gofakeit.Seed(0)
t1, err := time.Parse("2006-01-02", from)
if err != nil {
log.Fatalf("%s is in an illegal date format", from)
}
t2, err := time.Parse("2006-01-02", to)
if err != nil {
log.Fatalf("%s is in an illegal date format", to)
}
return gofakeit.DateRange(t1, t2).Format("2006-01-02")
}
// Default selection of teaching subjects: "MAT", "ENG", "PHYS", "BIO", "CHEM", "COMP", "VIS", "ECON", "HIST".
func All_teachingSubjects() []string {
return ([]string{"MAT", "ENG", "PHYS", "BIO", "CHEM", "COMP", "VIS", "ECON", "HIST"})
}
func teachingSubject() string {
return randomStringFromSlice(All_teachingSubjects())
}
func teachingSubjectLongName(shortname string) string {
switch shortname {
case "MAT":
return "Mathematics"
case "ENG":
return "English"
case "PHYS":
return "Physics"
case "BIO":
return "Biology"
case "CHEM":
return "Chemistry"
case "COMP":
return "Computer Science"
case "VIS":
return "Visual Design"
case "ECON":
return "Economics"
case "HIST":
return "History"
}
return "???"
}
func TeachingGroupKLA(shortname string) string {
switch shortname {
case "MAT":
return "M"
case "ENG":
return "E"
case "PHYS":
return "S"
case "BIO":
return "S"
case "CHEM":
return "S"
case "COMP":
return "I"
case "VIS":
return "D"
case "ECON":
return "B"
case "HIST":
return "H"
}
return "???"
}
func yearlevel2yr(yr string) int {
switch yr {
case "0":
return 0
case "1":
return 1
case "2":
return 2
case "3":
return 3
case "4":
return 4
case "5":
return 5
case "6":
return 6
case "7":
return 7
case "8":
return 8
case "9":
return 9
case "10":
return 10
case "11":
return 11
case "12":
return 12
case "13":
return 13
case "11MINUS":
return 6
case "12PLUS":
return 7
case "P":
return 0
case "K":
return -1
case "K4":
return -1
case "K3":
return -2
case "CC":
return -2
case "PS":
return -2
case "UG":
return rand.Intn(12) + 1
case "UGPri":
return rand.Intn(6) + 1
case "UGSec":
return rand.Intn(6) + 7
case "UGJunSec":
return rand.Intn(4) + 7
case "UGSnrSec":
return rand.Intn(2) + 11
}
return 1
}
// Map the School Type schooltype to a slice of year levels that a school of that type offers.
func Schooltype2Yearlevels(schooltype string) []string {
switch schooltype {
case "EarlyCh":
return []string{"CC"}
case "JunPri":
return []string{"1", "2", "3"}
case "Kgarten":
return []string{"K"}
case "Kind":
return []string{"PS", "K"}
case "Middle":
return []string{"5", "6", "7", "8"}
case "PreSch":
return []string{"PS"}
case "Pri/Sec":
return []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}
case "Spec/P-12":
return []string{"P", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}
case "Spec/Sec":
return []string{"7", "8", "9", "10", "11", "12"}
case "Prim":
return []string{"1", "2", "3", "4", "5", "6"}
case "Sec":
return []string{"7", "8", "9", "10", "11", "12"}
case "Senior":
return []string{"11", "12"}
}
return []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}
}
// The permitted periods for a school day.
func Periods() []string {
return []string{"9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM"}
}
func periodId2Hour(id int) int {
return 8 + id
}
// Set the time that the period with PeriodID id starts; e.g. period 1 maps to 9:05.
func PeriodStart(id int) time.Time {
t, _ := time.Parse("2006-01-02T15:04:05.000Z", fmt.Sprintf("2000-01-01T%02d:05:00.000Z", periodId2Hour(id)))
return t
}
// Set the time that the period with PeriodID id ends; e.g. period 1 maps to 9:55.
func PeriodEnd(id int) time.Time {
t, _ := time.Parse("2006-01-02T15:04:05.000Z", fmt.Sprintf("2000-01-01T%02d:55:00.000Z", periodId2Hour(id)))
return t
}
// Start date for the given semester in the given year. Set to February 1 and July 1.
func Term_start_date(year string, semester int) string {
if semester == 1 {
return fmt.Sprintf("%s-02-01", year)
} else {
return fmt.Sprintf("%s-07-01", year)
}
}
// End date for the given semester in the given year. Set to May 30 and November 30.
func Term_end_date(year string, semester int) string {
if semester == 1 {
return fmt.Sprintf("%s-05-30", year)
} else {
return fmt.Sprintf("%s-11-30", year)
}
}
// List of all codes for Australian Government Collections, to iterate through
func All_AGCollections() []string {
return ([]string{"COI", "FQ", "SES", "STATS"})
}
// Map Australian Government Collection code to human-readable name
func CollectionCode2Name(code string) string {
switch code {
case "COI":
return "Non-Government School Census"
case "FQ":
return "Financial Questionnaire"
case "SES":
return "Address Collection"
case "STATS":
return "Student Attendance"
}
return "XXX"
}
// Generate a code for the given collection round
func CollectionRoundCode(collection string, year string, round int) string {
return fmt.Sprintf("%s %s-%02d", collection, year, round)
}
// Map supported HTTP status code to human-readable title
func HTTPStatus2Text(code string) string {
switch code {
case "201":
return "Created"
case "422":
return "Unprocessable Entity"
case "500":
return "Internal Server Error"
}
return "XXX"
}
// Use the fake person generator to generate a person with the given gender
// Supported genders: "male", "female"
func GenderedFakePerson(gender string) *gofakeit.PersonInfo {
var p *gofakeit.PersonInfo
for ok := true; ok; ok = (p.Gender != gender) {
p = gofakeit.Person()
}
return p
}
type PersonInfo struct {
gofakeit.PersonInfo
MiddleName string
}
func FakePerson() *PersonInfo {
person := gofakeit.Person()
person2 := GenderedFakePerson(person.Gender)
ret := PersonInfo{PersonInfo: *person}
ret.MiddleName = person2.FirstName
return &ret
}