github.com/karalabe/go-ethereum@v0.8.5/ethutil/value.go (about)

     1  package ethutil
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"math/big"
     7  	"reflect"
     8  	"strconv"
     9  )
    10  
    11  // Data values are returned by the rlp decoder. The data values represents
    12  // one item within the rlp data structure. It's responsible for all the casting
    13  // It always returns something valid
    14  type Value struct {
    15  	Val  interface{}
    16  	kind reflect.Value
    17  }
    18  
    19  func (val *Value) String() string {
    20  	return fmt.Sprintf("%x", val.Val)
    21  }
    22  
    23  func NewValue(val interface{}) *Value {
    24  	t := val
    25  	if v, ok := val.(*Value); ok {
    26  		t = v.Val
    27  	}
    28  
    29  	return &Value{Val: t}
    30  }
    31  
    32  func (val *Value) Type() reflect.Kind {
    33  	return reflect.TypeOf(val.Val).Kind()
    34  }
    35  
    36  func (val *Value) IsNil() bool {
    37  	return val.Val == nil
    38  }
    39  
    40  func (val *Value) Len() int {
    41  	//return val.kind.Len()
    42  	if data, ok := val.Val.([]interface{}); ok {
    43  		return len(data)
    44  	}
    45  
    46  	return len(val.Bytes())
    47  }
    48  
    49  func (val *Value) Raw() interface{} {
    50  	return val.Val
    51  }
    52  
    53  func (val *Value) Interface() interface{} {
    54  	return val.Val
    55  }
    56  
    57  func (val *Value) Uint() uint64 {
    58  	if Val, ok := val.Val.(uint8); ok {
    59  		return uint64(Val)
    60  	} else if Val, ok := val.Val.(uint16); ok {
    61  		return uint64(Val)
    62  	} else if Val, ok := val.Val.(uint32); ok {
    63  		return uint64(Val)
    64  	} else if Val, ok := val.Val.(uint64); ok {
    65  		return Val
    66  	} else if Val, ok := val.Val.(float32); ok {
    67  		return uint64(Val)
    68  	} else if Val, ok := val.Val.(float64); ok {
    69  		return uint64(Val)
    70  	} else if Val, ok := val.Val.(int); ok {
    71  		return uint64(Val)
    72  	} else if Val, ok := val.Val.(uint); ok {
    73  		return uint64(Val)
    74  	} else if Val, ok := val.Val.([]byte); ok {
    75  		return new(big.Int).SetBytes(Val).Uint64()
    76  	} else if Val, ok := val.Val.(*big.Int); ok {
    77  		return Val.Uint64()
    78  	}
    79  
    80  	return 0
    81  }
    82  
    83  func (val *Value) Int() int64 {
    84  	if Val, ok := val.Val.(int8); ok {
    85  		return int64(Val)
    86  	} else if Val, ok := val.Val.(int16); ok {
    87  		return int64(Val)
    88  	} else if Val, ok := val.Val.(int32); ok {
    89  		return int64(Val)
    90  	} else if Val, ok := val.Val.(int64); ok {
    91  		return Val
    92  	} else if Val, ok := val.Val.(int); ok {
    93  		return int64(Val)
    94  	} else if Val, ok := val.Val.(float32); ok {
    95  		return int64(Val)
    96  	} else if Val, ok := val.Val.(float64); ok {
    97  		return int64(Val)
    98  	} else if Val, ok := val.Val.([]byte); ok {
    99  		return new(big.Int).SetBytes(Val).Int64()
   100  	} else if Val, ok := val.Val.(*big.Int); ok {
   101  		return Val.Int64()
   102  	} else if Val, ok := val.Val.(string); ok {
   103  		n, _ := strconv.Atoi(Val)
   104  		return int64(n)
   105  	}
   106  
   107  	return 0
   108  }
   109  
   110  func (val *Value) Byte() byte {
   111  	if Val, ok := val.Val.(byte); ok {
   112  		return Val
   113  	}
   114  
   115  	return 0x0
   116  }
   117  
   118  func (val *Value) BigInt() *big.Int {
   119  	if a, ok := val.Val.([]byte); ok {
   120  		b := new(big.Int).SetBytes(a)
   121  
   122  		return b
   123  	} else if a, ok := val.Val.(*big.Int); ok {
   124  		return a
   125  	} else if a, ok := val.Val.(string); ok {
   126  		return Big(a)
   127  	} else {
   128  		return big.NewInt(int64(val.Uint()))
   129  	}
   130  
   131  	return big.NewInt(0)
   132  }
   133  
   134  func (val *Value) Str() string {
   135  	if a, ok := val.Val.([]byte); ok {
   136  		return string(a)
   137  	} else if a, ok := val.Val.(string); ok {
   138  		return a
   139  	} else if a, ok := val.Val.(byte); ok {
   140  		return string(a)
   141  	}
   142  
   143  	return ""
   144  }
   145  
   146  func (val *Value) Bytes() []byte {
   147  	if a, ok := val.Val.([]byte); ok {
   148  		return a
   149  	} else if s, ok := val.Val.(byte); ok {
   150  		return []byte{s}
   151  	} else if s, ok := val.Val.(string); ok {
   152  		return []byte(s)
   153  	} else if s, ok := val.Val.(*big.Int); ok {
   154  		return s.Bytes()
   155  	} else {
   156  		return big.NewInt(val.Int()).Bytes()
   157  	}
   158  
   159  	return []byte{}
   160  }
   161  
   162  func (val *Value) Err() error {
   163  	if err, ok := val.Val.(error); ok {
   164  		return err
   165  	}
   166  
   167  	return nil
   168  }
   169  
   170  func (val *Value) Slice() []interface{} {
   171  	if d, ok := val.Val.([]interface{}); ok {
   172  		return d
   173  	}
   174  
   175  	return []interface{}{}
   176  }
   177  
   178  func (val *Value) SliceFrom(from int) *Value {
   179  	slice := val.Slice()
   180  
   181  	return NewValue(slice[from:])
   182  }
   183  
   184  func (val *Value) SliceTo(to int) *Value {
   185  	slice := val.Slice()
   186  
   187  	return NewValue(slice[:to])
   188  }
   189  
   190  func (val *Value) SliceFromTo(from, to int) *Value {
   191  	slice := val.Slice()
   192  
   193  	return NewValue(slice[from:to])
   194  }
   195  
   196  // TODO More type checking methods
   197  func (val *Value) IsSlice() bool {
   198  	return val.Type() == reflect.Slice
   199  }
   200  
   201  func (val *Value) IsStr() bool {
   202  	return val.Type() == reflect.String
   203  }
   204  
   205  func (self *Value) IsErr() bool {
   206  	_, ok := self.Val.(error)
   207  	return ok
   208  }
   209  
   210  // Special list checking function. Something is considered
   211  // a list if it's of type []interface{}. The list is usually
   212  // used in conjunction with rlp decoded streams.
   213  func (val *Value) IsList() bool {
   214  	_, ok := val.Val.([]interface{})
   215  
   216  	return ok
   217  }
   218  
   219  func (val *Value) IsEmpty() bool {
   220  	return val.Val == nil || ((val.IsSlice() || val.IsStr()) && val.Len() == 0)
   221  }
   222  
   223  // Threat the value as a slice
   224  func (val *Value) Get(idx int) *Value {
   225  	if d, ok := val.Val.([]interface{}); ok {
   226  		// Guard for oob
   227  		if len(d) <= idx {
   228  			return NewValue(nil)
   229  		}
   230  
   231  		if idx < 0 {
   232  			return NewValue(nil)
   233  		}
   234  
   235  		return NewValue(d[idx])
   236  	}
   237  
   238  	// If this wasn't a slice you probably shouldn't be using this function
   239  	return NewValue(nil)
   240  }
   241  
   242  func (self *Value) Copy() *Value {
   243  	switch val := self.Val.(type) {
   244  	case *big.Int:
   245  		return NewValue(new(big.Int).Set(val))
   246  	case []byte:
   247  		return NewValue(CopyBytes(val))
   248  	default:
   249  		return NewValue(self.Val)
   250  	}
   251  
   252  	return nil
   253  }
   254  
   255  func (val *Value) Cmp(o *Value) bool {
   256  	return reflect.DeepEqual(val.Val, o.Val)
   257  }
   258  
   259  func (self *Value) DeepCmp(o *Value) bool {
   260  	return bytes.Compare(self.Bytes(), o.Bytes()) == 0
   261  }
   262  
   263  func (val *Value) Encode() []byte {
   264  	return Encode(val.Val)
   265  }
   266  
   267  // Assume that the data we have is encoded
   268  func (self *Value) Decode() {
   269  	v, _ := Decode(self.Bytes(), 0)
   270  	self.Val = v
   271  	//self.Val = DecodeWithReader(bytes.NewBuffer(self.Bytes()))
   272  }
   273  
   274  func NewValueFromBytes(data []byte) *Value {
   275  	if len(data) != 0 {
   276  		value := NewValue(data)
   277  		value.Decode()
   278  
   279  		return value
   280  	}
   281  
   282  	return NewValue(nil)
   283  }
   284  
   285  // Value setters
   286  func NewSliceValue(s interface{}) *Value {
   287  	list := EmptyValue()
   288  
   289  	if s != nil {
   290  		if slice, ok := s.([]interface{}); ok {
   291  			for _, val := range slice {
   292  				list.Append(val)
   293  			}
   294  		} else if slice, ok := s.([]string); ok {
   295  			for _, val := range slice {
   296  				list.Append(val)
   297  			}
   298  		}
   299  	}
   300  
   301  	return list
   302  }
   303  
   304  func EmptyValue() *Value {
   305  	return NewValue([]interface{}{})
   306  }
   307  
   308  func (val *Value) AppendList() *Value {
   309  	list := EmptyValue()
   310  	val.Val = append(val.Slice(), list)
   311  
   312  	return list
   313  }
   314  
   315  func (val *Value) Append(v interface{}) *Value {
   316  	val.Val = append(val.Slice(), v)
   317  
   318  	return val
   319  }
   320  
   321  const (
   322  	valOpAdd = iota
   323  	valOpDiv
   324  	valOpMul
   325  	valOpPow
   326  	valOpSub
   327  )
   328  
   329  // Math stuff
   330  func (self *Value) doOp(op int, other interface{}) *Value {
   331  	left := self.BigInt()
   332  	right := NewValue(other).BigInt()
   333  
   334  	switch op {
   335  	case valOpAdd:
   336  		self.Val = left.Add(left, right)
   337  	case valOpDiv:
   338  		self.Val = left.Div(left, right)
   339  	case valOpMul:
   340  		self.Val = left.Mul(left, right)
   341  	case valOpPow:
   342  		self.Val = left.Exp(left, right, Big0)
   343  	case valOpSub:
   344  		self.Val = left.Sub(left, right)
   345  	}
   346  
   347  	return self
   348  }
   349  
   350  func (self *Value) Add(other interface{}) *Value {
   351  	return self.doOp(valOpAdd, other)
   352  }
   353  
   354  func (self *Value) Sub(other interface{}) *Value {
   355  	return self.doOp(valOpSub, other)
   356  }
   357  
   358  func (self *Value) Div(other interface{}) *Value {
   359  	return self.doOp(valOpDiv, other)
   360  }
   361  
   362  func (self *Value) Mul(other interface{}) *Value {
   363  	return self.doOp(valOpMul, other)
   364  }
   365  
   366  func (self *Value) Pow(other interface{}) *Value {
   367  	return self.doOp(valOpPow, other)
   368  }
   369  
   370  type ValueIterator struct {
   371  	value        *Value
   372  	currentValue *Value
   373  	idx          int
   374  }
   375  
   376  func (val *Value) NewIterator() *ValueIterator {
   377  	return &ValueIterator{value: val}
   378  }
   379  
   380  func (it *ValueIterator) Len() int {
   381  	return it.value.Len()
   382  }
   383  
   384  func (it *ValueIterator) Next() bool {
   385  	if it.idx >= it.value.Len() {
   386  		return false
   387  	}
   388  
   389  	it.currentValue = it.value.Get(it.idx)
   390  	it.idx++
   391  
   392  	return true
   393  }
   394  
   395  func (it *ValueIterator) Value() *Value {
   396  	return it.currentValue
   397  }
   398  
   399  func (it *ValueIterator) Idx() int {
   400  	return it.idx - 1
   401  }