github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/accounts/abi/reflect.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  )
    23  
    24  // indirect recursively dereferences the value until it either gets the value
    25  // or finds a big.Int
    26  func indirect(v reflect.Value) reflect.Value {
    27  	if v.Kind() == reflect.Ptr && v.Elem().Type() != big_t {
    28  		return indirect(v.Elem())
    29  	}
    30  	return v
    31  }
    32  
    33  // reflectIntKind returns the reflect using the given size and
    34  // unsignedness.
    35  func reflectIntKind(unsigned bool, size int) reflect.Kind {
    36  	switch size {
    37  	case 8:
    38  		if unsigned {
    39  			return reflect.Uint8
    40  		}
    41  		return reflect.Int8
    42  	case 16:
    43  		if unsigned {
    44  			return reflect.Uint16
    45  		}
    46  		return reflect.Int16
    47  	case 32:
    48  		if unsigned {
    49  			return reflect.Uint32
    50  		}
    51  		return reflect.Int32
    52  	case 64:
    53  		if unsigned {
    54  			return reflect.Uint64
    55  		}
    56  		return reflect.Int64
    57  	}
    58  	return reflect.Ptr
    59  }
    60  
    61  // mustArrayToBytesSlice creates a new byte slice with the exact same size as value
    62  // and copies the bytes in value to the new slice.
    63  func mustArrayToByteSlice(value reflect.Value) reflect.Value {
    64  	slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len())
    65  	reflect.Copy(slice, value)
    66  	return slice
    67  }
    68  
    69  // set attempts to assign src to dst by either setting, copying or otherwise.
    70  //
    71  // set is a bit more lenient when it comes to assignment and doesn't force an as
    72  // strict ruleset as bare `reflect` does.
    73  func set(dst, src reflect.Value, output Argument) error {
    74  	dstType := dst.Type()
    75  	srcType := src.Type()
    76  
    77  	switch {
    78  	case dstType.AssignableTo(src.Type()):
    79  		dst.Set(src)
    80  	case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice:
    81  		if !dstType.Elem().AssignableTo(r_byte) {
    82  			return fmt.Errorf("abi: cannot unmarshal %v in to array of elem %v", src.Type(), dstType.Elem())
    83  		}
    84  
    85  		if dst.Len() < output.Type.SliceSize {
    86  			return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.SliceSize, dst.Len())
    87  		}
    88  		reflect.Copy(dst, src)
    89  	case dstType.Kind() == reflect.Interface:
    90  		dst.Set(src)
    91  	case dstType.Kind() == reflect.Ptr:
    92  		return set(dst.Elem(), src, output)
    93  	default:
    94  		return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
    95  	}
    96  	return nil
    97  }