Commit f60867f3 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

f -> run, Function type.

parent fa7d1704
......@@ -6,11 +6,17 @@ import (
"strings"
)
// Command is an object that defines a command.
// Function is the type of function that Commands use.
// It reads from the Request, and writes results to the Response.
type Function func(*Request, *Response)
// Command is a runnable command, with input arguments and options (flags).
// It can also have subcommands, to group units of work into sets.
type Command struct {
Help string
Options []Option
f func(*Request, *Response)
Help string
Options []Option
run Function
subcommands map[string]*Command
}
......@@ -51,7 +57,7 @@ func (c *Command) Call(req *Request) *Response {
}
cmd := cmds[len(cmds)-1]
if cmd.f == nil {
if cmd.run == nil {
res.SetError(ErrNotCallable, ErrClient)
return res
}
......@@ -68,7 +74,7 @@ func (c *Command) Call(req *Request) *Response {
return res
}
cmd.f(req, res)
cmd.run(req, res)
return res
}
......
......@@ -8,7 +8,7 @@ func TestOptionValidation(t *testing.T) {
Option{[]string{"b", "beep"}, Int},
Option{[]string{"B", "boop"}, String},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
}
req := NewEmptyRequest()
......@@ -84,35 +84,35 @@ func TestRegistration(t *testing.T) {
Options: []Option{
Option{[]string{"beep"}, Int},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
&Command{
Options: []Option{
Option{[]string{"boop"}, Int},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
&Command{
Options: []Option{
Option{[]string{"boop"}, String},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
&Command{
Options: []Option{
Option{[]string{"bop"}, String},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
&Command{
Options: []Option{
Option{[]string{"enc"}, String},
},
f: func(req *Request, res *Response) {},
run: func(req *Request, res *Response) {},
},
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment