Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 306852d

Browse files
committed
fix(CLI): alt-media handling in CLI+API-docs
* API-docs now adjust depending on where 'alt' is set (either as global parameter, or as method-parameter) * CLI: download tracking now works for 'alt' as method-parameter * CLI: global parameter remapping allows them to be named consistently, but map to the name required by the google API. Fixes #61
1 parent 36a7cb2 commit 306852d

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

src/mako/api/lib/mbuild.mako

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
ThisType = mb_type(resource, method) + mb_tparams
4646
4747
params, request_value = build_all_params(c, m)
48+
alt_param = None
49+
for p in params:
50+
if p.name == 'alt':
51+
alt_param = p
52+
break
53+
# end
54+
# end
4855
4956
part_prop, parts = parts_from_params(params)
5057
part_desc = make_parts_desc(part_prop)
@@ -56,7 +63,11 @@ ${m.description | rust_doc_comment}
5663
% endif
5764
% if m.get('supportsMediaDownload', False):
5865
/// This method supports **media download**. To enable it, adjust the builder like this:
66+
% if alt_param:
67+
/// `.${mangle_ident(setter_fn_name(alt_param))}("media")`.
68+
% else:
5969
/// `${ADD_PARAM_MEDIA_EXAMPLE}`.
70+
% endif
6071
% if response_schema:
6172
/// Please note that due to missing multi-part support on the server side, you will only receive the media,
6273
/// but not the `${unique_type_name(response_schema.id)}` structure that you would usually get. The latter will be a default value.
@@ -140,7 +151,7 @@ ${self._setter_fn(resource, method, m, p, part_prop, ThisType, c)}\
140151
///
141152
/// # Additional Parameters
142153
///
143-
% for opn, op in parameters.iteritems():
154+
% for opn, op in list((opn, op) for (opn, op) in parameters.iteritems() if opn not in [p.name for p in params]):
144155
/// * *${opn}* (${op.location}-${op.type}) - ${op.description}
145156
% endfor
146157
% endif

src/mako/cli/lib/engine.mako

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,14 @@ self.opt.${cmd_ident(method)} {
130130
mc = new_method_context(resource, method, c)
131131
supports_media_download = mc.m.get('supportsMediaDownload', False)
132132
handle_output = mc.response_schema or supports_media_download
133-
track_download_flag = supports_media_download and parameters is not UNDEFINED and 'alt' in parameters
134-
135133
optional_props = [p for p in mc.optional_props if not p.get('skip_example', False)]
136134
optional_prop_names = set(p.name for p in optional_props)
135+
136+
global_parameter_names = set()
137+
track_download_flag = (supports_media_download and
138+
(parameters is not UNDEFINED and 'alt' in parameters) or ('alt' in optional_prop_names))
139+
if parameters is not UNDEFINED:
140+
global_parameter_names = list(pn for pn in sorted(parameters.keys()) if pn not in optional_prop_names)
137141
handle_props = optional_props or parameters is not UNDEFINED
138142
%>\
139143
## REQUIRED PARAMETERS
@@ -176,20 +180,28 @@ for parg in ${SOPT + arg_ident(VALUE_ARG)}.iter() {
176180
ptype = 'int64'
177181
value_unwrap = 'value.unwrap_or("%s")' % JSON_TYPE_RND_MAP[ptype]()
178182
%>\
179-
"${ident(p.name)}" => call = call.${mangle_ident(setter_fn_name(p))}(\
183+
"${mangle_subcommand(p.name)}" => {
184+
% if p.name == 'alt':
185+
if ${value_unwrap} == "media" {
186+
download_mode = true;
187+
}
188+
% endif
189+
call = call.${mangle_ident(setter_fn_name(p))}(\
180190
% if ptype != 'string':
181-
arg_from_str(${value_unwrap}, err, "${ident(p.name)}", "${p.type}")),
191+
arg_from_str(${value_unwrap}, err, "${mangle_subcommand(p.name)}", "${p.type}")\
182192
% else:
183-
${value_unwrap}),
193+
${value_unwrap}\
184194
% endif # handle conversion
195+
);
196+
},
185197
% endfor # each property
186198
% if parameters is not UNDEFINED:
187-
% for pn, p in list((pn, p) for (pn, p) in parameters.iteritems() if pn not in optional_prop_names):
199+
% for pn in global_parameter_names:
188200
\
189201
% if not loop.first:
190202
|\
191203
% endif
192-
"${ident(pn)}"\
204+
"${mangle_subcommand(pn)}"\
193205
% if not loop.last:
194206
195207
% endif
@@ -198,12 +210,17 @@ ${value_unwrap}),
198210
<%
199211
value_unwrap = 'value.unwrap_or("unset")'
200212
%>\
201-
% if track_download_flag:
213+
% if track_download_flag and 'alt' in global_parameter_names:
202214
if key == "alt" && ${value_unwrap} == "media" {
203215
download_mode = true;
204216
}
205217
% endif
206-
call = call.${ADD_PARAM_FN}(key, ${value_unwrap})
218+
let map = [
219+
% for pn in list(pn for pn in global_parameter_names if mangle_subcommand(pn) != pn):
220+
("${mangle_subcommand(pn)}", "${pn}"),
221+
% endfor # each global parameter
222+
];
223+
call = call.${ADD_PARAM_FN}(map.iter().find(|t| t.0 == key).unwrap_or(&("", key)).1, ${value_unwrap})
207224
},
208225
% endif # handle global parameters
209226
_ => err.issues.push(CLIError::UnknownParameter(key.to_string())),

0 commit comments

Comments
 (0)