-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
88 lines (76 loc) · 2.02 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
package durcov
import (
"encoding/json"
"time"
)
// Data represents a combination of global and country based statistics
type Data struct {
global *global
countries []*country
}
type country struct {
name string
slug string
code string
stats *statistics
}
type global struct {
stats *statistics
}
type statistics struct {
totalConfirmed int64
totalDeaths int64
totalRecovered int64
date time.Time
}
type expectedJSONShape struct {
Global expectedJSONGlobalShape `json:"Global"`
Countries []*expectedJSONCountryShape `json:"Countries"`
Date time.Time `json:"Date"`
}
type expectedJSONCountryShape struct {
Name string `json:"Country"`
Slug string `json:"Slug"`
Code string `json:"CountryCode"`
TotalConfirmed int64 `json:"TotalConfirmed"`
TotalDeaths int64 `json:"TotalDeaths"`
TotalRecovered int64 `json:"TotalRecovered"`
Date time.Time `json:"Date"`
}
type expectedJSONGlobalShape struct {
TotalConfirmed int64
TotalDeaths int64
TotalRecovered int64
}
// UnmarshalJSON complies with the json package for custom decoding
func (d *Data) UnmarshalJSON(data []byte) error {
var ingress expectedJSONShape
if err := json.Unmarshal(data, &ingress); err != nil {
return err
}
globalStats := &statistics{
totalConfirmed: ingress.Global.TotalConfirmed,
totalDeaths: ingress.Global.TotalDeaths,
totalRecovered: ingress.Global.TotalRecovered,
date: ingress.Date,
}
countries := []*country{}
for _, countryData := range ingress.Countries {
countryStats := &statistics{
totalConfirmed: countryData.TotalConfirmed,
totalDeaths: countryData.TotalDeaths,
totalRecovered: countryData.TotalRecovered,
date: countryData.Date,
}
country := &country{
name: countryData.Name,
slug: countryData.Slug,
code: countryData.Code,
stats: countryStats,
}
countries = append(countries, country)
}
d.global = &global{globalStats}
d.countries = countries
return nil
}