Skip to content

Commit

Permalink
feat: support strings option over HTTP API
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Nov 25, 2020
1 parent bb36028 commit 78aee27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
9 changes: 9 additions & 0 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ func getQuery(req *cmds.Request) (string, error) {
if OptionSkipMap[k] {
continue
}

optArr, ok := v.([]string)
if ok {
for _, o := range optArr {
query.Add(k, o)
}
continue
}

str := fmt.Sprintf("%v", v)
query.Set(k, str)
}
Expand Down
23 changes: 14 additions & 9 deletions http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,28 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
}

opts, stringArgs2 := parseOptions(r)
iopts := make(map[string]interface{}, len(opts))
optDefs, err := root.GetOptions(pth)
if err != nil {
return nil, err
}
for k, v := range opts {
iopts[k] = v
if optDef, ok := optDefs[k]; ok {
name := optDef.Names()[0]
if k != name {
opts[name] = v
delete(opts, k)
iopts[name] = v
delete(iopts, k)
}

if optDef.Type() != cmds.Strings && len(v) > 0 {
iopts[name] = v[0]
}
}
}
// default to setting encoding to JSON
if _, ok := opts[cmds.EncLong]; !ok {
opts[cmds.EncLong] = cmds.JSON
if _, ok := iopts[cmds.EncLong]; !ok {
iopts[cmds.EncLong] = cmds.JSON
}

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

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 {
return nil, err
}
Expand All @@ -162,17 +168,16 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
return req, err
}

func parseOptions(r *http.Request) (map[string]interface{}, []string) {
opts := make(map[string]interface{})
func parseOptions(r *http.Request) (map[string][]string, []string) {
opts := make(map[string][]string)
var args []string

query := r.URL.Query()
for k, v := range query {
if k == "arg" {
args = v
} else {

opts[k] = v[0]
opts[k] = v
}
}

Expand Down

0 comments on commit 78aee27

Please sign in to comment.