Skip to content

Commit

Permalink
Include route namespaces in reconnection helper keys (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlocke authored Aug 27, 2024
1 parent a51d760 commit 908ddb2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
12 changes: 7 additions & 5 deletions stone/backends/swift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
fmt_var,
fmt_type,
fmt_route_name,
fmt_route_name_namespace,
fmt_func_namespace,
fmt_objc_type,
mapped_list_info,
datatype_has_subtypes,
Expand Down Expand Up @@ -230,7 +232,7 @@ def _generate_routes(self, namespace):
'{}Routes.swift'.format(ns_class))

def _generate_request_boxes(self, api):
background_compatible_routes = self._background_compatible_routes(api)
background_compatible_routes = self._background_compatible_namespace_route_pairs(api)

if len(background_compatible_routes) == 0:
return
Expand All @@ -241,6 +243,8 @@ def _generate_request_boxes(self, api):
template_globals['request_type_signature'] = self._request_type_signature
template_globals['fmt_func'] = fmt_func
template_globals['fmt_route_objc_class'] = self._fmt_route_objc_class
template_globals['fmt_func_namespace'] = fmt_func_namespace
template_globals['fmt_route_name_namespace'] = fmt_route_name_namespace
swift_class_name = '{}RequestBox'.format(self.args.class_name)

if self.args.objc:
Expand Down Expand Up @@ -284,6 +288,8 @@ def _generate_reconnection_helpers(self, api):

template = self._jinja_template("SwiftReconnectionHelpers.jinja")
template.globals['fmt_route_name'] = fmt_route_name
template.globals['fmt_route_name_namespace'] = fmt_route_name_namespace
template.globals['fmt_func_namespace'] = fmt_func_namespace
template.globals['fmt_func'] = fmt_func
template.globals['fmt_class'] = fmt_class
template.globals['class_name'] = class_name
Expand All @@ -297,10 +303,6 @@ def _generate_reconnection_helpers(self, api):
output_from_parsed_template, '{}.swift'.format(class_name)
)

def _background_compatible_routes(self, api):
background_compatible_pairs = self._background_compatible_namespace_route_pairs(api)
return list(map(lambda arg_data: arg_data[1], background_compatible_pairs))

def _background_compatible_namespace_route_pairs(self, api):
namespaces = api.namespaces.values()
background_compatible_routes = []
Expand Down
6 changes: 5 additions & 1 deletion stone/backends/swift_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,17 @@ def fmt_default_value(field):
raise TypeError('Can\'t handle default value type %r' %
type(field.data_type))


def fmt_route_name(route):
if route.version == 1:
return route.name
else:
return '{}_v{}'.format(route.name, route.version)

def fmt_route_name_namespace(route, namespace_name):
return '{}/{}'.format(namespace_name, fmt_route_name(route))

def fmt_func_namespace(name, version, namespace_name):
return '{}_{}'.format(namespace_name, fmt_func(name, version))

def check_route_name_conflict(namespace):
"""
Expand Down
2 changes: 1 addition & 1 deletion stone/backends/swift_rsrc/ObjCRequestBox.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension {{ class_name }} {
{% set namespace = route_args_data[0] %}
{% set route = route_args_data[1] %}
{% set args_data = route_args_data[2] %}
case .{{ fmt_func(route.name, route.version) }}(let swift):
case .{{ fmt_func_namespace(route.name, route.version, namespace.name) }}(let swift):
return {{ fmt_route_objc_class(namespace, route, args_data) }}(swift: swift)
{% endfor %}
{% if include_default_in_switch %}
Expand Down
14 changes: 10 additions & 4 deletions stone/backends/swift_rsrc/SwiftReconnectionHelpers.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

import Foundation

// The case string below must match those created by ReconnectionHelpers+Handwritten.swift using
// the route name and namespace as formatted for the generated `Route` object in SwiftTypes.jinja
// Format: "<namespace>/<route_name>" e.g., "files/upload_session/append_v2" for Files.uploadSessionAppendV2
enum {{ class_name }}: ReconnectionHelpersShared {

static func rebuildRequest(apiRequest: ApiRequest, client: DropboxTransportClientInternal) throws -> {{ return_type }} {
let info = try persistedRequestInfo(from: apiRequest)

switch info.routeName {
{% for namespace, route in background_compatible_namespace_route_pairs %}
case "{{ fmt_route_name(route) }}":
return .{{ fmt_func(route.name, route.version) }}(
switch info.namespaceRouteName {
{% for route_args_data in background_compatible_namespace_route_pairs %}
{% set namespace = route_args_data[0] %}
{% set route = route_args_data[1] %}
{% set args_data = route_args_data[2] %}
case "{{ fmt_route_name_namespace(route, namespace.name) }}":
return .{{ fmt_func_namespace(route.name, route.version, namespace.name) }}(
rebuildRequest(
apiRequest: apiRequest,
info: info,
Expand Down
14 changes: 9 additions & 5 deletions stone/backends/swift_rsrc/SwiftRequestBox.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import Foundation

/// Allows for heterogenous collections of typed requests
public enum {{ class_name }}: CustomStringConvertible {
{% for route in background_compatible_routes %}
case {{ fmt_func(route.name, route.version) }}({{ request_type_signature(route) }})
{% for route_namespace_pair in background_compatible_routes %}
{% set namespace = route_namespace_pair[0] %}
{% set route = route_namespace_pair[1] %}
case {{ fmt_func_namespace(route.name, route.version, namespace.name) }}({{ request_type_signature(route) }})
{% endfor %}

public var description: String {
switch self {
{% for route in background_compatible_routes %}
case .{{ fmt_func(route.name, route.version) }}(_):
return "{{ fmt_func(route.name, route.version) }}"
{% for route_namespace_pair in background_compatible_routes %}
{% set namespace = route_namespace_pair[0] %}
{% set route = route_namespace_pair[1] %}
case .{{ fmt_func_namespace(route.name, route.version, namespace.name) }}:
return "{{ fmt_route_name_namespace(route, namespace.name) }}"
{% endfor %}
}
}
Expand Down

0 comments on commit 908ddb2

Please sign in to comment.