From 4f06c6fdbaeaf4b7d60a7e02fcb6f94d9f1b5270 Mon Sep 17 00:00:00 2001 From: Matt Bell <mappum@gmail.com> Date: Wed, 15 Oct 2014 20:51:17 -0700 Subject: [PATCH] commands: Formatted code --- commands/command.go | 114 +++++++++++++++++++-------------------- commands/command_test.go | 4 +- commands/request.go | 17 +++--- 3 files changed, 68 insertions(+), 67 deletions(-) diff --git a/commands/command.go b/commands/command.go index 449ce2aa..6f880400 100644 --- a/commands/command.go +++ b/commands/command.go @@ -1,9 +1,9 @@ package commands import ( + "errors" "fmt" "strings" - "errors" ) type Command struct { @@ -42,29 +42,29 @@ func (c *Command) Register(id string, sub *Command) error { func (c *Command) Call(req *Request) *Response { res := &Response{req: req} - cmds, err := c.Resolve(req.path) - if err != nil { - res.SetError(err, Client) - return res - } - cmd := cmds[len(cmds)-1] + cmds, err := c.Resolve(req.path) + if err != nil { + res.SetError(err, Client) + return res + } + cmd := cmds[len(cmds)-1] - if(cmd.f == nil) { - res.SetError(NotCallableError, Client) - return res - } + if cmd.f == nil { + res.SetError(NotCallableError, Client) + return res + } - options, err := c.GetOptions(req.path) - if err != nil { - res.SetError(err, Client) - return res - } + options, err := c.GetOptions(req.path) + if err != nil { + res.SetError(err, Client) + return res + } err = req.convertOptions(options) - if err != nil { - res.SetError(err, Client) - return res - } + if err != nil { + res.SetError(err, Client) + return res + } cmd.f(req, res) @@ -73,54 +73,54 @@ func (c *Command) Call(req *Request) *Response { // Resolve gets the subcommands at the given path func (c *Command) Resolve(path []string) ([]*Command, error) { - cmds := make([]*Command, len(path) + 1) - cmds[0] = c + cmds := make([]*Command, len(path)+1) + cmds[0] = c - cmd := c - for i, name := range path { - cmd = cmd.Sub(name) + cmd := c + for i, name := range path { + cmd = cmd.Sub(name) - if cmd == nil { - pathS := strings.Join(path[0:i], "/") - return nil, fmt.Errorf("Undefined command: '%s'", pathS) - } + if cmd == nil { + pathS := strings.Join(path[0:i], "/") + return nil, fmt.Errorf("Undefined command: '%s'", pathS) + } - cmds[i+1] = cmd - } + cmds[i+1] = cmd + } - return cmds, nil + return cmds, nil } func (c *Command) Get(path []string) (*Command, error) { - cmds, err := c.Resolve(path) - if err != nil { - return nil, err - } - return cmds[len(cmds) - 1], nil + cmds, err := c.Resolve(path) + if err != nil { + return nil, err + } + return cmds[len(cmds)-1], nil } // GetOptions gets the options in the given path of commands func (c *Command) GetOptions(path []string) (map[string]Option, error) { - options := make([]Option, len(c.Options)) - copy(options, c.Options) - options = append(options, globalOptions...) - - cmds, err := c.Resolve(path) - if err != nil { - return nil, err - } - for _, cmd := range cmds { - options = append(options, cmd.Options...) - } - - optionsMap := make(map[string]Option) - for _, opt := range options { - for _, name := range opt.Names { - optionsMap[name] = opt - } - } - - return optionsMap, nil + options := make([]Option, len(c.Options)) + copy(options, c.Options) + options = append(options, globalOptions...) + + cmds, err := c.Resolve(path) + if err != nil { + return nil, err + } + for _, cmd := range cmds { + options = append(options, cmd.Options...) + } + + optionsMap := make(map[string]Option) + for _, opt := range options { + for _, name := range opt.Names { + optionsMap[name] = opt + } + } + + return optionsMap, nil } // Sub returns the subcommand with the given id diff --git a/commands/command_test.go b/commands/command_test.go index 138faf14..f93bdb71 100644 --- a/commands/command_test.go +++ b/commands/command_test.go @@ -149,11 +149,11 @@ func TestResolving(t *testing.T) { cmdA.Register("b", cmdB) cmdB.Register("c", cmdC) - cmds, err := cmd.Resolve([]string{"a","b","c"}) + cmds, err := cmd.Resolve([]string{"a", "b", "c"}) if err != nil { t.Error(err) } if len(cmds) != 4 || cmds[0] != cmd || cmds[1] != cmdA || cmds[2] != cmdB || cmds[3] != cmdC { t.Error("Returned command path is different than expected", cmds) } -} \ No newline at end of file +} diff --git a/commands/request.go b/commands/request.go index 97ba6891..42e9ec0e 100644 --- a/commands/request.go +++ b/commands/request.go @@ -8,7 +8,7 @@ import ( // Request represents a call to a command from a consumer type Request struct { - path []string + path []string options map[string]interface{} arguments []string } @@ -33,21 +33,22 @@ func (r *Request) Arguments() []string { return r.arguments } -type converter func(string)(interface{}, error) +type converter func(string) (interface{}, error) + var converters map[reflect.Kind]converter = map[reflect.Kind]converter{ - Bool: func(v string)(interface{}, error) { + Bool: func(v string) (interface{}, error) { if v == "" { return true, nil } return strconv.ParseBool(v) }, - Int: func(v string)(interface{}, error) { + Int: func(v string) (interface{}, error) { return strconv.ParseInt(v, 0, 32) }, - Uint: func(v string)(interface{}, error) { + Uint: func(v string) (interface{}, error) { return strconv.ParseInt(v, 0, 32) }, - Float: func(v string)(interface{}, error) { + Float: func(v string) (interface{}, error) { return strconv.ParseFloat(v, 64) }, } @@ -88,7 +89,7 @@ func (r *Request) convertOptions(options map[string]Option) error { k, name) } - converted[name] = value + converted[name] = value } } @@ -102,7 +103,7 @@ func NewEmptyRequest() *Request { func NewRequest(path []string, opts map[string]interface{}, args []string) *Request { if path == nil { - path = make([]string, 0) + path = make([]string, 0) } if opts == nil { opts = make(map[string]interface{}) -- GitLab