Skip to content

Commit

Permalink
fix(mako): fix name clashes
Browse files Browse the repository at this point in the history
Scopes could be invalid, previosly, and the hub type could clash
with other types provided as Schema.

Also, we used reserved identifiers
  • Loading branch information
Byron committed Mar 10, 2015
1 parent df9f029 commit d99ba9c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
18 changes: 9 additions & 9 deletions gen/youtube3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,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;
use std::fs;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -5264,7 +5264,7 @@ impl<'a, C, NC, A> ChannelBannerInsertMethodBuilder<'a, C, NC, A> where NC: hype


/// Perform the operation you have build so far.
fn doit<R, RS>(mut self, stream: Option<(R, u64, mime::Mime)>, resumeable_stream: Option<(RS, u64, mime::Mime)>) -> Result<ChannelBannerResource> where R: Read, RS: ReadSeek {
fn doit<R, RS>(mut self, stream: Option<(R, u64, mime::Mime)>, resumeable_stream: Option<(RS, u64, mime::Mime)>) -> Result<ChannelBannerResource> where R: io::Read, RS: ReadSeek {
let mut params: Vec<(&str, String)> = Vec::with_capacity(3 + self._additional_params.len());
if self._on_behalf_of_content_owner.is_some() {
params.push(("onBehalfOfContentOwner", self._on_behalf_of_content_owner.unwrap().to_string()));
Expand Down Expand Up @@ -5304,7 +5304,7 @@ impl<'a, C, NC, A> ChannelBannerInsertMethodBuilder<'a, C, NC, A> where NC: hype
/// * *multipart*: yes
/// * *valid mime types*: 'application/octet-stream', 'image/jpeg' and 'image/png'
pub fn upload<R>(mut self, stream: R, size: u64, mime_type: mime::Mime) -> Result<ChannelBannerResource>
where R: Read {
where R: io::Read {
self.doit(Some((stream, size, mime_type)), None::<(fs::File, u64, mime::Mime)>, )
}
/// Upload media in a resumeable fashion.
Expand Down Expand Up @@ -7271,7 +7271,7 @@ impl<'a, C, NC, A> ThumbnailSetMethodBuilder<'a, C, NC, A> where NC: hyper::net:


/// Perform the operation you have build so far.
fn doit<R, RS>(mut self, stream: Option<(R, u64, mime::Mime)>, resumeable_stream: Option<(RS, u64, mime::Mime)>) -> Result<ThumbnailSetResponse> where R: Read, RS: ReadSeek {
fn doit<R, RS>(mut self, stream: Option<(R, u64, mime::Mime)>, resumeable_stream: Option<(RS, u64, mime::Mime)>) -> Result<ThumbnailSetResponse> where R: io::Read, RS: ReadSeek {
let mut params: Vec<(&str, String)> = Vec::with_capacity(3 + self._additional_params.len());
params.push(("videoId", self._video_id.to_string()));
if self._on_behalf_of_content_owner.is_some() {
Expand Down Expand Up @@ -7312,7 +7312,7 @@ impl<'a, C, NC, A> ThumbnailSetMethodBuilder<'a, C, NC, A> where NC: hyper::net:
/// * *multipart*: yes
/// * *valid mime types*: 'application/octet-stream', 'image/jpeg' and 'image/png'
pub fn upload<R>(mut self, stream: R, size: u64, mime_type: mime::Mime) -> Result<ThumbnailSetResponse>
where R: Read {
where R: io::Read {
self.doit(Some((stream, size, mime_type)), None::<(fs::File, u64, mime::Mime)>, )
}
/// Upload media in a resumeable fashion.
Expand Down Expand Up @@ -8540,7 +8540,7 @@ impl<'a, C, NC, A> VideoInsertMethodBuilder<'a, C, NC, A> where NC: hyper::net::


/// Perform the operation you have build so far.
fn doit<R, RS>(mut self, stream: Option<(R, u64, mime::Mime)>, resumeable_stream: Option<(RS, u64, mime::Mime)>) -> Result<Video> where R: Read, RS: ReadSeek {
fn doit<R, RS>(mut self, stream: Option<(R, u64, mime::Mime)>, resumeable_stream: Option<(RS, u64, mime::Mime)>) -> Result<Video> where R: io::Read, RS: ReadSeek {
let mut params: Vec<(&str, String)> = Vec::with_capacity(8 + self._additional_params.len());
if self._part.len() == 0 {
self._part = self._request.to_parts();
Expand Down Expand Up @@ -8596,7 +8596,7 @@ impl<'a, C, NC, A> VideoInsertMethodBuilder<'a, C, NC, A> where NC: hyper::net::
/// * *multipart*: yes
/// * *valid mime types*: 'application/octet-stream' and 'video/*'
pub fn upload<R>(mut self, stream: R, size: u64, mime_type: mime::Mime) -> Result<Video>
where R: Read {
where R: io::Read {
self.doit(Some((stream, size, mime_type)), None::<(fs::File, u64, mime::Mime)>, )
}
/// Upload media in a resumeable fashion.
Expand Down Expand Up @@ -12292,7 +12292,7 @@ impl<'a, C, NC, A> WatermarkSetMethodBuilder<'a, C, NC, A> where NC: hyper::net:


/// Perform the operation you have build so far.
fn doit<R, RS>(mut self, stream: Option<(R, u64, mime::Mime)>, resumeable_stream: Option<(RS, u64, mime::Mime)>) -> Result<()> where R: Read, RS: ReadSeek {
fn doit<R, RS>(mut self, stream: Option<(R, u64, mime::Mime)>, resumeable_stream: Option<(RS, u64, mime::Mime)>) -> Result<()> where R: io::Read, RS: ReadSeek {
let mut params: Vec<(&str, String)> = Vec::with_capacity(4 + self._additional_params.len());
params.push(("channelId", self._channel_id.to_string()));
if self._on_behalf_of_content_owner.is_some() {
Expand Down Expand Up @@ -12333,7 +12333,7 @@ impl<'a, C, NC, A> WatermarkSetMethodBuilder<'a, C, NC, A> where NC: hyper::net:
/// * *multipart*: yes
/// * *valid mime types*: 'application/octet-stream', 'image/jpeg' and 'image/png'
pub fn upload<R>(mut self, stream: R, size: u64, mime_type: mime::Mime) -> Result<()>
where R: Read {
where R: io::Read {
self.doit(Some((stream, size, mime_type)), None::<(fs::File, u64, mime::Mime)>, )
}
/// Upload media in a resumeable fashion.
Expand Down
4 changes: 2 additions & 2 deletions src/mako/lib.rs.mako
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
if schemas:
nested_schemas = list(iter_nested_types(schemas))
c = new_context(resources)
hub_type = hub_type(util.canonical_name())
hub_type = hub_type(schemas, util.canonical_name())
ht_params = hub_type_params_s()
%>\
<%block filter="rust_comment">\
Expand All @@ -37,7 +37,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;
use std::fs;
use std::collections::BTreeMap;

Expand Down
4 changes: 2 additions & 2 deletions src/mako/lib/mbuild.mako
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
###############################################################################################
<%def name="new(resource, method, c)">\
<%
hub_type_name = hub_type(util.canonical_name())
hub_type_name = hub_type(schemas,util.canonical_name())
m = c.fqan_map[to_fqan(c.rtc_map[resource], resource, method)]
# an identifier for a property. We prefix them to prevent clashes with the setters
mb_tparams = mb_type_params_s(m)
Expand Down Expand Up @@ -212,7 +212,7 @@ ${self._setter_fn(resource, method, m, p, part_prop, ThisType, c)}\
###############################################################################################
<%def name="usage(resource, method, m, params, request_value, parts)">\
<%
hub_type_name = hub_type(util.canonical_name())
hub_type_name = hub_type(schemas, util.canonical_name())
required_props, optional_props, part_prop = organize_params(params, request_value)
is_string_value = lambda v: v.endswith('"')
Expand Down
2 changes: 1 addition & 1 deletion src/mako/lib/rbuild.mako
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
###############################################################################################
<%def name="new(resource, c)">\
<%
hub_type_name = hub_type(util.canonical_name())
hub_type_name = hub_type(schemas, util.canonical_name())
rb_params = rb_type_params_s(resource, c)
ThisType = rb_type(resource) + rb_params
%>\
Expand Down
24 changes: 17 additions & 7 deletions src/mako/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
'description': """Upload media all at once.
If the upload fails for whichever reason, all progress is lost.""",
'default': 'fs::File',
'where': 'Read',
'where': 'io::Read',
'suffix': '',
'example_value': 'fs::File::open("file.ext").unwrap(), 148, "application/octet-stream".parse().unwrap()'
},
Expand Down Expand Up @@ -238,6 +238,7 @@ def extract_parts(desc):
# Return transformed string that could make a good type name
def canonical_type_name(s):
# can't use s.capitalize() as it will lower-case the remainder of the string
s = s.replace(' ', '')
return s[:1].upper() + s[1:]

def nested_type_name(sn, pn):
Expand All @@ -246,7 +247,7 @@ def nested_type_name(sn, pn):
# Make properties which are reserved keywords usable
def mangle_ident(n):
n = '_'.join(singular(w) for w in camel_to_under(n).split('.'))
if n == 'type':
if n in ('type', 'where', 'override', 'move'):
return n + '_'
return n

Expand Down Expand Up @@ -635,8 +636,11 @@ def mb_additional_type_params(m):
def mb_type(r, m):
return "%s%sMethodBuilder" % (singular(canonical_type_name(r)), dot_sep_to_canonical_type_name(m))

def hub_type(canonicalName):
return canonical_type_name(canonicalName)
def hub_type(schemas, canonicalName):
name = canonical_type_name(canonicalName)
if schemas and name in schemas:
name += 'Hub'
return name

# return e + d[n] + e + ' ' or ''
def get_word(d, n, e = ''):
Expand All @@ -661,16 +665,22 @@ def dot_sep_to_canonical_type_name(n):
def scope_url_to_variant(name, url, fully_qualified=True):
FULL = 'Full'
fqvn = lambda n: fully_qualified and 'Scope::%s' % n or n
repl = lambda n: n.replace('-', '.').replace('_', '.')

if url.endswith('/'):
url = name[:-1]
base = os.path.basename(url)

assert base, name
# special case, which works for now ... https://mail.gmail.com
# NO can do ! Must play safe here ...
if not base.startswith(name):
return fqvn(FULL)
return fqvn(dot_sep_to_canonical_type_name(repl(base)))
base = base[len(name):]
base = base.strip('-').strip('.')
if len(base) == 0:
return fqvn(FULL)
base = base.replace('-', '.')
return fqvn(dot_sep_to_canonical_type_name(base))
return fqvn(dot_sep_to_canonical_type_name(repl(base)))


# given a rust type-name (no optional, as from to_rust_type), you will get a suitable random default value
Expand Down

0 comments on commit d99ba9c

Please sign in to comment.