commands: Formatted code
Matt Bell committed
Oct 15, 2014 at 20:51 UTC
4f06c6fdbaeaf4b7d60a7e02fcb6f94d9f1b5270
3 files changed
+68
-67
commands/command.go
+57
-57
@@ -1,9 +1,9 @@
1
package commands
2
3
import (
4
+ "errors"
5
"fmt"
6
"strings"
6
- "errors"
7
)
8
9
type Command struct {
@@ -42,29 +42,29 @@ func (c *Command) Register(id string, sub *Command) error {
42
func (c *Command) Call(req *Request) *Response {
43
res := &Response{req: req}
44
45
- cmds, err := c.Resolve(req.path)
46
- if err != nil {
47
- res.SetError(err, Client)
48
- return res
49
- }
50
- cmd := cmds[len(cmds)-1]
45
+ cmds, err := c.Resolve(req.path)
46
+ if err != nil {
47
+ res.SetError(err, Client)
48
+ return res
49
+ }
50
+ cmd := cmds[len(cmds)-1]
51
52
- if(cmd.f == nil) {
53
- res.SetError(NotCallableError, Client)
54
- return res
55
- }
52
+ if cmd.f == nil {
53
+ res.SetError(NotCallableError, Client)
54
+ return res
55
+ }
56
57
- options, err := c.GetOptions(req.path)
58
- if err != nil {
59
- res.SetError(err, Client)
60
- return res
61
- }
57
+ options, err := c.GetOptions(req.path)
58
+ if err != nil {
59
+ res.SetError(err, Client)
60
+ return res
61
+ }
62
63
err = req.convertOptions(options)
64
- if err != nil {
65
- res.SetError(err, Client)
66
- return res
67
- }
64
+ if err != nil {
65
+ res.SetError(err, Client)
66
+ return res
67
+ }
68
69
cmd.f(req, res)
70
@@ -73,54 +73,54 @@ func (c *Command) Call(req *Request) *Response {
73
74
// Resolve gets the subcommands at the given path
75
func (c *Command) Resolve(path []string) ([]*Command, error) {
76
- cmds := make([]*Command, len(path) + 1)
77
- cmds[0] = c
76
+ cmds := make([]*Command, len(path)+1)
77
+ cmds[0] = c
78
79
- cmd := c
80
- for i, name := range path {
81
- cmd = cmd.Sub(name)
79
+ cmd := c
80
+ for i, name := range path {
81
+ cmd = cmd.Sub(name)
82
83
- if cmd == nil {
84
- pathS := strings.Join(path[0:i], "/")
85
- return nil, fmt.Errorf("Undefined command: '%s'", pathS)
86
- }
83
+ if cmd == nil {
84
+ pathS := strings.Join(path[0:i], "/")
85
+ return nil, fmt.Errorf("Undefined command: '%s'", pathS)
86
+ }
87
88
- cmds[i+1] = cmd
89
- }
88
+ cmds[i+1] = cmd
89
+ }
90
91
- return cmds, nil
91
+ return cmds, nil
92
}
93
94
func (c *Command) Get(path []string) (*Command, error) {
95
- cmds, err := c.Resolve(path)
96
- if err != nil {
97
- return nil, err
98
- }
99
- return cmds[len(cmds) - 1], nil
95
+ cmds, err := c.Resolve(path)
96
+ if err != nil {
97
+ return nil, err
98
+ }
99
+ return cmds[len(cmds)-1], nil
100
}
101
102
// GetOptions gets the options in the given path of commands
103
func (c *Command) GetOptions(path []string) (map[string]Option, error) {
104
- options := make([]Option, len(c.Options))
105
- copy(options, c.Options)
106
- options = append(options, globalOptions...)
107
-
108
- cmds, err := c.Resolve(path)
109
- if err != nil {
110
- return nil, err
111
- }
112
- for _, cmd := range cmds {
113
- options = append(options, cmd.Options...)
114
- }
115
-
116
- optionsMap := make(map[string]Option)
117
- for _, opt := range options {
118
- for _, name := range opt.Names {
119
- optionsMap[name] = opt
120
- }
121
- }
122
-
123
- return optionsMap, nil
104
+ options := make([]Option, len(c.Options))
105
+ copy(options, c.Options)
106
+ options = append(options, globalOptions...)
107
+
108
+ cmds, err := c.Resolve(path)
109
+ if err != nil {
110
+ return nil, err
111
+ }
112
+ for _, cmd := range cmds {
113
+ options = append(options, cmd.Options...)
114
+ }
115
+
116
+ optionsMap := make(map[string]Option)
117
+ for _, opt := range options {
118
+ for _, name := range opt.Names {
119
+ optionsMap[name] = opt
120
+ }
121
+ }
122
+
123
+ return optionsMap, nil
124
}
125
126
// Sub returns the subcommand with the given id
commands/command_test.go
+2
-2
@@ -149,11 +149,11 @@ func TestResolving(t *testing.T) {
149
cmdA.Register("b", cmdB)
150
cmdB.Register("c", cmdC)
151
152
- cmds, err := cmd.Resolve([]string{"a","b","c"})
152
+ cmds, err := cmd.Resolve([]string{"a", "b", "c"})
153
if err != nil {
154
t.Error(err)
155
}
156
if len(cmds) != 4 || cmds[0] != cmd || cmds[1] != cmdA || cmds[2] != cmdB || cmds[3] != cmdC {
157
t.Error("Returned command path is different than expected", cmds)
158
}
159
-}
\ No newline at end of file
159
+}
commands/request.go
+9
-8
@@ -8,7 +8,7 @@ import (
8
9
// Request represents a call to a command from a consumer
10
type Request struct {
11
- path []string
11
+ path []string
12
options map[string]interface{}
13
arguments []string
14
}
@@ -33,21 +33,22 @@ func (r *Request) Arguments() []string {
33
return r.arguments
34
}
35
36
-type converter func(string)(interface{}, error)
36
+type converter func(string) (interface{}, error)
37
+
38
var converters map[reflect.Kind]converter = map[reflect.Kind]converter{
38
- Bool: func(v string)(interface{}, error) {
39
+ Bool: func(v string) (interface{}, error) {
40
if v == "" {
41
return true, nil
42
}
43
return strconv.ParseBool(v)
44
},
44
- Int: func(v string)(interface{}, error) {
45
+ Int: func(v string) (interface{}, error) {
46
return strconv.ParseInt(v, 0, 32)
47
},
47
- Uint: func(v string)(interface{}, error) {
48
+ Uint: func(v string) (interface{}, error) {
49
return strconv.ParseInt(v, 0, 32)
50
},
50
- Float: func(v string)(interface{}, error) {
51
+ Float: func(v string) (interface{}, error) {
52
return strconv.ParseFloat(v, 64)
53
},
54
}
@@ -88,7 +89,7 @@ func (r *Request) convertOptions(options map[string]Option) error {
89
k, name)
90
}
91
91
- converted[name] = value
92
+ converted[name] = value
93
}
94
}
95
@@ -102,7 +103,7 @@ func NewEmptyRequest() *Request {
103
104
func NewRequest(path []string, opts map[string]interface{}, args []string) *Request {
105
if path == nil {
105
- path = make([]string, 0)
106
+ path = make([]string, 0)
107
}
108
if opts == nil {
109
opts = make(map[string]interface{})