From bb75c5b69871ec88c888618d0c3292741c9cffff Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 20 Mar 2015 17:57:50 +0100 Subject: [PATCH] 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 ... . --- src/mako/lib/lib.mako | 4 ++-- src/mako/lib/schema.mako | 28 ++++++++++++++++++++++++++-- src/mako/lib/util.py | 10 ++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/mako/lib/lib.mako b/src/mako/lib/lib.mako index 33f41510340..bf5ab3e8611 100644 --- a/src/mako/lib/lib.mako +++ b/src/mako/lib/lib.mako @@ -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 diff --git a/src/mako/lib/schema.mako b/src/mako/lib/schema.mako index 77a1005fd7b..5d72258f50a 100644 --- a/src/mako/lib/schema.mako +++ b/src/mako/lib/schema.mako @@ -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: diff --git a/src/mako/lib/util.py b/src/mako/lib/util.py index cd17064596f..38c21e95b2e 100644 --- a/src/mako/lib/util.py +++ b/src/mako/lib/util.py @@ -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 recurse_properties(prefix, rs, s, parent_ids): 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