-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
68 lines (57 loc) · 1.43 KB
/
types.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
package main
import (
"encoding/json"
"strconv"
)
type Coin struct {
Denom string `json:"denom"`
Amount uint64 `json:"amount,string"`
}
type BalanceResponse struct {
Balances []Coin `json:"balances"`
}
type SizeResponse struct {
Size uint64 `json:"size,string"`
}
type Count struct {
Total uint64 `json:"total,string"`
}
type PageResponse struct {
Pagination Count `json:"pagination"`
}
type StatsResponse struct {
Purchased uint64 `json:"purchased,string"`
Used uint64 `json:"used,string"`
UsedRatio float64 `json:"used_ratio,string"`
ActiveUsers uint64 `json:"activeUsers,string"`
TotalUsers uint64 `json:"uniqueUsers,string"`
UsersByPlan map[uint64]uint64 `json:"users_by_plan"`
}
func (s *StatsResponse) UnmarshalJSON(data []byte) error {
// Create a raw structure to hold the fields.
type Alias StatsResponse
aux := &struct {
UsersByPlan map[string]string `json:"users_by_plan"`
*Alias
}{
Alias: (*Alias)(s),
}
// Unmarshal into the temporary structure
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// Convert map keys and values from string to uint64
s.UsersByPlan = make(map[uint64]uint64)
for k, v := range aux.UsersByPlan {
key, err := strconv.ParseUint(k, 10, 64)
if err != nil {
return err
}
value, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return err
}
s.UsersByPlan[key] = value
}
return nil
}