value.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package common
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "math/big"
  7. "reflect"
  8. "strconv"
  9. "github.com/ethereum/go-ethereum/rlp"
  10. )
  11. // Value can hold values of certain basic types and provides ways to
  12. // convert between types without bothering to check whether the
  13. // conversion is actually meaningful.
  14. //
  15. // It currently supports the following types:
  16. //
  17. // - int{,8,16,32,64}
  18. // - uint{,8,16,32,64}
  19. // - *big.Int
  20. // - []byte, string
  21. // - []interface{}
  22. //
  23. // Value is useful whenever you feel that Go's types limit your
  24. // ability to express yourself. In these situations, use Value and
  25. // forget about this strong typing nonsense.
  26. type Value struct{ Val interface{} }
  27. func (val *Value) String() string {
  28. return fmt.Sprintf("%x", val.Val)
  29. }
  30. func NewValue(val interface{}) *Value {
  31. t := val
  32. if v, ok := val.(*Value); ok {
  33. t = v.Val
  34. }
  35. return &Value{Val: t}
  36. }
  37. func (val *Value) Type() reflect.Kind {
  38. return reflect.TypeOf(val.Val).Kind()
  39. }
  40. func (val *Value) IsNil() bool {
  41. return val.Val == nil
  42. }
  43. func (val *Value) Len() int {
  44. if data, ok := val.Val.([]interface{}); ok {
  45. return len(data)
  46. }
  47. return len(val.Bytes())
  48. }
  49. func (val *Value) Uint() uint64 {
  50. if Val, ok := val.Val.(uint8); ok {
  51. return uint64(Val)
  52. } else if Val, ok := val.Val.(uint16); ok {
  53. return uint64(Val)
  54. } else if Val, ok := val.Val.(uint32); ok {
  55. return uint64(Val)
  56. } else if Val, ok := val.Val.(uint64); ok {
  57. return Val
  58. } else if Val, ok := val.Val.(float32); ok {
  59. return uint64(Val)
  60. } else if Val, ok := val.Val.(float64); ok {
  61. return uint64(Val)
  62. } else if Val, ok := val.Val.(int); ok {
  63. return uint64(Val)
  64. } else if Val, ok := val.Val.(uint); ok {
  65. return uint64(Val)
  66. } else if Val, ok := val.Val.([]byte); ok {
  67. return new(big.Int).SetBytes(Val).Uint64()
  68. } else if Val, ok := val.Val.(*big.Int); ok {
  69. return Val.Uint64()
  70. }
  71. return 0
  72. }
  73. func (val *Value) Int() int64 {
  74. if Val, ok := val.Val.(int8); ok {
  75. return int64(Val)
  76. } else if Val, ok := val.Val.(int16); ok {
  77. return int64(Val)
  78. } else if Val, ok := val.Val.(int32); ok {
  79. return int64(Val)
  80. } else if Val, ok := val.Val.(int64); ok {
  81. return Val
  82. } else if Val, ok := val.Val.(int); ok {
  83. return int64(Val)
  84. } else if Val, ok := val.Val.(float32); ok {
  85. return int64(Val)
  86. } else if Val, ok := val.Val.(float64); ok {
  87. return int64(Val)
  88. } else if Val, ok := val.Val.([]byte); ok {
  89. return new(big.Int).SetBytes(Val).Int64()
  90. } else if Val, ok := val.Val.(*big.Int); ok {
  91. return Val.Int64()
  92. } else if Val, ok := val.Val.(string); ok {
  93. n, _ := strconv.Atoi(Val)
  94. return int64(n)
  95. }
  96. return 0
  97. }
  98. func (val *Value) Byte() byte {
  99. if Val, ok := val.Val.(byte); ok {
  100. return Val
  101. }
  102. return 0x0
  103. }
  104. func (val *Value) BigInt() *big.Int {
  105. if a, ok := val.Val.([]byte); ok {
  106. b := new(big.Int).SetBytes(a)
  107. return b
  108. } else if a, ok := val.Val.(*big.Int); ok {
  109. return a
  110. } else if a, ok := val.Val.(string); ok {
  111. return Big(a)
  112. } else {
  113. return big.NewInt(int64(val.Uint()))
  114. }
  115. return big.NewInt(0)
  116. }
  117. func (val *Value) Str() string {
  118. if a, ok := val.Val.([]byte); ok {
  119. return string(a)
  120. } else if a, ok := val.Val.(string); ok {
  121. return a
  122. } else if a, ok := val.Val.(byte); ok {
  123. return string(a)
  124. }
  125. return ""
  126. }
  127. func (val *Value) Bytes() []byte {
  128. if a, ok := val.Val.([]byte); ok {
  129. return a
  130. } else if s, ok := val.Val.(byte); ok {
  131. return []byte{s}
  132. } else if s, ok := val.Val.(string); ok {
  133. return []byte(s)
  134. } else if s, ok := val.Val.(*big.Int); ok {
  135. return s.Bytes()
  136. } else {
  137. return big.NewInt(val.Int()).Bytes()
  138. }
  139. return []byte{}
  140. }
  141. func (val *Value) Err() error {
  142. if err, ok := val.Val.(error); ok {
  143. return err
  144. }
  145. return nil
  146. }
  147. func (val *Value) Slice() []interface{} {
  148. if d, ok := val.Val.([]interface{}); ok {
  149. return d
  150. }
  151. return []interface{}{}
  152. }
  153. func (val *Value) SliceFrom(from int) *Value {
  154. slice := val.Slice()
  155. return NewValue(slice[from:])
  156. }
  157. func (val *Value) SliceTo(to int) *Value {
  158. slice := val.Slice()
  159. return NewValue(slice[:to])
  160. }
  161. func (val *Value) SliceFromTo(from, to int) *Value {
  162. slice := val.Slice()
  163. return NewValue(slice[from:to])
  164. }
  165. // TODO More type checking methods
  166. func (val *Value) IsSlice() bool {
  167. return val.Type() == reflect.Slice
  168. }
  169. func (val *Value) IsStr() bool {
  170. return val.Type() == reflect.String
  171. }
  172. func (self *Value) IsErr() bool {
  173. _, ok := self.Val.(error)
  174. return ok
  175. }
  176. // Special list checking function. Something is considered
  177. // a list if it's of type []interface{}. The list is usually
  178. // used in conjunction with rlp decoded streams.
  179. func (val *Value) IsList() bool {
  180. _, ok := val.Val.([]interface{})
  181. return ok
  182. }
  183. func (val *Value) IsEmpty() bool {
  184. return val.Val == nil || ((val.IsSlice() || val.IsStr()) && val.Len() == 0)
  185. }
  186. // Threat the value as a slice
  187. func (val *Value) Get(idx int) *Value {
  188. if d, ok := val.Val.([]interface{}); ok {
  189. // Guard for oob
  190. if len(d) <= idx {
  191. return NewValue(nil)
  192. }
  193. if idx < 0 {
  194. return NewValue(nil)
  195. }
  196. return NewValue(d[idx])
  197. }
  198. // If this wasn't a slice you probably shouldn't be using this function
  199. return NewValue(nil)
  200. }
  201. func (self *Value) Copy() *Value {
  202. switch val := self.Val.(type) {
  203. case *big.Int:
  204. return NewValue(new(big.Int).Set(val))
  205. case []byte:
  206. return NewValue(CopyBytes(val))
  207. default:
  208. return NewValue(self.Val)
  209. }
  210. return nil
  211. }
  212. func (val *Value) Cmp(o *Value) bool {
  213. return reflect.DeepEqual(val.Val, o.Val)
  214. }
  215. func (self *Value) DeepCmp(o *Value) bool {
  216. return bytes.Compare(self.Bytes(), o.Bytes()) == 0
  217. }
  218. func (self *Value) DecodeRLP(s *rlp.Stream) error {
  219. var v interface{}
  220. if err := s.Decode(&v); err != nil {
  221. return err
  222. }
  223. self.Val = v
  224. return nil
  225. }
  226. func (self *Value) EncodeRLP(w io.Writer) error {
  227. if self == nil {
  228. w.Write(rlp.EmptyList)
  229. return nil
  230. } else {
  231. return rlp.Encode(w, self.Val)
  232. }
  233. }
  234. // NewValueFromBytes decodes RLP data.
  235. // The contained value will be nil if data contains invalid RLP.
  236. func NewValueFromBytes(data []byte) *Value {
  237. v := new(Value)
  238. if len(data) != 0 {
  239. if err := rlp.DecodeBytes(data, v); err != nil {
  240. v.Val = nil
  241. }
  242. }
  243. return v
  244. }
  245. // Value setters
  246. func NewSliceValue(s interface{}) *Value {
  247. list := EmptyValue()
  248. if s != nil {
  249. if slice, ok := s.([]interface{}); ok {
  250. for _, val := range slice {
  251. list.Append(val)
  252. }
  253. } else if slice, ok := s.([]string); ok {
  254. for _, val := range slice {
  255. list.Append(val)
  256. }
  257. }
  258. }
  259. return list
  260. }
  261. func EmptyValue() *Value {
  262. return NewValue([]interface{}{})
  263. }
  264. func (val *Value) AppendList() *Value {
  265. list := EmptyValue()
  266. val.Val = append(val.Slice(), list)
  267. return list
  268. }
  269. func (val *Value) Append(v interface{}) *Value {
  270. val.Val = append(val.Slice(), v)
  271. return val
  272. }
  273. const (
  274. valOpAdd = iota
  275. valOpDiv
  276. valOpMul
  277. valOpPow
  278. valOpSub
  279. )
  280. // Math stuff
  281. func (self *Value) doOp(op int, other interface{}) *Value {
  282. left := self.BigInt()
  283. right := NewValue(other).BigInt()
  284. switch op {
  285. case valOpAdd:
  286. self.Val = left.Add(left, right)
  287. case valOpDiv:
  288. self.Val = left.Div(left, right)
  289. case valOpMul:
  290. self.Val = left.Mul(left, right)
  291. case valOpPow:
  292. self.Val = left.Exp(left, right, Big0)
  293. case valOpSub:
  294. self.Val = left.Sub(left, right)
  295. }
  296. return self
  297. }
  298. func (self *Value) Add(other interface{}) *Value {
  299. return self.doOp(valOpAdd, other)
  300. }
  301. func (self *Value) Sub(other interface{}) *Value {
  302. return self.doOp(valOpSub, other)
  303. }
  304. func (self *Value) Div(other interface{}) *Value {
  305. return self.doOp(valOpDiv, other)
  306. }
  307. func (self *Value) Mul(other interface{}) *Value {
  308. return self.doOp(valOpMul, other)
  309. }
  310. func (self *Value) Pow(other interface{}) *Value {
  311. return self.doOp(valOpPow, other)
  312. }
  313. type ValueIterator struct {
  314. value *Value
  315. currentValue *Value
  316. idx int
  317. }
  318. func (val *Value) NewIterator() *ValueIterator {
  319. return &ValueIterator{value: val}
  320. }
  321. func (it *ValueIterator) Len() int {
  322. return it.value.Len()
  323. }
  324. func (it *ValueIterator) Next() bool {
  325. if it.idx >= it.value.Len() {
  326. return false
  327. }
  328. it.currentValue = it.value.Get(it.idx)
  329. it.idx++
  330. return true
  331. }
  332. func (it *ValueIterator) Value() *Value {
  333. return it.currentValue
  334. }
  335. func (it *ValueIterator) Idx() int {
  336. return it.idx - 1
  337. }