diff --git a/Sources/AmiiboService/Internal/Protocols/KeyNameModel.swift b/Sources/AmiiboService/Internal/Protocols/KeyNameModel.swift index 8e03692..bee8866 100644 --- a/Sources/AmiiboService/Internal/Protocols/KeyNameModel.swift +++ b/Sources/AmiiboService/Internal/Protocols/KeyNameModel.swift @@ -27,6 +27,6 @@ protocol KeyNameModel: Sendable, Hashable { /// Initializes this model from a given payload. /// - Parameter payload: A payload that contains the values for the model. - init(_ payload: Components.Schemas.Tuple) + init(_ payload: some KeyNamePayload) } diff --git a/Sources/AmiiboService/Internal/Protocols/KeyNamePayload.swift b/Sources/AmiiboService/Internal/Protocols/KeyNamePayload.swift new file mode 100644 index 0000000..78b1ced --- /dev/null +++ b/Sources/AmiiboService/Internal/Protocols/KeyNamePayload.swift @@ -0,0 +1,33 @@ +// ===----------------------------------------------------------------------=== +// +// This source file is part of the Amiibo Service open source project +// +// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors +// Licensed under Apache license v2.0 +// +// See LICENSE for license information +// See CONTRIBUTORS for the list of Amiibo Service project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +// ===----------------------------------------------------------------------=== + +/// A protocol that unifies the generated payload types containing a `key` and `name` pair. +protocol KeyNamePayload { + + // MARK: Properties + + /// A hexadecimal key that uniquely identifies this payload. + var key: String { get } + + /// A display name for this payload. + var name: String { get } + +} + +// MARK: - Conformances + +extension Components.Schemas.AmiiboSeries: KeyNamePayload {} +extension Components.Schemas.AmiiboType: KeyNamePayload {} +extension Components.Schemas.GameCharacter: KeyNamePayload {} +extension Components.Schemas.GameSeries: KeyNamePayload {} diff --git a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift index 79e167d..b26627f 100644 --- a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift +++ b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift @@ -191,9 +191,9 @@ private extension AmiiboLiveClient { case let .json(output): switch output.amiibo { case let .AmiiboSeries(payload): - return makeModels(from: [payload.value1]) + return makeModels(from: [payload]) case let .case2(list): - return makeModels(from: list.map(\.value1)) + return makeModels(from: list) } } case .badRequest: @@ -227,9 +227,9 @@ private extension AmiiboLiveClient { case let .json(output): switch output.amiibo { case let .AmiiboType(payload): - return makeModels(from: [payload.value1]) + return makeModels(from: [payload]) case let .case2(list): - return makeModels(from: list.map(\.value1)) + return makeModels(from: list) } } case .badRequest: @@ -263,9 +263,9 @@ private extension AmiiboLiveClient { case let .json(output): switch output.amiibo { case let .GameCharacter(payload): - return makeModels(from: [payload.value1]) + return makeModels(from: [payload]) case let .case2(list): - return makeModels(from: list.map(\.value1)) + return makeModels(from: list) } } case .badRequest: @@ -299,9 +299,9 @@ private extension AmiiboLiveClient { case let .json(output): switch output.amiibo { case let .GameSeries(payload): - return makeModels(from: [payload.value1]) + return makeModels(from: [payload]) case let .case2(list): - return makeModels(from: list.map(\.value1)) + return makeModels(from: list) } } case .badRequest: @@ -350,11 +350,11 @@ private extension AmiiboLiveClient { } } - /// Maps a list of key-name tuples into a sorted list of models. - /// - Parameter tuples: A list of tuples to map into models. + /// Maps a list of key-name payloads into a sorted list of models. + /// - Parameter payloads: A list of payloads to map into models. /// - Returns: A list of models sorted by their keys in ascending order. - func makeModels(from tuples: [Components.Schemas.Tuple]) -> [Model] { - tuples + func makeModels(from payloads: [some KeyNamePayload]) -> [Model] { + payloads .map { Model($0) } .sorted { $0.key < $1.key } } diff --git a/Sources/AmiiboService/Public/Models/AmiiboSeries.swift b/Sources/AmiiboService/Public/Models/AmiiboSeries.swift index 2e4d0a5..27128ee 100644 --- a/Sources/AmiiboService/Public/Models/AmiiboSeries.swift +++ b/Sources/AmiiboService/Public/Models/AmiiboSeries.swift @@ -27,7 +27,7 @@ public struct AmiiboSeries: KeyNameModel { /// Initializes this model from a given payload. /// - Parameter payload: A payload that contains the values for the model. - init(_ payload: Components.Schemas.Tuple) { + init(_ payload: some KeyNamePayload) { self.key = payload.key self.name = payload.name } diff --git a/Sources/AmiiboService/Public/Models/AmiiboType.swift b/Sources/AmiiboService/Public/Models/AmiiboType.swift index 2ea8bfe..f2d1a38 100644 --- a/Sources/AmiiboService/Public/Models/AmiiboType.swift +++ b/Sources/AmiiboService/Public/Models/AmiiboType.swift @@ -27,7 +27,7 @@ public struct AmiiboType: KeyNameModel { /// Initializes this model from a given payload. /// - Parameter payload: A payload that contains the values for the model. - init(_ payload: Components.Schemas.Tuple) { + init(_ payload: some KeyNamePayload) { self.key = payload.key self.name = payload.name } diff --git a/Sources/AmiiboService/Public/Models/GameCharacter.swift b/Sources/AmiiboService/Public/Models/GameCharacter.swift index c323c60..e3c2020 100644 --- a/Sources/AmiiboService/Public/Models/GameCharacter.swift +++ b/Sources/AmiiboService/Public/Models/GameCharacter.swift @@ -27,7 +27,7 @@ public struct GameCharacter: KeyNameModel { /// Initializes this model from a given payload. /// - Parameter payload: A payload that contains the values for the model. - init(_ payload: Components.Schemas.Tuple) { + init(_ payload: some KeyNamePayload) { self.key = payload.key self.name = payload.name } diff --git a/Sources/AmiiboService/Public/Models/GameSeries.swift b/Sources/AmiiboService/Public/Models/GameSeries.swift index 6667f7d..120c5c7 100644 --- a/Sources/AmiiboService/Public/Models/GameSeries.swift +++ b/Sources/AmiiboService/Public/Models/GameSeries.swift @@ -27,7 +27,7 @@ public struct GameSeries: KeyNameModel { /// Initializes this model from a given payload. /// - Parameter payload: A payload that contains the values for the model. - init(_ payload: Components.Schemas.Tuple) { + init(_ payload: some KeyNamePayload) { self.key = payload.key self.name = payload.name } diff --git a/Sources/AmiiboService/openapi.yaml b/Sources/AmiiboService/openapi.yaml index e790eec..9b04f34 100644 --- a/Sources/AmiiboService/openapi.yaml +++ b/Sources/AmiiboService/openapi.yaml @@ -439,24 +439,34 @@ components: format: date-time AmiiboSeries: description: A type that represents an amiibo series. - allOf: - - $ref: '#/components/schemas/Tuple' - - type: object - properties: - key: - description: The hexadecimal key that uniquely identifies this amiibo series. - name: - description: The name of this amiibo series. + type: object + properties: + key: + description: The hexadecimal key that uniquely identifies this amiibo series. + type: string + pattern: "^0x[0-9a-fA-F]+$" + minLength: 3 + name: + description: The name of this amiibo series. + type: string + required: + - key + - name AmiiboType: description: A type that represents an amiibo type (e.g., Figure, Card, Yarn, Band). - allOf: - - $ref: '#/components/schemas/Tuple' - - type: object - properties: - key: - description: The hexadecimal key that uniquely identifies this amiibo type. - name: - description: The name of this amiibo type. + type: object + properties: + key: + description: The hexadecimal key that uniquely identifies this amiibo type. + type: string + pattern: "^0x[0-9a-fA-F]+$" + minLength: 3 + name: + description: The name of this amiibo type. + type: string + required: + - key + - name AmiiboUsage: description: A type that represents how an amiibo is used within a game. type: object @@ -472,24 +482,34 @@ components: - write GameCharacter: description: A type that represents a game character. - allOf: - - $ref: '#/components/schemas/Tuple' - - type: object - properties: - key: - description: The hexadecimal key that uniquely identifies this game character. - name: - description: The name of this game character. + type: object + properties: + key: + description: The hexadecimal key that uniquely identifies this game character. + type: string + pattern: "^0x[0-9a-fA-F]+$" + minLength: 3 + name: + description: The name of this game character. + type: string + required: + - key + - name GameSeries: description: A type that represents a game series. - allOf: - - $ref: '#/components/schemas/Tuple' - - type: object - properties: - key: - description: The hexadecimal key that uniquely identifies this game series. - name: - description: The name of this game series. + type: object + properties: + key: + description: The hexadecimal key that uniquely identifies this game series. + type: string + pattern: "^0x[0-9a-fA-F]+$" + minLength: 3 + name: + description: The name of this game series. + type: string + required: + - key + - name LastUpdated: description: A type that contains the date and time when the service data was last updated. type: object @@ -500,24 +520,6 @@ components: format: date-time required: - lastUpdated - Tuple: - description: | - A base type composed of a `key` and `name` pair. - - This type is the base schema for the `AmiiboSeries`, `AmiiboType`, `GameCharacter`, and `GameSeries` types. - type: object - properties: - key: - description: A hexadecimal key that uniquely identifies this resource. - type: string - pattern: "^0x[0-9a-fA-F]+$" - minLength: 3 - name: - description: A display name for this resource. - type: string - required: - - key - - name # Wrapper Entities AmiiboWrapper: description: A response wrapper that contains zero, one, or more amiibos.