Skip to content

Commit

Permalink
Optimize build times (#182)
Browse files Browse the repository at this point in the history
* Avoids calling SwiftFormat on nested types.

Added a format function to the base Renderer class so it’s now the only type that needs to import SwiftFormat.

Generator now passes `shouldFormat: true` to the top level renderers so it’s only run once per file.

* Updated the generators to create the Codable implementation.

* Generated the models with the Codable implementation.

* Added AnyCodingKey to use in all Codable implementations.

* Generated the models using AnyCodingKey.

* Added the API spec file to gitignore.

* Updated tests to contain the Codable implementation.

* Updated OneOfSchemaRenderer to be a Renderer and use renderEnum.

* Removed try! when used in a throwing function.

* Format rendered code in tests explicitly

* Ignore code coverage of AnyCodingKey

---------

Co-authored-by: Morten Bjerg Gregersen <[email protected]>
  • Loading branch information
CraigSiemens and MortenGregersen authored Jun 5, 2024
1 parent 03fa9fa commit 5f3c9e9
Show file tree
Hide file tree
Showing 812 changed files with 31,485 additions and 22,632 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ xcuserdata
.build
coverage.lcov
docs
app_store_connect_api_openapi.json
26 changes: 26 additions & 0 deletions Sources/Bagbutik-Core/AnyCodingKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public struct AnyCodingKey: CodingKey {
public let stringValue: String
public let intValue: Int? = nil

public init?(stringValue: String) {
self.stringValue = stringValue
}

public init?(intValue: Int) {
nil // We are not using this, so just return nil
}
}

extension AnyCodingKey: ExpressibleByStringLiteral {
public init(stringLiteral value: StringLiteralType) {
self.stringValue = value
}

public init(unicodeScalarLiteral value: String) {
self.stringValue = value
}

public init(extendedGraphemeClusterLiteral value: String) {
self.stringValue = value
}
}
12 changes: 4 additions & 8 deletions Sources/Bagbutik-Core/Models/DocumentLinks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ public struct DocumentLinks: Codable {
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
itself = try container.decode(String.self, forKey: .itself)
let container = try decoder.container(keyedBy: AnyCodingKey.self)
itself = try container.decode(String.self, forKey: "self")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(itself, forKey: .itself)
}

private enum CodingKeys: String, CodingKey {
case itself = "self"
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(itself, forKey: "self")
}
}
48 changes: 44 additions & 4 deletions Sources/Bagbutik-Core/Models/ErrorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public struct ErrorResponse: Codable {
self.errors = errors
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
errors = try container.decodeIfPresent([Errors].self, forKey: "errors")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encodeIfPresent(errors, forKey: "errors")
}

/**
# ErrorResponse.Errors
The details about an error that are returned when an API request isn’t successful.
Expand Down Expand Up @@ -56,6 +66,28 @@ public struct ErrorResponse: Codable {
self.title = title
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
code = try container.decode(String.self, forKey: "code")
detail = try container.decodeIfPresent(String.self, forKey: "detail")
id = try container.decodeIfPresent(String.self, forKey: "id")
meta = try container.decodeIfPresent(Meta.self, forKey: "meta")
source = try container.decodeIfPresent(Source.self, forKey: "source")
status = try container.decode(String.self, forKey: "status")
title = try container.decode(String.self, forKey: "title")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(code, forKey: "code")
try container.encodeIfPresent(detail, forKey: "detail")
try container.encodeIfPresent(id, forKey: "id")
try container.encodeIfPresent(meta, forKey: "meta")
try container.encodeIfPresent(source, forKey: "source")
try container.encode(status, forKey: "status")
try container.encode(title, forKey: "title")
}

public struct Meta: Codable {
public var additionalProperties: [String: String]?
public var associatedErrors: [String: [Errors]]?
Expand All @@ -66,6 +98,18 @@ public struct ErrorResponse: Codable {
self.additionalProperties = additionalProperties
self.associatedErrors = associatedErrors
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
additionalProperties = try container.decodeIfPresent([String: String].self, forKey: "additionalProperties")
associatedErrors = try container.decodeIfPresent([String: [Errors]].self, forKey: "associatedErrors")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encodeIfPresent(additionalProperties, forKey: "additionalProperties")
try container.encodeIfPresent(associatedErrors, forKey: "associatedErrors")
}
}

public enum Source: Codable {
Expand All @@ -91,10 +135,6 @@ public struct ErrorResponse: Codable {
try value.encode(to: encoder)
}
}

private enum CodingKeys: String, CodingKey {
case type
}
}
}
}
10 changes: 10 additions & 0 deletions Sources/Bagbutik-Core/Models/JsonPointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ public struct JsonPointer: Codable {
public init(pointer: String? = nil) {
self.pointer = pointer
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
pointer = try container.decodeIfPresent(String.self, forKey: "pointer")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encodeIfPresent(pointer, forKey: "pointer")
}
}
22 changes: 8 additions & 14 deletions Sources/Bagbutik-Core/Models/PagedDocumentLinks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,16 @@ public struct PagedDocumentLinks: Codable {
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
first = try container.decodeIfPresent(String.self, forKey: .first)
next = try container.decodeIfPresent(String.self, forKey: .next)
itself = try container.decode(String.self, forKey: .itself)
let container = try decoder.container(keyedBy: AnyCodingKey.self)
first = try container.decodeIfPresent(String.self, forKey: "first")
next = try container.decodeIfPresent(String.self, forKey: "next")
itself = try container.decode(String.self, forKey: "self")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(first, forKey: .first)
try container.encodeIfPresent(next, forKey: .next)
try container.encode(itself, forKey: .itself)
}

private enum CodingKeys: String, CodingKey {
case first
case itself = "self"
case next
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encodeIfPresent(first, forKey: "first")
try container.encodeIfPresent(next, forKey: "next")
try container.encode(itself, forKey: "self")
}
}
22 changes: 22 additions & 0 deletions Sources/Bagbutik-Core/Models/PagingInformation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public struct PagingInformation: Codable {
self.paging = paging
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
paging = try container.decode(Paging.self, forKey: "paging")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(paging, forKey: "paging")
}

/**
# PagingInformation.Paging
Paging details such as the total number of resources and the per-page limit.
Expand All @@ -39,5 +49,17 @@ public struct PagingInformation: Codable {
self.limit = limit
self.total = total
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
limit = try container.decode(Int.self, forKey: "limit")
total = try container.decodeIfPresent(Int.self, forKey: "total")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(limit, forKey: "limit")
try container.encodeIfPresent(total, forKey: "total")
}
}
}
10 changes: 10 additions & 0 deletions Sources/Bagbutik-Core/Models/Parameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ public struct Parameter: Codable {
public init(parameter: String? = nil) {
self.parameter = parameter
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
parameter = try container.decodeIfPresent(String.self, forKey: "parameter")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encodeIfPresent(parameter, forKey: "parameter")
}
}
12 changes: 4 additions & 8 deletions Sources/Bagbutik-Core/Models/ResourceLinks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ public struct ResourceLinks: Codable {
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
itself = try container.decodeIfPresent(String.self, forKey: .itself)
let container = try decoder.container(keyedBy: AnyCodingKey.self)
itself = try container.decodeIfPresent(String.self, forKey: "self")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(itself, forKey: .itself)
}

private enum CodingKeys: String, CodingKey {
case itself = "self"
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encodeIfPresent(itself, forKey: "self")
}
}
47 changes: 29 additions & 18 deletions Sources/Bagbutik-Models/AppStore/Actor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,21 @@ public struct Actor: Codable, Identifiable {
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
links = try container.decodeIfPresent(ResourceLinks.self, forKey: .links)
attributes = try container.decodeIfPresent(Attributes.self, forKey: .attributes)
if try container.decode(String.self, forKey: .type) != type {
throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Not matching \(type)")
let container = try decoder.container(keyedBy: AnyCodingKey.self)
id = try container.decode(String.self, forKey: "id")
links = try container.decodeIfPresent(ResourceLinks.self, forKey: "links")
attributes = try container.decodeIfPresent(Attributes.self, forKey: "attributes")
if try container.decode(String.self, forKey: "type") != type {
throw DecodingError.dataCorruptedError(forKey: "type", in: container, debugDescription: "Not matching \(type)")
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encodeIfPresent(links, forKey: .links)
try container.encode(type, forKey: .type)
try container.encodeIfPresent(attributes, forKey: .attributes)
}

private enum CodingKeys: String, CodingKey {
case attributes
case id
case links
case type
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(id, forKey: "id")
try container.encodeIfPresent(links, forKey: "links")
try container.encode(type, forKey: "type")
try container.encodeIfPresent(attributes, forKey: "attributes")
}

public struct Attributes: Codable {
Expand All @@ -61,6 +54,24 @@ public struct Actor: Codable, Identifiable {
self.userLastName = userLastName
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
actorType = try container.decodeIfPresent(ActorType.self, forKey: "actorType")
apiKeyId = try container.decodeIfPresent(String.self, forKey: "apiKeyId")
userEmail = try container.decodeIfPresent(String.self, forKey: "userEmail")
userFirstName = try container.decodeIfPresent(String.self, forKey: "userFirstName")
userLastName = try container.decodeIfPresent(String.self, forKey: "userLastName")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encodeIfPresent(actorType, forKey: "actorType")
try container.encodeIfPresent(apiKeyId, forKey: "apiKeyId")
try container.encodeIfPresent(userEmail, forKey: "userEmail")
try container.encodeIfPresent(userFirstName, forKey: "userFirstName")
try container.encodeIfPresent(userLastName, forKey: "userLastName")
}

public enum ActorType: String, Codable, CaseIterable {
case apiKey = "API_KEY"
case apple = "APPLE"
Expand Down
12 changes: 12 additions & 0 deletions Sources/Bagbutik-Models/AppStore/ActorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ public struct ActorResponse: Codable {
self.data = data
self.links = links
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
data = try container.decode(Actor.self, forKey: "data")
links = try container.decode(DocumentLinks.self, forKey: "links")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(data, forKey: "data")
try container.encode(links, forKey: "links")
}
}
14 changes: 14 additions & 0 deletions Sources/Bagbutik-Models/AppStore/ActorsResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@ public struct ActorsResponse: Codable, PagedResponse {
self.links = links
self.meta = meta
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: AnyCodingKey.self)
data = try container.decode([Actor].self, forKey: "data")
links = try container.decode(PagedDocumentLinks.self, forKey: "links")
meta = try container.decodeIfPresent(PagingInformation.self, forKey: "meta")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(data, forKey: "data")
try container.encode(links, forKey: "links")
try container.encodeIfPresent(meta, forKey: "meta")
}
}
Loading

0 comments on commit 5f3c9e9

Please sign in to comment.