Several fixes and optimizations all over the library #28
@@ -27,6 +27,6 @@ protocol KeyNameModel: Sendable, Hashable {
|
|||||||
|
|
||||||
/// Initializes this model from a given payload.
|
/// Initializes this model from a given payload.
|
||||||
/// - Parameter payload: A payload that contains the values for the model.
|
/// - Parameter payload: A payload that contains the values for the model.
|
||||||
init(_ payload: Components.Schemas.Tuple)
|
init(_ payload: some KeyNamePayload)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {}
|
||||||
@@ -191,9 +191,9 @@ private extension AmiiboLiveClient {
|
|||||||
case let .json(output):
|
case let .json(output):
|
||||||
switch output.amiibo {
|
switch output.amiibo {
|
||||||
case let .AmiiboSeries(payload):
|
case let .AmiiboSeries(payload):
|
||||||
return makeModels(from: [payload.value1])
|
return makeModels(from: [payload])
|
||||||
case let .case2(list):
|
case let .case2(list):
|
||||||
return makeModels(from: list.map(\.value1))
|
return makeModels(from: list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case .badRequest:
|
case .badRequest:
|
||||||
@@ -227,9 +227,9 @@ private extension AmiiboLiveClient {
|
|||||||
case let .json(output):
|
case let .json(output):
|
||||||
switch output.amiibo {
|
switch output.amiibo {
|
||||||
case let .AmiiboType(payload):
|
case let .AmiiboType(payload):
|
||||||
return makeModels(from: [payload.value1])
|
return makeModels(from: [payload])
|
||||||
case let .case2(list):
|
case let .case2(list):
|
||||||
return makeModels(from: list.map(\.value1))
|
return makeModels(from: list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case .badRequest:
|
case .badRequest:
|
||||||
@@ -263,9 +263,9 @@ private extension AmiiboLiveClient {
|
|||||||
case let .json(output):
|
case let .json(output):
|
||||||
switch output.amiibo {
|
switch output.amiibo {
|
||||||
case let .GameCharacter(payload):
|
case let .GameCharacter(payload):
|
||||||
return makeModels(from: [payload.value1])
|
return makeModels(from: [payload])
|
||||||
case let .case2(list):
|
case let .case2(list):
|
||||||
return makeModels(from: list.map(\.value1))
|
return makeModels(from: list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case .badRequest:
|
case .badRequest:
|
||||||
@@ -299,9 +299,9 @@ private extension AmiiboLiveClient {
|
|||||||
case let .json(output):
|
case let .json(output):
|
||||||
switch output.amiibo {
|
switch output.amiibo {
|
||||||
case let .GameSeries(payload):
|
case let .GameSeries(payload):
|
||||||
return makeModels(from: [payload.value1])
|
return makeModels(from: [payload])
|
||||||
case let .case2(list):
|
case let .case2(list):
|
||||||
return makeModels(from: list.map(\.value1))
|
return makeModels(from: list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case .badRequest:
|
case .badRequest:
|
||||||
@@ -350,11 +350,11 @@ private extension AmiiboLiveClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Maps a list of key-name tuples into a sorted list of models.
|
/// Maps a list of key-name payloads into a sorted list of models.
|
||||||
/// - Parameter tuples: A list of tuples to map into models.
|
/// - Parameter payloads: A list of payloads to map into models.
|
||||||
/// - Returns: A list of models sorted by their keys in ascending order.
|
/// - Returns: A list of models sorted by their keys in ascending order.
|
||||||
func makeModels<Model: KeyNameModel>(from tuples: [Components.Schemas.Tuple]) -> [Model] {
|
func makeModels<Model: KeyNameModel>(from payloads: [some KeyNamePayload]) -> [Model] {
|
||||||
tuples
|
payloads
|
||||||
.map { Model($0) }
|
.map { Model($0) }
|
||||||
.sorted { $0.key < $1.key }
|
.sorted { $0.key < $1.key }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public struct AmiiboSeries: KeyNameModel {
|
|||||||
|
|
||||||
/// Initializes this model from a given payload.
|
/// Initializes this model from a given payload.
|
||||||
/// - Parameter payload: A payload that contains the values for the model.
|
/// - 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.key = payload.key
|
||||||
self.name = payload.name
|
self.name = payload.name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public struct AmiiboType: KeyNameModel {
|
|||||||
|
|
||||||
/// Initializes this model from a given payload.
|
/// Initializes this model from a given payload.
|
||||||
/// - Parameter payload: A payload that contains the values for the model.
|
/// - 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.key = payload.key
|
||||||
self.name = payload.name
|
self.name = payload.name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public struct GameCharacter: KeyNameModel {
|
|||||||
|
|
||||||
/// Initializes this model from a given payload.
|
/// Initializes this model from a given payload.
|
||||||
/// - Parameter payload: A payload that contains the values for the model.
|
/// - 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.key = payload.key
|
||||||
self.name = payload.name
|
self.name = payload.name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public struct GameSeries: KeyNameModel {
|
|||||||
|
|
||||||
/// Initializes this model from a given payload.
|
/// Initializes this model from a given payload.
|
||||||
/// - Parameter payload: A payload that contains the values for the model.
|
/// - 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.key = payload.key
|
||||||
self.name = payload.name
|
self.name = payload.name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -439,24 +439,34 @@ components:
|
|||||||
format: date-time
|
format: date-time
|
||||||
AmiiboSeries:
|
AmiiboSeries:
|
||||||
description: A type that represents an amiibo series.
|
description: A type that represents an amiibo series.
|
||||||
allOf:
|
type: object
|
||||||
- $ref: '#/components/schemas/Tuple'
|
|
||||||
- type: object
|
|
||||||
properties:
|
properties:
|
||||||
key:
|
key:
|
||||||
description: The hexadecimal key that uniquely identifies this amiibo series.
|
description: The hexadecimal key that uniquely identifies this amiibo series.
|
||||||
|
type: string
|
||||||
|
pattern: "^0x[0-9a-fA-F]+$"
|
||||||
|
minLength: 3
|
||||||
name:
|
name:
|
||||||
description: The name of this amiibo series.
|
description: The name of this amiibo series.
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- key
|
||||||
|
- name
|
||||||
AmiiboType:
|
AmiiboType:
|
||||||
description: A type that represents an amiibo type (e.g., Figure, Card, Yarn, Band).
|
description: A type that represents an amiibo type (e.g., Figure, Card, Yarn, Band).
|
||||||
allOf:
|
type: object
|
||||||
- $ref: '#/components/schemas/Tuple'
|
|
||||||
- type: object
|
|
||||||
properties:
|
properties:
|
||||||
key:
|
key:
|
||||||
description: The hexadecimal key that uniquely identifies this amiibo type.
|
description: The hexadecimal key that uniquely identifies this amiibo type.
|
||||||
|
type: string
|
||||||
|
pattern: "^0x[0-9a-fA-F]+$"
|
||||||
|
minLength: 3
|
||||||
name:
|
name:
|
||||||
description: The name of this amiibo type.
|
description: The name of this amiibo type.
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- key
|
||||||
|
- name
|
||||||
AmiiboUsage:
|
AmiiboUsage:
|
||||||
description: A type that represents how an amiibo is used within a game.
|
description: A type that represents how an amiibo is used within a game.
|
||||||
type: object
|
type: object
|
||||||
@@ -472,24 +482,34 @@ components:
|
|||||||
- write
|
- write
|
||||||
GameCharacter:
|
GameCharacter:
|
||||||
description: A type that represents a game character.
|
description: A type that represents a game character.
|
||||||
allOf:
|
type: object
|
||||||
- $ref: '#/components/schemas/Tuple'
|
|
||||||
- type: object
|
|
||||||
properties:
|
properties:
|
||||||
key:
|
key:
|
||||||
description: The hexadecimal key that uniquely identifies this game character.
|
description: The hexadecimal key that uniquely identifies this game character.
|
||||||
|
type: string
|
||||||
|
pattern: "^0x[0-9a-fA-F]+$"
|
||||||
|
minLength: 3
|
||||||
name:
|
name:
|
||||||
description: The name of this game character.
|
description: The name of this game character.
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- key
|
||||||
|
- name
|
||||||
GameSeries:
|
GameSeries:
|
||||||
description: A type that represents a game series.
|
description: A type that represents a game series.
|
||||||
allOf:
|
type: object
|
||||||
- $ref: '#/components/schemas/Tuple'
|
|
||||||
- type: object
|
|
||||||
properties:
|
properties:
|
||||||
key:
|
key:
|
||||||
description: The hexadecimal key that uniquely identifies this game series.
|
description: The hexadecimal key that uniquely identifies this game series.
|
||||||
|
type: string
|
||||||
|
pattern: "^0x[0-9a-fA-F]+$"
|
||||||
|
minLength: 3
|
||||||
name:
|
name:
|
||||||
description: The name of this game series.
|
description: The name of this game series.
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- key
|
||||||
|
- name
|
||||||
LastUpdated:
|
LastUpdated:
|
||||||
description: A type that contains the date and time when the service data was last updated.
|
description: A type that contains the date and time when the service data was last updated.
|
||||||
type: object
|
type: object
|
||||||
@@ -500,24 +520,6 @@ components:
|
|||||||
format: date-time
|
format: date-time
|
||||||
required:
|
required:
|
||||||
- lastUpdated
|
- 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
|
# Wrapper Entities
|
||||||
AmiiboWrapper:
|
AmiiboWrapper:
|
||||||
description: A response wrapper that contains zero, one, or more amiibos.
|
description: A response wrapper that contains zero, one, or more amiibos.
|
||||||
|
|||||||
Reference in New Issue
Block a user