Skip to content

Commit

Permalink
Update packages, fix crash with special types
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Aug 15, 2023
1 parent 38d72ee commit fed20e2
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 40 deletions.
52 changes: 26 additions & 26 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "864c8d9e0ead5de7ba70b61c8982f89126710863",
"version" : "1.15.0"
"revision" : "78db67e5bf4a8543075787f228e8920097319281",
"version" : "1.18.0"
}
},
{
"identity" : "async-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/async-kit.git",
"state" : {
"revision" : "9acea4c92f51a5885c149904f0d11db4712dda80",
"version" : "1.16.0"
"revision" : "eab9edff78e8ace20bd7cb6e792ab46d54f59ab9",
"version" : "1.18.0"
}
},
{
Expand Down Expand Up @@ -50,8 +50,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/multipart-kit.git",
"state" : {
"revision" : "3a31859efeb054cdcd407fe152802ddc5c58bbce",
"version" : "4.5.3"
"revision" : "1adfd69df2da08f7931d4281b257475e32c96734",
"version" : "4.5.4"
}
},
{
Expand Down Expand Up @@ -104,71 +104,71 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "da0fe44138ab86e380f40a2acbd8a611b07d3f64",
"version" : "2.4.0"
"revision" : "60f13f60c4d093691934dc6cfdf5f508ada1f894",
"version" : "2.6.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
"version" : "1.5.2"
"revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed",
"version" : "1.5.3"
}
},
{
"identity" : "swift-metrics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-metrics.git",
"state" : {
"revision" : "e8bced74bc6d747745935e469f45d03f048d6cbd",
"version" : "2.3.4"
"revision" : "971ba26378ab69c43737ee7ba967a896cb74c0d1",
"version" : "2.4.1"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "9b2848d76f5caad08b97e71a04345aa5bdb23a06",
"version" : "2.49.0"
"revision" : "cf281631ff10ec6111f2761052aa81896a83a007",
"version" : "2.58.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "cc1e5275079380c859417dbea8588531f1a90ec3",
"version" : "1.18.0"
"revision" : "0e0d0aab665ff1a0659ce75ac003081f2b1c8997",
"version" : "1.19.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "38feec96bcd929028939107684073554bf01abeb",
"version" : "1.25.2"
"revision" : "a8ccf13fa62775277a5d56844878c828bbb3be1a",
"version" : "1.27.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "4fb7ead803e38949eb1d6fabb849206a72c580f3",
"version" : "2.23.0"
"revision" : "e866a626e105042a6a72a870c88b4c531ba05f83",
"version" : "2.24.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "c0d9a144cfaec8d3d596aadde3039286a266c15c",
"version" : "1.15.0"
"revision" : "e7403c35ca6bb539a7ca353b91cc2d8ec0362d58",
"version" : "1.19.0"
}
},
{
Expand All @@ -185,17 +185,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/vapor.git",
"state" : {
"revision" : "ba1a308a58ba9d78c5100588d7d8b6e615a98248",
"version" : "4.75.0"
"revision" : "474d91b67db9515b5bf3f3558635955c3fe18a44",
"version" : "4.78.1"
}
},
{
"identity" : "websocket-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/websocket-kit.git",
"state" : {
"revision" : "2b8885974e8d9f522e787805000553f4f7cce8a0",
"version" : "2.7.0"
"revision" : "53fe0639a98903858d0196b699720decb42aee7b",
"version" : "2.14.0"
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions Sources/RouteDocs/DefaultDocsView/docs.leaf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<meta name="generator" content="Vapor" />
<meta name="description" content="API Docs" />
<title>API Docs</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected].0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous" />
<link href="https://cdn.jsdelivr.net/npm/[email protected].1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous" />
<style>
.content-container { margin-bottom: 76px; }
.footer { height: 76px; }
Expand All @@ -21,7 +21,7 @@
width: 1rem;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected].0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
</head>
<body>
<div class="container content-container pb-3">
Expand Down
12 changes: 6 additions & 6 deletions Sources/RouteDocs/DocsViewContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public struct DocsViewContext: Encodable, Sendable {
public let otherDocumentations: Array<Documentation>
}

fileprivate extension HTTPMethod {
var sortOrder: String {
extension HTTPMethod {
fileprivate var sortOrder: String {
switch self {
case .GET: return "1GET"
case .PUT: return "2PUT"
Expand All @@ -85,8 +85,8 @@ fileprivate extension HTTPMethod {
}
}

fileprivate extension DocumentationType {
func docsTypeName(using namePath: KeyPath<DocumentationType, String>?) -> String {
extension DocumentationType {
fileprivate func docsTypeName(using namePath: KeyPath<DocumentationType, String>?) -> String {
customName ?? namePath.map { self[keyPath: $0] } ?? typeDescription.typeName(with: [.withParents])
}
}
Expand Down Expand Up @@ -157,8 +157,8 @@ extension DocsViewContext.Documentation {
}
}

fileprivate extension Sequence where Element == EndpointDocumentation {
func contextDocumentation(orderedBy keyPath: KeyPath<Element, some Comparable>,
extension Sequence where Element == EndpointDocumentation {
fileprivate func contextDocumentation(orderedBy keyPath: KeyPath<Element, some Comparable>,
usingName namePath: KeyPath<DocumentationType, String>?) -> Array<DocsViewContext.Documentation> {
sorted { $0[keyPath: keyPath] < $1[keyPath: keyPath] }
.map { DocsViewContext.Documentation(documentation: $0, usingName: namePath) }
Expand Down
9 changes: 8 additions & 1 deletion Sources/RouteDocs/DocumentationDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import NIOConcurrencyHelpers
import Vapor

extension CodingUserInfoKey {
public static let isDocumentationDecoder = CodingUserInfoKey(rawValue: "IsDocumentationDecoder")!
public static let isDocumentationDecoder = CodingUserInfoKey(rawValue: "de.sersoft.route-docs.is-documentation-decoder")!
}

extension Decoder {
@inlinable
public var isDocumentationDecoder: Bool {
userInfo[.isDocumentationDecoder] as? Bool == true
}
}

public struct DocumentationObject: Hashable, CustomStringConvertible, Sendable {
Expand Down
27 changes: 24 additions & 3 deletions Sources/RouteDocs/TypeDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ extension TypeDescription.NameOptions {
public static let withParents = TypeDescription.NameOptions(rawValue: 1 << 1)
}

fileprivate struct TypeParser<Text: StringProtocol> {
fileprivate struct TypeParser<Text: StringProtocol> where Text.SubSequence: RangeReplaceableCollection {
@frozen
fileprivate enum SpecialTypes<T: StringProtocol> {
static var any: T { "Any" }
static var void: T { "Void" }
static var voidAsTuple: T { "()" }
}

private struct Context {
let name: Text.SubSequence
var generics = Array<TypeDescription>()
Expand Down Expand Up @@ -188,16 +195,20 @@ fileprivate struct TypeParser<Text: StringProtocol> {
}

private mutating func parseModule() -> (String, isExtension: Bool) {
guard peekSpecialType() == nil else { return ("Swift", false) }
if currentChar == "(" {
let prefix = seek(to: ")").dropFirst() // Drop past the opening bracket
seek(to: ":") // (...):MODULE.TYPENAME <- Move past the colon
return (String(prefix.dropPrefix("extension in ") ?? prefix), true)
} else {
return (String(seek(to: ".")), false)
}
return (String(seek(to: ".")), false)
}

private mutating func parseIdentifier() -> Text.SubSequence {
if let (index, specialType) = peekSpecialType() {
currentIndex = index
return specialType
}
if currentChar == "(" { // TODO: Where else can this occur?
// (unknown context ...)
seek(to: ".")
Expand All @@ -211,6 +222,16 @@ fileprivate struct TypeParser<Text: StringProtocol> {
}
}

private func peekSpecialType() -> (indexAfter: Text.Index, type: Text.SubSequence)? {
let index = remainder.firstIndex(where: ",>".contains) ?? remainder.endIndex
let identifier = remainder[..<index]
switch identifier {
case SpecialTypes.any, SpecialTypes.void: return (index, identifier)
case SpecialTypes.voidAsTuple: return (index, SpecialTypes.void)
default: return nil
}
}

@discardableResult
private mutating func seek(to char: Text.Element) -> Text.SubSequence {
guard let index = remainder.firstIndex(of: char)
Expand Down
26 changes: 24 additions & 2 deletions Tests/RouteDocsTests/TypeDescriptionTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import XCTest
@testable import RouteDocs

Expand All @@ -9,7 +10,7 @@ extension Dictionary {
final class TypeDescriptionTests: XCTestCase {
func testVariousTypes() {
let typeDesc1 = TypeDescription(Dictionary<String, Dictionary<String, Int>.SomeNestedType>.Index.self)
let typeDesc2 = TypeDescription(Dictionary<String, Dictionary<String, Int>.Index>.SomeOtherNestedType.self)
let typeDesc2 = TypeDescription(Dictionary<UUID, Dictionary<String, Int>.Index>.SomeOtherNestedType.self)
let expected1 = TypeDescription(
module: "Swift",
parent: .init(
Expand Down Expand Up @@ -39,7 +40,7 @@ final class TypeDescriptionTests: XCTestCase {
parent: nil,
name: "Dictionary",
genericParameters: [
.init(module: "Swift", parent: nil, name: "String", genericParameters: []),
.init(module: "Foundation", parent: nil, name: "UUID", genericParameters: []),
.init(
module: "Swift",
parent: .init(module: "Swift", parent: nil, name: "Dictionary", genericParameters: [
Expand All @@ -57,4 +58,25 @@ final class TypeDescriptionTests: XCTestCase {
XCTAssertEqual(typeDesc1, expected1)
XCTAssertEqual(typeDesc2, expected2)
}

func testSpecialTypes() {
let anyTypeDesc = TypeDescription(Any.self)
let anyExpected = TypeDescription(module: "Swift", parent: nil, name: "Any", genericParameters: [])
let voidTypeDesc = TypeDescription(Void.self)
let voidExpected = TypeDescription(module: "Swift", parent: nil, name: "Void", genericParameters: [])
let deeperAnyTypeDesc = TypeDescription(Dictionary<String, Array<Any>>.self)
let deeperAnyExpected = TypeDescription(module: "Swift", parent: nil, name: "Dictionary", genericParameters: [
.init(module: "Swift", parent: nil, name: "String", genericParameters: []),
.init(module: "Swift", parent: nil, name: "Array", genericParameters: [anyExpected]),
])
let deeperVoidTypeDesc = TypeDescription(Dictionary<UUID, Array<Void>>.self)
let deeperVoidExpected = TypeDescription(module: "Swift", parent: nil, name: "Dictionary", genericParameters: [
.init(module: "Foundation", parent: nil, name: "UUID", genericParameters: []),
.init(module: "Swift", parent: nil, name: "Array", genericParameters: [voidExpected]),
])
XCTAssertEqual(anyTypeDesc, anyExpected)
XCTAssertEqual(voidTypeDesc, voidExpected)
XCTAssertEqual(deeperAnyTypeDesc, deeperAnyExpected)
XCTAssertEqual(deeperVoidTypeDesc, deeperVoidExpected)
}
}

0 comments on commit fed20e2

Please sign in to comment.