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

Skip to content

Commit bb75c5b

Browse files
committed
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 ... .
1 parent 55978ff commit bb75c5b

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/mako/lib/lib.mako

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
find_fattest_resource, build_all_params, pass_through, parts_from_params,
66
REQUEST_MARKER_TRAIT, RESPONSE_MARKER_TRAIT, supports_scopes, to_api_version,
77
to_fqan, METHODS_RESOURCE, ADD_PARAM_MEDIA_EXAMPLE, PROTOCOL_TYPE_INFO, enclose_in,
8-
upload_action_fn, unique_type_name) %>\
8+
upload_action_fn, unique_type_name, schema_doc_format) %>\
99
<%namespace name="util" file="util.mako"/>\
1010
<%namespace name="mbuild" file="mbuild.mako"/>\
1111
@@ -78,7 +78,7 @@ It seems there is nothing you can do here ... .
7878
sn = singular(canonical_type_name(r))
7979
8080
if sn in schemas:
81-
md_resource = link(md_resource, 'struct.%s.html' % unique_type_name(singular(canonical_type_name(r))))
81+
md_resource = link(md_resource, schema_doc_format(schemas[sn]) % unique_type_name(sn))
8282
%>\
8383
* ${md_resource} (${put_and(md_methods)})
8484
% endfor ## each resource activity

src/mako/lib/schema.mako

Lines changed: 26 additions & 2 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)
5+
PART_MARKER_TRAIT, canonical_type_name)
66
77
default_traits = ('RustcEncodable', 'Clone', 'Default')
88
%>\
@@ -23,6 +23,26 @@ ${struct} {
2323
}
2424
% elif 'additionalProperties' in s:
2525
${struct}(${to_rust_type(schemas, s.id, NESTED_TYPE_SUFFIX, s, allow_optionals=allow_optionals)});
26+
% elif 'variant' in s:
27+
<%
28+
et = unique_type_name(s.id)
29+
variant_type = lambda p: canonical_type_name(p.type_value)
30+
%>
31+
pub enum ${et} {
32+
% for p in s.variant.map:
33+
${p.get('description', 'no description provided') | rust_doc_comment, indent_all_but_first_by(1)}
34+
% if variant_type(p) != p.type_value:
35+
#[serde(alias="${p.type_value}")]
36+
% endif
37+
${variant_type(p)}(${to_rust_type(schemas, s.id, None, p, allow_optionals=allow_optionals)}),
38+
% endfor
39+
}
40+
41+
impl Default for ${et} {
42+
fn default() -> ${et} {
43+
${et}::${variant_type(s.variant.map[0])}(Default::default())
44+
}
45+
}
2646
% else: ## it's an empty struct, i.e. struct Foo;
2747
${struct};
2848
% endif ## 'properties' in s
@@ -35,7 +55,11 @@ ${struct};
3555
<%def name="new(s, c)">\
3656
<%
3757
markers = schema_markers(s, c, transitive=True)
38-
traits = ['Default', 'Clone', 'Debug']
58+
traits = ['Clone', 'Debug']
59+
60+
# default only works for structs, and 'variant' will be an enum
61+
if 'variant' not in s:
62+
traits.insert(0, 'Default')
3963
4064
allow_optionals = True
4165
if REQUEST_MARKER_TRAIT in markers:

src/mako/lib/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,13 @@ def rust_copy_value_s(n, tn, p):
506506
def schema_to_required_property(s, n):
507507
return type(s)({'name': n, TREF: s.id, 'priority': REQUEST_PRIORITY, 'is_query_param': False})
508508

509+
# Return relative URL format to the given schema. Handles structs and enums accordingly
510+
def schema_doc_format(s):
511+
prefix = 'struct.'
512+
if 'variant' in s:
513+
prefix = 'enum.'
514+
return prefix + '%s.html'
515+
509516
def is_required_property(p):
510517
return p.get('required', False) or p.get('priority', 0) > 0
511518

@@ -689,6 +696,9 @@ def recurse_properties(prefix, rs, s, parent_ids):
689696
elif 'items' in p:
690697
recurse_properties(nested_type_name(prefix, pn), rs,
691698
p.items, append_unique(parent_ids, rs.id))
699+
elif 'variant' in p:
700+
for enum in p.variant.map:
701+
recurse_properties(prefix, rs, enum, parent_ids)
692702
# end handle prop itself
693703
# end for each property
694704
# end utility

0 commit comments

Comments
 (0)