Skip to content

Commit

Permalink
Enforce wrapPreservingSubtypes usage when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlocke committed Oct 22, 2024
1 parent 8329a14 commit f637d9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
3 changes: 0 additions & 3 deletions stone/backends/swift_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 2 additions & 17 deletions stone/backends/swift_rsrc/ObjcTypes.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
22 changes: 14 additions & 8 deletions stone/backends/swift_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit f637d9e

Please sign in to comment.