Skip to content

Commit

Permalink
Swift test mocking enchancements (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlocke authored Dec 15, 2023
1 parent a105afe commit 3e08b02
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions stone/backends/swift_rsrc/SwiftRoutes.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
19 changes: 15 additions & 4 deletions stone/backends/swift_rsrc/SwiftTypes.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
Expand All @@ -29,11 +29,18 @@ 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)))"
} catch {
return "\(self)"
return "Failed to generate description for {{ fmt_class(data_type.name) }}: \(error)"
}
}
}
Expand Down Expand Up @@ -96,17 +103,21 @@ 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)))"
} catch {
return "\(self)"
return "Failed to generate description for {{ fmt_class(data_type.name) }}: \(error)"
}
}
}
Expand Down

0 comments on commit 3e08b02

Please sign in to comment.