Commit 4373bad3 authored by Adin Schmahmann's avatar Adin Schmahmann

feat: support strings option over HTTP API

parent bb36028e
...@@ -230,6 +230,15 @@ func getQuery(req *cmds.Request) (string, error) { ...@@ -230,6 +230,15 @@ func getQuery(req *cmds.Request) (string, error) {
if OptionSkipMap[k] { if OptionSkipMap[k] {
continue continue
} }
optArr, ok := v.([]string)
if ok {
for _, o := range optArr {
query.Add(k, o)
}
continue
}
str := fmt.Sprintf("%v", v) str := fmt.Sprintf("%v", v)
query.Set(k, str) query.Set(k, str)
} }
......
...@@ -61,22 +61,28 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) { ...@@ -61,22 +61,28 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
} }
opts, stringArgs2 := parseOptions(r) opts, stringArgs2 := parseOptions(r)
iopts := make(map[string]interface{}, len(opts))
optDefs, err := root.GetOptions(pth) optDefs, err := root.GetOptions(pth)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for k, v := range opts { for k, v := range opts {
iopts[k] = v
if optDef, ok := optDefs[k]; ok { if optDef, ok := optDefs[k]; ok {
name := optDef.Names()[0] name := optDef.Names()[0]
if k != name { if k != name {
opts[name] = v iopts[name] = v
delete(opts, k) delete(iopts, k)
}
if optDef.Type() != cmds.Strings && len(v) > 0 {
iopts[name] = v[0]
} }
} }
} }
// default to setting encoding to JSON // default to setting encoding to JSON
if _, ok := opts[cmds.EncLong]; !ok { if _, ok := iopts[cmds.EncLong]; !ok {
opts[cmds.EncLong] = cmds.JSON iopts[cmds.EncLong] = cmds.JSON
} }
stringArgs = append(stringArgs, stringArgs2...) stringArgs = append(stringArgs, stringArgs2...)
...@@ -148,7 +154,7 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) { ...@@ -148,7 +154,7 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
} }
ctx := logging.ContextWithLoggable(r.Context(), uuidLoggable()) ctx := logging.ContextWithLoggable(r.Context(), uuidLoggable())
req, err := cmds.NewRequest(ctx, pth, opts, args, f, root) req, err := cmds.NewRequest(ctx, pth, iopts, args, f, root)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -162,8 +168,8 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) { ...@@ -162,8 +168,8 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
return req, err return req, err
} }
func parseOptions(r *http.Request) (map[string]interface{}, []string) { func parseOptions(r *http.Request) (map[string][]string, []string) {
opts := make(map[string]interface{}) opts := make(map[string][]string)
var args []string var args []string
query := r.URL.Query() query := r.URL.Query()
...@@ -171,8 +177,7 @@ func parseOptions(r *http.Request) (map[string]interface{}, []string) { ...@@ -171,8 +177,7 @@ func parseOptions(r *http.Request) (map[string]interface{}, []string) {
if k == "arg" { if k == "arg" {
args = v args = v
} else { } else {
opts[k] = v
opts[k] = v[0]
} }
} }
......
...@@ -118,21 +118,28 @@ func checkAndConvertOptions(root *Command, opts OptMap, path []string) (OptMap, ...@@ -118,21 +118,28 @@ func checkAndConvertOptions(root *Command, opts OptMap, path []string) (OptMap,
kind := reflect.TypeOf(v).Kind() kind := reflect.TypeOf(v).Kind()
if kind != opt.Type() { if kind != opt.Type() {
if str, ok := v.(string); ok { if opt.Type() == Strings {
val, err := opt.Parse(str) if _, ok := v.([]string); !ok {
if err != nil { return options, fmt.Errorf("option %q should be type %q, but got type %q",
value := fmt.Sprintf("value %q", v) k, opt.Type().String(), kind.String())
if len(str) == 0 {
value = "empty value"
}
return options, fmt.Errorf("could not convert %s to type %q (for option %q)",
value, opt.Type().String(), "-"+k)
} }
options[k] = val
} else { } else {
return options, fmt.Errorf("option %q should be type %q, but got type %q", if str, ok := v.(string); ok {
k, opt.Type().String(), kind.String()) val, err := opt.Parse(str)
if err != nil {
value := fmt.Sprintf("value %q", v)
if len(str) == 0 {
value = "empty value"
}
return options, fmt.Errorf("could not convert %s to type %q (for option %q)",
value, opt.Type().String(), "-"+k)
}
options[k] = val
} else {
return options, fmt.Errorf("option %q should be type %q, but got type %q",
k, opt.Type().String(), kind.String())
}
} }
} }
......
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