forked from i25959341/orderbook
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorder.go
100 lines (86 loc) · 2.21 KB
/
order.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
99
100
package orderbook
import (
"encoding/json"
"fmt"
"time"
"github.com/shopspring/decimal"
)
// Order strores information about request
type Order struct {
side Side
id string
timestamp time.Time
quantity decimal.Decimal
price decimal.Decimal
}
// NewOrder creates new constant object Order
func NewOrder(orderID string, side Side, quantity, price decimal.Decimal, timestamp time.Time) *Order {
return &Order{
id: orderID,
side: side,
quantity: quantity,
price: price,
timestamp: timestamp,
}
}
// ID returns orderID field copy
func (o *Order) ID() string {
return o.id
}
// Side returns side of the order
func (o *Order) Side() Side {
return o.side
}
// Quantity returns quantity field copy
func (o *Order) Quantity() decimal.Decimal {
return o.quantity
}
// Price returns price field copy
func (o *Order) Price() decimal.Decimal {
return o.price
}
// Time returns timestamp field copy
func (o *Order) Time() time.Time {
return o.timestamp
}
// String implements Stringer interface
func (o *Order) String() string {
return fmt.Sprintf("\n\"%s\":\n\tside: %s\n\tquantity: %s\n\tprice: %s\n\ttime: %s\n", o.ID(), o.Side(), o.Quantity(), o.Price(), o.Time())
}
// MarshalJSON implements json.Marshaler interface
func (o *Order) MarshalJSON() ([]byte, error) {
return json.Marshal(
&struct {
S Side `json:"side"`
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Quantity decimal.Decimal `json:"quantity"`
Price decimal.Decimal `json:"price"`
}{
S: o.Side(),
ID: o.ID(),
Timestamp: o.Time(),
Quantity: o.Quantity(),
Price: o.Price(),
},
)
}
// UnmarshalJSON implements json.Unmarshaler interface
func (o *Order) UnmarshalJSON(data []byte) error {
obj := struct {
S Side `json:"side"`
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Quantity decimal.Decimal `json:"quantity"`
Price decimal.Decimal `json:"price"`
}{}
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
o.side = obj.S
o.id = obj.ID
o.timestamp = obj.Timestamp
o.quantity = obj.Quantity
o.price = obj.Price
return nil
}