@@ -54,6 +54,7 @@ prettySwiftDataWith indent = \case
54
54
++ " {"
55
55
++ newlineNonEmpty structFields
56
56
++ prettyStructFields indents structFields structDeprecatedFields
57
+ ++ prettyStructInitializer indents structFields structDeprecatedFields
57
58
++ newlineNonEmpty structPrivateTypes
58
59
++ prettyPrivateTypes indents structPrivateTypes
59
60
++ prettyTags indents structTags
@@ -287,6 +288,35 @@ prettyStructFields indents fields deprecatedFields = go fields
287
288
++ prettyField field
288
289
++ go fs
289
290
291
+ prettyStructInitializer :: String -> [Field ] -> [(String , Maybe String )] -> String
292
+ prettyStructInitializer indents fields deprecatedFields =
293
+ case activeFields of
294
+ [] -> " " -- No initializer needed if there are no active fields
295
+ _ ->
296
+ " \n "
297
+ ++ indents
298
+ ++ " public init("
299
+ ++ intercalate " , " (map prettyParam activeFields)
300
+ ++ " ) {\n "
301
+ ++ concatMap (prettyAssignment indents) activeFields
302
+ ++ indents
303
+ ++ " }\n "
304
+ where
305
+ deprecatedFieldNames = map fst deprecatedFields
306
+ activeFields = filter (\ (Field name _ _) -> name `notElem` deprecatedFieldNames) fields
307
+
308
+ isOptional :: MoatType -> Bool
309
+ isOptional (Optional _) = True
310
+ isOptional _ = False
311
+
312
+ prettyParam :: Field -> String
313
+ prettyParam (Field fieldName fieldType _) =
314
+ fieldName ++ " : " ++ prettyMoatType fieldType ++ (if isOptional fieldType then " = nil" else " " )
315
+
316
+ prettyAssignment :: String -> Field -> String
317
+ prettyAssignment indentStr (Field fieldName _ _) =
318
+ indentStr ++ " self." ++ fieldName ++ " = " ++ fieldName ++ " \n "
319
+
290
320
prettyNewtypeField :: String -> Field -> String -> String
291
321
prettyNewtypeField indents (Field alias fieldType _) fieldName =
292
322
indents
0 commit comments