Skip to content

Commit

Permalink
feat(methods): intermed. support for 'methods'
Browse files Browse the repository at this point in the history
These 'methods' have no resources, and need slightly special handling.
This version at least makes the generator work, even though
it produces duplicates.

However, as it is so ugly, I'd rather consider to change it
substantially ... this feature should just come naturally.
  • Loading branch information
Byron committed Mar 17, 2015
1 parent 3543707 commit 60d953a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 30 deletions.
2 changes: 1 addition & 1 deletion etc/api/api-list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ api:
fitness:
- v1
freebase:
- v1sandbox
- v1
fusiontables:
- v2
games:
Expand Down
2 changes: 1 addition & 1 deletion src/mako/README.md.mako
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%
from util import (markdown_comment, new_context)
c = new_context(schemas, resources)
c = new_context(schemas, resources, context.get('methods'))
%>\
<%namespace name="lib" file="lib/lib.mako"/>\
<%namespace name="util" file="lib/util.mako"/>\
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 @@ -8,7 +8,7 @@
rust_module_doc_comment, rb_type, hub_type, mangle_ident, hub_type_params_s,
hub_type_bounds, rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS)
c = new_context(schemas, resources)
c = new_context(schemas, resources, context.get('methods'))
hub_type = hub_type(c.schemas, util.canonical_name())
ht_params = hub_type_params_s()
%>\
Expand Down
12 changes: 8 additions & 4 deletions src/mako/lib/lib.mako
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ Handle the following *Resources* with ease from the central ${link('hub', hub_ur
<%
md_methods = list()
for method in sorted(c.rta_map[r]):
md_methods.append(link('*%s*' % split_camelcase_s(method),
'struct.%s.html' % mb_type(r, method)))
md_methods.append(link('*%s*' % split_camelcase_s(method),
'struct.%s.html' % mb_type(r, method)))
md_resource = split_camelcase_s(r)
sn = singular(canonical_type_name(r))
Expand Down Expand Up @@ -97,7 +97,11 @@ Or specifically ...
```ignore
% for an, a in c.sta_map[fr.id].iteritems():
<% category, resource, activity = activity_split(an) %>\
% if resource:
let r = hub.${mangle_ident(resource)}().${mangle_ident(activity)}(...).${api.terms.action}()
% else:
let r = hub.${mangle_ident(activity)}(...).${api.terms.action}()
% endif
% endfor
```
% endif
Expand Down Expand Up @@ -207,13 +211,13 @@ let mut hub = ${hub_type}::new(hyper::Client::new(), auth);\
fqan = None
last_param_count = None
for fqan in c.sta_map[fr.id]:
_, aresource, amethod = activity_split(fqan)
category, aresource, amethod = activity_split(fqan)
am = c.fqan_map[fqan]
build_all_params(c, am)
aparams, arequest_value = build_all_params(c, am)
if last_param_count is None or len(aparams) > last_param_count:
m, resource, method, params, request_value = am, aresource, amethod, aparams, arequest_value
m, resource, method, params, request_value = am, aresource or category, amethod, aparams, arequest_value
last_param_count = len(aparams)
# end for each fn to test
part_prop, parts = parts_from_params(params)
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 @@ -94,7 +94,7 @@ impl${rb_params} ${ThisType} {
% if prop_key == 'scopes' and (not auth or not auth.oauth2):
<% continue %>\
% endif
${custom_name}: Default::default(),
${custom_name}: Default::default(),
% endfor
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mako/lib/schema.mako
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ The list links the activity name, along with information about where it is used
% for a, iot in c.sta_map[s.id].iteritems():
<%
_, name, method = activity_split(a)
category, name, method = activity_split(a)
name = name or category
struct_url = 'struct.' + mb_type(name, method) + '.html'
method_name = split_camelcase_s(method) + ' ' + split_camelcase_s(name)
value_type = '|'.join(iot) or 'none'
Expand Down
65 changes: 45 additions & 20 deletions src/mako/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ def schema_markers(s, c):
else:
# it should have at least one activity that matches it's type to qualify for the Resource trait
for fqan, iot in activities.iteritems():
if activity_name_to_type_name(activity_split(fqan)[1]).lower() == sid.lower():
_, resource, _ = activity_split(fqan)
if resource and activity_name_to_type_name(resource).lower() == sid.lower():
res.add('cmn::%s' % RESOURCE_MARKER_TRAIT)
if IO_RESPONSE in iot:
res.add(RESPONSE_MARKER_TRAIT)
Expand All @@ -408,10 +409,16 @@ def schema_markers(s, c):
# -------------------------
## @name Activity Utilities
# @{
# return (category, name, method)
# return (category, name|None, method)
def activity_split(fqan):
t = fqan.split('.')
return t[0], t[1], '.'.join(t[2:])
mt = t[2:]
if not mt:
# make this the method, with not resource
mt = [t[1]]
t[1] = None
# end
return t[0], t[1], '.'.join(mt)

# Shorthand to get a type from parameters of activities
def activity_rust_type(schemas, p, allow_optionals=True):
Expand Down Expand Up @@ -569,7 +576,7 @@ def build_all_params(c, m):
Context = collections.namedtuple('Context', ['sta_map', 'fqan_map', 'rta_map', 'rtc_map', 'schemas'])

# return a newly build context from the given data
def new_context(schemas, resources):
def new_context(schemas, resources, methods):
# Returns (A, B) where
# A: { SchemaTypeName -> { fqan -> ['request'|'response', ...]}
# B: { fqan -> activity_method_data }
Expand All @@ -579,14 +586,18 @@ def build_activity_mappings(activities, res = None, fqan = None):
res = dict()
if fqan is None:
fqan = dict()
for an, a in activities.iteritems():
for a in activities.values():
if 'resources' in a:
build_activity_mappings(a.resources, res, fqan)
if 'methods' not in a:
continue
for mn, m in a.methods.iteritems():
assert m.id not in fqan
fqan[m.id] = m
category, resource, method = activity_split(m.id)
# Put the same method under different names to make access easier
if resource is None:
fqan[to_fqan(category, category, method)] = m
for in_out_type_name in IO_TYPES:
t = m.get(in_out_type_name, None)
if t is None:
Expand All @@ -601,12 +612,12 @@ def build_activity_mappings(activities, res = None, fqan = None):
# delete: has no response or request
# getrating: response is a 'SomethingResult', which is still related to activities name
# the latter is used to deduce the resource name
_, an, _ = activity_split(m.id)
tn = activity_name_to_type_name(an)
info = res.setdefault(tn, dict())
if m.id not in info:
info.setdefault(m.id, [])
# end handle other cases
if resource:
tn = activity_name_to_type_name(resource)
info = res.setdefault(tn, dict())
if m.id not in info:
info.setdefault(m.id, [])
# end handle other cases
# end for each method
# end for each activity
return res, fqan
Expand Down Expand Up @@ -676,17 +687,31 @@ def recurse_properties(prefix, rs, s, parent_ids):
# end utility

all_schemas = schemas and build_schema_map() or dict()
if not resources:
if not (resources or methods):
return Context(dict(), dict(), dict(), dict(), all_schemas)

sta_map, fqan_map = build_activity_mappings(resources)
rta_map = dict()
rtc_map = dict()
for an in fqan_map:
category, resource, activity = activity_split(an)
rta_map.setdefault(resource, list()).append(activity)
assert rtc_map.setdefault(resource, category) == category
# DEBUG
rta_map, rtc_map, sta_map, fqan_map = dict(), dict(), dict(), dict()

sources = list()
if bool(resources):
sources.append(resources)
if bool(methods):
sources.append({None : type(methods)({'methods' : methods})})

for data_source in sources:
_sta_map, _fqan_map = build_activity_mappings(data_source)
for an in _fqan_map:
category, resource, activity = activity_split(an)
resource = resource or category
rta_map.setdefault(resource, list()).append(activity)
assert rtc_map.setdefault(resource, category) == category
# end for each fqan
# TODO: DEBUG: Remove this when it was run on all APIs
assert len(set(sta_map.keys()) & set(_sta_map.keys())) == 0
assert len(set(fqan_map.keys()) & set(_fqan_map.keys())) == 0, set(fqan_map.keys()) & set(_fqan_map.keys())
sta_map.update(_sta_map)
fqan_map.update(_fqan_map)
# end for each data source
return Context(sta_map, fqan_map, rta_map, rtc_map, all_schemas)

# Expects v to be 'v\d+', throws otherwise
Expand Down

0 comments on commit 60d953a

Please sign in to comment.