github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/accounts/abi/error.go (about) 1 // Copyright 2022 Commercium 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum 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-ethereum 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-ethereum 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.GetType().Kind(), t.Size), val.Type()) 44 } 45 46 if t.T == ArrayTy && val.Len() != t.Size { 47 return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len())) 48 } 49 50 if t.Elem.T == SliceTy || t.Elem.T == ArrayTy { 51 if val.Len() > 0 { 52 return sliceTypeCheck(*t.Elem, val.Index(0)) 53 } 54 } 55 56 if val.Type().Elem().Kind() != t.Elem.GetType().Kind() { 57 return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), val.Type()) 58 } 59 return nil 60 } 61 62 // typeCheck checks that the given reflection value can be assigned to the reflection 63 // type in t. 64 func typeCheck(t Type, value reflect.Value) error { 65 if t.T == SliceTy || t.T == ArrayTy { 66 return sliceTypeCheck(t, value) 67 } 68 69 // Check base type validity. Element types will be checked later on. 70 if t.GetType().Kind() != value.Kind() { 71 return typeErr(t.GetType().Kind(), value.Kind()) 72 } else if t.T == FixedBytesTy && t.Size != value.Len() { 73 return typeErr(t.GetType(), value.Type()) 74 } else { 75 return nil 76 } 77 78 } 79 80 // typeErr returns a formatted type casting error. 81 func typeErr(expected, got interface{}) error { 82 return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected) 83 }