|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +/// The protocol that all generated `AcceptableContentType` enums conform to. |
| 16 | +public protocol AcceptableProtocol: RawRepresentable, Sendable, Hashable, CaseIterable where RawValue == String {} |
| 17 | + |
| 18 | +/// A quality value used to describe the order of priority in a comma-separated |
| 19 | +/// list of values, such as in the Accept header. |
| 20 | +public struct QualityValue: Sendable, Hashable { |
| 21 | + |
| 22 | + /// As the quality value only retains up to and including 3 decimal digits, |
| 23 | + /// we store it in terms of the thousands. |
| 24 | + /// |
| 25 | + /// This allows predictable equality comparisons and sorting. |
| 26 | + /// |
| 27 | + /// For example, 1000 thousands is the quality value of 1.0. |
| 28 | + private let thousands: UInt16 |
| 29 | + |
| 30 | + /// Returns a Boolean value indicating whether the quality value is |
| 31 | + /// at its default value 1.0. |
| 32 | + public var isDefault: Bool { |
| 33 | + thousands == 1000 |
| 34 | + } |
| 35 | + |
| 36 | + /// Creates a new quality value from the provided floating-point number. |
| 37 | + /// |
| 38 | + /// - Precondition: The value must be between 0.0 and 1.0, inclusive. |
| 39 | + public init(doubleValue: Double) { |
| 40 | + precondition( |
| 41 | + doubleValue >= 0.0 && doubleValue <= 1.0, |
| 42 | + "Provided quality number is out of range, must be between 0.0 and 1.0, inclusive." |
| 43 | + ) |
| 44 | + self.thousands = UInt16(doubleValue * 1000) |
| 45 | + } |
| 46 | + |
| 47 | + /// The value represented as a floating-point number between 0.0 and 1.0, inclusive. |
| 48 | + public var doubleValue: Double { |
| 49 | + Double(thousands) / 1000 |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +extension QualityValue: RawRepresentable { |
| 54 | + public init?(rawValue: String) { |
| 55 | + guard let doubleValue = Double(rawValue) else { |
| 56 | + return nil |
| 57 | + } |
| 58 | + self.init(doubleValue: doubleValue) |
| 59 | + } |
| 60 | + |
| 61 | + public var rawValue: String { |
| 62 | + String(format: "%0.3f", doubleValue) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +extension QualityValue: ExpressibleByIntegerLiteral { |
| 67 | + public init(integerLiteral value: UInt16) { |
| 68 | + precondition( |
| 69 | + value >= 0 && value <= 1, |
| 70 | + "Provided quality number is out of range, must be between 0 and 1, inclusive." |
| 71 | + ) |
| 72 | + self.thousands = value * 1000 |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +extension QualityValue: ExpressibleByFloatLiteral { |
| 77 | + public init(floatLiteral value: Double) { |
| 78 | + self.init(doubleValue: value) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +extension Array { |
| 83 | + |
| 84 | + /// Returns the default values for the acceptable type. |
| 85 | + public static func defaultValues<T: AcceptableProtocol>() -> [AcceptHeaderContentType<T>] |
| 86 | + where Element == AcceptHeaderContentType<T> { |
| 87 | + T.allCases.map { .init(contentType: $0) } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// A wrapper of an individual content type in the accept header. |
| 92 | +public struct AcceptHeaderContentType<ContentType: AcceptableProtocol>: Sendable, Hashable { |
| 93 | + |
| 94 | + /// The value representing the content type. |
| 95 | + public var contentType: ContentType |
| 96 | + |
| 97 | + /// The quality value of this content type. |
| 98 | + /// |
| 99 | + /// Used to describe the order of priority in a comma-separated |
| 100 | + /// list of values. |
| 101 | + /// |
| 102 | + /// Content types with a higher priority should be preferred by the server |
| 103 | + /// when deciding which content type to use in the response. |
| 104 | + /// |
| 105 | + /// Also called the "q-factor" or "q-value". |
| 106 | + public var quality: QualityValue |
| 107 | + |
| 108 | + /// Creates a new content type from the provided parameters. |
| 109 | + /// - Parameters: |
| 110 | + /// - value: The value representing the content type. |
| 111 | + /// - quality: The quality of the content type, between 0.0 and 1.0. |
| 112 | + /// - Precondition: Quality must be in the range 0.0 and 1.0 inclusive. |
| 113 | + public init(contentType: ContentType, quality: QualityValue = 1.0) { |
| 114 | + self.quality = quality |
| 115 | + self.contentType = contentType |
| 116 | + } |
| 117 | + |
| 118 | + /// Returns the default set of acceptable content types for this type, in |
| 119 | + /// the order specified in the OpenAPI document. |
| 120 | + public static var defaultValues: [Self] { |
| 121 | + ContentType.allCases.map { .init(contentType: $0) } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +extension AcceptHeaderContentType: RawRepresentable { |
| 126 | + public init?(rawValue: String) { |
| 127 | + guard let validMimeType = OpenAPIMIMEType(rawValue) else { |
| 128 | + // Invalid MIME type. |
| 129 | + return nil |
| 130 | + } |
| 131 | + let quality: QualityValue |
| 132 | + if let rawQuality = validMimeType.parameters["q"] { |
| 133 | + guard let parsedQuality = QualityValue(rawValue: rawQuality) else { |
| 134 | + // Invalid quality parameter. |
| 135 | + return nil |
| 136 | + } |
| 137 | + quality = parsedQuality |
| 138 | + } else { |
| 139 | + quality = 1.0 |
| 140 | + } |
| 141 | + guard let typeAndSubtype = ContentType(rawValue: validMimeType.kind.description.lowercased()) else { |
| 142 | + // Invalid type/subtype. |
| 143 | + return nil |
| 144 | + } |
| 145 | + self.init(contentType: typeAndSubtype, quality: quality) |
| 146 | + } |
| 147 | + |
| 148 | + public var rawValue: String { |
| 149 | + contentType.rawValue + (quality.isDefault ? "" : "; q=\(quality.rawValue)") |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +extension Array { |
| 154 | + |
| 155 | + /// Returns the array sorted by the quality value, highest quality first. |
| 156 | + public func sortedByQuality<T: AcceptableProtocol>() -> [AcceptHeaderContentType<T>] |
| 157 | + where Element == AcceptHeaderContentType<T> { |
| 158 | + sorted { a, b in |
| 159 | + a.quality.doubleValue > b.quality.doubleValue |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments