11<%namespace name =" util" file =" ../../lib/util.mako" /> \
22<%!
33 from util import (hub_type, mangle_ident, indent_all_but_first_by, activity_rust_type, setter_fn_name, ADD_PARAM_FN ,
4- upload_action_fn)
4+ upload_action_fn, is_schema_with_optionals, schema_markers )
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 ,
77 cmd_ident, call_method_ident, arg_ident, POD_TYPES , flag_ident, ident, JSON_TYPE_VALUE_MAP ,
@@ -238,13 +238,21 @@ ${value_unwrap}\
238238% endif # handle call parameters
239239% if mc.request_value:
240240<%
241+ allow_optionals_fn = lambda s : is_schema_with_optionals(schema_markers(s, c, transitive = False ))
242+
241243 def flatten_schema_fields (schema , res , cur = list ()):
242244 if len (cur) == 0 :
243245 cur = list ()
246+
247+ opt_access = ' .as_mut().unwrap()'
248+ if not allow_optionals_fn(schema):
249+ opt_access = ' '
244250 for fn, f in schema.fields.iteritems():
245- cur.append(fn)
251+
252+ cur.append([' %s%s ' % (mangle_ident(fn), opt_access), fn])
246253 if isinstance (f, SchemaEntry):
247- res.append((f, list (cur)))
254+ cur[- 1 ][0 ] = mangle_ident(fn)
255+ res.append((schema, f, list (cur)))
248256 else :
249257 flatten_schema_fields(f, res, cur)
250258 cur.pop()
@@ -261,35 +269,40 @@ for kvarg in ${SOPT + arg_ident(KEY_VALUE_ARG)}.iter() {
261269 err.issues.push(field_err);
262270 }
263271 match &field_name.to_string()[..] {
264- % for fv , f in schema_fields:
272+ % for schema, fe , f in schema_fields:
265273<%
266274 # TODO : Deduplicate !
267- ptype = fv .actual_property.type
268- if ptype == ' string' and ' Count' in f[- 1 ]:
275+ ptype = fe .actual_property.type
276+ if ptype == ' string' and ' Count' in f[- 1 ][ 1 ] :
269277 ptype = ' int64'
270278 value_unwrap = ' value.unwrap_or("%s ")' % JSON_TYPE_VALUE_MAP [ptype]
271- pname = FIELD_SEP .join(mangle_subcommand(ft) for ft in f)
279+ pname = FIELD_SEP .join(mangle_subcommand(t[1 ]) for t in f)
280+
281+ allow_optionals = True
282+ opt_prefix = ' Some('
283+ opt_suffix = ' )'
284+ if not allow_optionals_fn(schema):
285+ opt_prefix = opt_suffix = ' '
272286
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 ])
287+ struct_field = ' request.' + ' .' .join(t[0 ] for t in f)
277288%> \
278289 "${ pname} " => {
279- % if fv .container_type == CTYPE_POD :
280- ${ struct_field} = Some( \
290+ % if fe .container_type == CTYPE_POD :
291+ ${ struct_field} = ${ opt_prefix } \
281292 % else :
282- if ${ struct_field} .is_none() {
283- ${ struct_field} = Some(Default::default());
284- }
285- ${ struct_field} .as_mut().unwrap().push(\
293+ ${ struct_field} .push(\
286294 % endif
287295 % if ptype != ' string' :
288296arg_from_str(${ value_unwrap} , err, "${ pname} ", "${ ptype} ")\
289297 % else :
290298${ value_unwrap} .to_string()\
291299 % endif
292- );
300+ % if fe.container_type == CTYPE_POD :
301+ ${ opt_suffix} \
302+ % else :
303+ )\
304+ % endif
305+ ;
293306 },
294307 % endfor # each nested field
295308 _ => {
0 commit comments