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:
Jarrod Parkes
2021-12-04 00:05:51 -05:00
committed by GitHub
parent 6bc7390914
commit 15f45711a8
140 changed files with 8099 additions and 2 deletions

View File

@@ -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)
}