From 60e07a1bc504659f3be72d102521980d576cdd67 Mon Sep 17 00:00:00 2001 From: Joseph Sumabat Date: Tue, 7 Nov 2023 17:57:18 -0500 Subject: [PATCH] Fix swift optional newtype --- .../swiftBasicNewtypeJoinOptionalsSpec/golden | 2 +- src/Moat/Pretty/Swift.hs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.golden/swiftBasicNewtypeJoinOptionalsSpec/golden b/.golden/swiftBasicNewtypeJoinOptionalsSpec/golden index 9d23ae8..d34114e 100644 --- a/.golden/swiftBasicNewtypeJoinOptionalsSpec/golden +++ b/.golden/swiftBasicNewtypeJoinOptionalsSpec/golden @@ -1,4 +1,4 @@ struct Newtype { typealias NewtypeTag = Tagged - let value: NewtypeTag + let value: NewtypeTag? } \ No newline at end of file diff --git a/src/Moat/Pretty/Swift.hs b/src/Moat/Pretty/Swift.hs index db93ab3..33cc12c 100644 --- a/src/Moat/Pretty/Swift.hs +++ b/src/Moat/Pretty/Swift.hs @@ -78,7 +78,9 @@ prettySwiftDataWith indent = \case ++ " = Tagged<" ++ newtypeName ++ ", " - ++ prettyMoatType (fieldType newtypeField) + ++ case (fieldType newtypeField) of + Optional t -> prettyMoatType t + t -> prettyMoatType t ++ ">\n" ++ prettyNewtypeField indents newtypeField newtypeName ++ "}" @@ -255,7 +257,17 @@ prettyStructFields indents = go ++ go fs prettyNewtypeField :: String -> Field -> String -> String -prettyNewtypeField indents (Field alias _ _) fieldName = indents ++ "let " ++ alias ++ ": " ++ fieldName ++ "Tag" ++ "\n" +prettyNewtypeField indents (Field alias fieldType _) fieldName = + indents + ++ "let " + ++ alias + ++ ": " + ++ fieldName + ++ "Tag" + ++ case fieldType of + Optional _ -> "?" + _ -> "" + ++ "\n" prettyPrivateTypes :: String -> [MoatData] -> String prettyPrivateTypes indents = go