This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
types.go
180 lines (152 loc) · 3.34 KB
/
types.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package expr
import (
"errors"
"strconv"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/go-wire"
)
type Byteful interface {
Bytes() ([]byte, error)
}
//----------------------------------------
type Numeric struct {
Type string
Number string
}
func (n Numeric) Bytes() ([]byte, error) {
num, err := strconv.Atoi(n.Number)
if err != nil {
return nil, err
}
switch n.Type {
case "u": // Uvarint
return wire.BinaryBytes(uint(num)), nil
case "i": // Varint
return wire.BinaryBytes(int(num)), nil
case "u64": // Uint64
return wire.BinaryBytes(uint64(num)), nil
case "i64": // Int64
return wire.BinaryBytes(int64(num)), nil
case "u32": // Uint32
return wire.BinaryBytes(uint32(num)), nil
case "i32": // Int32
return wire.BinaryBytes(int32(num)), nil
case "u16": // Uint16
return wire.BinaryBytes(uint16(num)), nil
case "i16": // Int16
return wire.BinaryBytes(int16(num)), nil
case "u8": // Uint8
return wire.BinaryBytes(uint8(num)), nil
case "i8": // Int8
return wire.BinaryBytes(int8(num)), nil
}
return nil, errors.New(cmn.Fmt("Unknown Numeric type %v", n.Type))
}
//----------------------------------------
type Tuple []interface{}
func (t Tuple) Bytes() ([]byte, error) {
bz := []byte{}
for _, item := range t {
if _, ok := item.(Byteful); !ok {
return nil, errors.New("Tuple item was not Byteful")
}
bzi, err := item.(Byteful).Bytes()
if err != nil {
return nil, err
}
bz = append(bz, bzi...)
}
return bz, nil
}
func (t Tuple) String() string {
s := "("
for i, ti := range t {
if i == 0 {
s += cmn.Fmt("%v", ti)
} else {
s += cmn.Fmt(" %v", ti)
}
}
s += ")"
return s
}
//----------------------------------------
type Array []interface{}
func (arr Array) Bytes() ([]byte, error) {
bz := wire.BinaryBytes(int(len(arr)))
for _, item := range arr {
if _, ok := item.(Byteful); !ok {
return nil, errors.New("Array item was not Byteful")
}
bzi, err := item.(Byteful).Bytes()
if err != nil {
return nil, err
}
bz = append(bz, bzi...)
}
return bz, nil
}
func (t Array) String() string {
s := "["
for i, ti := range t {
if i == 0 {
s += cmn.Fmt("%v", ti)
} else {
s += cmn.Fmt(",%v", ti)
}
}
s += "]"
return s
}
//----------------------------------------
type Bytes struct {
Data []byte
LengthPrefixed bool
}
func NewBytes(bz []byte, lengthPrefixed bool) Bytes {
return Bytes{
Data: bz,
LengthPrefixed: lengthPrefixed,
}
}
func (b Bytes) Bytes() ([]byte, error) {
if b.LengthPrefixed {
bz := wire.BinaryBytes(len(b.Data))
bz = append(bz, b.Data...)
return bz, nil
} else {
return b.Data, nil
}
}
func (b Bytes) String() string {
if b.LengthPrefixed {
return cmn.Fmt("0x%X", []byte(b.Data))
} else {
return cmn.Fmt("x%X", []byte(b.Data))
}
}
//----------------------------------------
type Placeholder struct {
Label string
}
func (p Placeholder) Bytes() ([]byte, error) {
return []byte{0x00}, nil
}
func (p Placeholder) String() string {
return cmn.Fmt("<%v>", p.Label)
}
//----------------------------------------
type String struct {
Text string
}
func NewString(text string) String {
return String{text}
}
func (s String) Bytes() ([]byte, error) {
bz := wire.BinaryBytes(int(len(s.Text)))
bz = append(bz, []byte(s.Text)...)
return bz, nil
}
func (s String) String() string {
return strconv.Quote(s.Text)
}