Skip to content

Commit

Permalink
Fix swift builds
Browse files Browse the repository at this point in the history
(cherry picked from commit ddaa61f)
  • Loading branch information
abnegate committed Feb 3, 2023
1 parent f1a4746 commit 35ec927
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/SDK/Language/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ public function getFiles(): array
],
[
'scope' => 'default',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/Extensions/Codable+JSON.swift',
'template' => 'swift/Sources/Extensions/Codable+JSON.swift.twig',
'destination' => '/Sources/JSONCodable/Codable+JSON.swift',
'template' => 'swift/Sources/JSONCodable/Codable+JSON.swift.twig',
],
[
'scope' => 'default',
Expand Down
13 changes: 10 additions & 3 deletions templates/swift/Package.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
products: [
.library(
name: "{{spec.title | caseUcfirst}}",
targets: ["{{spec.title | caseUcfirst}}", "{{spec.title | caseUcfirst}}Models"]
targets: ["{{spec.title | caseUcfirst}}", "{{spec.title | caseUcfirst}}Models", "JSONCodable"]
),
],
dependencies: [
Expand All @@ -26,11 +26,18 @@ let package = Package(
dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "NIOWebSocket", package: "swift-nio"),
"{{spec.title | caseUcfirst}}Models"
"{{spec.title | caseUcfirst}}Models",
"JSONCodable"
]
),
.target(
name: "{{spec.title | caseUcfirst}}Models"
name: "{{spec.title | caseUcfirst}}Models",
dependencies: [
"JSONCodable"
]
),
.target(
name: "JSONCodable"
),
.testTarget(
name: "{{spec.title | caseUcfirst}}Tests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ let jsonDecoder = JSONDecoder()
// MARK: - Conversions

extension Encodable {
func toJson() throws -> String {
public func toJson() throws -> String {
return String(data: try jsonEncoder.encode(self), encoding: .utf8)!
}
}

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

extension JsonConvert {
func jsonCast<T : Decodable>(to model: T.Type) throws -> T {
public func jsonCast<T : Decodable>(to model: T.Type) throws -> T {
let string = try (self as Encodable).toJson()
return try string.fromJson(to: model)
}
Expand Down
1 change: 1 addition & 0 deletions templates/swift/Sources/Models/Model.swift.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import JSONCodable

/// {{ definition.description }}
{% if definition.properties | length == 0 and not definition.additionalProperties %}
Expand Down
6 changes: 3 additions & 3 deletions templates/swift/Sources/Services/Service.swift.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AsyncHTTPClient
import Foundation
import NIO
import JSONCodable
import {{spec.title | caseUcfirst}}Models

/// {{ service.description }}
Expand All @@ -25,7 +26,7 @@ open class {{ service.name | caseUcfirst }}: Service {
{%~ endif %}
open func {{ method.name | caseCamel }}{% if method.responseModel | hasGenericType(spec) %}<T>{% endif %}(
{%~ for parameter in method.parameters.all %}
{{ 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 %}
{{ 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 %}

{%~ endfor %}
{%~ if method.responseModel | hasGenericType(spec) %}
Expand Down Expand Up @@ -100,8 +101,7 @@ open class {{ service.name | caseUcfirst }}: Service {
) async throws -> {{ method | returnType(spec, '[String: AnyCodable]') | raw }} {
return try await {{ method.name | caseCamel }}(
{%~ for parameter in method.parameters.all %}
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last %},{% endif %}

{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter.name | caseCamel | escapeKeyword }},
{%~ endfor %}
nestedType: [String: AnyCodable].self
{%~ if 'multipart/form-data' in method.consumes %}
Expand Down

0 comments on commit 35ec927

Please sign in to comment.