Skip to content

Commit 6ac61b2

Browse files
committed
Simplify type logic
1 parent fec6d41 commit 6ac61b2

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

Sources/RouteDocs/EndpointDocumentation.swift

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
import Vapor
22

3-
fileprivate protocol _OptionalCustomDocumentationNamed {
4-
static var _documentationNameType: Any.Type { get }
5-
}
6-
7-
extension Optional: _OptionalCustomDocumentationNamed {
8-
static var _documentationNameType: Any.Type {
9-
(Wrapped.self as? _OptionalCustomDocumentationNamed.Type)?._documentationNameType ?? Wrapped.self
10-
}
11-
}
12-
133
public struct DocumentationType: Codable, Equatable, CustomStringConvertible, Sendable {
144
public let typeDescription: TypeDescription
155
public let customName: String?
@@ -20,21 +10,10 @@ public struct DocumentationType: Codable, Equatable, CustomStringConvertible, Se
2010

2111
public var description: String { defaultName }
2212

23-
init(_ type: Any.Type) {
24-
typeDescription = .init(any: type)
25-
let docNameType = (type as? _OptionalCustomDocumentationNamed.Type)?._documentationNameType ?? type
26-
customName = (docNameType as? CustomDocumentationNamed.Type)?.documentationName
27-
}
28-
29-
init<T>(_ type: T.Type) {
30-
typeDescription = .init(type)
31-
let docNameType = (type as? _OptionalCustomDocumentationNamed.Type)?._documentationNameType ?? type
32-
customName = (docNameType as? CustomDocumentationNamed.Type)?.documentationName
33-
}
34-
35-
init<T: CustomDocumentationNamed>(_ type: T.Type) {
36-
typeDescription = .init(type)
37-
customName = type.documentationName
13+
fileprivate init(parsing type: Any.Type) {
14+
let actualType = _leafType(of: _openOptionals(in: type))
15+
typeDescription = .init(any: actualType)
16+
customName = (actualType as? CustomDocumentationNamed.Type)?.documentationName
3817
}
3918
}
4019

@@ -294,8 +273,8 @@ extension EndpointDocumentation.Object {
294273
}
295274

296275
private init(documentation: DocumentationObject) {
297-
let actualType = (documentation.type as? AnyTypeWrapping.Type)?.leafType ?? documentation.type
298-
self.init(type: DocumentationType(actualType), body: .init(documentation: documentation.body))
276+
self.init(type: DocumentationType(parsing: documentation.type),
277+
body: .init(documentation: documentation.body))
299278
}
300279
}
301280

@@ -313,9 +292,9 @@ extension EndpointDocumentation.Object.Body {
313292

314293
extension EndpointDocumentation.Object.Body.Field {
315294
fileprivate init(name: String, documentation: DocumentationObject) {
316-
let optionalCleanedType = (documentation.type as? AnyOptionalType.Type)?.anyWrappedType ?? documentation.type
317-
let leafType = (optionalCleanedType as? AnyTypeWrapping.Type)?.leafType ?? optionalCleanedType
318-
self.init(name: name, type: DocumentationType(leafType), isOptional: documentation.isOptional)
295+
self.init(name: name,
296+
type: DocumentationType(parsing: documentation.type),
297+
isOptional: documentation.isOptional)
319298
}
320299
}
321300

@@ -331,3 +310,11 @@ fileprivate extension RangeReplaceableCollection where Element: Equatable {
331310
append(element)
332311
}
333312
}
313+
314+
fileprivate func _openOptionals(in type: Any.Type) -> Any.Type {
315+
(type as? AnyOptionalType.Type).map { _openOptionals(in: $0.anyWrappedType) } ?? type
316+
}
317+
318+
fileprivate func _leafType(of type: Any.Type) -> Any.Type {
319+
(type as? AnyTypeWrapping.Type)?.leafType ?? type
320+
}

0 commit comments

Comments
 (0)