提交 793a8de9 作者: Matt Bell 提交者: Juan Batiz-Benet

commands: Refactored to make Request contain command path

上级 86bc450b
...@@ -37,11 +37,11 @@ func (c *Command) Register(id string, sub *Command) error { ...@@ -37,11 +37,11 @@ func (c *Command) Register(id string, sub *Command) error {
} }
// Call invokes the command at the given subcommand path // Call invokes the command at the given subcommand path
func (c *Command) Call(path []string, req *Request) *Response { func (c *Command) Call(req *Request) *Response {
cmd := c cmd := c
res := &Response{req: req} res := &Response{req: req}
options, err := cmd.GetOptions(path) options, err := cmd.GetOptions(req.path)
if err != nil { if err != nil {
res.SetError(err, Client) res.SetError(err, Client)
return res return res
...@@ -82,6 +82,7 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) { ...@@ -82,6 +82,7 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
copy(options, c.Options) copy(options, c.Options)
options = append(options, globalOptions...) options = append(options, globalOptions...)
// a nil path means this command, not a subcommand (same as an empty path)
if path != nil { if path != nil {
for i, id := range path { for i, id := range path {
cmd := c.Sub(id) cmd := c.Sub(id)
......
...@@ -13,7 +13,7 @@ func TestOptionValidation(t *testing.T) { ...@@ -13,7 +13,7 @@ func TestOptionValidation(t *testing.T) {
req := NewRequest() req := NewRequest()
req.options["foo"] = 5 req.options["foo"] = 5
res := cmd.Call(nil, req) res := cmd.Call(req)
if res.Error == nil { if res.Error == nil {
t.Error("Should have failed (unrecognized command)") t.Error("Should have failed (unrecognized command)")
} }
...@@ -21,21 +21,21 @@ func TestOptionValidation(t *testing.T) { ...@@ -21,21 +21,21 @@ func TestOptionValidation(t *testing.T) {
req = NewRequest() req = NewRequest()
req.options["beep"] = 5 req.options["beep"] = 5
req.options["b"] = 10 req.options["b"] = 10
res = cmd.Call(nil, req) res = cmd.Call(req)
if res.Error == nil { if res.Error == nil {
t.Error("Should have failed (duplicate options)") t.Error("Should have failed (duplicate options)")
} }
req = NewRequest() req = NewRequest()
req.options["beep"] = "foo" req.options["beep"] = "foo"
res = cmd.Call(nil, req) res = cmd.Call(req)
if res.Error == nil { if res.Error == nil {
t.Error("Should have failed (incorrect type)") t.Error("Should have failed (incorrect type)")
} }
req = NewRequest() req = NewRequest()
req.options["beep"] = 5 req.options["beep"] = 5
res = cmd.Call(nil, req) res = cmd.Call(req)
if res.Error != nil { if res.Error != nil {
t.Error("Should have passed") t.Error("Should have passed")
} }
...@@ -43,7 +43,7 @@ func TestOptionValidation(t *testing.T) { ...@@ -43,7 +43,7 @@ func TestOptionValidation(t *testing.T) {
req = NewRequest() req = NewRequest()
req.options["beep"] = 5 req.options["beep"] = 5
req.options["boop"] = "test" req.options["boop"] = "test"
res = cmd.Call(nil, req) res = cmd.Call(req)
if res.Error != nil { if res.Error != nil {
t.Error("Should have passed") t.Error("Should have passed")
} }
...@@ -51,14 +51,14 @@ func TestOptionValidation(t *testing.T) { ...@@ -51,14 +51,14 @@ func TestOptionValidation(t *testing.T) {
req = NewRequest() req = NewRequest()
req.options["b"] = 5 req.options["b"] = 5
req.options["B"] = "test" req.options["B"] = "test"
res = cmd.Call(nil, req) res = cmd.Call(req)
if res.Error != nil { if res.Error != nil {
t.Error("Should have passed") t.Error("Should have passed")
} }
req = NewRequest() req = NewRequest()
req.options["enc"] = "json" req.options["enc"] = "json"
res = cmd.Call(nil, req) res = cmd.Call(req)
if res.Error != nil { if res.Error != nil {
t.Error("Should have passed") t.Error("Should have passed")
} }
......
...@@ -2,10 +2,15 @@ package commands ...@@ -2,10 +2,15 @@ package commands
// 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
options map[string]interface{} options map[string]interface{}
arguments []string arguments []string
} }
func (r *Request) Path() []string {
return r.path
}
func (r *Request) Option(name string) interface{} { func (r *Request) Option(name string) interface{} {
return r.options[name] return r.options[name]
} }
...@@ -24,6 +29,7 @@ func (r *Request) Arguments() []string { ...@@ -24,6 +29,7 @@ func (r *Request) Arguments() []string {
func NewRequest() *Request { func NewRequest() *Request {
return &Request{ return &Request{
make([]string, 0),
make(map[string]interface{}), make(map[string]interface{}),
make([]string, 0), make([]string, 0),
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论