github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/mobile/interface.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 // Contains perverted wrappers to allow crossing over empty interfaces. 13 14 package geth 15 16 import ( 17 "errors" 18 "math/big" 19 20 "github.com/Sberex/go-sberex/common" 21 ) 22 23 // Interface represents a wrapped version of Go's interface{}, with the capacity 24 // to store arbitrary data types. 25 // 26 // Since it's impossible to get the arbitrary-ness converted between Go and mobile 27 // platforms, we're using explicit getters and setters for the conversions. There 28 // is of course no point in enumerating everything, just enough to support the 29 // contract bindins requiring client side generated code. 30 type Interface struct { 31 object interface{} 32 } 33 34 // NewInterface creates a new empty interface that can be used to pass around 35 // generic types. 36 func NewInterface() *Interface { 37 return new(Interface) 38 } 39 40 func (i *Interface) SetBool(b bool) { i.object = &b } 41 func (i *Interface) SetBools(bs []bool) { i.object = &bs } 42 func (i *Interface) SetString(str string) { i.object = &str } 43 func (i *Interface) SetStrings(strs *Strings) { i.object = &strs.strs } 44 func (i *Interface) SetBinary(binary []byte) { b := common.CopyBytes(binary); i.object = &b } 45 func (i *Interface) SetBinaries(binaries [][]byte) { i.object = &binaries } 46 func (i *Interface) SetAddress(address *Address) { i.object = &address.address } 47 func (i *Interface) SetAddresses(addrs *Addresses) { i.object = &addrs.addresses } 48 func (i *Interface) SetHash(hash *Hash) { i.object = &hash.hash } 49 func (i *Interface) SetHashes(hashes *Hashes) { i.object = &hashes.hashes } 50 func (i *Interface) SetInt8(n int8) { i.object = &n } 51 func (i *Interface) SetInt16(n int16) { i.object = &n } 52 func (i *Interface) SetInt32(n int32) { i.object = &n } 53 func (i *Interface) SetInt64(n int64) { i.object = &n } 54 func (i *Interface) SetUint8(bigint *BigInt) { n := uint8(bigint.bigint.Uint64()); i.object = &n } 55 func (i *Interface) SetUint16(bigint *BigInt) { n := uint16(bigint.bigint.Uint64()); i.object = &n } 56 func (i *Interface) SetUint32(bigint *BigInt) { n := uint32(bigint.bigint.Uint64()); i.object = &n } 57 func (i *Interface) SetUint64(bigint *BigInt) { n := bigint.bigint.Uint64(); i.object = &n } 58 func (i *Interface) SetBigInt(bigint *BigInt) { i.object = &bigint.bigint } 59 func (i *Interface) SetBigInts(bigints *BigInts) { i.object = &bigints.bigints } 60 61 func (i *Interface) SetDefaultBool() { i.object = new(bool) } 62 func (i *Interface) SetDefaultBools() { i.object = new([]bool) } 63 func (i *Interface) SetDefaultString() { i.object = new(string) } 64 func (i *Interface) SetDefaultStrings() { i.object = new([]string) } 65 func (i *Interface) SetDefaultBinary() { i.object = new([]byte) } 66 func (i *Interface) SetDefaultBinaries() { i.object = new([][]byte) } 67 func (i *Interface) SetDefaultAddress() { i.object = new(common.Address) } 68 func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) } 69 func (i *Interface) SetDefaultHash() { i.object = new(common.Hash) } 70 func (i *Interface) SetDefaultHashes() { i.object = new([]common.Hash) } 71 func (i *Interface) SetDefaultInt8() { i.object = new(int8) } 72 func (i *Interface) SetDefaultInt16() { i.object = new(int16) } 73 func (i *Interface) SetDefaultInt32() { i.object = new(int32) } 74 func (i *Interface) SetDefaultInt64() { i.object = new(int64) } 75 func (i *Interface) SetDefaultUint8() { i.object = new(uint8) } 76 func (i *Interface) SetDefaultUint16() { i.object = new(uint16) } 77 func (i *Interface) SetDefaultUint32() { i.object = new(uint32) } 78 func (i *Interface) SetDefaultUint64() { i.object = new(uint64) } 79 func (i *Interface) SetDefaultBigInt() { i.object = new(*big.Int) } 80 func (i *Interface) SetDefaultBigInts() { i.object = new([]*big.Int) } 81 82 func (i *Interface) GetBool() bool { return *i.object.(*bool) } 83 func (i *Interface) GetBools() []bool { return *i.object.(*[]bool) } 84 func (i *Interface) GetString() string { return *i.object.(*string) } 85 func (i *Interface) GetStrings() *Strings { return &Strings{*i.object.(*[]string)} } 86 func (i *Interface) GetBinary() []byte { return *i.object.(*[]byte) } 87 func (i *Interface) GetBinaries() [][]byte { return *i.object.(*[][]byte) } 88 func (i *Interface) GetAddress() *Address { return &Address{*i.object.(*common.Address)} } 89 func (i *Interface) GetAddresses() *Addresses { return &Addresses{*i.object.(*[]common.Address)} } 90 func (i *Interface) GetHash() *Hash { return &Hash{*i.object.(*common.Hash)} } 91 func (i *Interface) GetHashes() *Hashes { return &Hashes{*i.object.(*[]common.Hash)} } 92 func (i *Interface) GetInt8() int8 { return *i.object.(*int8) } 93 func (i *Interface) GetInt16() int16 { return *i.object.(*int16) } 94 func (i *Interface) GetInt32() int32 { return *i.object.(*int32) } 95 func (i *Interface) GetInt64() int64 { return *i.object.(*int64) } 96 func (i *Interface) GetUint8() *BigInt { 97 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))} 98 } 99 func (i *Interface) GetUint16() *BigInt { 100 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint16)))} 101 } 102 func (i *Interface) GetUint32() *BigInt { 103 return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint32)))} 104 } 105 func (i *Interface) GetUint64() *BigInt { 106 return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))} 107 } 108 func (i *Interface) GetBigInt() *BigInt { return &BigInt{*i.object.(**big.Int)} } 109 func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} } 110 111 // Interfaces is a slices of wrapped generic objects. 112 type Interfaces struct { 113 objects []interface{} 114 } 115 116 // NewInterfaces creates a slice of uninitialized interfaces. 117 func NewInterfaces(size int) *Interfaces { 118 return &Interfaces{ 119 objects: make([]interface{}, size), 120 } 121 } 122 123 // Size returns the number of interfaces in the slice. 124 func (i *Interfaces) Size() int { 125 return len(i.objects) 126 } 127 128 // Get returns the bigint at the given index from the slice. 129 func (i *Interfaces) Get(index int) (iface *Interface, _ error) { 130 if index < 0 || index >= len(i.objects) { 131 return nil, errors.New("index out of bounds") 132 } 133 return &Interface{i.objects[index]}, nil 134 } 135 136 // Set sets the big int at the given index in the slice. 137 func (i *Interfaces) Set(index int, object *Interface) error { 138 if index < 0 || index >= len(i.objects) { 139 return errors.New("index out of bounds") 140 } 141 i.objects[index] = object.object 142 return nil 143 }