/
bytes.go
67 lines (52 loc) · 1.13 KB
/
bytes.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 base
import (
"bytes"
"encoding/hex"
"encoding/json"
"github.com/ovrclk/akash/util"
)
type Bytes []byte
func (t Bytes) Marshal() ([]byte, error) {
return t, nil
}
func (t *Bytes) MarshalTo(data []byte) (n int, err error) {
return copy(data, *t), nil
}
func (t *Bytes) Unmarshal(data []byte) error {
*t = data
return nil
}
func (t Bytes) MarshalJSON() ([]byte, error) {
return json.Marshal(t.EncodeString())
}
func (t *Bytes) UnmarshalJSON(buf []byte) error {
var val string
if err := json.Unmarshal(buf, &val); err != nil {
return err
}
return t.DecodeString(val)
}
func (t *Bytes) DecodeString(buf string) error {
val, err := hex.DecodeString(buf)
*t = val
return err
}
func (t Bytes) EncodeString() string {
return util.X(t)
}
func (t Bytes) String() string {
return t.EncodeString()
}
func (t Bytes) Compare(other Bytes) int {
return bytes.Compare([]byte(t), []byte(other))
}
func (t Bytes) Equal(other Bytes) bool {
return bytes.Equal([]byte(t), []byte(other))
}
func (t Bytes) Size() int {
return len(t)
}
func DecodeString(buf string) (Bytes, error) {
val := new(Bytes)
return *val, val.DecodeString(buf)
}