OSDN Git Service

use map
authorfreewind <nowindlee@gmail.com>
Thu, 24 May 2018 14:57:54 +0000 (22:57 +0800)
committerfreewind <nowindlee@gmail.com>
Thu, 24 May 2018 14:57:54 +0000 (22:57 +0800)
api/transact.go

index bdd8b16..63c61e5 100644 (file)
@@ -26,26 +26,17 @@ var (
 )
 
 func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, error), bool) {
-       var decoder func([]byte) (txbuilder.Action, error)
-       switch action {
-       case "control_address":
-               decoder = txbuilder.DecodeControlAddressAction
-       case "control_program":
-               decoder = txbuilder.DecodeControlProgramAction
-       case "control_receiver":
-               decoder = txbuilder.DecodeControlReceiverAction
-       case "issue":
-               decoder = a.wallet.AssetReg.DecodeIssueAction
-       case "retire":
-               decoder = txbuilder.DecodeRetireAction
-       case "spend_account":
-               decoder = a.wallet.AccountMgr.DecodeSpendAction
-       case "spend_account_unspent_output":
-               decoder = a.wallet.AccountMgr.DecodeSpendUTXOAction
-       default:
-               return nil, false
+       decoders := map[string]func([]byte) (txbuilder.Action, error){
+               "control_address":              txbuilder.DecodeControlAddressAction,
+               "control_program":              txbuilder.DecodeControlProgramAction,
+               "control_receiver":             txbuilder.DecodeControlReceiverAction,
+               "issue":                        a.wallet.AssetReg.DecodeIssueAction,
+               "retire":                       txbuilder.DecodeRetireAction,
+               "spend_account":                a.wallet.AccountMgr.DecodeSpendAction,
+               "spend_account_unspent_output": a.wallet.AccountMgr.DecodeSpendUTXOAction,
        }
-       return decoder, true
+       decoder, ok := decoders[action]
+       return decoder, ok
 }
 
 func onlyHaveSpendActions(req *BuildRequest) bool {