forked from jyap808/go-poloniex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volume.go
89 lines (83 loc) · 1.75 KB
/
volume.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
package poloniex
import (
"encoding/json"
"strconv"
)
type Volume map[string]float64
type VolumeCollection struct {
TotalBTC float64 `json:"totalBTC,string"`
TotalETH float64 `json:"totalETH,string"`
TotalUSDT float64 `json:"totalUSDT,string"`
TotalXMR float64 `json:"totalXMR,string"`
TotalXUSD float64 `json:"totalXUSD,string"`
Volumes map[string]Volume
}
func (tc *VolumeCollection) UnmarshalJSON(b []byte) error {
m := make(map[string]json.RawMessage)
if err := json.Unmarshal(b, &m); err != nil {
return err
}
tc.Volumes = make(map[string]Volume)
for k, v := range m {
switch k {
case "totalBTC":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalBTC = f
case "totalETH":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalETH = f
case "totalUSDT":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalUSDT = f
case "totalXMR":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalXMR = f
case "totalXUSD":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalXUSD = f
default:
t := make(Volume)
if err := json.Unmarshal(v, &t); err != nil {
return err
}
tc.Volumes[k] = t
}
}
return nil
}
func (t *Volume) UnmarshalJSON(b []byte) error {
m := make(map[string]json.RawMessage)
if err := json.Unmarshal(b, &m); err != nil {
return err
}
for k, v := range m {
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
(*t)[k] = f
}
return nil
}
func parseJSONFloatString(b json.RawMessage) (float64, error) {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return 0, err
}
return strconv.ParseFloat(s, 64)
}