提交 4f06c6fd 作者: Matt Bell 提交者: Juan Batiz-Benet

commands: Formatted code

上级 d2176c05
package commands package commands
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
"errors"
) )
type Command struct { type Command struct {
...@@ -42,29 +42,29 @@ func (c *Command) Register(id string, sub *Command) error { ...@@ -42,29 +42,29 @@ func (c *Command) Register(id string, sub *Command) error {
func (c *Command) Call(req *Request) *Response { func (c *Command) Call(req *Request) *Response {
res := &Response{req: req} res := &Response{req: req}
cmds, err := c.Resolve(req.path) cmds, err := c.Resolve(req.path)
if err != nil { if err != nil {
res.SetError(err, Client) res.SetError(err, Client)
return res return res
} }
cmd := cmds[len(cmds)-1] cmd := cmds[len(cmds)-1]
if(cmd.f == nil) { if cmd.f == nil {
res.SetError(NotCallableError, Client) res.SetError(NotCallableError, Client)
return res return res
} }
options, err := c.GetOptions(req.path) options, err := c.GetOptions(req.path)
if err != nil { if err != nil {
res.SetError(err, Client) res.SetError(err, Client)
return res return res
} }
err = req.convertOptions(options) err = req.convertOptions(options)
if err != nil { if err != nil {
res.SetError(err, Client) res.SetError(err, Client)
return res return res
} }
cmd.f(req, res) cmd.f(req, res)
...@@ -73,54 +73,54 @@ func (c *Command) Call(req *Request) *Response { ...@@ -73,54 +73,54 @@ func (c *Command) Call(req *Request) *Response {
// Resolve gets the subcommands at the given path // Resolve gets the subcommands at the given path
func (c *Command) Resolve(path []string) ([]*Command, error) { func (c *Command) Resolve(path []string) ([]*Command, error) {
cmds := make([]*Command, len(path) + 1) cmds := make([]*Command, len(path)+1)
cmds[0] = c cmds[0] = c
cmd := c cmd := c
for i, name := range path { for i, name := range path {
cmd = cmd.Sub(name) cmd = cmd.Sub(name)
if cmd == nil { if cmd == nil {
pathS := strings.Join(path[0:i], "/") pathS := strings.Join(path[0:i], "/")
return nil, fmt.Errorf("Undefined command: '%s'", pathS) 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) { func (c *Command) Get(path []string) (*Command, error) {
cmds, err := c.Resolve(path) cmds, err := c.Resolve(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return cmds[len(cmds) - 1], nil return cmds[len(cmds)-1], nil
} }
// GetOptions gets the options in the given path of commands // GetOptions gets the options in the given path of commands
func (c *Command) GetOptions(path []string) (map[string]Option, error) { func (c *Command) GetOptions(path []string) (map[string]Option, error) {
options := make([]Option, len(c.Options)) options := make([]Option, len(c.Options))
copy(options, c.Options) copy(options, c.Options)
options = append(options, globalOptions...) options = append(options, globalOptions...)
cmds, err := c.Resolve(path) cmds, err := c.Resolve(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, cmd := range cmds { for _, cmd := range cmds {
options = append(options, cmd.Options...) options = append(options, cmd.Options...)
} }
optionsMap := make(map[string]Option) optionsMap := make(map[string]Option)
for _, opt := range options { for _, opt := range options {
for _, name := range opt.Names { for _, name := range opt.Names {
optionsMap[name] = opt optionsMap[name] = opt
} }
} }
return optionsMap, nil return optionsMap, nil
} }
// Sub returns the subcommand with the given id // Sub returns the subcommand with the given id
......
...@@ -149,11 +149,11 @@ func TestResolving(t *testing.T) { ...@@ -149,11 +149,11 @@ func TestResolving(t *testing.T) {
cmdA.Register("b", cmdB) cmdA.Register("b", cmdB)
cmdB.Register("c", cmdC) cmdB.Register("c", cmdC)
cmds, err := cmd.Resolve([]string{"a","b","c"}) cmds, err := cmd.Resolve([]string{"a", "b", "c"})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if len(cmds) != 4 || cmds[0] != cmd || cmds[1] != cmdA || cmds[2] != cmdB || cmds[3] != cmdC { 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) t.Error("Returned command path is different than expected", cmds)
} }
} }
\ No newline at end of file
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
// Request represents a call to a command from a consumer // Request represents a call to a command from a consumer
type Request struct { type Request struct {
path []string path []string
options map[string]interface{} options map[string]interface{}
arguments []string arguments []string
} }
...@@ -33,21 +33,22 @@ func (r *Request) Arguments() []string { ...@@ -33,21 +33,22 @@ func (r *Request) Arguments() []string {
return r.arguments 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{ var converters map[reflect.Kind]converter = map[reflect.Kind]converter{
Bool: func(v string)(interface{}, error) { Bool: func(v string) (interface{}, error) {
if v == "" { if v == "" {
return true, nil return true, nil
} }
return strconv.ParseBool(v) return strconv.ParseBool(v)
}, },
Int: func(v string)(interface{}, error) { Int: func(v string) (interface{}, error) {
return strconv.ParseInt(v, 0, 32) return strconv.ParseInt(v, 0, 32)
}, },
Uint: func(v string)(interface{}, error) { Uint: func(v string) (interface{}, error) {
return strconv.ParseInt(v, 0, 32) return strconv.ParseInt(v, 0, 32)
}, },
Float: func(v string)(interface{}, error) { Float: func(v string) (interface{}, error) {
return strconv.ParseFloat(v, 64) return strconv.ParseFloat(v, 64)
}, },
} }
...@@ -88,7 +89,7 @@ func (r *Request) convertOptions(options map[string]Option) error { ...@@ -88,7 +89,7 @@ func (r *Request) convertOptions(options map[string]Option) error {
k, name) k, name)
} }
converted[name] = value converted[name] = value
} }
} }
...@@ -102,7 +103,7 @@ func NewEmptyRequest() *Request { ...@@ -102,7 +103,7 @@ func NewEmptyRequest() *Request {
func NewRequest(path []string, opts map[string]interface{}, args []string) *Request { func NewRequest(path []string, opts map[string]interface{}, args []string) *Request {
if path == nil { if path == nil {
path = make([]string, 0) path = make([]string, 0)
} }
if opts == nil { if opts == nil {
opts = make(map[string]interface{}) opts = make(map[string]interface{})
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论