Skip to content

Commit

Permalink
Create empty functions when no fields are present (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsc authored Apr 17, 2024
1 parent 8e1176e commit a571e86
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions Sources/SwaggerSwiftCore/Models/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ struct Model {

model += "\n\n" + initMethod.indentLines(1)

let fieldsChangesSwaggerFieldNames = fields.contains(where: { $0.usesSwaggerFieldName == false })
let fieldHasDefaultValue = fields.contains(where: { $0.defaultValue != nil })
let fieldHasOptionalURL = fields.contains(where: { $0.type.toString(required: true) == "URL" && !$0.isRequired })

if isCodable {
model += "\n\n"
model += decodeFunction(accessControl: accessControl).indentLines(1)
Expand Down Expand Up @@ -93,20 +89,17 @@ struct Model {
encodeIfPresent = ""
}

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

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

let functionBody = """
var functionBody = ""

if !encodeFields.isEmpty {
functionBody = """
var container = encoder.container(keyedBy: StringCodingKey.self)
\(encodeFields)
"""
}

return """
\(accessControl.rawValue) func encode(to encoder: Encoder) throws {
Expand Down Expand Up @@ -148,10 +141,13 @@ var container = encoder.container(keyedBy: StringCodingKey.self)
}
}.joined(separator: "\n")

let functionBody = """
var functionBody = ""
if !decodeFields.isEmpty {
functionBody = """
let container = try decoder.container(keyedBy: StringCodingKey.self)
\(decodeFields)
"""
}

return """
\(accessControl.rawValue) init(from decoder: Decoder) throws {
Expand Down

0 comments on commit a571e86

Please sign in to comment.