Skip to content

Commit

Permalink
Update JS and TS routes to remove null parameters (#186)
Browse files Browse the repository at this point in the history
* Update JS and TS routes to remove null parameters

- Fix for ([JS #41](dropbox/dropbox-sdk-js#41))
- Update route to not have `arg` when there is no parameter
- Update comments to also not contain `arg` when no parameter
- Update tests for both clients
  • Loading branch information
rogebrd committed Oct 5, 2020
1 parent 5ba8974 commit 07c4d19
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -38,7 +38,7 @@

dist = setup(
name='stone',
version='2.2.2',
version='3.0.0',
install_requires=install_reqs,
setup_requires=setup_requires,
tests_require=test_reqs,
Expand Down
30 changes: 22 additions & 8 deletions stone/backends/js_client.py
Expand Up @@ -22,6 +22,7 @@
fmt_type,
fmt_url,
)
from stone.ir import Void

_cmdline_parser = argparse.ArgumentParser(prog='js-client-backend')
_cmdline_parser.add_argument(
Expand Down Expand Up @@ -80,26 +81,39 @@ def _generate_route(self, route_schema, namespace, route):
if route.deprecated:
self.emit(' * @deprecated')

self.emit(' * @arg {%s} arg - The request parameters.' %
fmt_type(route.arg_data_type))
if route.arg_data_type.__class__ != Void:
self.emit(' * @arg {%s} arg - The request parameters.' %
fmt_type(route.arg_data_type))
self.emit(' * @returns {Promise.<%s, %s>}' %
(fmt_type(route.result_data_type),
fmt_error_type(route.error_data_type)))
self.emit(' */')

self.emit('routes.%s = function (arg) {' % (function_name))
if route.arg_data_type.__class__ != Void:
self.emit('routes.%s = function (arg) {' % (function_name))
else:
self.emit('routes.%s = function () {' % (function_name))
with self.indent(dent=2):
url = fmt_url(namespace.name, route.name, route.version)
if route_schema.fields:
additional_args = []
for field in route_schema.fields:
additional_args.append(fmt_obj(route.attrs[field.name]))
self.emit(
"return this.request('{}', arg, {});".format(
url, ', '.join(additional_args)))
if route.arg_data_type.__class__ != Void:
self.emit(
"return this.request('{}', arg, {});".format(
url, ', '.join(additional_args)))
else:
self.emit(
"return this.request('{}', null, {});".format(
url, ', '.join(additional_args)))
else:
self.emit(
'return this.request("%s", arg);' % url)
if route.arg_data_type.__class__ != Void:
self.emit(
'return this.request("%s", arg);' % url)
else:
self.emit(
'return this.request("%s", null);' % url)
self.emit('};')

def _docf(self, tag, val):
Expand Down
13 changes: 10 additions & 3 deletions stone/backends/tsd_client.py
Expand Up @@ -23,6 +23,7 @@
fmt_tag,
fmt_type,
)
from stone.ir import Void


_cmdline_parser = argparse.ArgumentParser(prog='tsd-client-backend')
Expand Down Expand Up @@ -121,11 +122,17 @@ def _generate_route(self, namespace, route):
if route.deprecated:
self.emit(' * @deprecated')

self.emit(' * @param arg The request parameters.')
if route.arg_data_type.__class__ != Void:
self.emit(' * @param arg The request parameters.')
self.emit(' */')

self.emit('public %s(arg: %s): Promise<%s>;' %
(function_name, fmt_type(route.arg_data_type), fmt_type(route.result_data_type)))
if route.arg_data_type.__class__ != Void:
self.emit('public %s(arg: %s): Promise<%s>;' %
(function_name, fmt_type(route.arg_data_type),
fmt_type(route.result_data_type)))
else:
self.emit('public %s(): Promise<%s>;' %
(function_name, fmt_type(route.result_data_type)))

def _docf(self, tag, val):
"""
Expand Down
23 changes: 17 additions & 6 deletions test/test_js_client.py
Expand Up @@ -23,9 +23,12 @@ def _get_api(self):
route1.set_attributes(None, ':route:`get_metadata`', Void(), Void(), Void(), {})
route2 = ApiRoute('get_metadata', 2, None)
route2.set_attributes(None, ':route:`get_metadata:2`', Void(), Int32(), Void(), {})
route3 = ApiRoute('get_metadata', 3, None)
route3.set_attributes(None, ':route:`get_metadata:3`', Int32(), Int32(), Void(), {})
ns = ApiNamespace('files')
ns.add_route(route1)
ns.add_route(route2)
ns.add_route(route3)
api.namespaces[ns.name] = ns
return api, ns

Expand All @@ -46,21 +49,29 @@ def test_route_versions(self):
/**
* get_metadata
* @function DropboxBase#filesGetMetadata
* @arg {void} arg - The request parameters.
* @returns {Promise.<void, Error.<void>>}
*/
routes.filesGetMetadata = function (arg) {
return this.request("files/get_metadata", arg);
routes.filesGetMetadata = function () {
return this.request("files/get_metadata", null);
};
/**
* get_metadata_v2
* @function DropboxBase#filesGetMetadataV2
* @arg {void} arg - The request parameters.
* @returns {Promise.<number, Error.<void>>}
*/
routes.filesGetMetadataV2 = function (arg) {
return this.request("files/get_metadata_v2", arg);
routes.filesGetMetadataV2 = function () {
return this.request("files/get_metadata_v2", null);
};
/**
* get_metadata_v3
* @function DropboxBase#filesGetMetadataV3
* @arg {number} arg - The request parameters.
* @returns {Promise.<number, Error.<void>>}
*/
routes.filesGetMetadataV3 = function (arg) {
return this.request("files/get_metadata_v3", arg);
};
export { routes };
Expand Down
15 changes: 12 additions & 3 deletions test/test_tsd_client.py
Expand Up @@ -22,9 +22,12 @@ def _get_api(self):
route1.set_attributes(None, ':route:`get_metadata`', Void(), Void(), Void(), {})
route2 = ApiRoute('get_metadata', 2, None)
route2.set_attributes(None, ':route:`get_metadata:2`', Void(), Int32(), Void(), {})
route3 = ApiRoute('get_metadata', 3, None)
route3.set_attributes(None, ':route:`get_metadata:3`', Int32(), Int32(), Void(), {})
ns = ApiNamespace('files')
ns.add_route(route1)
ns.add_route(route2)
ns.add_route(route3)
api.namespaces[ns.name] = ns
return api, ns

Expand All @@ -44,17 +47,23 @@ def test__generate_types_single_ns(self):
* getMetadata()
*
* When an error occurs, the route rejects the promise with type Error<void>.
* @param arg The request parameters.
*/
public filesGetMetadata(arg: void): Promise<void>;
public filesGetMetadata(): Promise<void>;
/**
* getMetadataV2()
*
* When an error occurs, the route rejects the promise with type Error<void>.
*/
public filesGetMetadataV2(): Promise<number>;
/**
* getMetadataV3()
*
* When an error occurs, the route rejects the promise with type Error<void>.
* @param arg The request parameters.
*/
public filesGetMetadataV2(arg: void): Promise<number>;
public filesGetMetadataV3(arg: number): Promise<number>;
''')
self.assertEqual(result, expected)

Expand Down

0 comments on commit 07c4d19

Please sign in to comment.