44 upload_action_fn)
55 from cli import (mangle_subcommand, new_method_context, PARAM_FLAG , STRUCT_FLAG , UPLOAD_FLAG , OUTPUT_FLAG , VALUE_ARG ,
66 CONFIG_DIR , SCOPE_FLAG , is_request_value_property, FIELD_SEP , docopt_mode, FILE_ARG , MIME_ARG , OUT_ARG ,
7- cmd_ident, call_method_ident, arg_ident, POD_TYPES , flag_ident, ident, JSON_TYPE_VALUE_MAP )
7+ cmd_ident, call_method_ident, arg_ident, POD_TYPES , flag_ident, ident, JSON_TYPE_VALUE_MAP ,
8+ KEY_VALUE_ARG , to_cli_schema, SchemaEntry, CTYPE_POD )
89
910 v_arg = ' <%s >' % VALUE_ARG
1011 SOPT = ' self.opt.'
2627%> \
2728mod cmn;
2829use cmn::{InvalidOptionsError, CLIError, JsonTokenStorage, arg_from_str, writer_from_opts, parse_kv_arg,
29- input_file_from_opts, input_mime_from_opts};
30+ input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError };
3031
3132use std::default::Default;
3233use std::str::FromStr;
@@ -145,6 +146,8 @@ self.opt.${cmd_ident(method)} {
145146 if parameters is not UNDEFINED :
146147 global_parameter_names = list (pn for pn in sorted (parameters.keys()) if pn not in optional_prop_names)
147148 handle_props = optional_props or parameters is not UNDEFINED
149+ if mc.request_value:
150+ request_cli_schema = to_cli_schema(c, mc.request_value)
148151%> \
149152 ## REQUIRED PARAMETERS
150153% for p in mc.required_props:
@@ -154,7 +157,7 @@ self.opt.${cmd_ident(method)} {
154157 opt_ident = to_opt_arg_ident(p)
155158%> \
156159 % if is_request_value_property(mc, p):
157- let ${ prop_name} : api::${ prop_type} = Default::default();
160+ let mut ${ prop_name} : api::${ prop_type} = Default::default();
158161 % elif p.type != ' string' :
159162let ${ prop_name} : ${ prop_type} = arg_from_str(& ${ opt_ident} , err, "<${ mangle_subcommand(p.name)} >", "${ p.type} ");
160163 % endif # handle request value
@@ -194,7 +197,7 @@ for parg in ${SOPT + arg_ident(VALUE_ARG)}.iter() {
194197 % endif
195198 call = call.${ mangle_ident(setter_fn_name(p))} (\
196199 % if ptype != ' string' :
197- arg_from_str(${ value_unwrap} , err, "${ mangle_subcommand(p.name)} ", "${ p.type } ")\
200+ arg_from_str(${ value_unwrap} , err, "${ mangle_subcommand(p.name)} ", "${ ptype } ")\
198201 % else :
199202${ value_unwrap} \
200203 % endif # handle conversion
@@ -233,6 +236,68 @@ ${value_unwrap}\
233236 }
234237}
235238% endif # handle call parameters
239+ % if mc.request_value:
240+ <%
241+ def flatten_schema_fields (schema , res , cur = list ()):
242+ if len (cur) == 0 :
243+ cur = list ()
244+ for fn, f in schema.fields.iteritems():
245+ cur.append(fn)
246+ if isinstance (f, SchemaEntry):
247+ res.append((f, list (cur)))
248+ else :
249+ flatten_schema_fields(f, res, cur)
250+ cur.pop()
251+ # endfor
252+ # end utility
253+
254+ schema_fields = list ()
255+ flatten_schema_fields(request_cli_schema, schema_fields)
256+ %> \
257+ let mut field_name: FieldCursor = Default::default();
258+ for kvarg in ${ SOPT + arg_ident(KEY_VALUE_ARG )} .iter() {
259+ let (key, value) = parse_kv_arg(& *kvarg, err);
260+ if let Err(field_err) = field_name.set(& *key) {
261+ err.issues.push(field_err);
262+ }
263+ match &field_name.to_string()[..] {
264+ % for fv, f in schema_fields:
265+ <%
266+ # TODO : Deduplicate !
267+ ptype = fv.actual_property.type
268+ if ptype == ' string' and ' Count' in f[- 1 ]:
269+ ptype = ' int64'
270+ value_unwrap = ' value.unwrap_or("%s ")' % JSON_TYPE_VALUE_MAP [ptype]
271+ pname = FIELD_SEP .join(mangle_subcommand(ft) for ft in f)
272+
273+ struct_field = ' request.' + ' .' .join(' %s .as_mut().unwrap()' % mangle_ident(ft) for ft in f[:- 1 ])
274+ if len (f) > 1 :
275+ struct_field += ' .'
276+ struct_field += mangle_ident(f[- 1 ])
277+ %> \
278+ "${ pname} " => {
279+ % if fv.container_type == CTYPE_POD :
280+ ${ struct_field} = Some(\
281+ % else :
282+ if ${ struct_field} .is_none() {
283+ ${ struct_field} = Some(Default::default());
284+ }
285+ ${ struct_field} .as_mut().unwrap().push(\
286+ % endif
287+ % if ptype != ' string' :
288+ arg_from_str(${ value_unwrap} , err, "${ pname} ", "${ ptype} ")\
289+ % else :
290+ ${ value_unwrap} .to_string()\
291+ % endif
292+ );
293+ },
294+ % endfor # each nested field
295+ _ => {
296+ err.issues.push(CLIError::Field(FieldError::Unknown(field_name.to_string())));
297+ }
298+ }
299+ }
300+ % endif # handle struct parsing
236301% if mc.media_params:
237302let protocol =
238303% for p in mc.media_params:
0 commit comments