-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
102 lines (88 loc) · 1.71 KB
/
data.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
package main
import (
_ "embed"
"encoding/csv"
"encoding/json"
"io"
"strconv"
"strings"
)
//go:embed data/articles.csv
var csvArticles string
//go:embed data/authors.csv
var csvAuthors string
//go:embed data/institutions.json
var jsonInstitutions []byte
func readAuthors() ([]Author, error) {
r := csv.NewReader(strings.NewReader(csvAuthors))
results := make([]Author, 0)
// skip the title row
_, err := r.Read()
if err == io.EOF {
return results, nil
}
if err != nil {
return nil, err
}
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
id, err := strconv.ParseInt(record[0], 10, 32)
if err != nil {
return nil, err
}
results = append(results, Author{
ID: int(id),
FirstName: record[1],
LastName: record[2],
})
}
return results, nil
}
func readArticles() ([]Article, error) {
r := csv.NewReader(strings.NewReader(csvArticles))
results := make([]Article, 0)
// skip the title row
_, err := r.Read()
if err == io.EOF {
return results, nil
}
if err != nil {
return nil, err
}
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
id, err := strconv.ParseInt(record[0], 10, 32)
if err != nil {
return nil, err
}
authorID, err := strconv.ParseInt(record[2], 10, 32)
if err != nil {
return nil, err
}
results = append(results, Article{
ID: int(id),
Title: record[1],
AuthorID: int(authorID),
})
}
return results, nil
}
func readInstitutions() ([]Institution, error) {
var institutions []Institution
if err := json.Unmarshal(jsonInstitutions, &institutions); err != nil {
return nil, err
}
return institutions, nil
}