Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom coding key and implement codable functions on our own to reduce binary size #144

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/SwaggerSwiftCore/Generator/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public struct Generator {
try dateDecodingStrategy.replacingOccurrences(of: "<ACCESSCONTROL>", with: accControl).write(toFile: "\(targetPath)/DateDecodingStrategy.swift")
try apiInitializeFile.replacingOccurrences(of: "<ACCESSCONTROL>", with: accControl).write(toFile: "\(targetPath)/APIInitialize.swift")
try apiInitializerFile.replacingOccurrences(of: "<ACCESSCONTROL>", with: accControl).write(toFile: "\(targetPath)/APIInitializer.swift")
try stringCodingKey.replacingOccurrences(of: "<ACCESSCONTROL>", with: accControl).write(toFile: "\(targetPath)/StringCodingKey.swift")

if swaggerFile.createSwiftPackage == false {
let globalHeadersDefinitions = globalHeadersModel.writeExtensions(inCommonPackageNamed: nil)
Expand Down
56 changes: 39 additions & 17 deletions Sources/SwaggerSwiftCore/Models/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ struct Model {
let fieldHasDefaultValue = fields.contains(where: { $0.defaultValue != nil })
let fieldHasOptionalURL = fields.contains(where: { $0.type.toString(required: true) == "URL" && !$0.isRequired })

if isCodable && (fieldsChangesSwaggerFieldNames || fieldHasDefaultValue || fieldHasOptionalURL) {
if isCodable {
model += "\n\n"
model += decodeFunction().indentLines(1)
}

if isCodable && fieldsChangesSwaggerFieldNames {
model += decodeFunction(accessControl: accessControl).indentLines(1)
model += "\n\n"
model += codingKeysFunction().indentLines(1)
model += encodeFunction(accessControl: accessControl).indentLines(1)
}

if embeddedDefinitions.count > 0 {
Expand All @@ -85,19 +82,44 @@ struct Model {
return model
}

private func codingKeysFunction() -> String {
let cases = fields.map { "case \($0.safeParameterName.value.variableNameFormatted) = \"\($0.argumentLabel)\"" }.joined(separator: "\n").indentLines(1)
private func encodeFunction(accessControl: APIAccessControl) -> String {
let encodeFields = fields.map {
let variableName = $0.safePropertyName.value.variableNameFormatted
let codingKey = $0.argumentLabel
let typeName = $0.type.toString(required: true)
let encodeIfPresent: String
if $0.isRequired == false || $0.defaultValue != nil {
encodeIfPresent = "IfPresent"
} else {
encodeIfPresent = ""
}

let defaultValue: String
if let defaultValueValue = $0.defaultValue {
defaultValue = " ?? \(defaultValueValue)"
} else {
defaultValue = ""
}

return "try container.encode\(encodeIfPresent)(\(variableName)\(defaultValue), forKey: \"\(codingKey)\")"
}.joined(separator: "\n")

let functionBody = """
var container = encoder.container(keyedBy: StringCodingKey.self)
\(encodeFields)
"""

return """
enum CodingKeys: String, CodingKey {
\(cases)
}
"""
\(accessControl.rawValue) func encode(to encoder: Encoder) throws {
\(functionBody.indentLines(1))
}
"""
}

private func decodeFunction() -> String {
private func decodeFunction(accessControl: APIAccessControl) -> String {
let decodeFields = fields.map {
let variableName = $0.safePropertyName.value.variableNameFormatted
let codingKey = $0.argumentLabel
let typeName = $0.type.toString(required: true)
let decodeIfPresent: String
if $0.isRequired == false || $0.defaultValue != nil {
Expand All @@ -116,24 +138,24 @@ struct Model {
if !$0.isRequired, typeName == "URL" {
return """
// Allows the backend to return badly formatted urls
if let urlString = try container.decode\(decodeIfPresent)(String.self, forKey: .\(variableName))\(defaultValue) {
if let urlString = try container.decode\(decodeIfPresent)(String.self, forKey: \"\(codingKey)\")\(defaultValue) {
self.\(variableName) = URL(string: urlString)
} else {
self.\(variableName) = nil
}
"""
} else {
return "self.\(variableName) = try container.decode\(decodeIfPresent)(\(typeName).self, forKey: .\(variableName))\(defaultValue)"
return "self.\(variableName) = try container.decode\(decodeIfPresent)(\(typeName).self, forKey: \"\(codingKey)\")\(defaultValue)"
}
}.joined(separator: "\n")

let functionBody = """
let container = try decoder.container(keyedBy: CodingKeys.self)
let container = try decoder.container(keyedBy: StringCodingKey.self)
\(decodeFields)
"""

return """
public init(from decoder: Decoder) throws {
\(accessControl.rawValue) init(from decoder: Decoder) throws {
\(functionBody.indentLines(1))
}
"""
Expand Down
29 changes: 29 additions & 0 deletions Sources/SwaggerSwiftCore/Static Files/StringCodingKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let stringCodingKey = """
import Foundation

<ACCESSCONTROL> struct StringCodingKey: CodingKey, ExpressibleByStringLiteral {
private let string: String
private var int: Int?

<ACCESSCONTROL> var stringValue: String { return string }

<ACCESSCONTROL> init(string: String) {
self.string = string
}

<ACCESSCONTROL> init?(stringValue: String) {
self.string = stringValue
}

<ACCESSCONTROL> var intValue: Int? { return int }

<ACCESSCONTROL> init?(intValue: Int) {
self.string = String(describing: intValue)
self.int = intValue
}

<ACCESSCONTROL> init(stringLiteral value: String) {
self.string = value
}
}
"""
19 changes: 17 additions & 2 deletions Tests/SwaggerSwiftCoreTests/SwaggerSwiftCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ public struct Test: Codable, Sendable {
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let container = try decoder.container(keyedBy: StringCodingKey.self)
// Allows the backend to return badly formatted urls
if let urlString = try container.decodeIfPresent(String.self, forKey: .url) {
if let urlString = try container.decodeIfPresent(String.self, forKey: "url") {
self.url = URL(string: urlString)
} else {
self.url = nil
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
try container.encodeIfPresent(url, forKey: "url")
}
}
""")
}
Expand All @@ -52,6 +57,16 @@ public struct Test: Codable, Sendable {
public init(url: URL) {
self.url = url
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: StringCodingKey.self)
self.url = try container.decode(URL.self, forKey: "url")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
try container.encode(url, forKey: "url")
}
}
""")
}
Expand Down
Loading