This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarshal.go
98 lines (86 loc) · 2.18 KB
/
marshal.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
package coincross
import (
"encoding/json"
"fmt"
"strings"
"time"
)
// String returns "BTC/USD" for {USD, BTC}.
func (p Pair) String() string {
return string(p.Target + "/" + p.Base)
}
// Set sets the pair to {USD, BTC} from "BTC/USD".
func (p *Pair) Set(s string) error {
parts := strings.Split(strings.ToUpper(s), "/")
*p = Pair{Symbol(parts[1]), Symbol(parts[0])}
return nil
}
// LowerString returns "btc_usd" for {USD, BTC}.
func (p Pair) LowerString() string {
return strings.ToLower(string(p.Target + "_" + p.Base))
}
// MarshalJSON to "btc_usd" from {USD, BTC}.
func (p *Pair) MarshalJSON() ([]byte, error) {
return json.Marshal(p.LowerString())
}
// UnmarshalJSON from "btc_usd" to {USD, BTC}.
func (p *Pair) UnmarshalJSON(b []byte) (err error) {
var s string
err = json.Unmarshal(b, &s)
if err == nil {
parts := strings.Split(strings.ToUpper(s), "_")
*p = Pair{Symbol(parts[1]), Symbol(parts[0])}
}
return
}
func (o Order) String() string {
return fmt.Sprintf("%s\t%d\t%s\t%s\t%f\t%f(%f)", time.Unix(o.Timestamp, 0).Format("20060102 15:04:05"), o.Id, o.Type, o.Pair, o.Price, o.Remain, o.Amount)
}
func (t Trade) String() string {
return fmt.Sprintf("%s %d\t%s\t%8.3f@%-8.6g\t!%s", t.Pair, t.Id, t.Type, t.Amount, t.Price, time.Unix(t.Timestamp, 0).Format("15:04:05"))
}
func (t Transaction) String() string {
amounts := ""
for k, v := range t.Amounts {
amounts = amounts + fmt.Sprintf("\t%s:%f", k, v)
}
return fmt.Sprintf("%s\t%d%s\t%s", time.Unix(t.Timestamp, 0).Format("20060102 15:04:05"), t.Id, amounts, t.Descritpion)
}
func (t *TradeType) MarshalJSON() ([]byte, error) {
var s string
switch *t {
case Buy:
s = "buy"
case Sell:
s = "sell"
}
return json.Marshal(s)
}
func (t *TradeType) UnmarshalJSON(b []byte) (err error) {
var s string
err = json.Unmarshal(b, &s)
if err == nil {
switch strings.ToLower(s) {
case "buy", "bid":
*t = Buy
case "sell", "ask":
*t = Sell
default:
return fmt.Errorf("Unknown TradeType: %v", *t)
}
}
return
}
func (t TradeType) String() string {
switch t {
case Sell:
return "Sell"
case Buy:
return "Buy"
default:
return ""
}
}
func (t *TradeType) Set(s string) error {
return t.UnmarshalJSON([]byte(s))
}