Skip to content

Commit 4373bad

Browse files
committed
feat: support strings option over HTTP API
1 parent bb36028 commit 4373bad

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

http/client.go

+9
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ func getQuery(req *cmds.Request) (string, error) {
230230
if OptionSkipMap[k] {
231231
continue
232232
}
233+
234+
optArr, ok := v.([]string)
235+
if ok {
236+
for _, o := range optArr {
237+
query.Add(k, o)
238+
}
239+
continue
240+
}
241+
233242
str := fmt.Sprintf("%v", v)
234243
query.Set(k, str)
235244
}

http/parse.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,28 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
6161
}
6262

6363
opts, stringArgs2 := parseOptions(r)
64+
iopts := make(map[string]interface{}, len(opts))
6465
optDefs, err := root.GetOptions(pth)
6566
if err != nil {
6667
return nil, err
6768
}
6869
for k, v := range opts {
70+
iopts[k] = v
6971
if optDef, ok := optDefs[k]; ok {
7072
name := optDef.Names()[0]
7173
if k != name {
72-
opts[name] = v
73-
delete(opts, k)
74+
iopts[name] = v
75+
delete(iopts, k)
76+
}
77+
78+
if optDef.Type() != cmds.Strings && len(v) > 0 {
79+
iopts[name] = v[0]
7480
}
7581
}
7682
}
7783
// default to setting encoding to JSON
78-
if _, ok := opts[cmds.EncLong]; !ok {
79-
opts[cmds.EncLong] = cmds.JSON
84+
if _, ok := iopts[cmds.EncLong]; !ok {
85+
iopts[cmds.EncLong] = cmds.JSON
8086
}
8187

8288
stringArgs = append(stringArgs, stringArgs2...)
@@ -148,7 +154,7 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
148154
}
149155

150156
ctx := logging.ContextWithLoggable(r.Context(), uuidLoggable())
151-
req, err := cmds.NewRequest(ctx, pth, opts, args, f, root)
157+
req, err := cmds.NewRequest(ctx, pth, iopts, args, f, root)
152158
if err != nil {
153159
return nil, err
154160
}
@@ -162,17 +168,16 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
162168
return req, err
163169
}
164170

165-
func parseOptions(r *http.Request) (map[string]interface{}, []string) {
166-
opts := make(map[string]interface{})
171+
func parseOptions(r *http.Request) (map[string][]string, []string) {
172+
opts := make(map[string][]string)
167173
var args []string
168174

169175
query := r.URL.Query()
170176
for k, v := range query {
171177
if k == "arg" {
172178
args = v
173179
} else {
174-
175-
opts[k] = v[0]
180+
opts[k] = v
176181
}
177182
}
178183

request.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,28 @@ func checkAndConvertOptions(root *Command, opts OptMap, path []string) (OptMap,
118118

119119
kind := reflect.TypeOf(v).Kind()
120120
if kind != opt.Type() {
121-
if str, ok := v.(string); ok {
122-
val, err := opt.Parse(str)
123-
if err != nil {
124-
value := fmt.Sprintf("value %q", v)
125-
if len(str) == 0 {
126-
value = "empty value"
127-
}
128-
return options, fmt.Errorf("could not convert %s to type %q (for option %q)",
129-
value, opt.Type().String(), "-"+k)
121+
if opt.Type() == Strings {
122+
if _, ok := v.([]string); !ok {
123+
return options, fmt.Errorf("option %q should be type %q, but got type %q",
124+
k, opt.Type().String(), kind.String())
130125
}
131-
options[k] = val
132-
133126
} else {
134-
return options, fmt.Errorf("option %q should be type %q, but got type %q",
135-
k, opt.Type().String(), kind.String())
127+
if str, ok := v.(string); ok {
128+
val, err := opt.Parse(str)
129+
if err != nil {
130+
value := fmt.Sprintf("value %q", v)
131+
if len(str) == 0 {
132+
value = "empty value"
133+
}
134+
return options, fmt.Errorf("could not convert %s to type %q (for option %q)",
135+
value, opt.Type().String(), "-"+k)
136+
}
137+
options[k] = val
138+
139+
} else {
140+
return options, fmt.Errorf("option %q should be type %q, but got type %q",
141+
k, opt.Type().String(), kind.String())
142+
}
136143
}
137144
}
138145

0 commit comments

Comments
 (0)