From 76d4ac1acbbafd282abd4dd3b791d90db8a8e442 Mon Sep 17 00:00:00 2001 From: Shude Li Date: Wed, 23 Aug 2023 21:53:38 +0800 Subject: [PATCH] account/abi: convert if-else-if chain to tagged switch (#27869) account/abi: conver if-else-if chain to tagged switch --- accounts/abi/method.go | 7 ++++--- accounts/abi/method_test.go | 7 ++++--- accounts/abi/unpack.go | 7 ++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/accounts/abi/method.go b/accounts/abi/method.go index f69e3ee9b..b6e1eef3c 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -127,11 +127,12 @@ func NewMethod(name string, rawName string, funType FunctionType, mutability str state = state + " " } identity := fmt.Sprintf("function %v", rawName) - if funType == Fallback { + switch funType { + case Fallback: identity = "fallback" - } else if funType == Receive { + case Receive: identity = "receive" - } else if funType == Constructor { + case Constructor: identity = "constructor" } str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", ")) diff --git a/accounts/abi/method_test.go b/accounts/abi/method_test.go index 395a52896..9230e307a 100644 --- a/accounts/abi/method_test.go +++ b/accounts/abi/method_test.go @@ -84,11 +84,12 @@ func TestMethodString(t *testing.T) { for _, test := range table { var got string - if test.method == "fallback" { + switch test.method { + case "fallback": got = abi.Fallback.String() - } else if test.method == "receive" { + case "receive": got = abi.Receive.String() - } else { + default: got = abi.Methods[test.method].String() } if got != test.expectation { diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index 68451483c..905b5ce62 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -160,13 +160,14 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) // this value will become our slice or our array, depending on the type var refSlice reflect.Value - if t.T == SliceTy { + switch t.T { + case SliceTy: // declare our slice refSlice = reflect.MakeSlice(t.GetType(), size, size) - } else if t.T == ArrayTy { + case ArrayTy: // declare our array refSlice = reflect.New(t.GetType()).Elem() - } else { + default: return nil, errors.New("abi: invalid type in array/slice unpacking stage") }