-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathorderid.go
67 lines (59 loc) · 2.17 KB
/
orderid.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
package match
import (
"encoding/hex"
"fmt"
)
// OrderID represents an order's unique ID.
// This is a byte array alias because sometimes the ID will be represented as text, and sometimes it will be represented as bytes.
// We conform it to the BinaryMarshaler interface and TextMarshaler interface.
type OrderID [32]byte
// MarshalBinary encodes the receiver into a binary form and returns the result. This conforms to the BinaryMarshaler interface
func (o *OrderID) MarshalBinary() (data []byte, err error) {
// size array, then copy
oSlice := o[:]
data = make([]byte, len(oSlice))
copy(data, oSlice)
return
}
// UnmarshalBinary decodes the form generated by MarshalBinary. This conforms to the BinaryMarshaler interface
func (o *OrderID) UnmarshalBinary(data []byte) (err error) {
// no need to size because orderid is [32]byte
copy(o[:], data)
return
}
// MarshalText encodes the receiver into UTF-8-encoded text and returns the result. This conforms to the TextMarshaler interface
func (o *OrderID) MarshalText() (text []byte, err error) {
// size array, then copy
oSlice := []byte(hex.EncodeToString(o[:]))
text = make([]byte, len(oSlice))
copy(text, oSlice)
return
}
// UnmarshalText deocdes the form generated by MarshalText. This conforms to the TextMarshaler interface
func (o *OrderID) UnmarshalText(text []byte) (err error) {
if _, err = hex.Decode(o[:], text); err != nil {
err = fmt.Errorf("Error unmarshalling text OrderID: %s", err)
return
}
return
}
// GobEncode returns a byte slice representing the encoding of the
// receiver for transmission to a GobDecoder, usually of the same
// concrete type.
func (o *OrderID) GobEncode() (ret []byte, err error) {
if ret, err = o.MarshalBinary(); err != nil {
err = fmt.Errorf("Error gob encoding by marshalling binary: %s", err)
return
}
return
}
// GobDecode overwrites the receiver, which must be a pointer,
// with the value represented by the byte slice, which was written
// by GobEncode, usually for the same concrete type.
func (o *OrderID) GobDecode(in []byte) (err error) {
if err = o.UnmarshalBinary(in); err != nil {
err = fmt.Errorf("Error gob encoding by unmarshalling binary: %s", err)
return
}
return
}