From f637d9e16047eae2b2618974ed2cbb3745907f65 Mon Sep 17 00:00:00 2001 From: Julian Locke Date: Mon, 21 Oct 2024 16:11:43 -0400 Subject: [PATCH] Enforce wrapPreservingSubtypes usage when needed --- stone/backends/swift_helpers.py | 3 --- stone/backends/swift_rsrc/ObjcTypes.jinja | 19 ++----------------- stone/backends/swift_types.py | 22 ++++++++++++++-------- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/stone/backends/swift_helpers.py b/stone/backends/swift_helpers.py index a750bb1a..ed6f7d1f 100644 --- a/stone/backends/swift_helpers.py +++ b/stone/backends/swift_helpers.py @@ -300,8 +300,5 @@ def datatype_subtype_value_types(data_type): ret.append((subtype[1])) return ret -def field_datatype_has_subtypes(field) -> bool: - return datatype_has_subtypes(field.data_type) - def datatype_has_subtypes(data_type) -> bool: return len(objc_datatype_value_type_tuples(data_type)) > 0 diff --git a/stone/backends/swift_rsrc/ObjcTypes.jinja b/stone/backends/swift_rsrc/ObjcTypes.jinja index 5d077d04..6a729d92 100644 --- a/stone/backends/swift_rsrc/ObjcTypes.jinja +++ b/stone/backends/swift_rsrc/ObjcTypes.jinja @@ -22,23 +22,7 @@ public class DBX{{ namespace_class_name }}{{ data_type_class_name }}: {{ 'NSObje {% for field in data_type.fields %} {{ struct_field_doc(field, ' ') }} @objc - {% if field_datatype_has_subtypes(field) %} - public var {{ fmt_var(field.name) }}: {{ fmt_objc_type(field.data_type) }} { - {% if (field_is_user_defined(field)) or (field_is_user_defined_optional(field)) %} - {% if field_is_user_defined_optional(field) %} - return {{ swift_var_name }}.{{ fmt_var(field.name) }}.flatMap { {{ fmt_objc_type(field.data_type, False) }}.wrapPreservingSubtypes(swift: $0) } - {% else %} - return {{ fmt_objc_type(field.data_type) }}.wrapPreservingSubtypes(swift: {{ swift_var_name }}.{{ fmt_var(field.name) }}) - {% endif %} - {% elif (field_is_user_defined_map(field)) or (field_is_user_defined_list(field)) %} - {{ swift_var_name }}.{{ fmt_var(field.name) }}.{{ 'mapValues' if field_is_user_defined_map(field) else 'map' }} { - return {{ fmt_objc_type(field.data_type.data_type) }}.wrapPreservingSubtypes(swift: $0) - } - {% endif %} - } - {% else %} public var {{ fmt_var(field.name) }}: {{ fmt_objc_type(field.data_type) }} { {{ objc_return_field_value_oneliner(data_type, field) }} } - {% endif %} {% endfor %} {% if data_type.fields %} @@ -69,7 +53,7 @@ public class DBX{{ namespace_class_name }}{{ data_type_class_name }}: {{ 'NSObje public let {{ swift_var_name }}: {{ swift_type }} - public init(swift: {{ swift_type }}) { + {{ "fileprivate" if (objc_datatype_value_type_tuples(data_type)|length > 0) else "public" }} init(swift: {{ swift_type }}) { self.{{ swift_var_name }} = swift {% if data_type.parent_type %} super.init(swift: swift) @@ -87,6 +71,7 @@ public class DBX{{ namespace_class_name }}{{ data_type_class_name }}: {{ 'NSObje return DBX{{ namespace_class_name }}{{ data_type_class_name }}(swift: swift) } } + {% else %} {% endif %} @objc diff --git a/stone/backends/swift_types.py b/stone/backends/swift_types.py index 44434292..3864c892 100644 --- a/stone/backends/swift_types.py +++ b/stone/backends/swift_types.py @@ -31,7 +31,7 @@ field_is_user_defined_list, objc_datatype_value_type_tuples, datatype_subtype_value_types, - field_datatype_has_subtypes + datatype_has_subtypes, ) from stone.ir import ( @@ -199,8 +199,6 @@ def generate(self, api): template_globals['field_is_user_defined_optional'] = field_is_user_defined_optional template_globals['field_is_user_defined_list'] = field_is_user_defined_list template_globals['field_is_user_defined_map'] = field_is_user_defined_map - in_jinja_key = 'field_datatype_has_subtypes' - template_globals[in_jinja_key] = field_datatype_has_subtypes template_globals['objc_datatype_value_type_tuples'] = objc_datatype_value_type_tuples template_globals['objc_init_args_to_swift'] = self._objc_init_args_to_swift template_globals['objc_union_arg'] = self._objc_union_arg @@ -428,10 +426,13 @@ def _objc_return_field_value_oneliner(self, parent_type, field): prefix, objc_type) else: - value = '{}{}.map {}{{ {}(swift: $0) }}'.format(value, + has_subtypes = datatype_has_subtypes(list_data_type) + value = '{}{}.map {}{{ {}{}(swift: $0) }}'.format(value, '?' if nullable else '', prefix, - objc_type) + objc_type, + '.wrapPreservingSubtypes' if + has_subtypes else '') elif is_numeric_type(list_data_type): map_func = 'compactMap' if list_nullable else 'map' value = '{}{}.{} {}{{ $0 as NSNumber{} }}'.format(value, @@ -445,10 +446,13 @@ def _objc_return_field_value_oneliner(self, parent_type, field): objc_type = fmt_objc_type(data_type.value_data_type) value = '{}.{}'.format(swift_var_name, fmt_var(field.name)) + has_subtypes = datatype_has_subtypes(data_type.value_data_type) if is_user_defined_type(data_type.value_data_type): - value = '{}{}.mapValues {{ {}(swift: $0) }}'.format(value, + value = '{}{}.mapValues {{ {}{}(swift: $0) }}'.format(value, '?' if nullable else '', - objc_type) + objc_type, + '.wrapPreservingSubtypes' if + has_subtypes else '') return value elif is_float_type(data_type.value_data_type): value = '{}.{}{}.mapValues({{ $0 as NSNumber }})'.format(swift_var_name, @@ -470,8 +474,10 @@ def _objc_return_field_value_oneliner(self, parent_type, field): fmt_objc_type(data_type, False), swift_arg_name) else: - return '{}{}(swift: {})'.format(value, + has_subtypes = datatype_has_subtypes(data_type) + return '{}{}{}(swift: {})'.format(value, fmt_objc_type(data_type, False), + '.wrapPreservingSubtypes' if has_subtypes else '', swift_arg_name) elif is_numeric_type(data_type) or is_boolean_type(data_type): return '{}.{} as NSNumber{}'.format(swift_var_name,