Skip to content

Commit 3a0b829

Browse files
Merge pull request #609 from appwrite/fix-swift
2 parents edc4df3 + ddaa61f commit 3a0b829

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

src/SDK/Language/Swift.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ public function getFiles(): array
176176
],
177177
[
178178
'scope' => 'default',
179-
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/Extensions/Codable+JSON.swift',
180-
'template' => 'swift/Sources/Extensions/Codable+JSON.swift.twig',
179+
'destination' => '/Sources/JSONCodable/Codable+JSON.swift',
180+
'template' => 'swift/Sources/JSONCodable/Codable+JSON.swift.twig',
181181
],
182182
[
183183
'scope' => 'default',

templates/swift/Package.swift.twig

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let package = Package(
1313
products: [
1414
.library(
1515
name: "{{spec.title | caseUcfirst}}",
16-
targets: ["{{spec.title | caseUcfirst}}", "{{spec.title | caseUcfirst}}Models"]
16+
targets: ["{{spec.title | caseUcfirst}}", "{{spec.title | caseUcfirst}}Models", "JSONCodable"]
1717
),
1818
],
1919
dependencies: [
@@ -26,11 +26,18 @@ let package = Package(
2626
dependencies: [
2727
.product(name: "AsyncHTTPClient", package: "async-http-client"),
2828
.product(name: "NIOWebSocket", package: "swift-nio"),
29-
"{{spec.title | caseUcfirst}}Models"
29+
"{{spec.title | caseUcfirst}}Models",
30+
"JSONCodable"
3031
]
3132
),
3233
.target(
33-
name: "{{spec.title | caseUcfirst}}Models"
34+
name: "{{spec.title | caseUcfirst}}Models",
35+
dependencies: [
36+
"JSONCodable"
37+
]
38+
),
39+
.target(
40+
name: "JSONCodable"
3441
),
3542
.testTarget(
3643
name: "{{spec.title | caseUcfirst}}Tests",

templates/swift/Sources/Extensions/Codable+JSON.swift.twig renamed to templates/swift/Sources/JSONCodable/Codable+JSON.swift.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ let jsonDecoder = JSONDecoder()
66
// MARK: - Conversions
77

88
extension Encodable {
9-
func toJson() throws -> String {
9+
public func toJson() throws -> String {
1010
return String(data: try jsonEncoder.encode(self), encoding: .utf8)!
1111
}
1212
}
1313

1414
extension String {
15-
func fromJson<T : Decodable>(to model: T.Type) throws -> T {
15+
public func fromJson<T : Decodable>(to model: T.Type) throws -> T {
1616
return try jsonDecoder.decode(model, from: self.data(using: .utf8)!)
1717
}
1818
}
@@ -22,7 +22,7 @@ protocol JsonConvert : Encodable {
2222
}
2323

2424
extension JsonConvert {
25-
func jsonCast<T : Decodable>(to model: T.Type) throws -> T {
25+
public func jsonCast<T : Decodable>(to model: T.Type) throws -> T {
2626
let string = try (self as Encodable).toJson()
2727
return try string.fromJson(to: model)
2828
}

templates/swift/Sources/Models/Model.swift.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import JSONCodable
23

34
/// {{ definition.description }}
45
{% if definition.properties | length == 0 and not definition.additionalProperties %}

templates/swift/Sources/Services/Service.swift.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import AsyncHTTPClient
22
import Foundation
33
import NIO
4+
import JSONCodable
45
import {{spec.title | caseUcfirst}}Models
56

67
/// {{ service.description }}
@@ -25,7 +26,7 @@ open class {{ service.name | caseUcfirst }}: Service {
2526
{%~ endif %}
2627
open func {{ method.name | caseCamel }}{% if method.responseModel | hasGenericType(spec) %}<T>{% endif %}(
2728
{%~ for parameter in method.parameters.all %}
28-
{{ parameter.name | caseCamel | escapeKeyword }}: {% if parameter.type == "object" %}T{% else %}{{ parameter | typeName | raw }}{% endif %}{% if not parameter.required %}? = nil{% endif %}{% if not loop.last or 'multipart/form-data' in method.consumes or method.responseModel | hasGenericType(spec) %},{% endif %}
29+
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName | raw }}{% if not parameter.required %}? = nil{% endif %}{% if not loop.last or 'multipart/form-data' in method.consumes or method.responseModel | hasGenericType(spec) %},{% endif %}
2930

3031
{%~ endfor %}
3132
{%~ if method.responseModel | hasGenericType(spec) %}
@@ -100,8 +101,7 @@ open class {{ service.name | caseUcfirst }}: Service {
100101
) async throws -> {{ method | returnType(spec, '[String: AnyCodable]') | raw }} {
101102
return try await {{ method.name | caseCamel }}(
102103
{%~ for parameter in method.parameters.all %}
103-
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last %},{% endif %}
104-
104+
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.name | caseCamel | escapeKeyword }},
105105
{%~ endfor %}
106106
nestedType: [String: AnyCodable].self
107107
{%~ if 'multipart/form-data' in method.consumes %}

0 commit comments

Comments
 (0)