value.go 7.58 KB
Newer Older
obscuren's avatar
obscuren committed
1 2 3
package ethutil

import (
obscuren's avatar
obscuren committed
4
	"bytes"
obscuren's avatar
obscuren committed
5 6 7
	"fmt"
	"math/big"
	"reflect"
obscuren's avatar
obscuren committed
8
	"strconv"
obscuren's avatar
obscuren committed
9 10 11 12 13 14 15 16 17 18 19
)

// Data values are returned by the rlp decoder. The data values represents
// one item within the rlp data structure. It's responsible for all the casting
// It always returns something valid
type Value struct {
	Val  interface{}
	kind reflect.Value
}

func (val *Value) String() string {
obscuren's avatar
obscuren committed
20
	return fmt.Sprintf("%x", val.Val)
obscuren's avatar
obscuren committed
21 22 23
}

func NewValue(val interface{}) *Value {
24 25 26 27 28 29
	t := val
	if v, ok := val.(*Value); ok {
		t = v.Val
	}

	return &Value{Val: t}
obscuren's avatar
obscuren committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
}

func (val *Value) Type() reflect.Kind {
	return reflect.TypeOf(val.Val).Kind()
}

func (val *Value) IsNil() bool {
	return val.Val == nil
}

func (val *Value) Len() int {
	//return val.kind.Len()
	if data, ok := val.Val.([]interface{}); ok {
		return len(data)
	}

obscuren's avatar
obscuren committed
46
	return len(val.Bytes())
obscuren's avatar
obscuren committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

func (val *Value) Raw() interface{} {
	return val.Val
}

func (val *Value) Interface() interface{} {
	return val.Val
}

func (val *Value) Uint() uint64 {
	if Val, ok := val.Val.(uint8); ok {
		return uint64(Val)
	} else if Val, ok := val.Val.(uint16); ok {
		return uint64(Val)
	} else if Val, ok := val.Val.(uint32); ok {
		return uint64(Val)
	} else if Val, ok := val.Val.(uint64); ok {
		return Val
66 67 68 69
	} else if Val, ok := val.Val.(float32); ok {
		return uint64(Val)
	} else if Val, ok := val.Val.(float64); ok {
		return uint64(Val)
obscuren's avatar
obscuren committed
70 71 72 73
	} else if Val, ok := val.Val.(int); ok {
		return uint64(Val)
	} else if Val, ok := val.Val.(uint); ok {
		return uint64(Val)
obscuren's avatar
obscuren committed
74
	} else if Val, ok := val.Val.([]byte); ok {
obscuren's avatar
obscuren committed
75
		return new(big.Int).SetBytes(Val).Uint64()
obscuren's avatar
obscuren committed
76 77
	} else if Val, ok := val.Val.(*big.Int); ok {
		return Val.Uint64()
obscuren's avatar
obscuren committed
78 79 80 81 82
	}

	return 0
}

obscuren's avatar
obscuren committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
func (val *Value) Int() int64 {
	if Val, ok := val.Val.(int8); ok {
		return int64(Val)
	} else if Val, ok := val.Val.(int16); ok {
		return int64(Val)
	} else if Val, ok := val.Val.(int32); ok {
		return int64(Val)
	} else if Val, ok := val.Val.(int64); ok {
		return Val
	} else if Val, ok := val.Val.(int); ok {
		return int64(Val)
	} else if Val, ok := val.Val.(float32); ok {
		return int64(Val)
	} else if Val, ok := val.Val.(float64); ok {
		return int64(Val)
	} else if Val, ok := val.Val.([]byte); ok {
		return new(big.Int).SetBytes(Val).Int64()
	} else if Val, ok := val.Val.(*big.Int); ok {
		return Val.Int64()
obscuren's avatar
obscuren committed
102 103 104
	} else if Val, ok := val.Val.(string); ok {
		n, _ := strconv.Atoi(Val)
		return int64(n)
obscuren's avatar
obscuren committed
105 106 107 108 109
	}

	return 0
}

obscuren's avatar
obscuren committed
110 111 112 113 114 115 116 117 118 119 120 121 122
func (val *Value) Byte() byte {
	if Val, ok := val.Val.(byte); ok {
		return Val
	}

	return 0x0
}

func (val *Value) BigInt() *big.Int {
	if a, ok := val.Val.([]byte); ok {
		b := new(big.Int).SetBytes(a)

		return b
obscuren's avatar
obscuren committed
123 124
	} else if a, ok := val.Val.(*big.Int); ok {
		return a
obscuren's avatar
obscuren committed
125 126
	} else if a, ok := val.Val.(string); ok {
		return Big(a)
obscuren's avatar
obscuren committed
127 128 129 130 131 132 133 134 135 136 137 138
	} else {
		return big.NewInt(int64(val.Uint()))
	}

	return big.NewInt(0)
}

func (val *Value) Str() string {
	if a, ok := val.Val.([]byte); ok {
		return string(a)
	} else if a, ok := val.Val.(string); ok {
		return a
139 140
	} else if a, ok := val.Val.(byte); ok {
		return string(a)
obscuren's avatar
obscuren committed
141 142 143 144 145 146 147 148
	}

	return ""
}

func (val *Value) Bytes() []byte {
	if a, ok := val.Val.([]byte); ok {
		return a
149 150
	} else if s, ok := val.Val.(byte); ok {
		return []byte{s}
obscuren's avatar
obscuren committed
151 152
	} else if s, ok := val.Val.(string); ok {
		return []byte(s)
obscuren's avatar
obscuren committed
153 154
	} else if s, ok := val.Val.(*big.Int); ok {
		return s.Bytes()
155 156
	} else {
		return big.NewInt(val.Int()).Bytes()
obscuren's avatar
obscuren committed
157 158
	}

obscuren's avatar
obscuren committed
159
	return []byte{}
obscuren's avatar
obscuren committed
160 161
}

obscuren's avatar
obscuren committed
162 163 164 165 166 167 168 169
func (val *Value) Err() error {
	if err, ok := val.Val.(error); ok {
		return err
	}

	return nil
}

obscuren's avatar
obscuren committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
func (val *Value) Slice() []interface{} {
	if d, ok := val.Val.([]interface{}); ok {
		return d
	}

	return []interface{}{}
}

func (val *Value) SliceFrom(from int) *Value {
	slice := val.Slice()

	return NewValue(slice[from:])
}

func (val *Value) SliceTo(to int) *Value {
	slice := val.Slice()

	return NewValue(slice[:to])
}

func (val *Value) SliceFromTo(from, to int) *Value {
	slice := val.Slice()

	return NewValue(slice[from:to])
}

obscuren's avatar
obscuren committed
196 197 198 199 200 201 202 203 204
// TODO More type checking methods
func (val *Value) IsSlice() bool {
	return val.Type() == reflect.Slice
}

func (val *Value) IsStr() bool {
	return val.Type() == reflect.String
}

obscuren's avatar
obscuren committed
205 206 207 208 209
func (self *Value) IsErr() bool {
	_, ok := self.Val.(error)
	return ok
}

210 211 212 213 214 215 216 217 218
// Special list checking function. Something is considered
// a list if it's of type []interface{}. The list is usually
// used in conjunction with rlp decoded streams.
func (val *Value) IsList() bool {
	_, ok := val.Val.([]interface{})

	return ok
}

obscuren's avatar
obscuren committed
219
func (val *Value) IsEmpty() bool {
obscuren's avatar
obscuren committed
220
	return val.Val == nil || ((val.IsSlice() || val.IsStr()) && val.Len() == 0)
obscuren's avatar
obscuren committed
221 222
}

obscuren's avatar
obscuren committed
223 224 225 226 227 228 229 230 231
// Threat the value as a slice
func (val *Value) Get(idx int) *Value {
	if d, ok := val.Val.([]interface{}); ok {
		// Guard for oob
		if len(d) <= idx {
			return NewValue(nil)
		}

		if idx < 0 {
232
			return NewValue(nil)
obscuren's avatar
obscuren committed
233 234 235 236 237 238 239 240 241
		}

		return NewValue(d[idx])
	}

	// If this wasn't a slice you probably shouldn't be using this function
	return NewValue(nil)
}

obscuren's avatar
obscuren committed
242 243 244 245 246 247 248 249 250 251 252 253 254
func (self *Value) Copy() *Value {
	switch val := self.Val.(type) {
	case *big.Int:
		return NewValue(new(big.Int).Set(val))
	case []byte:
		return NewValue(CopyBytes(val))
	default:
		return NewValue(self.Val)
	}

	return nil
}

obscuren's avatar
obscuren committed
255 256 257 258
func (val *Value) Cmp(o *Value) bool {
	return reflect.DeepEqual(val.Val, o.Val)
}

obscuren's avatar
obscuren committed
259
func (self *Value) DeepCmp(o *Value) bool {
obscuren's avatar
obscuren committed
260
	return bytes.Compare(self.Bytes(), o.Bytes()) == 0
obscuren's avatar
obscuren committed
261 262
}

obscuren's avatar
obscuren committed
263 264 265 266
func (val *Value) Encode() []byte {
	return Encode(val.Val)
}

obscuren's avatar
obscuren committed
267 268 269 270
// Assume that the data we have is encoded
func (self *Value) Decode() {
	v, _ := Decode(self.Bytes(), 0)
	self.Val = v
obscuren's avatar
obscuren committed
271
	//self.Val = DecodeWithReader(bytes.NewBuffer(self.Bytes()))
obscuren's avatar
obscuren committed
272 273
}

obscuren's avatar
obscuren committed
274 275
func NewValueFromBytes(data []byte) *Value {
	if len(data) != 0 {
obscuren's avatar
obscuren committed
276 277 278 279
		value := NewValue(data)
		value.Decode()

		return value
obscuren's avatar
obscuren committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	}

	return NewValue(nil)
}

// Value setters
func NewSliceValue(s interface{}) *Value {
	list := EmptyValue()

	if s != nil {
		if slice, ok := s.([]interface{}); ok {
			for _, val := range slice {
				list.Append(val)
			}
		} else if slice, ok := s.([]string); ok {
			for _, val := range slice {
				list.Append(val)
			}
		}
	}

	return list
}

func EmptyValue() *Value {
	return NewValue([]interface{}{})
}

func (val *Value) AppendList() *Value {
	list := EmptyValue()
	val.Val = append(val.Slice(), list)

	return list
}

func (val *Value) Append(v interface{}) *Value {
	val.Val = append(val.Slice(), v)

	return val
}
320

obscuren's avatar
obscuren committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
const (
	valOpAdd = iota
	valOpDiv
	valOpMul
	valOpPow
	valOpSub
)

// Math stuff
func (self *Value) doOp(op int, other interface{}) *Value {
	left := self.BigInt()
	right := NewValue(other).BigInt()

	switch op {
	case valOpAdd:
		self.Val = left.Add(left, right)
	case valOpDiv:
		self.Val = left.Div(left, right)
	case valOpMul:
		self.Val = left.Mul(left, right)
	case valOpPow:
		self.Val = left.Exp(left, right, Big0)
	case valOpSub:
		self.Val = left.Sub(left, right)
	}

	return self
}

func (self *Value) Add(other interface{}) *Value {
	return self.doOp(valOpAdd, other)
}

func (self *Value) Sub(other interface{}) *Value {
	return self.doOp(valOpSub, other)
}

func (self *Value) Div(other interface{}) *Value {
	return self.doOp(valOpDiv, other)
}

func (self *Value) Mul(other interface{}) *Value {
	return self.doOp(valOpMul, other)
}

func (self *Value) Pow(other interface{}) *Value {
	return self.doOp(valOpPow, other)
}

370 371 372 373 374 375 376 377 378 379
type ValueIterator struct {
	value        *Value
	currentValue *Value
	idx          int
}

func (val *Value) NewIterator() *ValueIterator {
	return &ValueIterator{value: val}
}

obscuren's avatar
obscuren committed
380 381 382 383
func (it *ValueIterator) Len() int {
	return it.value.Len()
}

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
func (it *ValueIterator) Next() bool {
	if it.idx >= it.value.Len() {
		return false
	}

	it.currentValue = it.value.Get(it.idx)
	it.idx++

	return true
}

func (it *ValueIterator) Value() *Value {
	return it.currentValue
}

func (it *ValueIterator) Idx() int {
obscuren's avatar
obscuren committed
400
	return it.idx - 1
401
}