forked from i25959341/orderbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
side.go
46 lines (38 loc) · 751 Bytes
/
side.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
package orderbook
import (
"encoding/json"
"reflect"
)
// Side of the order
type Side int
// Sell (asks) or Buy (bids)
const (
Sell Side = iota
Buy
)
// String implements fmt.Stringer interface
func (s Side) String() string {
if s == Buy {
return "buy"
}
return "sell"
}
// MarshalJSON implements json.Marshaler interface
func (s Side) MarshalJSON() ([]byte, error) {
return []byte(`"` + s.String() + `"`), nil
}
// UnmarshalJSON implements json.Unmarshaler interface
func (s *Side) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"buy"`:
*s = Buy
case `"sell"`:
*s = Sell
default:
return &json.UnsupportedValueError{
Value: reflect.New(reflect.TypeOf(data)),
Str: string(data),
}
}
return nil
}