From 825621c18511feae1a1a1ecc9c06d21fa22af77e Mon Sep 17 00:00:00 2001 From: Julian Locke Date: Fri, 15 Dec 2023 11:49:04 -0500 Subject: [PATCH 1/2] Fix stack overflow on failure to create route object description --- stone/backends/swift_rsrc/SwiftTypes.jinja | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stone/backends/swift_rsrc/SwiftTypes.jinja b/stone/backends/swift_rsrc/SwiftTypes.jinja index a15d0d60..ad7c6850 100644 --- a/stone/backends/swift_rsrc/SwiftTypes.jinja +++ b/stone/backends/swift_rsrc/SwiftTypes.jinja @@ -33,7 +33,7 @@ public class {{ fmt_class(namespace.name) }} { do { return "\(SerializeUtil.prepareJSONForSerialization(try {{ fmt_class(data_type.name) }}Serializer().serialize(self)))" } catch { - return "\(self)" + return "Failed to generate description for {{ fmt_class(data_type.name) }}: \(error)" } } } @@ -106,7 +106,7 @@ public class {{ fmt_class(namespace.name) }} { do { return "\(SerializeUtil.prepareJSONForSerialization(try {{ fmt_class(data_type.name) }}Serializer().serialize(self)))" } catch { - return "\(self)" + return "Failed to generate description for {{ fmt_class(data_type.name) }}: \(error)" } } } From f02b6de60ee36780f8c37216ab765fea21ffbf40 Mon Sep 17 00:00:00 2001 From: Julian Locke Date: Fri, 15 Dec 2023 11:50:02 -0500 Subject: [PATCH 2/2] Support more ergonomic swift SDK route response mocking --- stone/backends/swift_rsrc/SwiftRoutes.jinja | 4 ++-- stone/backends/swift_rsrc/SwiftTypes.jinja | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/stone/backends/swift_rsrc/SwiftRoutes.jinja b/stone/backends/swift_rsrc/SwiftRoutes.jinja index 89477d55..89303e0b 100644 --- a/stone/backends/swift_rsrc/SwiftRoutes.jinja +++ b/stone/backends/swift_rsrc/SwiftRoutes.jinja @@ -8,9 +8,9 @@ import Foundation /// Routes for the {{ class_name(namespace.name) }} namespace /// For Objective-C compatible routes see DB{{ fmt_class(namespace.name) }}Routes -public class {{ fmt_class(class_name(namespace.name)) }}Routes { +public class {{ fmt_class(class_name(namespace.name)) }}Routes: DropboxTransportClientOwning { public let client: {{ transport_client_name }} - init(client: {{ transport_client_name }}) { + required init(client: {{ transport_client_name }}) { self.client = client } diff --git a/stone/backends/swift_rsrc/SwiftTypes.jinja b/stone/backends/swift_rsrc/SwiftTypes.jinja index ad7c6850..8e4c87e0 100644 --- a/stone/backends/swift_rsrc/SwiftTypes.jinja +++ b/stone/backends/swift_rsrc/SwiftTypes.jinja @@ -11,7 +11,7 @@ public class {{ fmt_class(namespace.name) }} { {% for data_type in namespace.linearize_data_types() %} {{ data_type_doc(data_type) }} {% if is_struct_type(data_type) %} - public class {{ fmt_class(data_type.name) }}: {{ 'CustomStringConvertible' if not data_type.parent_type else fmt_type(data_type.parent_type) }} { + public class {{ fmt_class(data_type.name) }}: {{ 'CustomStringConvertible, JSONRepresentable' if not data_type.parent_type else fmt_type(data_type.parent_type) }} { {% for field in data_type.fields %} {{ struct_field_doc(field) }} public let {{ fmt_var(field.name) }}: {{ fmt_type(field.data_type) }} @@ -29,6 +29,13 @@ public class {{ fmt_class(namespace.name) }} { {% endif %} } {% endif %} + + {% if not data_type.parent_type %} + func json() throws -> JSON { + try {{ fmt_class(data_type.name) }}Serializer().serialize(self) + } + {% endif %} + {{ 'public var' if not data_type.parent_type else 'public override var' }} description: String { do { return "\(SerializeUtil.prepareJSONForSerialization(try {{ fmt_class(data_type.name) }}Serializer().serialize(self)))" @@ -96,12 +103,16 @@ public class {{ fmt_class(namespace.name) }} { } } {% elif is_union_type(data_type) %} - public enum {{ fmt_class(data_type.name) }}: CustomStringConvertible { + public enum {{ fmt_class(data_type.name) }}: CustomStringConvertible, JSONRepresentable { {% for field in data_type.all_fields %} {{ union_field_doc(field) }} case {{ fmt_var(field.name) }}{{ format_tag_type(field.data_type) }} {% endfor %} + func json() throws -> JSON { + try {{ fmt_class(data_type.name) }}Serializer().serialize(self) + } + public var description: String { do { return "\(SerializeUtil.prepareJSONForSerialization(try {{ fmt_class(data_type.name) }}Serializer().serialize(self)))"