Not crazy about how I got there, but now it is relatively easy to print arbitrary enum's allCases as the list of possible values in the format specced out by OpenAPI.

This commit is contained in:
Mathew Polzin
2019-01-20 15:39:54 -08:00
parent cf746e182f
commit dc42ec27fc
5 changed files with 77 additions and 92 deletions
+27 -27
View File
@@ -20,7 +20,7 @@ extension Sampleable {
let properties: [(String, JSONNode)] = try mirror.children.compactMap { child in
// see if we can enumerate the possible values
let maybeAllCases: [Any]? = {
let maybeAllCases: [AnyCodable]? = {
switch type(of: child.value) {
case let valType as AnyJSONCaseIterable.Type:
return valType.allCases
@@ -49,32 +49,32 @@ extension Sampleable {
let newNode: JSONNode?
if let allCases = maybeAllCases,
let openAPINode = maybeOpenAPINode {
newNode = try {
if let cases = allCases as? [JSONTypeFormat.BooleanFormat.SwiftType] {
return try openAPINode.with(allowedValues: cases)
} else if let cases = allCases as? [JSONTypeFormat.ArrayFormat.SwiftType] {
return try openAPINode.with(allowedValues: cases)
} else if let cases = allCases as? [JSONTypeFormat.ObjectFormat.SwiftType] {
return try openAPINode.with(allowedValues: cases)
} else if let cases = allCases as? [JSONTypeFormat.NumberFormat.SwiftType] {
return try openAPINode.with(allowedValues: cases)
} else if let cases = allCases as? [JSONTypeFormat.IntegerFormat.SwiftType] {
return try openAPINode.with(allowedValues: cases)
} else if let cases = allCases as? [JSONTypeFormat.StringFormat.SwiftType] {
return try openAPINode.with(allowedValues: cases)
} else if allCases.compactMap({ $0 as? RawStringRepresentable }).count == allCases.count {
return try openAPINode.with(allowedValues: allCases.compactMap { ($0 as? RawStringRepresentable)?.rawValue })
} else {
throw SampleableError.allowedValuesNotOfExpectedType(forNode: openAPINode, allowedValues: allCases)
}
}()
newNode = try openAPINode.with(allowedValues: allCases) // try {
// if let cases = allCases as? [JSONTypeFormat.BooleanFormat.SwiftType] {
// return try openAPINode.with(allowedValues: cases)
//
// } else if let cases = allCases as? [JSONTypeFormat.ArrayFormat.SwiftType] {
// return try openAPINode.with(allowedValues: cases)
//
// } else if let cases = allCases as? [JSONTypeFormat.ObjectFormat.SwiftType] {
// return try openAPINode.with(allowedValues: cases)
//
// } else if let cases = allCases as? [JSONTypeFormat.NumberFormat.SwiftType] {
// return try openAPINode.with(allowedValues: cases)
//
// } else if let cases = allCases as? [JSONTypeFormat.IntegerFormat.SwiftType] {
// return try openAPINode.with(allowedValues: cases)
//
// } else if let cases = allCases as? [JSONTypeFormat.StringFormat.SwiftType] {
// return try openAPINode.with(allowedValues: cases)
//
// } else if allCases.compactMap({ $0 as? RawStringRepresentable }).count == allCases.count {
// return try openAPINode.with(allowedValues: allCases.compactMap { ($0 as? RawStringRepresentable)?.rawValue })
//
// } else {
// throw SampleableError.allowedValuesNotOfExpectedType(forNode: openAPINode, allowedValues: allCases)
// }
// }()
} else {
newNode = maybeOpenAPINode
}