Skip to content

Commit

Permalink
fix(nestedtypes): recursion for nested types
Browse files Browse the repository at this point in the history
Drive has recursive nested types, which were not handled preeviously.
  • Loading branch information
Byron committed Mar 8, 2015
1 parent 66f3ae1 commit 0d9f636
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 42 deletions.
13 changes: 7 additions & 6 deletions gen/youtube3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ let r = hub.resource().activity(...).doit()
Or specifically ...

```ignore
let r = hub.videos().rate(...).doit()
let r = hub.videos().getRating(...).doit()
let r = hub.videos().list(...).doit()
let r = hub.videos().insert(...).doit()
let r = hub.videos().update(...).doit()
let r = hub.videos().delete(...).doit()
let r = hub.live_broadcasts().control(...).doit()
let r = hub.live_broadcasts().insert(...).doit()
let r = hub.live_broadcasts().list(...).doit()
let r = hub.live_broadcasts().transition(...).doit()
let r = hub.live_broadcasts().update(...).doit()
let r = hub.live_broadcasts().delete(...).doit()
let r = hub.live_broadcasts().bind(...).doit()
```

The `resource()` and `activity(...)` calls create [builders][builder-pattern]. The second one dealing with `Activities`
Expand Down
67 changes: 44 additions & 23 deletions gen/youtube3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@
//! Or specifically ...
//!
//! ```ignore
//! let r = hub.videos().rate(...).doit()
//! let r = hub.videos().getRating(...).doit()
//! let r = hub.videos().list(...).doit()
//! let r = hub.videos().insert(...).doit()
//! let r = hub.videos().update(...).doit()
//! let r = hub.videos().delete(...).doit()
//! let r = hub.live_broadcasts().control(...).doit()
//! let r = hub.live_broadcasts().insert(...).doit()
//! let r = hub.live_broadcasts().list(...).doit()
//! let r = hub.live_broadcasts().transition(...).doit()
//! let r = hub.live_broadcasts().update(...).doit()
//! let r = hub.live_broadcasts().delete(...).doit()
//! let r = hub.live_broadcasts().bind(...).doit()
//! ```
//!
//! The `resource()` and `activity(...)` calls create [builders][builder-pattern]. The second one dealing with `Activities`
Expand Down Expand Up @@ -126,7 +127,7 @@ use std::marker::PhantomData;
use std::borrow::BorrowMut;
use std::cell::RefCell;
use std::default::Default;
use std::io::{Read, Seek}
use std::io::{Read, Seek};

pub use cmn::{Hub, ResourceMethodsBuilder, MethodBuilder, Resource, Part, ResponseResult, RequestValue, NestedType};

Expand Down Expand Up @@ -589,7 +590,9 @@ impl Part for ChannelAuditDetails {}
/// This type is used in activities, which are methods you may call on this type or where this type is involved in.
/// The list links the activity name, along with information about where it is used (one of *request* and *response*).
///
/// * delete (none)
/// * update (request|response)
/// * list (none)
/// * insert (request|response)
///
///
Expand Down Expand Up @@ -1102,6 +1105,8 @@ impl Part for InvideoPromotion {}
///
/// * insert (request|response)
/// * update (request|response)
/// * list (none)
/// * delete (none)
///
///
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
Expand Down Expand Up @@ -1223,7 +1228,7 @@ impl Part for ChannelSectionSnippet {}
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct ChannelContentDetails {
/// no description provided
pub related_playlists: HashMap<String, ChannelContentDetailsRelatedplaylists>,
pub related_playlists: HashMap<String, ChannelContentDetailsRelatedPlaylists>,
/// The googlePlusUserId object identifies the Google+ profile ID associated with this channel.
pub google_plus_user_id: Option<String>,
}
Expand Down Expand Up @@ -1389,7 +1394,13 @@ impl Part for ActivityContentDetails {}

/// A i18nRegion resource identifies a region where YouTube is available.
///
/// This type is not used in any activity, and only used as *part* of another schema.
/// # Activities
///
/// This type is used in activities, which are methods you may call on this type or where this type is involved in.
/// The list links the activity name, along with information about where it is used (one of *request* and *response*).
///
/// * list (none)
///
///
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct I18nRegion {
Expand All @@ -1403,7 +1414,7 @@ pub struct I18nRegion {
pub id: Option<String>,
}

impl Part for I18nRegion {}
impl Resource for I18nRegion {}


/// The contentOwnerDetails object encapsulates channel data that is relevant for YouTube Partners linked with the channel.
Expand Down Expand Up @@ -2098,8 +2109,10 @@ impl Part for LocalizedProperty {}
///
/// * control (response)
/// * insert (request|response)
/// * update (request|response)
/// * list (none)
/// * transition (response)
/// * update (request|response)
/// * delete (none)
/// * bind (response)
///
///
Expand Down Expand Up @@ -2653,7 +2666,9 @@ impl ResponseResult for ChannelListResponse {}
/// This type is used in activities, which are methods you may call on this type or where this type is involved in.
/// The list links the activity name, along with information about where it is used (one of *request* and *response*).
///
/// * delete (none)
/// * update (request|response)
/// * list (none)
/// * insert (request|response)
///
///
Expand Down Expand Up @@ -3115,7 +3130,13 @@ impl Part for ActivityContentDetailsBulletin {}

/// An i18nLanguage resource identifies a UI language currently supported by YouTube.
///
/// This type is not used in any activity, and only used as *part* of another schema.
/// # Activities
///
/// This type is used in activities, which are methods you may call on this type or where this type is involved in.
/// The list links the activity name, along with information about where it is used (one of *request* and *response*).
///
/// * list (none)
///
///
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct I18nLanguage {
Expand All @@ -3129,7 +3150,7 @@ pub struct I18nLanguage {
pub id: Option<String>,
}

impl Part for I18nLanguage {}
impl Resource for I18nLanguage {}


/// There is no detailed description.
Expand Down Expand Up @@ -3426,7 +3447,7 @@ impl Part for PageInfo {}
/// This type is not used in any activity, and only used as *part* of another schema.
///
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct ChannelContentDetailsRelatedplaylists {
pub struct ChannelContentDetailsRelatedPlaylists {
/// The ID of the playlist that contains the channel"s uploaded videos. Use the videos.insert method to upload new videos and the videos.delete method to delete previously uploaded videos.
pub uploads: Option<String>,
/// The ID of the playlist that contains the channel"s watch history. Use the playlistItems.insert and playlistItems.delete to add or remove items from that list.
Expand All @@ -3439,8 +3460,8 @@ pub struct ChannelContentDetailsRelatedplaylists {
pub watch_later: Option<String>,
}

impl NestedType for ChannelContentDetailsRelatedplaylists {}
impl Part for ChannelContentDetailsRelatedplaylists {}
impl NestedType for ChannelContentDetailsRelatedPlaylists {}
impl Part for ChannelContentDetailsRelatedPlaylists {}



Expand Down Expand Up @@ -3941,8 +3962,8 @@ impl<'a, C, NC, A> VideoMethodsBuilder<'a, C, NC, A> {
/// Create a builder to help you perform the following task:
///
/// Retrieves the ratings that the authorized user gave to a list of specified videos.
pub fn get_rating(&self, id: &str) -> VideoGetratingMethodBuilder<'a, C, NC, A> {
VideoGetratingMethodBuilder {
pub fn get_rating(&self, id: &str) -> VideoGetRatingMethodBuilder<'a, C, NC, A> {
VideoGetRatingMethodBuilder {
hub: self.hub,
_id: id.to_string(),
_on_behalf_of_content_owner: Default::default(),
Expand Down Expand Up @@ -6511,7 +6532,7 @@ impl<'a, C, NC, A> VideoRateMethodBuilder<'a, C, NC, A> {
/// // TODO: show how to handle the result !
/// # }
/// ```
pub struct VideoGetratingMethodBuilder<'a, C, NC, A>
pub struct VideoGetRatingMethodBuilder<'a, C, NC, A>
where NC: 'a,
C: 'a,
A: 'a, {
Expand All @@ -6521,9 +6542,9 @@ pub struct VideoGetratingMethodBuilder<'a, C, NC, A>
_on_behalf_of_content_owner: Option<String>,
}

impl<'a, C, NC, A> MethodBuilder for VideoGetratingMethodBuilder<'a, C, NC, A> {}
impl<'a, C, NC, A> MethodBuilder for VideoGetRatingMethodBuilder<'a, C, NC, A> {}

impl<'a, C, NC, A> VideoGetratingMethodBuilder<'a, C, NC, A> {
impl<'a, C, NC, A> VideoGetRatingMethodBuilder<'a, C, NC, A> {


/// Perform the operation you have build so far.
Expand All @@ -6539,7 +6560,7 @@ impl<'a, C, NC, A> VideoGetratingMethodBuilder<'a, C, NC, A> {
/// we provide this method for API completeness.
///
/// The id parameter specifies a comma-separated list of the YouTube video ID(s) for the resource(s) for which you are retrieving rating data. In a video resource, the id property specifies the video's ID.
pub fn id(mut self, new_value: &str) -> VideoGetratingMethodBuilder<'a, C, NC, A> {
pub fn id(mut self, new_value: &str) -> VideoGetRatingMethodBuilder<'a, C, NC, A> {
self._id = new_value.to_string();
return self;
}
Expand All @@ -6549,7 +6570,7 @@ impl<'a, C, NC, A> VideoGetratingMethodBuilder<'a, C, NC, A> {
/// Note: This parameter is intended exclusively for YouTube content partners.
///
/// The onBehalfOfContentOwner parameter indicates that the request's authorization credentials identify a YouTube CMS user who is acting on behalf of the content owner specified in the parameter value. This parameter is intended for YouTube content partners that own and manage many different YouTube channels. It allows content owners to authenticate once and get access to all their video and channel data, without having to provide authentication credentials for each individual channel. The CMS account that the user authenticates with must be linked to the specified YouTube content owner.
pub fn on_behalf_of_content_owner(mut self, new_value: &str) -> VideoGetratingMethodBuilder<'a, C, NC, A> {
pub fn on_behalf_of_content_owner(mut self, new_value: &str) -> VideoGetRatingMethodBuilder<'a, C, NC, A> {
self._on_behalf_of_content_owner = Some(new_value.to_string());
return self;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mako/lib.rs.mako
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::marker::PhantomData;
use std::borrow::BorrowMut;
use std::cell::RefCell;
use std::default::Default;
use std::io::{Read, Seek}
use std::io::{Read, Seek};

pub use cmn::{Hub, ResourceMethodsBuilder, MethodBuilder, Resource, Part, ResponseResult, RequestValue, NestedType};

Expand Down
4 changes: 2 additions & 2 deletions src/mako/lib/lib.mako
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%! from util import (activity_split, put_and, md_italic, split_camelcase_s, canonical_type_name,
rust_test_fn_invisible, rust_doc_test_norun, rust_doc_comment, markdown_rust_block,
unindent_first_by) %>\
unindent_first_by, mangle_ident) %>\
<%namespace name="util" file="util.mako"/>\
## If rust-doc is True, examples will be made to work for rust doc tests. Otherwise they are set
Expand Down Expand Up @@ -47,7 +47,7 @@ Or specifically ...
```ignore
% for an, a in c.sta_map[fr.id].iteritems():
<% resource, activity = activity_split(an) %>\
let r = hub.${resource}().${activity}(...).${api.terms.action}()
let r = hub.${mangle_ident(resource)}().${activity}(...).${api.terms.action}()
% endfor
```

Expand Down
2 changes: 1 addition & 1 deletion src/mako/lib/schema.mako
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## 's' contains the schema structure from json to build
<%def name="new(s, c)">\
<%
assert s.type == "object"
## assert s.type == "object"
markers = schema_markers(s, c)
%>\
<%block filter="rust_doc_comment">\
Expand Down
31 changes: 22 additions & 9 deletions src/mako/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
'double' : 'f64',
'float' : 'f32',
'int32' : 'i32',
'int64' : 'i64',
'uint64' : 'u64',
'array' : 'Vec',
'string' : 'String',
'object' : 'HashMap'}

_words = [w.strip(',') for w in "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.".split(' ')]
RUST_TYPE_RND_MAP = {'bool': lambda: str(bool(randint(0, 1))).lower(),
'u32' : lambda: randint(0, 100),
'u64' : lambda: randint(0, 100),
'f64' : lambda: random(),
'f32' : lambda: random(),
'i32' : lambda: randint(-101, -1),
'i64' : lambda: randint(-101, -1),
'String': lambda: '"%s"' % choice(_words),
}
TREF = '$ref'
Expand Down Expand Up @@ -218,7 +223,7 @@ def canonical_type_name(s):
return s[:1].upper() + s[1:]

def nested_type_name(sn, pn):
return sn + pn.capitalize()
return sn + canonical_type_name(pn)

# Make properties which are reserved keywords usable
def mangle_ident(n):
Expand Down Expand Up @@ -273,7 +278,7 @@ def nested_type(nt):

# return True if this property is actually a nested type
def is_nested_type_property(t):
return 'type' in t and t.type == 'object' and 'properties' in t
return 'type' in t and t.type == 'object' and 'properties' in t or ('items' in t and 'properties' in t.items)

# Return True if the schema is nested
def is_nested_type(s):
Expand All @@ -295,16 +300,24 @@ def is_pod_property(p):

# return an iterator yielding fake-schemas that identify a nested type
def iter_nested_types(schemas):
for s in schemas.values():
if 'properties' not in s:
continue
for pn, p in s.properties.iteritems():
def iter_nested_properties(prefix, properties):
for pn, p in properties.iteritems():
if is_nested_type_property(p):
ns = p.copy()
ns.id = nested_type_name(s.id, pn)
ns.id = nested_type_name(prefix, pn)
ns[NESTED_TYPE_MARKER] = True
if 'items' in p:
ns.update(p.items.iteritems())
yield ns
for np in iter_nested_properties(prefix + canonical_type_name(pn), ns.properties):
yield np
# can be recursive ...
# end for ach property
for s in schemas.values():
if 'properties' not in s:
continue
for np in iter_nested_properties(s.id, s.properties):
yield np
# end for aech schma

# Return sorted type names of all markers applicable to the given schema
Expand Down Expand Up @@ -353,7 +366,7 @@ def to_fqan(name, resource, method):

# videos -> Video
def activity_name_to_type_name(an):
return an.capitalize()[:-1]
return canonical_type_name(an)[:-1]

# yields (resource, activity, activity_data)
def iter_acitivities(c):
Expand Down Expand Up @@ -552,7 +565,7 @@ def mb_type_params_s(m):

# return type name for a method on the given resource
def mb_type(r, m):
return "%s%sMethodBuilder" % (singular(canonical_type_name(r)), m.capitalize())
return "%s%sMethodBuilder" % (singular(canonical_type_name(r)), canonical_type_name(m))

def hub_type(canonicalName):
return canonical_type_name(canonicalName)
Expand Down

0 comments on commit 0d9f636

Please sign in to comment.