@@ -39,31 +39,86 @@ struct ServerFileTranslator: FileTranslator {
39
39
+ config. additionalImports
40
40
. map { ImportDescription ( moduleName: $0) }
41
41
42
- let serverMethodDeclPairs =
42
+ let allOperations =
43
43
try OperationDescription
44
44
. all (
45
45
from: doc. paths,
46
46
in: components,
47
47
asSwiftSafeName: swiftSafeName
48
48
)
49
+
50
+ let ( registerHandlersDecl, serverMethodDecls) = try translateRegisterHandlers ( allOperations)
51
+
52
+ let protocolExtensionDecl : Declaration = . extension(
53
+ accessModifier: nil ,
54
+ onType: Constants . APIProtocol. typeName,
55
+ declarations: [
56
+ registerHandlersDecl
57
+ ]
58
+ )
59
+
60
+ let serverExtensionDecl : Declaration = . extension(
61
+ accessModifier: . fileprivate,
62
+ onType: Constants . Server. Universal. typeName,
63
+ whereClause: . init( requirements: [
64
+ . conformance(
65
+ Constants . Server. Universal. apiHandlerName,
66
+ Constants . APIProtocol. typeName
67
+ )
68
+ ] ) ,
69
+ declarations: serverMethodDecls
70
+ )
71
+
72
+ return StructuredSwiftRepresentation (
73
+ file: . init(
74
+ name: GeneratorMode . server. outputFileName,
75
+ contents: . init(
76
+ topComment: topComment,
77
+ imports: imports,
78
+ codeBlocks: [
79
+ . declaration( protocolExtensionDecl) ,
80
+ . declaration( serverExtensionDecl) ,
81
+ ]
82
+ )
83
+ )
84
+ )
85
+ }
86
+
87
+ /// Returns a declaration of the registerHandlers method and
88
+ /// the declarations of the individual operation methods.
89
+ /// - Parameter operations: The operations found in the OpenAPI document.
90
+ func translateRegisterHandlers(
91
+ _ operations: [ OperationDescription ]
92
+ ) throws -> ( Declaration , [ Declaration ] ) {
93
+ var registerHandlersDeclBody : [ CodeBlock ] = [ ]
94
+ let serverMethodDeclPairs =
95
+ try operations
49
96
. map { operation in
50
97
try translateServerMethod ( operation, serverUrlVariableName: " server " )
51
98
}
52
99
let serverMethodDecls = serverMethodDeclPairs. map ( \. functionDecl)
53
100
54
- let serverMethodRegisterCalls = serverMethodDeclPairs. map ( \. registerCall)
101
+ // To avoid an unused variable warning, we add the server variable declaration
102
+ // and server method register calls to the body of the register handler declaration
103
+ // only when there is at least one registration call.
104
+ if !serverMethodDeclPairs. isEmpty {
105
+ let serverMethodRegisterCalls = serverMethodDeclPairs. map ( \. registerCall)
106
+ let registerHandlerServerVarDecl : Declaration = . variable(
107
+ kind: . let,
108
+ left: " server " ,
109
+ right: . identifier( Constants . Server. Universal. typeName)
110
+ . call ( [
111
+ . init( label: " serverURL " , expression: . identifier( " serverURL " ) ) ,
112
+ . init( label: " handler " , expression: . identifier( " self " ) ) ,
113
+ . init( label: " configuration " , expression: . identifier( " configuration " ) ) ,
114
+ . init( label: " middlewares " , expression: . identifier( " middlewares " ) ) ,
115
+ ] )
116
+ )
117
+
118
+ registerHandlersDeclBody. append ( . declaration( registerHandlerServerVarDecl) )
119
+ registerHandlersDeclBody. append ( contentsOf: serverMethodRegisterCalls. map { . expression( $0) } )
120
+ }
55
121
56
- let registerHandlerServerVarDecl : Declaration = . variable(
57
- kind: . let,
58
- left: " server " ,
59
- right: . identifier( Constants . Server. Universal. typeName)
60
- . call ( [
61
- . init( label: " serverURL " , expression: . identifier( " serverURL " ) ) ,
62
- . init( label: " handler " , expression: . identifier( " self " ) ) ,
63
- . init( label: " configuration " , expression: . identifier( " configuration " ) ) ,
64
- . init( label: " middlewares " , expression: . identifier( " middlewares " ) ) ,
65
- ] )
66
- )
67
122
let registerHandlersDecl : Declaration = . commentable(
68
123
. doc(
69
124
#"""
@@ -104,44 +159,9 @@ struct ServerFileTranslator: FileTranslator {
104
159
keywords: [
105
160
. throws
106
161
] ,
107
- body: [
108
- . declaration( registerHandlerServerVarDecl)
109
- ] + serverMethodRegisterCalls. map { . expression( $0) }
110
- )
111
- )
112
-
113
- let protocolExtensionDecl : Declaration = . extension(
114
- accessModifier: nil ,
115
- onType: Constants . APIProtocol. typeName,
116
- declarations: [
117
- registerHandlersDecl
118
- ]
119
- )
120
-
121
- let serverExtensionDecl : Declaration = . extension(
122
- accessModifier: . fileprivate,
123
- onType: Constants . Server. Universal. typeName,
124
- whereClause: . init( requirements: [
125
- . conformance(
126
- Constants . Server. Universal. apiHandlerName,
127
- Constants . APIProtocol. typeName
128
- )
129
- ] ) ,
130
- declarations: serverMethodDecls
131
- )
132
-
133
- return StructuredSwiftRepresentation (
134
- file: . init(
135
- name: GeneratorMode . server. outputFileName,
136
- contents: . init(
137
- topComment: topComment,
138
- imports: imports,
139
- codeBlocks: [
140
- . declaration( protocolExtensionDecl) ,
141
- . declaration( serverExtensionDecl) ,
142
- ]
143
- )
162
+ body: registerHandlersDeclBody
144
163
)
145
164
)
165
+ return ( registerHandlersDecl, serverMethodDecls)
146
166
}
147
167
}
0 commit comments