-
Notifications
You must be signed in to change notification settings - Fork 67
/
side.go
92 lines (83 loc) · 2.07 KB
/
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
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
package match
import (
"encoding/json"
"fmt"
"strings"
)
// Side is a representation of buy or sell side
type Side bool
const (
Buy = Side(true) // This should serialize to 0x01
Sell = Side(false) // This should serialize to 0x00
buyString = "buy" // just for string representation
sellString = "sell" // just for string representation
)
// TODO: Rather than use this string method, implement TextMarshaler
// and TextUnmarshaler
// String returns the string representation of a buy or sell side
func (s Side) String() string {
if s {
return buyString
}
return sellString
}
// UnmarshalJSON implements the JSON unmarshalling interface
func (s Side) UnmarshalJSON(b []byte) (err error) {
var str string
if err = json.Unmarshal(b, &str); err != nil {
return
}
switch strings.ToLower(str) {
default:
err = fmt.Errorf("Cannot unmarshal side json, not buy or sell")
return
case buyString:
s = Buy
case sellString:
s = Sell
}
return
}
// FromString takes a string and, if valid, sets the Side to the
// correct value based on the string
func (s Side) FromString(str string) (err error) {
switch strings.ToLower(str) {
default:
err = fmt.Errorf("Cannot get side from string, not buy or sell")
return
case buyString:
s = Buy
case sellString:
s = Sell
}
return
}
// MarshalBinary implements the BinaryMarshaler interface from
// encoding.
func (s Side) MarshalBinary() (data []byte, err error) {
if s {
data = []byte{0x01}
return
}
data = []byte{0x00}
return
}
// UnmarshalBinary implements the BinaryUnmarshaler interface from
// encoding.
// This takes a pointer as a receiver because it's not possible nor
// does it make sense to try to modify the value being called.
func (s *Side) UnmarshalBinary(data []byte) (err error) {
if len(data) != 1 {
err = fmt.Errorf("Error marshalling binary for a Side, length of data should be 1")
return
}
if data[0] == 0x00 {
*s = Sell
return
} else if data[0] == 0x01 {
*s = Buy
return
}
err = fmt.Errorf("Error unmarshalling Side, invalid data - should be 0x00 or 0x01")
return
}