feat(schema): support for 'variant' schema

Documentation links, at one spot, have been updated as well.
The variant schema is represented natively as enum, it all looks
very good.

Json has been taken care of as well ... .
This commit is contained in:
Sebastian Thiel
2015-03-20 17:57:50 +01:00
parent 55978ff9a2
commit bb75c5b698
3 changed files with 38 additions and 4 deletions

View File

@@ -5,7 +5,7 @@
find_fattest_resource, build_all_params, pass_through, parts_from_params,
REQUEST_MARKER_TRAIT, RESPONSE_MARKER_TRAIT, supports_scopes, to_api_version,
to_fqan, METHODS_RESOURCE, ADD_PARAM_MEDIA_EXAMPLE, PROTOCOL_TYPE_INFO, enclose_in,
upload_action_fn, unique_type_name) %>\
upload_action_fn, unique_type_name, schema_doc_format) %>\
<%namespace name="util" file="util.mako"/>\
<%namespace name="mbuild" file="mbuild.mako"/>\
@@ -78,7 +78,7 @@ It seems there is nothing you can do here ... .
sn = singular(canonical_type_name(r))
if sn in schemas:
md_resource = link(md_resource, 'struct.%s.html' % unique_type_name(singular(canonical_type_name(r))))
md_resource = link(md_resource, schema_doc_format(schemas[sn]) % unique_type_name(sn))
%>\
* ${md_resource} (${put_and(md_methods)})
% endfor ## each resource activity

View File

@@ -2,7 +2,7 @@
from util import (schema_markers, rust_doc_comment, mangle_ident, to_rust_type, put_and,
IO_TYPES, activity_split, enclose_in, REQUEST_MARKER_TRAIT, mb_type, indent_all_but_first_by,
NESTED_TYPE_SUFFIX, RESPONSE_MARKER_TRAIT, split_camelcase_s, METHODS_RESOURCE, unique_type_name,
PART_MARKER_TRAIT)
PART_MARKER_TRAIT, canonical_type_name)
default_traits = ('RustcEncodable', 'Clone', 'Default')
%>\
@@ -23,6 +23,26 @@ ${struct} {
}
% elif 'additionalProperties' in s:
${struct}(${to_rust_type(schemas, s.id, NESTED_TYPE_SUFFIX, s, allow_optionals=allow_optionals)});
% elif 'variant' in s:
<%
et = unique_type_name(s.id)
variant_type = lambda p: canonical_type_name(p.type_value)
%>
pub enum ${et} {
% for p in s.variant.map:
${p.get('description', 'no description provided') | rust_doc_comment, indent_all_but_first_by(1)}
% if variant_type(p) != p.type_value:
#[serde(alias="${p.type_value}")]
% endif
${variant_type(p)}(${to_rust_type(schemas, s.id, None, p, allow_optionals=allow_optionals)}),
% endfor
}
impl Default for ${et} {
fn default() -> ${et} {
${et}::${variant_type(s.variant.map[0])}(Default::default())
}
}
% else: ## it's an empty struct, i.e. struct Foo;
${struct};
% endif ## 'properties' in s
@@ -35,7 +55,11 @@ ${struct};
<%def name="new(s, c)">\
<%
markers = schema_markers(s, c, transitive=True)
traits = ['Default', 'Clone', 'Debug']
traits = ['Clone', 'Debug']
# default only works for structs, and 'variant' will be an enum
if 'variant' not in s:
traits.insert(0, 'Default')
allow_optionals = True
if REQUEST_MARKER_TRAIT in markers:

View File

@@ -506,6 +506,13 @@ def rust_copy_value_s(n, tn, p):
def schema_to_required_property(s, n):
return type(s)({'name': n, TREF: s.id, 'priority': REQUEST_PRIORITY, 'is_query_param': False})
# Return relative URL format to the given schema. Handles structs and enums accordingly
def schema_doc_format(s):
prefix = 'struct.'
if 'variant' in s:
prefix = 'enum.'
return prefix + '%s.html'
def is_required_property(p):
return p.get('required', False) or p.get('priority', 0) > 0
@@ -689,6 +696,9 @@ def new_context(schemas, resources, methods):
elif 'items' in p:
recurse_properties(nested_type_name(prefix, pn), rs,
p.items, append_unique(parent_ids, rs.id))
elif 'variant' in p:
for enum in p.variant.map:
recurse_properties(prefix, rs, enum, parent_ids)
# end handle prop itself
# end for each property
# end utility