-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.go
63 lines (60 loc) · 1.52 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package commander
// The following are ACTION functions, chose one if you like it.
type (
Action func(c Context) _Result // default internal function
ActionResult func() _Result
ActionNormal func(c Context) error
ActionSimple func(c Context)
ActionNative func()
ActionNativeSimple func() error
ActionNativeDocopt func(m map[string]interface{}) error
)
// parseAction handle function to Action type
func parseAction(arg interface{}) (a Action) {
switch action := arg.(type) {
case func(c Context) _Result: // Action
a = action
case func() _Result: // ActionResult
a = func(c Context) _Result {
return action()
}
case func(c Context) error: // ActionNormal
a = func(c Context) _Result {
if err := action(c); err != nil {
return newResultError(err)
}
return resultPass()
}
case func(c Context): // ActionSimple
a = func(c Context) _Result {
action(c)
return resultPass()
}
case func(): // ActionNative
a = func(c Context) _Result {
action()
return resultPass()
}
case func() error: // ActionNativeSimple
a = func(c Context) _Result {
if err := action(); err != nil {
return newResultError(err)
}
return resultPass()
}
case func(m map[string]interface{}) error: // ActionNativeDocopt
a = func(c Context) _Result {
if err := action(c.Map()); err != nil {
return newResultError(err)
}
return resultPass()
}
default:
a = nil
}
return
}
// emptyAction if action is empty
func emptyAction(a Action) bool {
return a == nil
}