github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/error.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package abi
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"reflect"
    24  )
    25  
    26  var (
    27  	errBadBool = errors.New("abi: improperly encoded boolean value")
    28  )
    29  
    30  // formatSliceString formats the reflection kind with the given slice size
    31  // and returns a formatted string representation.
    32  func formatSliceString(kind reflect.Kind, sliceSize int) string {
    33  	if sliceSize == -1 {
    34  		return fmt.Sprintf("[]%v", kind)
    35  	}
    36  	return fmt.Sprintf("[%d]%v", sliceSize, kind)
    37  }
    38  
    39  // sliceTypeCheck checks that the given slice can by assigned to the reflection
    40  // type in t.
    41  func sliceTypeCheck(t Type, val reflect.Value) error {
    42  	if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
    43  		return typeErr(formatSliceString(t.Kind, t.Size), val.Type())
    44  	}
    45  
    46  	if t.T == ArrayTy && val.Len() != t.Size {
    47  		return typeErr(formatSliceString(t.Elem.Kind, t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
    48  	}
    49  
    50  	if t.Elem.T == SliceTy {
    51  		if val.Len() > 0 {
    52  			return sliceTypeCheck(*t.Elem, val.Index(0))
    53  		}
    54  	} else if t.Elem.T == ArrayTy {
    55  		return sliceTypeCheck(*t.Elem, val.Index(0))
    56  	}
    57  
    58  	if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind {
    59  		return typeErr(formatSliceString(t.Elem.Kind, t.Size), val.Type())
    60  	}
    61  	return nil
    62  }
    63  
    64  // typeCheck checks that the given reflection value can be assigned to the reflection
    65  // type in t.
    66  func typeCheck(t Type, value reflect.Value) error {
    67  	if t.T == SliceTy || t.T == ArrayTy {
    68  		return sliceTypeCheck(t, value)
    69  	}
    70  
    71  	// Check base type validity. Element types will be checked later on.
    72  	if t.Kind != value.Kind() {
    73  		return typeErr(t.Kind, value.Kind())
    74  	} else if t.T == FixedBytesTy && t.Size != value.Len() {
    75  		return typeErr(t.Type, value.Type())
    76  	} else {
    77  		return nil
    78  	}
    79  
    80  }
    81  
    82  // typeErr returns a formatted type casting error.
    83  func typeErr(expected, got interface{}) error {
    84  	return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected)
    85  }