@@ -218,9 +218,45 @@ def deserialize_value_code(field_name, expr, type, untyped: true)
218
218
raise NotImplementedError , "Unexpected #{ type . kind } argument type"
219
219
end
220
220
end
221
-
221
+
222
+ def generate_input_init ( type )
223
+ text = "public init("
224
+ input_fields = type . required_input_fields + type . optional_input_fields
225
+ input_fields . each do |field |
226
+ text << escape_reserved_word ( field . camelize_name )
227
+ text << ": "
228
+ text << swift_input_type ( field . type )
229
+ text << ( field . type . non_null? ? "" : " = nil" )
230
+ text << ( field == input_fields . last ? "" : ", " )
231
+ end
232
+ text << ")"
233
+ text << " {\n "
234
+ type . input_fields . each do |field |
235
+ name = escape_reserved_word ( field . camelize_name )
236
+ text << "self." + name + " = " + name
237
+ text << "\n "
238
+ end
239
+ text << "}"
240
+ end
241
+
242
+ def remove_linebreaks ( text )
243
+ text . gsub ( "\n " , " " )
244
+ end
245
+
246
+ def input_field_description ( type )
247
+ unless type . input_fields . count == 0
248
+ text = "/// - parameters:" + ""
249
+ type . input_fields . each do |field |
250
+ description = ( field . description . nil? ? "No description" : remove_linebreaks ( field . description ) )
251
+ text << "\n /// - " + field . name + ": " + description
252
+ end
253
+ text << "\n ///"
254
+ text
255
+ end
256
+ end
257
+
222
258
def swift_arg_defs ( field )
223
- defs = [ "aliasSuffix : String? = nil" ]
259
+ defs = [ "alias : String? = nil" ]
224
260
field . args . each do |arg |
225
261
arg_def = "#{ escape_reserved_word ( arg . name ) } : #{ swift_input_type ( arg . type ) } "
226
262
arg_def << " = nil" unless arg . type . non_null?
@@ -242,16 +278,72 @@ def generate_append_objects_code(expr, type, non_null: false)
242
278
end
243
279
return "#{ expr } .forEach {\n #{ generate_append_objects_code ( '$0' , type . of_type ) } \n }" if type . list?
244
280
245
- abstract_response = type . object? ? expr : "#{ expr } as! GraphQL.AbstractResponse"
281
+ abstract_response = type . object? ? expr : "( #{ expr } as! GraphQL.AbstractResponse) "
246
282
"response.append(#{ abstract_response } )\n " \
247
- "response.append(contentsOf: #{ expr } .childResponseObjectMap())"
283
+ "response.append(contentsOf: #{ abstract_response } .childResponseObjectMap())"
248
284
end
249
285
250
286
def swift_attributes ( deprecatable )
251
287
return unless deprecatable . deprecated?
252
288
if deprecatable . deprecation_reason
253
289
message_argument = ", message:#{ deprecatable . deprecation_reason . inspect } "
254
290
end
255
- "@available(*, deprecated#{ message_argument } )"
291
+ "@available(*, deprecated#{ message_argument } )\n "
292
+ end
293
+
294
+ def swift_doc ( element , include_args = true )
295
+ doc = ''
296
+
297
+ unless element . description . nil?
298
+ description = element . description
299
+ description = wrap_text ( description , '/// ' )
300
+ doc << "\n \n " + description
301
+ end
302
+
303
+ if include_args && element . respond_to? ( :args )
304
+ if element . args . count > 0
305
+ doc << "\n ///\n "
306
+ doc << "/// - parameters:"
307
+ element . args . each do |arg |
308
+ doc << "\n "
309
+ doc << '/// - ' + arg . name + ': ' + ( arg . description . nil? ? "No description" : format_arg_list ( arg . description , 7 ) )
310
+ end
311
+ doc << "\n ///"
312
+ end
313
+ end
314
+ doc
315
+ end
316
+
317
+ def wrap_text ( text , prefix , width = 80 )
318
+ container = ''
319
+ line = "" + prefix
320
+
321
+ parts = text . split ( " " )
322
+ parts . each do |part |
323
+ if line . length + part . length < width
324
+ line << part
325
+ line << ' '
326
+ else
327
+ container << line
328
+ container << "\n "
329
+ line = "" + prefix
330
+ line << part
331
+ line << ' '
332
+ end
333
+ end
334
+
335
+ if line . length > 0
336
+ container << line
337
+ end
338
+ container
339
+ end
340
+
341
+ def format_arg_list ( text , spacing )
342
+ parts = text . split ( "\n " )
343
+ commented = parts . drop ( 1 ) . map do |part |
344
+ "/// " + ( " " * spacing ) + part
345
+ end
346
+ commented . unshift ( parts . first )
347
+ commented . join ( "\n " )
256
348
end
257
349
end
0 commit comments