|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +/// A set of specialized types for using the recursion detector for |
| 16 | +/// declarations. |
| 17 | +struct DeclarationRecursionDetector { |
| 18 | + |
| 19 | + /// A node for a pair of a Swift type name and a corresponding declaration. |
| 20 | + struct Node: TypeNode, Equatable { |
| 21 | + |
| 22 | + /// The type of the name is a string. |
| 23 | + typealias NameType = String |
| 24 | + |
| 25 | + /// The name of the node. |
| 26 | + var name: NameType |
| 27 | + |
| 28 | + /// Whether the type can be boxed. |
| 29 | + var isBoxable: Bool |
| 30 | + |
| 31 | + /// The names of nodes pointed to by this node. |
| 32 | + var edges: [NameType] |
| 33 | + |
| 34 | + /// The declaration represented by this node. |
| 35 | + var decl: Declaration |
| 36 | + |
| 37 | + /// Creates a new node. |
| 38 | + /// - Parameters: |
| 39 | + /// - name: The name of the node. |
| 40 | + /// - isBoxable: Whether the type can be boxed. |
| 41 | + /// - edges: The names of nodes pointed to by this node. |
| 42 | + /// - decl: The declaration represented by this node. |
| 43 | + private init(name: NameType, isBoxable: Bool, edges: [NameType], decl: Declaration) { |
| 44 | + self.name = name |
| 45 | + self.isBoxable = isBoxable |
| 46 | + self.edges = edges |
| 47 | + self.decl = decl |
| 48 | + } |
| 49 | + |
| 50 | + /// Creates a new node from the provided declaration. |
| 51 | + /// |
| 52 | + /// Returns nil when the declaration is missing a name. |
| 53 | + /// - Parameter decl: A declaration. |
| 54 | + init?(_ decl: Declaration) { |
| 55 | + guard let name = decl.name else { |
| 56 | + return nil |
| 57 | + } |
| 58 | + let edges = decl.schemaComponentNamesOfUnbreakableReferences |
| 59 | + self.init( |
| 60 | + name: name, |
| 61 | + isBoxable: decl.isBoxable, |
| 62 | + edges: edges, |
| 63 | + decl: decl |
| 64 | + ) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// A container for declarations. |
| 69 | + struct Container: TypeNodeContainer { |
| 70 | + |
| 71 | + /// The type of the node. |
| 72 | + typealias Node = DeclarationRecursionDetector.Node |
| 73 | + |
| 74 | + /// An error thrown by the container. |
| 75 | + enum ContainerError: Swift.Error { |
| 76 | + |
| 77 | + /// The node for the provided name was not found. |
| 78 | + case nodeNotFound(Node.NameType) |
| 79 | + } |
| 80 | + |
| 81 | + /// The lookup map from the name to the node. |
| 82 | + var lookupMap: [String: Node] |
| 83 | + |
| 84 | + func lookup(_ name: String) throws -> DeclarationRecursionDetector.Node { |
| 85 | + guard let node = lookupMap[name] else { |
| 86 | + throw ContainerError.nodeNotFound(name) |
| 87 | + } |
| 88 | + return node |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +extension Declaration { |
| 94 | + |
| 95 | + /// A name of the declaration, if it has one. |
| 96 | + var name: String? { |
| 97 | + switch self { |
| 98 | + case .struct(let desc): |
| 99 | + return desc.name |
| 100 | + case .enum(let desc): |
| 101 | + return desc.name |
| 102 | + case .typealias(let desc): |
| 103 | + return desc.name |
| 104 | + case .commentable(_, let decl), .deprecated(_, let decl): |
| 105 | + return decl.name |
| 106 | + case .variable, .extension, .protocol, .function, .enumCase: |
| 107 | + return nil |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// A Boolean value representing whether this declaration can be boxed. |
| 112 | + var isBoxable: Bool { |
| 113 | + switch self { |
| 114 | + case .struct, .enum: |
| 115 | + return true |
| 116 | + case .commentable(_, let decl), .deprecated(_, let decl): |
| 117 | + return decl.isBoxable |
| 118 | + case .typealias, .variable, .extension, .protocol, .function, .enumCase: |
| 119 | + return false |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// An array of names that can be found in `#/components/schemas` in |
| 124 | + /// the OpenAPI document that represent references that can cause |
| 125 | + /// a reference cycle. |
| 126 | + var schemaComponentNamesOfUnbreakableReferences: [String] { |
| 127 | + switch self { |
| 128 | + case .struct(let desc): |
| 129 | + return desc |
| 130 | + .members |
| 131 | + .compactMap { (member) -> [String]? in |
| 132 | + switch member.strippingTopComment { |
| 133 | + case .variable, // A reference to a reusable type. |
| 134 | + .struct, .enum: // An inline type. |
| 135 | + return member.schemaComponentNamesOfUnbreakableReferences |
| 136 | + default: |
| 137 | + return nil |
| 138 | + } |
| 139 | + } |
| 140 | + .flatMap { $0 } |
| 141 | + case .enum(let desc): |
| 142 | + return desc |
| 143 | + .members |
| 144 | + .compactMap { (member) -> [String]? in |
| 145 | + guard case .enumCase = member.strippingTopComment else { |
| 146 | + return nil |
| 147 | + } |
| 148 | + return member |
| 149 | + .schemaComponentNamesOfUnbreakableReferences |
| 150 | + } |
| 151 | + .flatMap { $0 } |
| 152 | + case .commentable(_, let decl), .deprecated(_, let decl): |
| 153 | + return decl |
| 154 | + .schemaComponentNamesOfUnbreakableReferences |
| 155 | + case .typealias(let desc): |
| 156 | + return desc |
| 157 | + .existingType |
| 158 | + .referencedSchemaComponentName |
| 159 | + .map { [$0] } ?? [] |
| 160 | + case .variable(let desc): |
| 161 | + return desc.type?.referencedSchemaComponentName.map { [$0] } ?? [] |
| 162 | + case .enumCase(let desc): |
| 163 | + switch desc.kind { |
| 164 | + case .nameWithAssociatedValues(let values): |
| 165 | + return values.compactMap { $0.type.referencedSchemaComponentName } |
| 166 | + default: |
| 167 | + return [] |
| 168 | + } |
| 169 | + case .extension, .protocol, .function: |
| 170 | + return [] |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +fileprivate extension Array where Element == String { |
| 176 | + |
| 177 | + /// The name in the `Components.Schemas.` namespace. |
| 178 | + var nameIfTopLevelSchemaComponent: String? { |
| 179 | + let components = self |
| 180 | + guard |
| 181 | + components.count == 3, |
| 182 | + components.starts(with: Constants.Components.Schemas.components) |
| 183 | + else { |
| 184 | + return nil |
| 185 | + } |
| 186 | + return components[2] |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +extension ExistingTypeDescription { |
| 191 | + |
| 192 | + /// The name in the `Components.Schemas.` namespace, if the type can appear |
| 193 | + /// there. Nil otherwise. |
| 194 | + var referencedSchemaComponentName: String? { |
| 195 | + switch self { |
| 196 | + case .member(let components): |
| 197 | + return components.nameIfTopLevelSchemaComponent |
| 198 | + case .array(let desc), .dictionaryValue(let desc), .any(let desc), .optional(let desc): |
| 199 | + return desc.referencedSchemaComponentName |
| 200 | + case .generic: |
| 201 | + return nil |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments