@@ -102,20 +102,23 @@ extension TypesFileTranslator {
102
102
asSwiftSafeName: swiftSafeName
103
103
)
104
104
105
- let responseStructDecl = translateStructBlueprint (
106
- . init(
107
- comment: nil ,
108
- access: config. access,
109
- typeName: typeName,
110
- conformances: Constants . Operation. Output. Payload. conformances,
111
- properties: [
112
- headersProperty,
113
- contentProperty,
114
- ]
115
- )
105
+ let structBlueprint : StructBlueprint = . init(
106
+ comment: nil ,
107
+ access: config. access,
108
+ typeName: typeName,
109
+ conformances: Constants . Operation. Output. Payload. conformances,
110
+ properties: [
111
+ headersProperty,
112
+ contentProperty,
113
+ ]
116
114
)
117
115
118
- return responseStructDecl
116
+ let responseStructDecl = _calculateRequiredHeadersForInitialize (
117
+ with: headers,
118
+ from: translateStructBlueprint ( structBlueprint)
119
+ )
120
+
121
+ return responseStructDecl. deprecate ( if: structBlueprint. isDeprecated)
119
122
}
120
123
121
124
/// Returns a list of declarations for the specified reusable response
@@ -138,4 +141,82 @@ extension TypesFileTranslator {
138
141
response: response
139
142
)
140
143
}
144
+
145
+ /// Calculate the necessary parameters for initializing the response headers
146
+ /// and return the list of specified reusable response declarations.
147
+ /// - Parameters:
148
+ /// - headers: the typed response headers
149
+ /// - responseStructDecl: A structure declaration before calculate.
150
+ /// - Returns: A structure declaration.
151
+ private func _calculateRequiredHeadersForInitialize(
152
+ with headers: [ TypedResponseHeader ] ,
153
+ from responseStructDecl: Declaration
154
+ ) -> Declaration {
155
+ guard case . commentable( let comment, let structDec) = responseStructDecl,
156
+ case . struct( let structDescription) = structDec
157
+ else {
158
+ return responseStructDecl
159
+ }
160
+
161
+ let newMembers : [ Declaration ] = structDescription
162
+ . members
163
+ . reduce ( into: [ Declaration] ( ) ) { partialResult, member in
164
+ let labelHeaders = " headers "
165
+
166
+ if case . commentable( let memberComment, let memberDecl) = member,
167
+ case . function( let memberFuncDescription) = memberDecl,
168
+ memberFuncDescription. signature. kind == . initializer,
169
+ memberFuncDescription. signature. parameters. first ( where: { $0. label == labelHeaders } ) != nil
170
+ {
171
+
172
+ let initParameters : [ ParameterDescription ] = memberFuncDescription
173
+ . signature
174
+ . parameters
175
+ . map { parameterDesc in
176
+ guard parameterDesc. label == labelHeaders else {
177
+ return parameterDesc
178
+ }
179
+ let defaultValue : Expression ? = {
180
+ if headers. isEmpty {
181
+ return PropertyBlueprint . DefaultValue. emptyInit. asExpression
182
+ }
183
+ if headers. first ( where: { !$0. isOptional } ) != nil {
184
+ return nil
185
+ }
186
+ return PropertyBlueprint . DefaultValue. emptyInit. asExpression
187
+ } ( )
188
+ return . init(
189
+ label: parameterDesc. label,
190
+ name: parameterDesc. name,
191
+ type: parameterDesc. type,
192
+ defaultValue: defaultValue
193
+ )
194
+ }
195
+
196
+ let initDescription : FunctionDescription = . init(
197
+ accessModifier: memberFuncDescription. signature. accessModifier,
198
+ kind: memberFuncDescription. signature. kind,
199
+ parameters: initParameters,
200
+ keywords: memberFuncDescription. signature. keywords,
201
+ returnType: memberFuncDescription. signature. returnType,
202
+ body: memberFuncDescription. body
203
+ )
204
+
205
+ partialResult. append (
206
+ . commentable( memberComment, . function( initDescription) )
207
+ )
208
+ } else {
209
+ partialResult. append ( member)
210
+ }
211
+ }
212
+
213
+ let structDescriptionWithCalc = StructDescription (
214
+ accessModifier: structDescription. accessModifier,
215
+ name: structDescription. name,
216
+ conformances: structDescription. conformances,
217
+ members: newMembers
218
+ )
219
+
220
+ return . commentable( comment, . struct( structDescriptionWithCalc) )
221
+ }
141
222
}
0 commit comments