github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/error.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:31</date> 10 //</624450061845139456> 11 12 13 package abi 14 15 import ( 16 "errors" 17 "fmt" 18 "reflect" 19 ) 20 21 var ( 22 errBadBool = errors.New("abi: improperly encoded boolean value") 23 ) 24 25 //FormatSliceString将反射类型格式化为给定的切片大小 26 //并返回格式化的字符串表示形式。 27 func formatSliceString(kind reflect.Kind, sliceSize int) string { 28 if sliceSize == -1 { 29 return fmt.Sprintf("[]%v", kind) 30 } 31 return fmt.Sprintf("[%d]%v", sliceSize, kind) 32 } 33 34 //slicetypecheck检查给定切片是否可以通过分配给反射 35 //T型。 36 func sliceTypeCheck(t Type, val reflect.Value) error { 37 if val.Kind() != reflect.Slice && val.Kind() != reflect.Array { 38 return typeErr(formatSliceString(t.Kind, t.Size), val.Type()) 39 } 40 41 if t.T == ArrayTy && val.Len() != t.Size { 42 return typeErr(formatSliceString(t.Elem.Kind, t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len())) 43 } 44 45 if t.Elem.T == SliceTy { 46 if val.Len() > 0 { 47 return sliceTypeCheck(*t.Elem, val.Index(0)) 48 } 49 } else if t.Elem.T == ArrayTy { 50 return sliceTypeCheck(*t.Elem, val.Index(0)) 51 } 52 53 if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind { 54 return typeErr(formatSliceString(t.Elem.Kind, t.Size), val.Type()) 55 } 56 return nil 57 } 58 59 //类型检查检查给定的反射值是否可以分配给反射 60 //T型。 61 func typeCheck(t Type, value reflect.Value) error { 62 if t.T == SliceTy || t.T == ArrayTy { 63 return sliceTypeCheck(t, value) 64 } 65 66 //检查基类型有效性。稍后将检查元素类型。 67 if t.Kind != value.Kind() { 68 return typeErr(t.Kind, value.Kind()) 69 } else if t.T == FixedBytesTy && t.Size != value.Len() { 70 return typeErr(t.Type, value.Type()) 71 } else { 72 return nil 73 } 74 75 } 76 77 //类型错误返回格式化的类型转换错误。 78 func typeErr(expected, got interface{}) error { 79 return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected) 80 } 81