Skip to content

Commit 60d953a

Browse files
committed
feat(methods): intermed. support for 'methods'
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.
1 parent 3543707 commit 60d953a

7 files changed

Lines changed: 59 additions & 30 deletions

File tree

etc/api/api-list.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ api:
7474
fitness:
7575
- v1
7676
freebase:
77-
- v1sandbox
77+
- v1
7878
fusiontables:
7979
- v2
8080
games:

src/mako/README.md.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<%
22
from util import (markdown_comment, new_context)
3-
c = new_context(schemas, resources)
3+
c = new_context(schemas, resources, context.get('methods'))
44
%>\
55
<%namespace name="lib" file="lib/lib.mako"/>\
66
<%namespace name="util" file="lib/util.mako"/>\

src/mako/lib.rs.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
rust_module_doc_comment, rb_type, hub_type, mangle_ident, hub_type_params_s,
99
hub_type_bounds, rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS)
1010
11-
c = new_context(schemas, resources)
11+
c = new_context(schemas, resources, context.get('methods'))
1212
hub_type = hub_type(c.schemas, util.canonical_name())
1313
ht_params = hub_type_params_s()
1414
%>\

src/mako/lib/lib.mako

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ Handle the following *Resources* with ease from the central ${link('hub', hub_ur
5050
<%
5151
md_methods = list()
5252
for method in sorted(c.rta_map[r]):
53-
md_methods.append(link('*%s*' % split_camelcase_s(method),
54-
'struct.%s.html' % mb_type(r, method)))
53+
md_methods.append(link('*%s*' % split_camelcase_s(method),
54+
'struct.%s.html' % mb_type(r, method)))
5555
md_resource = split_camelcase_s(r)
5656
sn = singular(canonical_type_name(r))
5757
@@ -97,7 +97,11 @@ Or specifically ...
9797
```ignore
9898
% for an, a in c.sta_map[fr.id].iteritems():
9999
<% category, resource, activity = activity_split(an) %>\
100+
% if resource:
100101
let r = hub.${mangle_ident(resource)}().${mangle_ident(activity)}(...).${api.terms.action}()
102+
% else:
103+
let r = hub.${mangle_ident(activity)}(...).${api.terms.action}()
104+
% endif
101105
% endfor
102106
```
103107
% endif
@@ -207,13 +211,13 @@ let mut hub = ${hub_type}::new(hyper::Client::new(), auth);\
207211
fqan = None
208212
last_param_count = None
209213
for fqan in c.sta_map[fr.id]:
210-
_, aresource, amethod = activity_split(fqan)
214+
category, aresource, amethod = activity_split(fqan)
211215
am = c.fqan_map[fqan]
212216
build_all_params(c, am)
213217
aparams, arequest_value = build_all_params(c, am)
214218
215219
if last_param_count is None or len(aparams) > last_param_count:
216-
m, resource, method, params, request_value = am, aresource, amethod, aparams, arequest_value
220+
m, resource, method, params, request_value = am, aresource or category, amethod, aparams, arequest_value
217221
last_param_count = len(aparams)
218222
# end for each fn to test
219223
part_prop, parts = parts_from_params(params)

src/mako/lib/rbuild.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl${rb_params} ${ThisType} {
9494
% if prop_key == 'scopes' and (not auth or not auth.oauth2):
9595
<% continue %>\
9696
% endif
97-
${custom_name}: Default::default(),
97+
${custom_name}: Default::default(),
9898
% endfor
9999
}
100100
}

src/mako/lib/schema.mako

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ The list links the activity name, along with information about where it is used
109109
110110
% for a, iot in c.sta_map[s.id].iteritems():
111111
<%
112-
_, name, method = activity_split(a)
113-
112+
category, name, method = activity_split(a)
113+
name = name or category
114114
struct_url = 'struct.' + mb_type(name, method) + '.html'
115115
method_name = split_camelcase_s(method) + ' ' + split_camelcase_s(name)
116116
value_type = '|'.join(iot) or 'none'

src/mako/lib/util.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ def schema_markers(s, c):
387387
else:
388388
# it should have at least one activity that matches it's type to qualify for the Resource trait
389389
for fqan, iot in activities.iteritems():
390-
if activity_name_to_type_name(activity_split(fqan)[1]).lower() == sid.lower():
390+
_, resource, _ = activity_split(fqan)
391+
if resource and activity_name_to_type_name(resource).lower() == sid.lower():
391392
res.add('cmn::%s' % RESOURCE_MARKER_TRAIT)
392393
if IO_RESPONSE in iot:
393394
res.add(RESPONSE_MARKER_TRAIT)
@@ -408,10 +409,16 @@ def schema_markers(s, c):
408409
# -------------------------
409410
## @name Activity Utilities
410411
# @{
411-
# return (category, name, method)
412+
# return (category, name|None, method)
412413
def activity_split(fqan):
413414
t = fqan.split('.')
414-
return t[0], t[1], '.'.join(t[2:])
415+
mt = t[2:]
416+
if not mt:
417+
# make this the method, with not resource
418+
mt = [t[1]]
419+
t[1] = None
420+
# end
421+
return t[0], t[1], '.'.join(mt)
415422

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

571578
# return a newly build context from the given data
572-
def new_context(schemas, resources):
579+
def new_context(schemas, resources, methods):
573580
# Returns (A, B) where
574581
# A: { SchemaTypeName -> { fqan -> ['request'|'response', ...]}
575582
# B: { fqan -> activity_method_data }
@@ -579,14 +586,18 @@ def build_activity_mappings(activities, res = None, fqan = None):
579586
res = dict()
580587
if fqan is None:
581588
fqan = dict()
582-
for an, a in activities.iteritems():
589+
for a in activities.values():
583590
if 'resources' in a:
584591
build_activity_mappings(a.resources, res, fqan)
585592
if 'methods' not in a:
586593
continue
587594
for mn, m in a.methods.iteritems():
588595
assert m.id not in fqan
589596
fqan[m.id] = m
597+
category, resource, method = activity_split(m.id)
598+
# Put the same method under different names to make access easier
599+
if resource is None:
600+
fqan[to_fqan(category, category, method)] = m
590601
for in_out_type_name in IO_TYPES:
591602
t = m.get(in_out_type_name, None)
592603
if t is None:
@@ -601,12 +612,12 @@ def build_activity_mappings(activities, res = None, fqan = None):
601612
# delete: has no response or request
602613
# getrating: response is a 'SomethingResult', which is still related to activities name
603614
# the latter is used to deduce the resource name
604-
_, an, _ = activity_split(m.id)
605-
tn = activity_name_to_type_name(an)
606-
info = res.setdefault(tn, dict())
607-
if m.id not in info:
608-
info.setdefault(m.id, [])
609-
# end handle other cases
615+
if resource:
616+
tn = activity_name_to_type_name(resource)
617+
info = res.setdefault(tn, dict())
618+
if m.id not in info:
619+
info.setdefault(m.id, [])
620+
# end handle other cases
610621
# end for each method
611622
# end for each activity
612623
return res, fqan
@@ -676,17 +687,31 @@ def recurse_properties(prefix, rs, s, parent_ids):
676687
# end utility
677688

678689
all_schemas = schemas and build_schema_map() or dict()
679-
if not resources:
690+
if not (resources or methods):
680691
return Context(dict(), dict(), dict(), dict(), all_schemas)
681692

682-
sta_map, fqan_map = build_activity_mappings(resources)
683-
rta_map = dict()
684-
rtc_map = dict()
685-
for an in fqan_map:
686-
category, resource, activity = activity_split(an)
687-
rta_map.setdefault(resource, list()).append(activity)
688-
assert rtc_map.setdefault(resource, category) == category
689-
# DEBUG
693+
rta_map, rtc_map, sta_map, fqan_map = dict(), dict(), dict(), dict()
694+
695+
sources = list()
696+
if bool(resources):
697+
sources.append(resources)
698+
if bool(methods):
699+
sources.append({None : type(methods)({'methods' : methods})})
700+
701+
for data_source in sources:
702+
_sta_map, _fqan_map = build_activity_mappings(data_source)
703+
for an in _fqan_map:
704+
category, resource, activity = activity_split(an)
705+
resource = resource or category
706+
rta_map.setdefault(resource, list()).append(activity)
707+
assert rtc_map.setdefault(resource, category) == category
708+
# end for each fqan
709+
# TODO: DEBUG: Remove this when it was run on all APIs
710+
assert len(set(sta_map.keys()) & set(_sta_map.keys())) == 0
711+
assert len(set(fqan_map.keys()) & set(_fqan_map.keys())) == 0, set(fqan_map.keys()) & set(_fqan_map.keys())
712+
sta_map.update(_sta_map)
713+
fqan_map.update(_fqan_map)
714+
# end for each data source
690715
return Context(sta_map, fqan_map, rta_map, rtc_map, all_schemas)
691716

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

0 commit comments

Comments
 (0)