forked from loafle/openapi-generator-original
add swift5 option for generating frozen enums (#11013)
* add swift5 option for generating frozen enums * use case unknownDefault to avoid conflicts * update comments to reflect unknownDefault case * set default values for unknown case to avoid conflict * dont need vendor extensions to detect enum raw data type * move CaseIterableDefaultsLast into models mustache template * comment catch all case and add support for other types * add frozen enums to ci pipeline * remove extraneous edit to extensions template * remove left over protocols files * small comment and case adjustments
This commit is contained in:
@@ -14,6 +14,28 @@ extension JSONEncodable {
|
||||
func encodeToJSON() -> Any { self }
|
||||
}
|
||||
|
||||
/// An enum where the last case value can be used as a default catch-all.
|
||||
protocol CaseIterableDefaultsLast: Decodable & CaseIterable & RawRepresentable
|
||||
where RawValue: Decodable, AllCases: BidirectionalCollection {}
|
||||
|
||||
extension CaseIterableDefaultsLast {
|
||||
/// Initializes an enum such that if a known raw value is found, then it is decoded.
|
||||
/// Otherwise the last case is used.
|
||||
/// - Parameter decoder: A decoder.
|
||||
public init(from decoder: Decoder) throws {
|
||||
if let value = try Self(rawValue: decoder.singleValueContainer().decode(RawValue.self)) {
|
||||
self = value
|
||||
} else if let lastValue = Self.allCases.last {
|
||||
self = lastValue
|
||||
} else {
|
||||
throw DecodingError.valueNotFound(
|
||||
Self.Type.self,
|
||||
.init(codingPath: decoder.codingPath, debugDescription: "CaseIterableDefaultsLast")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ErrorResponse: Error {
|
||||
case error(Int, Data?, URLResponse?, Error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user