7 cmn "github.com/tendermint/tmlibs/common"
8 "github.com/tendermint/go-wire"
11 type Byteful interface {
12 Bytes() ([]byte, error)
15 //----------------------------------------
22 func (n Numeric) Bytes() ([]byte, error) {
23 num, err := strconv.Atoi(n.Number)
29 return wire.BinaryBytes(uint(num)), nil
31 return wire.BinaryBytes(int(num)), nil
33 return wire.BinaryBytes(uint64(num)), nil
35 return wire.BinaryBytes(int64(num)), nil
37 return wire.BinaryBytes(uint32(num)), nil
39 return wire.BinaryBytes(int32(num)), nil
41 return wire.BinaryBytes(uint16(num)), nil
43 return wire.BinaryBytes(int16(num)), nil
45 return wire.BinaryBytes(uint8(num)), nil
47 return wire.BinaryBytes(int8(num)), nil
49 return nil, errors.New(cmn.Fmt("Unknown Numeric type %v", n.Type))
52 //----------------------------------------
54 type Tuple []interface{}
56 func (t Tuple) Bytes() ([]byte, error) {
58 for _, item := range t {
59 if _, ok := item.(Byteful); !ok {
60 return nil, errors.New("Tuple item was not Byteful")
62 bzi, err := item.(Byteful).Bytes()
66 bz = append(bz, bzi...)
71 func (t Tuple) String() string {
73 for i, ti := range t {
75 s += cmn.Fmt("%v", ti)
77 s += cmn.Fmt(" %v", ti)
84 //----------------------------------------
86 type Array []interface{}
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")
94 bzi, err := item.(Byteful).Bytes()
98 bz = append(bz, bzi...)
103 func (t Array) String() string {
105 for i, ti := range t {
107 s += cmn.Fmt("%v", ti)
109 s += cmn.Fmt(",%v", ti)
116 //----------------------------------------
123 func NewBytes(bz []byte, lengthPrefixed bool) Bytes {
126 LengthPrefixed: lengthPrefixed,
130 func (b Bytes) Bytes() ([]byte, error) {
131 if b.LengthPrefixed {
132 bz := wire.BinaryBytes(len(b.Data))
133 bz = append(bz, b.Data...)
140 func (b Bytes) String() string {
141 if b.LengthPrefixed {
142 return cmn.Fmt("0x%X", []byte(b.Data))
144 return cmn.Fmt("x%X", []byte(b.Data))
148 //----------------------------------------
150 type Placeholder struct {
154 func (p Placeholder) Bytes() ([]byte, error) {
155 return []byte{0x00}, nil
158 func (p Placeholder) String() string {
159 return cmn.Fmt("<%v>", p.Label)
162 //----------------------------------------
168 func NewString(text string) String {
172 func (s String) Bytes() ([]byte, error) {
173 bz := wire.BinaryBytes(int(len(s.Text)))
174 bz = append(bz, []byte(s.Text)...)
178 func (s String) String() string {
179 return strconv.Quote(s.Text)