|
| 1 | +/// A utility enum that models values which may appear as either a single item or an array of items, |
| 2 | +/// commonly seen in JSON APIs that are not schema-consistent. |
| 3 | +/// |
| 4 | +/// `OneOrMany` normalizes access to such values while preserving the original shape when re-encoding. |
| 5 | +/// Use the `array` property to always work with `[T]`, regardless of whether the source was a single |
| 6 | +/// value or an array. |
| 7 | +/// |
| 8 | +/// - Generic Parameter: |
| 9 | +/// - T: The element type. Must conform to `RawCodable`. |
| 10 | +/// |
| 11 | +/// - Conforms To: |
| 12 | +/// - `RawCodable` (and, by extension, `Codable` if `RawCodable` refines it) |
| 13 | +/// |
| 14 | +/// - Decoding Behavior: |
| 15 | +/// - Attempts to decode an array `[T]` first. If that fails, falls back to decoding a single `T`. |
| 16 | +/// - This means that if both `[T]` and `T` could plausibly decode from the same payload, the array |
| 17 | +/// form wins. |
| 18 | +/// - `null` is not accepted; use `OneOrMany<T>?` if the field can be `null` or omitted. |
| 19 | +/// |
| 20 | +/// - Encoding Behavior: |
| 21 | +/// - Preserves shape: `.one` encodes as a single `T`; `.many` encodes as `[T]`. |
| 22 | +/// |
| 23 | +/// - Normalization: |
| 24 | +/// - Use `array` to always obtain `[T]` for iteration and higher-level logic. |
| 25 | +/// |
| 26 | +/// - Bridging: |
| 27 | +/// - The `raw` property exposes an `AnyCodable` representation of the wrapped value(s), satisfying |
| 28 | +/// `RawCodable` requirements. |
| 29 | +/// |
| 30 | +/// - Use Cases: |
| 31 | +/// - Fields like `"tag": "swift"` vs `"tag": ["swift", "ios"]` |
| 32 | +/// |
| 33 | +/// - Examples: |
| 34 | +/// ```swift |
| 35 | +/// struct Response: Codable { |
| 36 | +/// let tags: OneOrMany<Tag> |
| 37 | +/// } |
| 38 | +/// |
| 39 | +/// // JSON: { "tags": "swift" } |
| 40 | +/// // -> Response.tags == .one(Tag("swift")) |
| 41 | +/// // -> Response.tags.array == [Tag("swift")] |
| 42 | +/// |
| 43 | +/// // JSON: { "tags": ["swift", "ios"] } |
| 44 | +/// // -> Response.tags == .many([Tag("swift"), Tag("ios")]) |
| 45 | +/// // -> Response.tags.array == [Tag("swift"), Tag("ios")] |
| 46 | +/// |
| 47 | +/// // Encoding preserves shape: |
| 48 | +/// // OneOrMany.one(Tag("swift")) -> "swift" |
| 49 | +/// // OneOrMany.many([Tag("swift"), Tag("ios")]) -> ["swift", "ios"] |
| 50 | +/// ``` |
| 51 | +/// |
| 52 | +/// - See Also: |
| 53 | +/// - `RawCodable` |
| 54 | +/// - `AnyCodable` |
| 55 | +/// |
| 56 | +/// |
| 57 | +/// Case: `.one` |
| 58 | +/// Wraps a single value of type `T`. |
| 59 | +/// |
| 60 | +/// |
| 61 | +/// Case: `.many` |
| 62 | +/// Wraps multiple values as an array `[T]`. |
| 63 | +/// |
| 64 | +/// |
| 65 | +/// Property: `array` |
| 66 | +/// A normalized view of the contents as `[T]`. |
| 67 | +/// - If the receiver is `.one(v)`, returns `[v]`. |
| 68 | +/// - If the receiver is `.many(vs)`, returns `vs` unchanged. |
| 69 | +/// |
| 70 | +/// |
| 71 | +/// Property: `raw` |
| 72 | +/// The `RawCodable` raw representation as `AnyCodable`, mirroring the underlying shape: |
| 73 | +/// - For `.one(v)`, returns `AnyCodable(v)`. |
| 74 | +/// - For `.many(vs)`, returns `AnyCodable(vs)`. |
| 75 | +/// |
| 76 | +/// |
| 77 | +/// Initializer: `init(from:)` |
| 78 | +/// Decodes the value from a single-value container, preferring arrays. |
| 79 | +/// - Parameters: |
| 80 | +/// - decoder: The decoder to read data from. |
| 81 | +/// - Throws: An error if neither `[T]` nor `T` can be decoded from the payload. |
| 82 | +/// |
| 83 | +/// |
| 84 | +/// Method: `encode(to:)` |
| 85 | +/// Encodes the value into a single-value container, preserving shape. |
| 86 | +/// - Parameters: |
| 87 | +/// - encoder: The encoder to write data to. |
| 88 | +/// - Throws: An error if encoding fails. |
| 89 | +public enum OneOrMany<T: RawCodable>: RawCodable { |
| 90 | + case one(T) |
| 91 | + case many([T]) |
| 92 | + |
| 93 | + /// A normalized view of the wrapped value(s) as an array. |
| 94 | + /// |
| 95 | + /// This property allows you to work with the contents uniformly as `[T]`, |
| 96 | + /// regardless of whether the underlying representation is a single value |
| 97 | + /// (`.one`) or an array (`.many`). |
| 98 | + /// |
| 99 | + /// Behavior: |
| 100 | + /// - If the receiver is `.one(value)`, returns `[value]`. |
| 101 | + /// - If the receiver is `.many(values)`, returns `values` unchanged. |
| 102 | + /// |
| 103 | + /// Notes: |
| 104 | + /// - Order and duplicates are preserved. |
| 105 | + /// - This is a computed convenience; accessing it does not mutate the enum. |
| 106 | + /// - If you need to preserve the original shape for re-encoding, use the enum |
| 107 | + /// cases directly; `array` is only for normalized access. |
| 108 | + /// |
| 109 | + /// Example: |
| 110 | + /// ```swift |
| 111 | + /// switch tags.array { |
| 112 | + /// case []: |
| 113 | + /// // no tags |
| 114 | + /// default: |
| 115 | + /// // iterate uniformly over tags |
| 116 | + /// for tag in tags.array { /* ... */ } |
| 117 | + /// } |
| 118 | + /// ``` |
| 119 | + public var array: [T] { |
| 120 | + switch self { |
| 121 | + case .one(let v): return [v] |
| 122 | + case .many(let v): return v |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public var raw: AnyCodable? { |
| 127 | + switch self { |
| 128 | + case .one(let v): return .init(v) |
| 129 | + case .many(let v): return .init(v) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public init(from decoder: Decoder) throws { |
| 134 | + let c = try decoder.singleValueContainer() |
| 135 | + |
| 136 | + if let many = try? c.decode([T].self) { |
| 137 | + self = .many(many) |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + self = .one(try c.decode(T.self)) |
| 142 | + } |
| 143 | + |
| 144 | + public func encode(to encoder: Encoder) throws { |
| 145 | + var c = encoder.singleValueContainer() |
| 146 | + switch self { |
| 147 | + case .one(let v): try c.encode(v) |
| 148 | + case .many(let v): try c.encode(v) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments