mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user