Skip to content

Commit ecd0000

Browse files
Run linter and fix Swift 5.9 compile issue
1 parent ca06171 commit ecd0000

File tree

4 files changed

+81
-82
lines changed

4 files changed

+81
-82
lines changed

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ enum Constants {
5656

5757
/// The prefix of each generated method name.
5858
static let propertyPrefix: String = "server"
59-
6059
/// The name of each generated static function.
6160
static let urlStaticFunc: String = "url"
6261

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServers.swift

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ extension TypesFileTranslator {
4747
deprecated: DeprecationDescription?,
4848
variableGenerators variables: [any ServerVariableGenerator]
4949
) -> Declaration {
50-
let name = deprecated == nil ? Constants.ServerURL.urlStaticFunc : "\(Constants.ServerURL.propertyPrefix)\(index + 1)"
50+
let name =
51+
deprecated == nil ? Constants.ServerURL.urlStaticFunc : "\(Constants.ServerURL.propertyPrefix)\(index + 1)"
5152
return .commentable(
5253
.functionComment(abstract: server.description, parameters: variables.map(\.functionComment)),
5354
.function(
@@ -68,12 +69,13 @@ extension TypesFileTranslator {
6869
.init(
6970
label: "variables",
7071
expression: .literal(.array(variables.map(\.initializer)))
71-
)
72+
),
7273
])
7374
)
7475
)
7576
]
76-
).deprecate(if: deprecated)
77+
)
78+
.deprecate(if: deprecated)
7779
)
7880
}
7981

@@ -88,12 +90,16 @@ extension TypesFileTranslator {
8890
/// - pathToReplacementSymbol: The Swift path of the symbol
8991
/// which has resulted in the deprecation of this symbol.
9092
/// - Returns: A static function declaration.
91-
func translateServerAsDeprecated(index: Int, server: OpenAPI.Server, renamedTo pathToReplacementSymbol: String) -> Declaration {
93+
func translateServerAsDeprecated(index: Int, server: OpenAPI.Server, renamedTo pathToReplacementSymbol: String)
94+
-> Declaration
95+
{
9296
let serverVariables = translateServerVariables(index: index, server: server, generateAsEnum: false)
93-
return translateServerStaticFunction(index: index,
94-
server: server,
95-
deprecated: DeprecationDescription(renamed: pathToReplacementSymbol),
96-
variableGenerators: serverVariables)
97+
return translateServerStaticFunction(
98+
index: index,
99+
server: server,
100+
deprecated: DeprecationDescription(renamed: pathToReplacementSymbol),
101+
variableGenerators: serverVariables
102+
)
97103
}
98104

99105
/// Returns a namespace (enum) declaration for a server defined in
@@ -112,13 +118,16 @@ extension TypesFileTranslator {
112118
/// - Returns: A static function declaration.
113119
func translateServer(index: Int, server: OpenAPI.Server) -> (pathToStaticFunction: String, decl: Declaration) {
114120
let serverVariables = translateServerVariables(index: index, server: server, generateAsEnum: true)
115-
let methodDecl = translateServerStaticFunction(index: index,
116-
server: server,
117-
deprecated: nil,
118-
variableGenerators: serverVariables)
119-
121+
let methodDecl = translateServerStaticFunction(
122+
index: index,
123+
server: server,
124+
deprecated: nil,
125+
variableGenerators: serverVariables
126+
)
120127
let namespaceName = "\(Constants.ServerURL.serverNamespacePrefix)\(index + 1)"
121-
let typeName = TypeName(swiftKeyPath: [Constants.ServerURL.namespace, namespaceName, Constants.ServerURL.urlStaticFunc])
128+
let typeName = TypeName(swiftKeyPath: [
129+
Constants.ServerURL.namespace, namespaceName, Constants.ServerURL.urlStaticFunc,
130+
])
122131
let decl = Declaration.commentable(
123132
server.description.map(Comment.doc(_:)),
124133
.enum(
@@ -137,15 +146,17 @@ extension TypesFileTranslator {
137146
/// - Returns: A declaration of an enum namespace of the server URLs type.
138147
func translateServers(_ servers: [OpenAPI.Server]) -> Declaration {
139148
var serverDecls: [Declaration] = []
140-
141149
for (index, server) in servers.enumerated() {
142150
let translatedServer = translateServer(index: index, server: server)
143151
serverDecls.append(contentsOf: [
144152
translatedServer.decl,
145-
translateServerAsDeprecated(index: index, server: server, renamedTo: translatedServer.pathToStaticFunction)
153+
translateServerAsDeprecated(
154+
index: index,
155+
server: server,
156+
renamedTo: translatedServer.pathToStaticFunction
157+
),
146158
])
147159
}
148-
149160
return .commentable(
150161
.doc("Server URLs defined in the OpenAPI document."),
151162
.enum(accessModifier: config.access, name: Constants.ServerURL.namespace, members: serverDecls)

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateServersVariables.swift

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313
//===----------------------------------------------------------------------===//
1414
import OpenAPIKit
1515

16+
/// Represents a server variable and the function of generation that should be applied.
17+
protocol ServerVariableGenerator {
18+
/// Returns the declaration (enum) that should be added to the server's namespace.
19+
/// If the server variable does not require any codegen then it should return `nil`.
20+
var declaration: Declaration? { get }
21+
22+
/// Returns the description of the parameter that will be used to define the variable
23+
/// in the static method for a given server.
24+
var parameter: ParameterDescription { get }
25+
26+
/// Returns an expression for the variable initializer that is used in the body of a server's
27+
/// static method by passing it along to the URL resolver.
28+
var initializer: Expression { get }
29+
30+
/// Returns the description of this variables documentation for the function comment of
31+
/// the server's static method.
32+
var functionComment: (name: String, comment: String?) { get }
33+
}
34+
1635
extension TypesFileTranslator {
1736
/// Returns a declaration of a namespace (enum) for a specific server and will define
1837
/// one enum member for each of the server's variables in the OpenAPI Document.
@@ -25,16 +44,13 @@ extension TypesFileTranslator {
2544
/// only `RawStringTranslatedServerVariable` generators will be returned.
2645
/// - Returns: A declaration of the server variables namespace, or `nil` if no
2746
/// variables are declared.
28-
func translateServerVariables(index: Int, server: OpenAPI.Server, generateAsEnum: Bool) -> [any ServerVariableGenerator] {
29-
return server.variables.map { key, variable in
47+
func translateServerVariables(index: Int, server: OpenAPI.Server, generateAsEnum: Bool)
48+
-> [any ServerVariableGenerator]
49+
{
50+
server.variables.map { key, variable in
3051
guard generateAsEnum, let enumValues = variable.enum else {
31-
return RawStringTranslatedServerVariable(
32-
key: key,
33-
variable: variable,
34-
context: context
35-
)
52+
return RawStringTranslatedServerVariable(key: key, variable: variable, context: context)
3653
}
37-
3854
return GeneratedEnumTranslatedServerVariable(
3955
key: key,
4056
variable: variable,
@@ -46,26 +62,7 @@ extension TypesFileTranslator {
4662
}
4763

4864
// MARK: Generators
49-
50-
/// Represents a server variable and the function of generation that should be applied.
51-
protocol ServerVariableGenerator {
52-
/// Returns the declaration (enum) that should be added to the server's namespace.
53-
/// If the server variable does not require any codegen then it should return `nil`.
54-
var declaration: Declaration? { get }
55-
56-
/// Returns the description of the parameter that will be used to define the variable
57-
/// in the static method for a given server.
58-
var parameter: ParameterDescription { get }
59-
60-
/// Returns an expression for the variable initializer that is used in the body of a server's
61-
/// static method by passing it along to the URL resolver.
62-
var initializer: Expression { get }
6365

64-
/// Returns the description of this variables documentation for the function comment of
65-
/// the server's static method.
66-
var functionComment: (name: String, comment: String?) { get }
67-
}
68-
6966
/// Represents a variable that is required to be represented as a `Swift.String`.
7067
private struct RawStringTranslatedServerVariable: ServerVariableGenerator {
7168
/// The key of the variable defined in the Open API document.
@@ -96,17 +93,13 @@ extension TypesFileTranslator {
9693
var declaration: Declaration? {
9794
// A variable being represented by a `Swift.String` does not have a declaration that needs to
9895
// be added to the server's namespace.
99-
return nil
96+
nil
10097
}
10198

10299
/// Returns the description of the parameter that will be used to define the variable
103100
/// in the static method for a given server.
104101
var parameter: ParameterDescription {
105-
return .init(
106-
label: swiftSafeKey,
107-
type: .init(TypeName.string),
108-
defaultValue: .literal(variable.default)
109-
)
102+
.init(label: swiftSafeKey, type: .init(TypeName.string), defaultValue: .literal(variable.default))
110103
}
111104

112105
/// Returns an expression for the variable initializer that is used in the body of a server's
@@ -117,19 +110,16 @@ extension TypesFileTranslator {
117110
.init(label: "value", expression: .identifierPattern(swiftSafeKey)),
118111
]
119112
if let allowedValues = variable.enum {
120-
arguments.append(.init(
121-
label: "allowedValues",
122-
expression: .literal(.array(allowedValues.map { .literal($0) }))
123-
))
113+
arguments.append(
114+
.init(label: "allowedValues", expression: .literal(.array(allowedValues.map { .literal($0) })))
115+
)
124116
}
125117
return .dot("init").call(arguments)
126118
}
127119

128120
/// Returns the description of this variables documentation for the function comment of
129121
/// the server's static method.
130-
var functionComment: (name: String, comment: String?) {
131-
(name: swiftSafeKey, comment: variable.description)
132-
}
122+
var functionComment: (name: String, comment: String?) { (name: swiftSafeKey, comment: variable.description) }
133123
}
134124

135125
/// Represents an Open API "Server Variable Object" that will be generated as an enum and added
@@ -166,30 +156,33 @@ extension TypesFileTranslator {
166156
/// - accessModifier: The access modifier to use for generated declarations.
167157
/// - context: The translator context the generator should use to create
168158
/// Swift safe identifiers.
169-
init(key: String, variable: OpenAPI.Server.Variable, enumValues: [String], accessModifier: AccessModifier, context: TranslatorContext) {
159+
init(
160+
key: String,
161+
variable: OpenAPI.Server.Variable,
162+
enumValues: [String],
163+
accessModifier: AccessModifier,
164+
context: TranslatorContext
165+
) {
170166
self.key = key
171167
swiftSafeKey = context.asSwiftSafeName(key)
172168
enumName = context.asSwiftSafeName(key.localizedCapitalized)
173169
self.variable = variable
174170
self.enumValues = enumValues
175-
176171
self.context = context
177172
self.accessModifier = accessModifier
178173
}
179174

180175
/// Returns the declaration (enum) that should be added to the server's namespace.
181176
/// If the server variable does not require any codegen then it should return `nil`.
182177
var declaration: Declaration? {
183-
let description: String = if let description = variable.description {
184-
description + "\n\n"
185-
} else {
186-
""
187-
}
178+
let description: String = if let description = variable.description { description + "\n\n" } else { "" }
188179

189180
return .commentable(
190-
.doc("""
191-
\(description)The "\(key)" variable defined in the OpenAPI document. The default value is "\(variable.default)".
192-
"""),
181+
.doc(
182+
"""
183+
\(description)The "\(key)" variable defined in the OpenAPI document. The default value is "\(variable.default)".
184+
"""
185+
),
193186
.enum(
194187
isFrozen: true,
195188
accessModifier: accessModifier,
@@ -203,7 +196,7 @@ extension TypesFileTranslator {
203196
/// Returns the description of the parameter that will be used to define the variable
204197
/// in the static method for a given server.
205198
var parameter: ParameterDescription {
206-
return .init(
199+
.init(
207200
label: swiftSafeKey,
208201
type: .member([enumName]),
209202
defaultValue: .memberAccess(.dot(context.asSwiftSafeName(variable.default)))
@@ -213,22 +206,19 @@ extension TypesFileTranslator {
213206
/// Returns an expression for the variable initializer that is used in the body of a server's
214207
/// static method by passing it along to the URL resolver.
215208
var initializer: Expression {
216-
.dot("init").call(
217-
[
209+
.dot("init")
210+
.call([
218211
.init(label: "name", expression: .literal(key)),
219-
.init(label: "value", expression: .memberAccess(.init(
220-
left: .identifierPattern(swiftSafeKey),
221-
right: "rawValue"
222-
))),
223-
]
224-
)
212+
.init(
213+
label: "value",
214+
expression: .memberAccess(.init(left: .identifierPattern(swiftSafeKey), right: "rawValue"))
215+
),
216+
])
225217
}
226218

227219
/// Returns the description of this variables documentation for the function comment of
228220
/// the server's static method.
229-
var functionComment: (name: String, comment: String?) {
230-
(name: swiftSafeKey, comment: variable.description)
231-
}
221+
var functionComment: (name: String, comment: String?) { (name: swiftSafeKey, comment: variable.description) }
232222

233223
/// Returns an enum case declaration for a raw string enum.
234224
///

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5497,14 +5497,13 @@ extension SnippetBasedReferenceTests {
54975497
components: components
54985498
)
54995499
}
5500-
55015500
func makeTypesTranslator(
55025501
accessModifier: AccessModifier = .public,
55035502
featureFlags: FeatureFlags = [],
55045503
ignoredDiagnosticMessages: Set<String> = [],
55055504
components: OpenAPI.Components = .noComponents
55065505
) throws -> TypesFileTranslator {
5507-
return TypesFileTranslator(
5506+
TypesFileTranslator(
55085507
config: Config(mode: .types, access: accessModifier, featureFlags: featureFlags),
55095508
diagnostics: XCTestDiagnosticCollector(test: self, ignoredDiagnosticMessages: ignoredDiagnosticMessages),
55105509
components: components

0 commit comments

Comments
 (0)