OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / expr / types.go
1 package expr
2
3 import (
4         "errors"
5         "strconv"
6
7         cmn "github.com/tendermint/tmlibs/common"
8         "github.com/tendermint/go-wire"
9 )
10
11 type Byteful interface {
12         Bytes() ([]byte, error)
13 }
14
15 //----------------------------------------
16
17 type Numeric struct {
18         Type   string
19         Number string
20 }
21
22 func (n Numeric) Bytes() ([]byte, error) {
23         num, err := strconv.Atoi(n.Number)
24         if err != nil {
25                 return nil, err
26         }
27         switch n.Type {
28         case "u": // Uvarint
29                 return wire.BinaryBytes(uint(num)), nil
30         case "i": // Varint
31                 return wire.BinaryBytes(int(num)), nil
32         case "u64": // Uint64
33                 return wire.BinaryBytes(uint64(num)), nil
34         case "i64": // Int64
35                 return wire.BinaryBytes(int64(num)), nil
36         case "u32": // Uint32
37                 return wire.BinaryBytes(uint32(num)), nil
38         case "i32": // Int32
39                 return wire.BinaryBytes(int32(num)), nil
40         case "u16": // Uint16
41                 return wire.BinaryBytes(uint16(num)), nil
42         case "i16": // Int16
43                 return wire.BinaryBytes(int16(num)), nil
44         case "u8": // Uint8
45                 return wire.BinaryBytes(uint8(num)), nil
46         case "i8": // Int8
47                 return wire.BinaryBytes(int8(num)), nil
48         }
49         return nil, errors.New(cmn.Fmt("Unknown Numeric type %v", n.Type))
50 }
51
52 //----------------------------------------
53
54 type Tuple []interface{}
55
56 func (t Tuple) Bytes() ([]byte, error) {
57         bz := []byte{}
58         for _, item := range t {
59                 if _, ok := item.(Byteful); !ok {
60                         return nil, errors.New("Tuple item was not Byteful")
61                 }
62                 bzi, err := item.(Byteful).Bytes()
63                 if err != nil {
64                         return nil, err
65                 }
66                 bz = append(bz, bzi...)
67         }
68         return bz, nil
69 }
70
71 func (t Tuple) String() string {
72         s := "("
73         for i, ti := range t {
74                 if i == 0 {
75                         s += cmn.Fmt("%v", ti)
76                 } else {
77                         s += cmn.Fmt(" %v", ti)
78                 }
79         }
80         s += ")"
81         return s
82 }
83
84 //----------------------------------------
85
86 type Array []interface{}
87
88 func (arr Array) Bytes() ([]byte, error) {
89         bz := wire.BinaryBytes(int(len(arr)))
90         for _, item := range arr {
91                 if _, ok := item.(Byteful); !ok {
92                         return nil, errors.New("Array item was not Byteful")
93                 }
94                 bzi, err := item.(Byteful).Bytes()
95                 if err != nil {
96                         return nil, err
97                 }
98                 bz = append(bz, bzi...)
99         }
100         return bz, nil
101 }
102
103 func (t Array) String() string {
104         s := "["
105         for i, ti := range t {
106                 if i == 0 {
107                         s += cmn.Fmt("%v", ti)
108                 } else {
109                         s += cmn.Fmt(",%v", ti)
110                 }
111         }
112         s += "]"
113         return s
114 }
115
116 //----------------------------------------
117
118 type Bytes struct {
119         Data           []byte
120         LengthPrefixed bool
121 }
122
123 func NewBytes(bz []byte, lengthPrefixed bool) Bytes {
124         return Bytes{
125                 Data:           bz,
126                 LengthPrefixed: lengthPrefixed,
127         }
128 }
129
130 func (b Bytes) Bytes() ([]byte, error) {
131         if b.LengthPrefixed {
132                 bz := wire.BinaryBytes(len(b.Data))
133                 bz = append(bz, b.Data...)
134                 return bz, nil
135         } else {
136                 return b.Data, nil
137         }
138 }
139
140 func (b Bytes) String() string {
141         if b.LengthPrefixed {
142                 return cmn.Fmt("0x%X", []byte(b.Data))
143         } else {
144                 return cmn.Fmt("x%X", []byte(b.Data))
145         }
146 }
147
148 //----------------------------------------
149
150 type Placeholder struct {
151         Label string
152 }
153
154 func (p Placeholder) Bytes() ([]byte, error) {
155         return []byte{0x00}, nil
156 }
157
158 func (p Placeholder) String() string {
159         return cmn.Fmt("<%v>", p.Label)
160 }
161
162 //----------------------------------------
163
164 type String struct {
165         Text string
166 }
167
168 func NewString(text string) String {
169         return String{text}
170 }
171
172 func (s String) Bytes() ([]byte, error) {
173         bz := wire.BinaryBytes(int(len(s.Text)))
174         bz = append(bz, []byte(s.Text)...)
175         return bz, nil
176 }
177
178 func (s String) String() string {
179         return strconv.Quote(s.Text)
180 }