Skip to content

Commit

Permalink
Merge pull request #70 from siemensikkema/feature/use-encode-if-present
Browse files Browse the repository at this point in the history
Use encodeIfPresent instead of custom helper function
  • Loading branch information
mattpolzin authored May 14, 2020
2 parents 68b8072 + f980ba2 commit 732251a
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 189 deletions.
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/Component Object/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ extension OpenAPI.Components {
case examples
case requestBodies
case headers
case securitySchemes
case securitySchemes
// case links
// case callbacks

Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/Content/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ extension OpenAPI.Content: Encodable {
try container.encode(example, forKey: .example)
}

try encoding.encodeIfNotNil(to: &container, forKey: .encoding)
try container.encodeIfPresent(encoding, forKey: .encoding)

try encodeExtensions(to: &container)
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/OpenAPIKit/Content/ContentEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ extension OpenAPI.Content.Encoding: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try contentType.encodeIfNotNil(to: &container, forKey: .contentType)

try headers.encodeIfNotNil(to: &container, forKey: .headers)
try container.encodeIfPresent(contentType, forKey: .contentType)
try container.encodeIfPresent(headers, forKey: .headers)

if style != Self.defaultStyle {
try container.encode(style, forKey: .style)
Expand Down
4 changes: 1 addition & 3 deletions Sources/OpenAPIKit/Discriminator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ extension OpenAPI.Discriminator: Encodable {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(propertyName, forKey: .propertyName)

try mapping.encodeIfNotNil(to: &container, forKey: .mapping)
try container.encodeIfPresent(mapping, forKey: .mapping)
}
}

Expand All @@ -40,7 +39,6 @@ extension OpenAPI.Discriminator: Decodable {
let container = try decoder.container(keyedBy: CodingKeys.self)

propertyName = try container.decode(String.self, forKey: .propertyName)

mapping = try container.decodeIfPresent([String: String].self, forKey: .mapping)
}
}
Expand Down
11 changes: 2 additions & 9 deletions Sources/OpenAPIKit/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ extension OpenAPI.Document: Encodable {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(openAPIVersion, forKey: .openAPIVersion)

try container.encode(info, forKey: .info)

if !servers.isEmpty {
Expand All @@ -96,9 +95,8 @@ extension OpenAPI.Document: Encodable {
try encodeSecurity(requirements: security, to: &container, forKey: .security)
}

try tags.encodeIfNotNil(to: &container, forKey: .tags)

try externalDocs.encodeIfNotNil(to: &container, forKey: .externalDocs)
try container.encodeIfPresent(tags, forKey: .tags)
try container.encodeIfPresent(externalDocs, forKey: .externalDocs)

try encodeExtensions(to: &container)
}
Expand All @@ -110,9 +108,7 @@ extension OpenAPI.Document: Decodable {

do {
openAPIVersion = try container.decode(OpenAPI.Document.Version.self, forKey: .openAPIVersion)

info = try container.decode(OpenAPI.Document.Info.self, forKey: .info)

servers = try container.decodeIfPresent([OpenAPI.Server].self, forKey: .servers) ?? []

let components = try container.decodeIfPresent(OpenAPI.Components.self, forKey: .components) ?? .noComponents
Expand All @@ -123,11 +119,8 @@ extension OpenAPI.Document: Decodable {
try validateSecurityRequirements(in: paths, against: components)

security = try decodeSecurityRequirements(from: container, forKey: .security, given: components) ?? []

tags = try container.decodeIfPresent([OpenAPI.Tag].self, forKey: .tags)

externalDocs = try container.decodeIfPresent(OpenAPI.ExternalDocumentation.self, forKey: .externalDocs)

vendorExtensions = try Self.extensions(from: decoder)

} catch let error as OpenAPI.Error.Decoding.Path {
Expand Down
31 changes: 8 additions & 23 deletions Sources/OpenAPIKit/DocumentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ extension OpenAPI.Document.Info.License: Encodable {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(name, forKey: .name)

try url.encodeIfNotNil(to: &container, forKey: .url)
try container.encodeIfPresent(url, forKey: .url)

try encodeExtensions(to: &container)
}
Expand Down Expand Up @@ -192,11 +191,9 @@ extension OpenAPI.Document.Info.Contact: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try name.encodeIfNotNil(to: &container, forKey: .name)

try url.encodeIfNotNil(to: &container, forKey: .url)

try email.encodeIfNotNil(to: &container, forKey: .email)
try container.encodeIfPresent(name, forKey: .name)
try container.encodeIfPresent(url, forKey: .url)
try container.encodeIfPresent(email, forKey: .email)

try encodeExtensions(to: &container)
}
Expand All @@ -207,9 +204,7 @@ extension OpenAPI.Document.Info.Contact: Decodable {
let container = try decoder.container(keyedBy: CodingKeys.self)

name = try container.decodeIfPresent(String.self, forKey: .name)

url = try container.decodeIfPresent(URL.self, forKey: .url)

email = try container.decodeIfPresent(String.self, forKey: .email)

vendorExtensions = try Self.extensions(from: decoder)
Expand Down Expand Up @@ -277,15 +272,10 @@ extension OpenAPI.Document.Info: Encodable {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(title, forKey: .title)

try description.encodeIfNotNil(to: &container, forKey: .description)

try termsOfService.encodeIfNotNil(to: &container, forKey: .termsOfService)

try contact.encodeIfNotNil(to: &container, forKey: .contact)

try license.encodeIfNotNil(to: &container, forKey: .license)

try container.encodeIfPresent(description, forKey: .description)
try container.encodeIfPresent(termsOfService, forKey: .termsOfService)
try container.encodeIfPresent(contact, forKey: .contact)
try container.encodeIfPresent(license, forKey: .license)
try container.encode(version, forKey: .version)

try encodeExtensions(to: &container)
Expand All @@ -297,15 +287,10 @@ extension OpenAPI.Document.Info: Decodable {
let container = try decoder.container(keyedBy: CodingKeys.self)

title = try container.decode(String.self, forKey: .title)

description = try container.decodeIfPresent(String.self, forKey: .description)

termsOfService = try container.decodeIfPresent(URL.self, forKey: .termsOfService)

contact = try container.decodeIfPresent(Contact.self, forKey: .contact)

license = try container.decodeIfPresent(License.self, forKey: .license)

version = try container.decode(String.self, forKey: .version)

vendorExtensions = try Self.extensions(from: decoder)
Expand Down
6 changes: 2 additions & 4 deletions Sources/OpenAPIKit/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ extension OpenAPI.Example: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try summary.encodeIfNotNil(to: &container, forKey: .summary)

try description.encodeIfNotNil(to: &container, forKey: .description)
try container.encodeIfPresent(summary, forKey: .summary)
try container.encodeIfPresent(description, forKey: .description)

switch value {
case .a(let url):
Expand Down Expand Up @@ -95,7 +94,6 @@ extension OpenAPI.Example: Decodable {
let externalValue = try container.decodeIfPresent(URL.self, forKey: .externalValue)

summary = try container.decodeIfPresent(String.self, forKey: .summary)

description = try container.decodeIfPresent(String.self, forKey: .description)

value = try externalValue.map(Either.init)
Expand Down
4 changes: 1 addition & 3 deletions Sources/OpenAPIKit/ExternalDoc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ extension OpenAPI.ExternalDocumentation: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try description.encodeIfNotNil(to: &container, forKey: .description)

try container.encodeIfPresent(description, forKey: .description)
try container.encode(url, forKey: .url)
}
}
Expand All @@ -40,7 +39,6 @@ extension OpenAPI.ExternalDocumentation: Decodable {
let container = try decoder.container(keyedBy: CodingKeys.self)

description = try container.decodeIfPresent(String.self, forKey: .description)

url = try container.decode(URL.self, forKey: .url)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ extension OpenAPI.Header: Encodable {
try container.encode(contentMap, forKey: .content)
}

try description.encodeIfNotNil(to: &container, forKey: .description)
try container.encodeIfPresent(description, forKey: .description)

if deprecated {
try container.encode(deprecated, forKey: .deprecated)
Expand Down
10 changes: 5 additions & 5 deletions Sources/OpenAPIKit/OAuthFlows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ extension OpenAPI.OAuthFlows: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try implicit.encodeIfNotNil(to: &container, forKey: .implicit)
try password.encodeIfNotNil(to: &container, forKey: .password)
try clientCredentials.encodeIfNotNil(to: &container, forKey: .clientCredentials)
try authorizationCode.encodeIfNotNil(to: &container, forKey: .authorizationCode)
try container.encodeIfPresent(implicit, forKey: .implicit)
try container.encodeIfPresent(password, forKey: .password)
try container.encodeIfPresent(clientCredentials, forKey: .clientCredentials)
try container.encodeIfPresent(authorizationCode, forKey: .authorizationCode)
}
}

Expand All @@ -171,7 +171,7 @@ extension OpenAPI.OAuthFlows.CommonFields: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try refreshUrl.encodeIfNotNil(to: &container, forKey: .refreshUrl)
try container.encodeIfPresent(refreshUrl, forKey: .refreshUrl)
try container.encode(scopes, forKey: .scopes)
}
}
Expand Down
20 changes: 8 additions & 12 deletions Sources/OpenAPIKit/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,17 @@ extension OpenAPI.Operation: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try tags.encodeIfNotNil(to: &container, forKey: .tags)

try summary.encodeIfNotNil(to: &container, forKey: .summary)

try description.encodeIfNotNil(to: &container, forKey: .description)

try externalDocs.encodeIfNotNil(to: &container, forKey: .externalDocs)

try operationId.encodeIfNotNil(to: &container, forKey: .operationId)
try container.encodeIfPresent(tags, forKey: .tags)
try container.encodeIfPresent(summary, forKey: .summary)
try container.encodeIfPresent(description, forKey: .description)
try container.encodeIfPresent(externalDocs, forKey: .externalDocs)
try container.encodeIfPresent(operationId, forKey: .operationId)

if !parameters.isEmpty {
try container.encode(parameters, forKey: .parameters)
}

try requestBody.encodeIfNotNil(to: &container, forKey: .requestBody)
try container.encodeIfPresent(requestBody, forKey: .requestBody)

try container.encode(responses, forKey: .responses)

Expand All @@ -146,8 +142,8 @@ extension OpenAPI.Operation: Encodable {
try encodeSecurity(requirements: securityRequirements, to: &container, forKey: .security)
}

try servers.encodeIfNotNil(to: &container, forKey: .servers)

try container.encodeIfPresent(servers, forKey: .servers)
try encodeExtensions(to: &container)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/Parameter/Parameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ extension OpenAPI.Parameter: Encodable {
try container.encode(contentMap, forKey: .content)
}

try description.encodeIfNotNil(to: &container, forKey: .description)
try container.encodeIfPresent(description, forKey: .description)

if deprecated {
try container.encode(deprecated, forKey: .deprecated)
Expand Down
27 changes: 11 additions & 16 deletions Sources/OpenAPIKit/PathItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,22 @@ extension OpenAPI.PathItem: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try summary.encodeIfNotNil(to: &container, forKey: .summary)

try description.encodeIfNotNil(to: &container, forKey: .description)

try servers.encodeIfNotNil(to: &container, forKey: .servers)
try container.encodeIfPresent(summary, forKey: .summary)
try container.encodeIfPresent(description, forKey: .description)
try container.encodeIfPresent(servers, forKey: .servers)

if !parameters.isEmpty {
try container.encode(parameters, forKey: .parameters)
}

try get.encodeIfNotNil(to: &container, forKey: .get)
try put.encodeIfNotNil(to: &container, forKey: .put)
try post.encodeIfNotNil(to: &container, forKey: .post)
try delete.encodeIfNotNil(to: &container, forKey: .delete)
try options.encodeIfNotNil(to: &container, forKey: .options)
try head.encodeIfNotNil(to: &container, forKey: .head)
try patch.encodeIfNotNil(to: &container, forKey: .patch)
try trace.encodeIfNotNil(to: &container, forKey: .trace)
try container.encodeIfPresent(get, forKey: .get)
try container.encodeIfPresent(put, forKey: .put)
try container.encodeIfPresent(post, forKey: .post)
try container.encodeIfPresent(delete, forKey: .delete)
try container.encodeIfPresent(options, forKey: .options)
try container.encodeIfPresent(head, forKey: .head)
try container.encodeIfPresent(patch, forKey: .patch)
try container.encodeIfPresent(trace, forKey: .trace)

try encodeExtensions(to: &container)
}
Expand All @@ -262,11 +260,8 @@ extension OpenAPI.PathItem: Decodable {

do {
summary = try container.decodeIfPresent(String.self, forKey: .summary)

description = try container.decodeIfPresent(String.self, forKey: .description)

servers = try container.decodeIfPresent([OpenAPI.Server].self, forKey: .servers)

parameters = try container.decodeIfPresent(OpenAPI.Parameter.Array.self, forKey: .parameters) ?? []

get = try container.decodeIfPresent(OpenAPI.Operation.self, forKey: .get)
Expand Down
5 changes: 1 addition & 4 deletions Sources/OpenAPIKit/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ extension OpenAPI.Request: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try description.encodeIfNotNil(to: &container, forKey: .description)

try container.encodeIfPresent(description, forKey: .description)
try container.encode(content, forKey: .content)

if required {
Expand All @@ -56,9 +55,7 @@ extension OpenAPI.Request: Decodable {

do {
description = try container.decodeIfPresent(String.self, forKey: .description)

content = try container.decode(OpenAPI.Content.Map.self, forKey: .content)

required = try container.decodeIfPresent(Bool.self, forKey: .required) ?? false
} catch let error as InconsistencyError {

Expand Down
5 changes: 1 addition & 4 deletions Sources/OpenAPIKit/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ extension OpenAPI.Response: Encodable {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(description, forKey: .description)

try headers.encodeIfNotNil(to: &container, forKey: .headers)
try container.encodeIfPresent(headers, forKey: .headers)

if content.count > 0 {
try container.encode(content, forKey: .content)
Expand All @@ -166,9 +165,7 @@ extension OpenAPI.Response: Decodable {

do {
description = try container.decode(String.self, forKey: .description)

headers = try container.decodeIfPresent(OpenAPI.Header.Map.self, forKey: .headers)

content = try container.decodeIfPresent(OpenAPI.Content.Map.self, forKey: .content) ?? [:]

} catch let error as InconsistencyError {
Expand Down
Loading

0 comments on commit 732251a

Please sign in to comment.