Unverified Commit e872083d authored by Marius van der Wijden's avatar Marius van der Wijden Committed by GitHub

accounts/abi: removed Kind from Type struct (#21009)

* accounts/abi: removed Kind from Type struct

* accounts/abi: removed unused code
parent 510b6f90
......@@ -39,11 +39,11 @@ func formatSliceString(kind reflect.Kind, sliceSize int) string {
// type in t.
func sliceTypeCheck(t Type, val reflect.Value) error {
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
return typeErr(formatSliceString(t.Kind, t.Size), val.Type())
return typeErr(formatSliceString(t.Type.Kind(), t.Size), val.Type())
}
if t.T == ArrayTy && val.Len() != t.Size {
return typeErr(formatSliceString(t.Elem.Kind, t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
return typeErr(formatSliceString(t.Elem.Type.Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
}
if t.Elem.T == SliceTy || t.Elem.T == ArrayTy {
......@@ -52,8 +52,8 @@ func sliceTypeCheck(t Type, val reflect.Value) error {
}
}
if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind {
return typeErr(formatSliceString(t.Elem.Kind, t.Size), val.Type())
if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Type.Kind() {
return typeErr(formatSliceString(t.Elem.Type.Kind(), t.Size), val.Type())
}
return nil
}
......@@ -66,8 +66,8 @@ func typeCheck(t Type, value reflect.Value) error {
}
// Check base type validity. Element types will be checked later on.
if t.Kind != value.Kind() {
return typeErr(t.Kind, value.Kind())
if t.Type.Kind() != value.Kind() {
return typeErr(t.Type.Kind(), value.Kind())
} else if t.T == FixedBytesTy && t.Size != value.Len() {
return typeErr(t.Type, value.Type())
} else {
......
......@@ -39,32 +39,32 @@ func indirectInterfaceOrPtr(v reflect.Value) reflect.Value {
return v
}
// reflectIntKind returns the reflect using the given size and
// reflectIntType returns the reflect using the given size and
// unsignedness.
func reflectIntKindAndType(unsigned bool, size int) (reflect.Kind, reflect.Type) {
func reflectIntType(unsigned bool, size int) reflect.Type {
if unsigned {
switch size {
case 8:
return reflect.Uint8, uint8T
return uint8T
case 16:
return reflect.Uint16, uint16T
return uint16T
case 32:
return reflect.Uint32, uint32T
return uint32T
case 64:
return reflect.Uint64, uint64T
return uint64T
}
}
switch size {
case 8:
return reflect.Int8, int8T
return int8T
case 16:
return reflect.Int16, int16T
return int16T
case 32:
return reflect.Int32, int32T
return int32T
case 64:
return reflect.Int64, int64T
return int64T
}
return reflect.Ptr, bigT
return bigT
}
// mustArrayToBytesSlice creates a new byte slice with the exact same size as value
......
......@@ -45,7 +45,6 @@ const (
// Type is the reflection of the supported argument type
type Type struct {
Elem *Type
Kind reflect.Kind
Type reflect.Type
Size int
T byte // Our own type checking
......@@ -94,14 +93,12 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
if len(intz) == 0 {
// is a slice
typ.T = SliceTy
typ.Kind = reflect.Slice
typ.Elem = &embeddedType
typ.Type = reflect.SliceOf(embeddedType.Type)
typ.stringKind = embeddedType.stringKind + sliced
} else if len(intz) == 1 {
// is a array
typ.T = ArrayTy
typ.Kind = reflect.Array
typ.Elem = &embeddedType
typ.Size, err = strconv.Atoi(intz[0])
if err != nil {
......@@ -139,34 +136,29 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
// varType is the parsed abi type
switch varType := parsedType[1]; varType {
case "int":
typ.Kind, typ.Type = reflectIntKindAndType(false, varSize)
typ.Type = reflectIntType(false, varSize)
typ.Size = varSize
typ.T = IntTy
case "uint":
typ.Kind, typ.Type = reflectIntKindAndType(true, varSize)
typ.Type = reflectIntType(true, varSize)
typ.Size = varSize
typ.T = UintTy
case "bool":
typ.Kind = reflect.Bool
typ.T = BoolTy
typ.Type = reflect.TypeOf(bool(false))
case "address":
typ.Kind = reflect.Array
typ.Type = addressT
typ.Size = 20
typ.T = AddressTy
case "string":
typ.Kind = reflect.String
typ.Type = reflect.TypeOf("")
typ.T = StringTy
case "bytes":
if varSize == 0 {
typ.T = BytesTy
typ.Kind = reflect.Slice
typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0)))
} else {
typ.T = FixedBytesTy
typ.Kind = reflect.Array
typ.Size = varSize
typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0)))
}
......@@ -199,7 +191,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
}
}
expression += ")"
typ.Kind = reflect.Struct
typ.Type = reflect.StructOf(fields)
typ.TupleElems = elems
typ.TupleRawNames = names
......@@ -217,7 +208,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
}
case "function":
typ.Kind = reflect.Array
typ.T = FunctionTy
typ.Size = 24
typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
......
This diff is collapsed.
......@@ -33,28 +33,28 @@ var (
)
// ReadInteger reads the integer based on its kind and returns the appropriate value
func ReadInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
switch kind {
case reflect.Uint8:
func ReadInteger(typ Type, b []byte) interface{} {
switch typ.Type {
case uint8T:
return b[len(b)-1]
case reflect.Uint16:
case uint16T:
return binary.BigEndian.Uint16(b[len(b)-2:])
case reflect.Uint32:
case uint32T:
return binary.BigEndian.Uint32(b[len(b)-4:])
case reflect.Uint64:
case uint64T:
return binary.BigEndian.Uint64(b[len(b)-8:])
case reflect.Int8:
case int8T:
return int8(b[len(b)-1])
case reflect.Int16:
case int16T:
return int16(binary.BigEndian.Uint16(b[len(b)-2:]))
case reflect.Int32:
case int32T:
return int32(binary.BigEndian.Uint32(b[len(b)-4:]))
case reflect.Int64:
case int64T:
return int64(binary.BigEndian.Uint64(b[len(b)-8:]))
default:
// the only case left for integer is int256/uint256.
ret := new(big.Int).SetBytes(b)
if typ == UintTy {
if typ.T == UintTy {
return ret
}
// big.SetBytes can't tell if a number is negative or positive in itself.
......@@ -228,7 +228,7 @@ func ToGoType(index int, t Type, output []byte) (interface{}, error) {
case StringTy: // variable arrays are written at the end of the return bytes
return string(output[begin : begin+length]), nil
case IntTy, UintTy:
return ReadInteger(t.T, t.Kind, returnOutput), nil
return ReadInteger(t, returnOutput), nil
case BoolTy:
return readBool(returnOutput)
case AddressTy:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment