forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
165 lines (138 loc) · 3.51 KB
/
request.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package commands
import (
"fmt"
"reflect"
"strconv"
"github.com/jbenet/go-ipfs/config"
"github.com/jbenet/go-ipfs/core"
)
type optMap map[string]interface{}
type Context struct {
ConfigRoot string
Config *config.Config
Node *core.IpfsNode
}
// Request represents a call to a command from a consumer
type Request interface {
Path() []string
Option(name string) (interface{}, bool)
Options() map[string]interface{}
SetOption(name string, val interface{})
Arguments() []interface{} // TODO: make argument value type instead of using interface{}
Context() *Context
SetContext(Context)
Command() *Command
ConvertOptions(options map[string]Option) error
}
type request struct {
path []string
options optMap
arguments []interface{}
cmd *Command
ctx Context
}
// Path returns the command path of this request
func (r *request) Path() []string {
return r.path
}
// Option returns the value of the option for given name.
func (r *request) Option(name string) (interface{}, bool) {
val, err := r.options[name]
return val, err
}
// Options returns a copy of the option map
func (r *request) Options() map[string]interface{} {
output := make(optMap)
for k, v := range r.options {
output[k] = v
}
return output
}
// SetOption sets the value of the option for given name.
func (r *request) SetOption(name string, val interface{}) {
r.options[name] = val
}
// Arguments returns the arguments slice
func (r *request) Arguments() []interface{} {
return r.arguments
}
func (r *request) Context() *Context {
return &r.ctx
}
func (r *request) SetContext(ctx Context) {
r.ctx = ctx
}
func (r *request) Command() *Command {
return r.cmd
}
type converter func(string) (interface{}, error)
var converters = map[reflect.Kind]converter{
Bool: func(v string) (interface{}, error) {
if v == "" {
return true, nil
}
return strconv.ParseBool(v)
},
Int: func(v string) (interface{}, error) {
return strconv.ParseInt(v, 0, 32)
},
Uint: func(v string) (interface{}, error) {
return strconv.ParseInt(v, 0, 32)
},
Float: func(v string) (interface{}, error) {
return strconv.ParseFloat(v, 64)
},
}
func (r *request) ConvertOptions(options map[string]Option) error {
converted := make(map[string]interface{})
for k, v := range r.options {
opt, ok := options[k]
if !ok {
continue
}
kind := reflect.TypeOf(v).Kind()
var value interface{}
if kind != opt.Type {
if kind == String {
convert := converters[opt.Type]
val, err := convert(v.(string))
if err != nil {
return fmt.Errorf("Could not convert string value '%s' to type '%s'",
v, opt.Type.String())
}
value = val
} else {
return fmt.Errorf("Option '%s' should be type '%s', but got type '%s'",
k, opt.Type.String(), kind.String())
}
} else {
value = v
}
for _, name := range opt.Names {
if _, ok := r.options[name]; name != k && ok {
return fmt.Errorf("Duplicate command options were provided ('%s' and '%s')",
k, name)
}
converted[name] = value
}
}
r.options = converted
return nil
}
// NewEmptyRequest initializes an empty request
func NewEmptyRequest() Request {
return NewRequest(nil, nil, nil, nil)
}
// NewRequest returns a request initialized with given arguments
func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command) Request {
if path == nil {
path = make([]string, 0)
}
if opts == nil {
opts = make(map[string]interface{})
}
if args == nil {
args = make([]interface{}, 0)
}
return &request{path, opts, args, cmd, Context{}}
}