@@ -60,33 +60,45 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
60
60
return nil , ErrNotFound
61
61
}
62
62
63
- opts , stringArgs2 := parseOptions (r )
64
- iopts := make (map [string ]interface {}, len (opts ))
63
+ opts := make (map [string ]interface {})
65
64
optDefs , err := root .GetOptions (pth )
66
65
if err != nil {
67
66
return nil , err
68
67
}
69
- for k , v := range opts {
70
- iopts [k ] = v
71
- if optDef , ok := optDefs [k ]; ok {
72
- name := optDef .Names ()[0 ]
73
- if k != name {
74
- iopts [name ] = v
75
- delete (iopts , k )
68
+
69
+ query := r .URL .Query ()
70
+ // Note: len(v) is guaranteed by the above function to always be greater than 0
71
+ for k , v := range query {
72
+ if k == "arg" {
73
+ stringArgs = append (stringArgs , v ... )
74
+ } else {
75
+ optDef , ok := optDefs [k ]
76
+ if ! ok {
77
+ opts [k ] = v [0 ]
78
+ continue
76
79
}
77
80
78
- if optDef .Type () != cmds .Strings && len (v ) > 0 {
79
- iopts [name ] = v [0 ]
81
+ name := optDef .Names ()[0 ]
82
+ opts [name ] = v
83
+
84
+ switch optType := optDef .Type (); optType {
85
+ case cmds .Strings :
86
+ opts [name ] = v
87
+ case cmds .Bool , cmds .Int , cmds .Int64 , cmds .Uint , cmds .Uint64 , cmds .Float , cmds .String :
88
+ if len (v ) > 1 {
89
+ return nil , fmt .Errorf ("expected key %s to have only a single value, received %v" , name , v )
90
+ }
91
+ opts [name ] = v [0 ]
92
+ default :
93
+ return nil , fmt .Errorf ("unsupported option type. key: %s, type: %v" , k , optType )
80
94
}
81
95
}
82
96
}
83
97
// default to setting encoding to JSON
84
- if _ , ok := iopts [cmds .EncLong ]; ! ok {
85
- iopts [cmds .EncLong ] = cmds .JSON
98
+ if _ , ok := opts [cmds .EncLong ]; ! ok {
99
+ opts [cmds .EncLong ] = cmds .JSON
86
100
}
87
101
88
- stringArgs = append (stringArgs , stringArgs2 ... )
89
-
90
102
// count required argument definitions
91
103
numRequired := 0
92
104
for _ , argDef := range cmd .Arguments {
@@ -154,7 +166,7 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
154
166
}
155
167
156
168
ctx := logging .ContextWithLoggable (r .Context (), uuidLoggable ())
157
- req , err := cmds .NewRequest (ctx , pth , iopts , args , f , root )
169
+ req , err := cmds .NewRequest (ctx , pth , opts , args , f , root )
158
170
if err != nil {
159
171
return nil , err
160
172
}
@@ -168,22 +180,6 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
168
180
return req , err
169
181
}
170
182
171
- func parseOptions (r * http.Request ) (map [string ][]string , []string ) {
172
- opts := make (map [string ][]string )
173
- var args []string
174
-
175
- query := r .URL .Query ()
176
- for k , v := range query {
177
- if k == "arg" {
178
- args = v
179
- } else {
180
- opts [k ] = v
181
- }
182
- }
183
-
184
- return opts , args
185
- }
186
-
187
183
// parseResponse decodes a http.Response to create a cmds.Response
188
184
func parseResponse (httpRes * http.Response , req * cmds.Request ) (cmds.Response , error ) {
189
185
res := & Response {
0 commit comments