Skip to content

Commit 86afa25

Browse files
Merge pull request #591 from appwrite/feat-swift-generics
Feat swift generic updates
2 parents 83b3a7f + 5160f30 commit 86afa25

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

templates/swift/Sources/Client.swift.twig

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,28 @@ open class Client {
426426
_ request: inout HTTPClientRequest,
427427
with params: [String: Any?] = [:]
428428
) throws {
429-
let json = try JSONSerialization.data(withJSONObject: params, options: [])
429+
var encodedParams = [String:Any]()
430+
431+
for (key, param) in params {
432+
if param is String
433+
|| param is Int
434+
|| param is Float
435+
|| param is Bool
436+
|| param is [String]
437+
|| param is [Int]
438+
|| param is [Float]
439+
|| param is [Bool]
440+
|| param is [String: Any]
441+
|| param is [Int: Any]
442+
|| param is [Float: Any]
443+
|| param is [Bool: Any] {
444+
encodedParams[key] = param
445+
} else {
446+
encodedParams[key] = try! (param as! Encodable).toJson()
447+
}
448+
}
449+
450+
let json = try JSONSerialization.data(withJSONObject: encodedParams, options: [])
430451

431452
request.body = .bytes(json)
432453
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ open class {{ service.name | caseUcfirst }}: Service {
2828
{{ 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 %},{% endif %}
2929

3030
{%~ endfor %}
31+
{%~ if method.responseModel | hasGenericType(spec) %}
32+
nestedType: T.Type,
33+
{%~ endif %}
3134
{%~ if 'multipart/form-data' in method.consumes %}
3235
onProgress: ((UploadProgress) -> Void)? = nil
3336
{%~ endif %}
@@ -100,6 +103,7 @@ open class {{ service.name | caseUcfirst }}: Service {
100103
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last %},{% endif %}
101104

102105
{%~ endfor %}
106+
nestedType: [String: AnyCodable].self
103107
{%~ if 'multipart/form-data' in method.consumes %}
104108
onProgress: onProgress
105109
{%~ endif %}

0 commit comments

Comments
 (0)