|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +extension SymbolGraph { |
| 14 | + /// Enumeration that can hold either a single boolean, integer, floating point, or string value or a null value. |
| 15 | + /// |
| 16 | + /// This enumeration is used when a field within the symbol graph can hold a different value type |
| 17 | + /// depending on the context. One such case would be the default value of a dictionary property, whose |
| 18 | + /// value depends on the data type of the property (eg, a string or integer). |
| 19 | + public enum AnyScalar: Codable, Equatable { |
| 20 | + case null |
| 21 | + case boolean(Bool) |
| 22 | + case integer(Int) |
| 23 | + case float(Double) |
| 24 | + case string(String) |
| 25 | + |
| 26 | + // This empty-marker case is here because non-frozen enums are only available when Library |
| 27 | + // Evolution is enabled, which is not available to Swift Packages without unsafe flags |
| 28 | + // (rdar://78773361). This can be removed once that is available and applied to SymbolKit |
| 29 | + // (rdar://89033233). |
| 30 | + @available(*, deprecated, message: "this enum is nonfrozen and may be expanded in the future; please add a `default` case instead of matching this one") |
| 31 | + case _nonfrozenEnum_useDefaultCase |
| 32 | + |
| 33 | + public func encode(to encoder : Encoder) throws { |
| 34 | + var container = encoder.singleValueContainer() |
| 35 | + switch self { |
| 36 | + case .null: |
| 37 | + try container.encodeNil() |
| 38 | + case .boolean(let b): |
| 39 | + try container.encode(b) |
| 40 | + case .integer(let i): |
| 41 | + try container.encode(i) |
| 42 | + case .float(let d): |
| 43 | + try container.encode(d) |
| 44 | + case .string(let s): |
| 45 | + try container.encode(s) |
| 46 | + case ._nonfrozenEnum_useDefaultCase: |
| 47 | + fatalError("_nonfrozenEnum_useDefaultCase is not a supported case in AnyScalar") |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public init(from decoder: Decoder) throws { |
| 52 | + let singleValue = try decoder.singleValueContainer() |
| 53 | + if singleValue.decodeNil() { |
| 54 | + self = .null |
| 55 | + } else if let value = try? singleValue.decode(Swift.Bool.self) { |
| 56 | + self = .boolean(value) |
| 57 | + } else if let value = try? singleValue.decode(Swift.Int.self) { |
| 58 | + self = .integer(value) |
| 59 | + } else if let value = try? singleValue.decode(Swift.Double.self) { |
| 60 | + self = .float(value) |
| 61 | + } else if let value = try? singleValue.decode(Swift.String.self) { |
| 62 | + self = .string(value) |
| 63 | + } else { |
| 64 | + throw DecodingError.typeMismatch( |
| 65 | + AnyScalar.self, |
| 66 | + DecodingError.Context( |
| 67 | + codingPath: decoder.codingPath, |
| 68 | + debugDescription: "Unable to parse scalar value" |
| 69 | + ) |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Enumeration that can hold either an integer or floating point value. |
| 76 | + /// |
| 77 | + /// This enumeration is used when a field within the symbol graph can hold a different value type |
| 78 | + /// depending on the context. One such case would be the minimum value of a dictionary property, whose |
| 79 | + /// value depends on the numerical type of the property (eg, an integer or floating point). |
| 80 | + public enum AnyNumber: Codable, Equatable { |
| 81 | + case integer(Int) |
| 82 | + case float(Double) |
| 83 | + |
| 84 | + // This empty-marker case is here because non-frozen enums are only available when Library |
| 85 | + // Evolution is enabled, which is not available to Swift Packages without unsafe flags |
| 86 | + // (rdar://78773361). This can be removed once that is available and applied to SymbolKit |
| 87 | + // (rdar://89033233). |
| 88 | + @available(*, deprecated, message: "this enum is nonfrozen and may be expanded in the future; please add a `default` case instead of matching this one") |
| 89 | + case _nonfrozenEnum_useDefaultCase |
| 90 | + |
| 91 | + public func encode(to encoder : Encoder) throws { |
| 92 | + var container = encoder.singleValueContainer() |
| 93 | + switch self { |
| 94 | + case .integer(let i): |
| 95 | + try container.encode(i) |
| 96 | + case .float(let d): |
| 97 | + try container.encode(d) |
| 98 | + case ._nonfrozenEnum_useDefaultCase: |
| 99 | + fatalError("_nonfrozenEnum_useDefaultCase is not a supported case in AnyNumber") |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + public init(from decoder: Decoder) throws { |
| 105 | + let container = try decoder.singleValueContainer() |
| 106 | + if let value = try? container.decode(Swift.Int.self) { |
| 107 | + self = .integer(value) |
| 108 | + } else if let value = try? container.decode(Swift.Double.self) { |
| 109 | + self = .float(value) |
| 110 | + } else { |
| 111 | + throw DecodingError.typeMismatch(AnyScalar.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Unable to parse number value")) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +extension String { |
| 118 | + public init(_ value: SymbolGraph.AnyScalar) { |
| 119 | + switch value { |
| 120 | + case .null: |
| 121 | + self = "null" |
| 122 | + case .boolean(let b): |
| 123 | + self = String(b) |
| 124 | + case .integer(let i): |
| 125 | + self = String(i) |
| 126 | + case .float(let d): |
| 127 | + self = String(d) |
| 128 | + case .string(let s): |
| 129 | + self = s |
| 130 | + case ._nonfrozenEnum_useDefaultCase: |
| 131 | + fatalError("_nonfrozenEnum_useDefaultCase is not a supported case in AnyScalar") |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public init(_ value: SymbolGraph.AnyNumber) { |
| 136 | + switch value { |
| 137 | + case .integer(let i): |
| 138 | + self = String(i) |
| 139 | + case .float(let d): |
| 140 | + self = String(d) |
| 141 | + case ._nonfrozenEnum_useDefaultCase: |
| 142 | + fatalError("_nonfrozenEnum_useDefaultCase is not a supported case in AnyNumber") |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments