Skip to content

Commit

Permalink
Update generator to support route version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiming Yuan committed Jun 29, 2018
1 parent 91f3f9f commit d0b609b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
62 changes: 53 additions & 9 deletions generator/csharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import defaultdict, namedtuple
from contextlib import contextmanager

from stone.data_type import (
from stone.ir.data_types import (
Float32,
Float64,
Int32,
Expand All @@ -28,7 +28,7 @@
is_union_type,
is_void_type,
)
from stone.generator import CodeGenerator
from stone.backend import CodeBackend


def memo_one(fn):
Expand All @@ -49,7 +49,7 @@ def wrapper(self, arg):
ConstructorArg = namedtuple('ConstructorArg', ('type', 'name', 'arg', 'doc'))


class _CSharpGenerator(CodeGenerator):
class _CSharpGenerator(CodeBackend):
_CAMEL_CASE_RE = re.compile('((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
_CSHARP_KEYWORDS = frozenset({
'abstract', 'add', 'alias', 'as', 'ascending', 'async', 'await',
Expand Down Expand Up @@ -548,9 +548,17 @@ def tag_handler(tag, value):
return '<a href="{0}">{1}</a>'.format(uri, text)
elif tag == 'route':
if '.' in value:
ns_name, route_name = map(self._public_name, value.split('.'))
ns_name, route_name = value.split('.')
ns_name = self._public_name(ns_name)
else:
ns_name, route_name = ns, self._public_name(value)
ns_name, route_name = ns, value

if ':' in route_name:
route_name, version = route_name.split(':')
else:
route_name, version = route_name, 1

route_name = self._public_route_name(route_name, version)
auth_type = self._route_auth_map[(ns_name, route_name)]

return ('<see cref="{0}.{1}.Routes.{1}{2}Routes.{3}Async" />'.format(
Expand Down Expand Up @@ -830,6 +838,42 @@ def _arg_name(self, name, is_doc=False):
if arg_name in _CSharpGenerator._CSHARP_KEYWORDS and not is_doc:
return '@' + arg_name
return arg_name

def _route_url(self, ns_name, route_name, route_version):
"""
Get url path for given route.
Args:
ns_name (Union[str, unicode]): The name of the namespace.
route_name (Union[str, unicode]): The name of the route.
route_version (int): The version of the route.
"""

route_url = '"/{}/{}"'.format(ns_name, route_name)

if route_version != 1:
route_url = "{}_v{}".format(route_url, version)

return route_url

def _public_route_name(self, name, version):
"""
Creates an initial capitalize CamelCase representation of a route name with optional version suffix.
This performs the following transformations.
foo_bar -> FooBar
foo_bar:2 -> FooBarV2
Args:
route (Union[str, unicode]): Name of the route.
version (int): Version number
"""
route_name = self._public_name(name)

if version != 1:
route_name = '{}V{}'.format(route_name, version)

return route_name

@memo_one
def _segment_name(self, name):
Expand Down Expand Up @@ -859,7 +903,7 @@ def _public_name(self, name):
name (Union[str, unicode]): The name to transform
"""
return ''.join(x.capitalize() for x in self._segment_name(name))

@memo_one
def _name_words(self, name):
"""
Expand Down Expand Up @@ -959,7 +1003,7 @@ def _generate_route_auth_map(self, api):

for ns in api.namespaces.itervalues():
for route in ns.routes:
key = (self._public_name(ns.name), self._public_name(route.name))
key = (self._public_name(ns.name), self._public_route_name(route.name, route.version))
d[key] = self._public_name(self._get_auth_type(route))

self._route_auth_map = d
Expand Down Expand Up @@ -1835,7 +1879,7 @@ def _generate_route(self, ns, route):
ns (stone.api.ApiNamespace): The namespace of the route.
route (stone.api.ApiRoute): The route in question.
"""
public_name = self._public_name(route.name)
public_name = self._public_route_name(route.name, route.version)
async_name = '{0}Async'.format(public_name)
route_host = route.attrs.get('host', 'api')
route_style = route.attrs.get('style', 'rpc')
Expand Down Expand Up @@ -1908,7 +1952,7 @@ def _generate_route(self, ns, route):
args.append('body')
args.extend([
'"{0}"'.format(route_host),
'"/{0}/{1}"'.format(ns.name, route.name),
self._route_url(ns.name, route.name, route.version),
'"{0}"'.format(auth_type),
self._get_encoder(route.arg_data_type),
self._get_decoder(route.result_data_type),
Expand Down
2 changes: 1 addition & 1 deletion stone
Submodule stone updated 84 files
+6 −0 .travis.yml
+50 −24 README.rst
+78 −69 docs/backend_ref.rst
+127 −27 docs/builtin_backends.rst
+69 −69 docs/evolve_spec.rst
+26 −9 docs/json_serializer.rst
+294 −16 docs/lang_ref.rst
+3 −3 docs/managing_specs.rst
+ docs/overview.png
+0 −0 example/backend/__init__.py
+0 −0 example/backend/ex1/__init__.py
+3 −2 example/backend/ex1/ex1.stoneg.py
+0 −0 example/backend/ex2/__init__.py
+3 −2 example/backend/ex2/ex2.stoneg.py
+0 −0 example/backend/ex3/__init__.py
+5 −4 example/backend/ex3/ex3.stoneg.py
+0 −0 example/backend/unstone/__init__.py
+9 −8 example/backend/unstone/unstone.stoneg.py
+1 −1 mypy-run.sh
+2 −2 mypy.ini
+8 −5 setup.py
+13 −13 stone/backend.py
+0 −0 stone/backends/__init__.py
+0 −0 stone/backends/helpers.py
+6 −6 stone/backends/js_client.py
+2 −2 stone/backends/js_helpers.py
+8 −6 stone/backends/js_types.py
+5 −5 stone/backends/obj_c.py
+9 −10 stone/backends/obj_c_client.py
+1 −1 stone/backends/obj_c_helpers.py
+0 −0 stone/backends/obj_c_rsrc/DBSerializableProtocol.h
+0 −0 stone/backends/obj_c_rsrc/DBStoneBase.h
+0 −0 stone/backends/obj_c_rsrc/DBStoneBase.m
+0 −0 stone/backends/obj_c_rsrc/DBStoneSerializers.h
+242 −0 stone/backends/obj_c_rsrc/DBStoneSerializers.m
+11 −7 stone/backends/obj_c_rsrc/DBStoneValidators.h
+145 −0 stone/backends/obj_c_rsrc/DBStoneValidators.m
+77 −40 stone/backends/obj_c_types.py
+62 −41 stone/backends/python_client.py
+48 −33 stone/backends/python_helpers.py
+1 −0 stone/backends/python_rsrc/__init__.py
+37 −6 stone/backends/python_rsrc/stone_base.py
+485 −414 stone/backends/python_rsrc/stone_serializers.py
+83 −2 stone/backends/python_rsrc/stone_validators.py
+12 −5 stone/backends/python_type_mapping.py
+62 −19 stone/backends/python_type_stubs.py
+182 −106 stone/backends/python_types.py
+10 −5 stone/backends/swift.py
+14 −10 stone/backends/swift_client.py
+21 −3 stone/backends/swift_helpers.py
+5 −2 stone/backends/swift_rsrc/StoneBase.swift
+0 −0 stone/backends/swift_rsrc/StoneSerializers.swift
+0 −0 stone/backends/swift_rsrc/StoneValidators.swift
+11 −7 stone/backends/swift_types.py
+7 −6 stone/backends/tsd_client.py
+20 −2 stone/backends/tsd_helpers.py
+161 −55 stone/backends/tsd_types.py
+71 −48 stone/cli.py
+36 −36 stone/compiler.py
+0 −0 stone/frontend/__init__.py
+413 −0 stone/frontend/ast.py
+0 −0 stone/frontend/exception.py
+55 −0 stone/frontend/frontend.py
+725 −161 stone/frontend/ir_generator.py
+139 −59 stone/frontend/lexer.py
+195 −391 stone/frontend/parser.py
+2 −0 stone/ir/__init__.py
+78 −23 stone/ir/api.py
+200 −71 stone/ir/data_types.py
+0 −186 stone/target/obj_c_rsrc/DBStoneSerializers.m
+0 −112 stone/target/obj_c_rsrc/DBStoneValidators.m
+0 −1 stone/target/python_rsrc/__init__.py
+28 −0 test/backend_test_util.py
+1 −0 test/resources/typescript.template
+16 −18 test/test_backend.py
+88 −0 test/test_python_client.py
+1,874 −14 test/test_python_gen.py
+113 −45 test/test_python_type_stubs.py
+93 −0 test/test_python_types.py
+1,718 −532 test/test_stone.py
+111 −102 test/test_stone_internal.py
+375 −0 test/test_stone_route_whitelist.py
+527 −0 test/test_tsd_types.py
+4 −4 tox.ini

0 comments on commit d0b609b

Please sign in to comment.