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

Skip to content

Commit bf22bef

Browse files
committed
fix(CLI): struct access compiles ...
... but currently wouldn't run as we don't initialize the optional sub- structures at all.
1 parent 15b78cd commit bf22bef

4 files changed

Lines changed: 37 additions & 25 deletions

File tree

src/mako/api/lib/schema.mako

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from util import (schema_markers, rust_doc_comment, mangle_ident, to_rust_type, put_and,
33
IO_TYPES, activity_split, enclose_in, REQUEST_MARKER_TRAIT, mb_type, indent_all_but_first_by,
44
NESTED_TYPE_SUFFIX, RESPONSE_MARKER_TRAIT, split_camelcase_s, METHODS_RESOURCE, unique_type_name,
5-
PART_MARKER_TRAIT, canonical_type_name, TO_PARTS_MARKER, UNUSED_TYPE_MARKER)
5+
PART_MARKER_TRAIT, canonical_type_name, TO_PARTS_MARKER, UNUSED_TYPE_MARKER, is_schema_with_optionals)
66
77
default_traits = ('RustcEncodable', 'Clone', 'Default')
88
%>\
@@ -63,15 +63,11 @@ ${struct};
6363
if 'variant' not in s:
6464
traits.insert(0, 'Default')
6565
66-
allow_optionals = True
6766
if RESPONSE_MARKER_TRAIT in markers:
6867
traits.append('Deserialize')
6968
7069
nt_markers = schema_markers(s, c, transitive=False)
71-
if ( PART_MARKER_TRAIT in nt_markers
72-
or RESPONSE_MARKER_TRAIT in nt_markers and REQUEST_MARKER_TRAIT not in nt_markers):
73-
allow_optionals = False
74-
# use optionals only when needed
70+
allow_optionals = is_schema_with_optionals(nt_markers)
7571
7672
# waiting for Default: https://github.com/rust-lang/rustc-serialize/issues/71
7773
if s.type == 'any':

src/mako/cli/lib/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
import collections
66
from copy import deepcopy
7-
from random import (randint, random, choice, seed)
7+
from random import (randint, random, choice)
88

99
SPLIT_START = '>>>>>>>'
1010
SPLIT_END = '<<<<<<<'

src/mako/cli/lib/engine.mako

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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':
288296
arg_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
_ => {

src/mako/lib/util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ def schema_markers(s, c, transitive=True):
448448

449449
## -- End Rust TypeSystem -- @}
450450

451+
def is_schema_with_optionals(schema_markers):
452+
return not (PART_MARKER_TRAIT in schema_markers
453+
or RESPONSE_MARKER_TRAIT in schema_markers and REQUEST_MARKER_TRAIT not in schema_markers)
451454

452455
# -------------------------
453456
## @name Activity Utilities

0 commit comments

Comments
 (0)