refactor(mbuild): new _setter method

Just to make things a little easier to read. Don't expect it to
grow much larger though.
This commit is contained in:
Sebastian Thiel
2015-03-04 20:29:58 +01:00
parent 582aca3249
commit 1dc168497e
3 changed files with 306 additions and 235 deletions

View File

@@ -2,7 +2,7 @@
from util import (put_and, rust_test_fn_invisible, rust_doc_test_norun, rust_doc_comment,
rb_type, mb_type, singular, hub_type, to_fqan, indent_all_but_first_by,
method_params, activity_rust_type, mangle_ident, activity_input_type, get_word,
split_camelcase_s)
split_camelcase_s, property, TREF)
%>\
<%namespace name="util" file="util.mako"/>\
<%namespace name="lib" file="lib.mako"/>\
@@ -15,7 +15,6 @@
hub_type_name = hub_type(canonicalName)
m = c.fqan_map[to_fqan(name, resource, method)]
# an identifier for a property. We prefix them to prevent clashes with the setters
property = lambda x: '_' + mangle_ident(x)
ThisType = mb_type(resource, method) + "<'a, C, NC, A>"
%>\
% if 'description' in m:
@@ -51,6 +50,7 @@ pub struct ${ThisType}
A: 'a, {
hub: &'a ${hub_type_name}<C, NC, A>,
## PROPERTIES ###############
% for p in method_params(m):
${property(p.name)}: ${activity_rust_type(p)},
% endfor
@@ -69,15 +69,40 @@ impl<'a, C, NC, A> ${ThisType} {
}
## PROPERTIES ###############
% for p in method_params(m):
${self._setter(resource, method, m, p, ThisType, c)}\
% endfor
}
</%def>
## creates a setter for the call builder
###############################################################################################
###############################################################################################
<%def name="_setter(resource, method, m, p, ThisType, c)">\
<%
InType = activity_input_type(p)
def show_part_info(m, p):
if p.name != 'part':
return False
if not (m.get('request') and m.get('response')):
return False
return m.request.get(TREF, 'first') == m.response.get(TREF, 'second')
%>\
/// Sets the *${split_camelcase_s(p.name)}* ${get_word(p, 'location')}property to the given value.
% if p.get('required', False):
///
% if show_part_info(m, p):
/// Even though the *parts* list is automatically derived from *Resource* passed in
/// during instantiation and indicates which values you are passing, the response would contain the very same parts.
/// This may not always be desirable, as you can obtain (newly generated) parts you cannot pass in,
/// like statistics that are generated server side. Therefore you should use this method to specify
/// the parts you provide in addition to the ones you want in the response.
% elif p.get('required', False):
/// Even though the property as already been set when instantiating this call,
/// we provide this method for API completeness.
%endif
% endif
///
% if 'description' in p:
${p.description | rust_doc_comment, indent_all_but_first_by(1)}
@@ -90,7 +115,4 @@ impl<'a, C, NC, A> ${ThisType} {
% endif
return self;
}
% endfor
}
</%def>

View File

@@ -396,3 +396,7 @@ def get_word(d, n, e = ''):
else:
return ''
# n = 'FooBar' -> _foo_bar
def property(n):
return '_' + mangle_ident(n)