-
Notifications
You must be signed in to change notification settings - Fork 1
/
data-indexes.go
64 lines (55 loc) · 1.58 KB
/
data-indexes.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
package covid19brazilimporter
import "fmt"
type Page struct {
ParentName string `json:"parentName"`
ParentID int `json:"parentId"`
ID int `json:"id"`
Name string `json:"name"`
}
type DataListing struct {
Name string `json:"name"`
Cases int `json:"cases"`
Deaths int `json:"deaths"`
ParentName *string `json:"parentName,omitempty"`
}
func newDataListing(region *Region, parent *Region) *DataListing {
var parentName *string
if parent != nil {
parentName = &parent.Name
}
return &DataListing{
Name: region.Name,
Cases: region.LastData.Cases,
Deaths: region.LastData.Deaths,
ParentName: parentName,
}
}
func buildIndexes(regions Regions) ([]*Page, map[int][]*DataListing, error) {
pages := make([]*Page, len(regions)-1)
dataListing := map[int][]*DataListing{}
regionIndex := 0
for _, region := range regions {
// Ignore parent -1 because that is the root node
if region.ParentID == -1 {
continue
}
parent, parentFound := regions[region.ParentID]
if !parentFound {
return nil, nil, fmt.Errorf("Parent not found: ParentId %d", region.ParentID)
}
pages[regionIndex] = &Page{
ID: region.ID,
Name: region.Name,
ParentID: region.ParentID,
ParentName: parent.Name,
}
parentListing, listingFound := dataListing[region.ParentID]
if listingFound {
dataListing[region.ParentID] = append(parentListing, newDataListing(region, parent))
} else {
dataListing[region.ParentID] = []*DataListing{newDataListing(region, parent)}
}
regionIndex++
}
return pages, dataListing, nil
}