Skip to content

Commit

Permalink
generate public inits for swift
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebshr committed Nov 20, 2024
1 parent d4cc121 commit fa9d21b
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .golden/swiftAdvancedNewtypeWithEnumFieldSpec/golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public struct Newtype {
public var newtypeField: Enum

public init(newtypeField: Enum) {
self.newtypeField = newtypeField
}
}
5 changes: 5 additions & 0 deletions .golden/swiftAdvancedRecordSpec/golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
public struct Data: CaseIterable, Hashable, Codable {
public var field0: Int
public var field1: Int?

public init(field0: Int, field1: Int?) {
self.field0 = field0
self.field1 = field1
}
}
5 changes: 5 additions & 0 deletions .golden/swiftBasicDocSpec/golden
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public struct Data {
public var first: Int
/// Second field, it's maybe an Int
public var second: Int?

public init(first: Int, second: Int?) {
self.first = first
self.second = second
}
}
4 changes: 4 additions & 0 deletions .golden/swiftBasicNewtypeWithConcreteFieldSpec/golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public struct Newtype {
public var newtypeField: String

public init(newtypeField: String) {
self.newtypeField = newtypeField
}
}
4 changes: 4 additions & 0 deletions .golden/swiftBasicNewtypeWithEitherFieldSpec/golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public struct Newtype {
public var newtypeField: Result<Int, String>

public init(newtypeField: Result<Int, String>) {
self.newtypeField = newtypeField
}
}
5 changes: 5 additions & 0 deletions .golden/swiftBasicRecordSpec/golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
public struct Data {
public var field0: Int
public var field1: Int?

public init(field0: Int, field1: Int?) {
self.field0 = field0
self.field1 = field1
}
}
4 changes: 4 additions & 0 deletions .golden/swiftDeprecatedFieldSpec/golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ public struct Data {
public var field0: Int
// Deprecated since build 500
// public var field1: Int?

public init(field0: Int) {
self.field0 = field0
}
}
5 changes: 5 additions & 0 deletions .golden/swiftGenericStructSpec/golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
public struct Tree<A: Hashable & Codable>: Hashable, Codable {
public var rootLabel: A
public var subForest: [Tree<A>]

public init(rootLabel: A, subForest: [Tree<A>]) {
self.rootLabel = rootLabel
self.subForest = subForest
}
}
5 changes: 5 additions & 0 deletions .golden/swiftMultipleTypeVariableSpec/golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
public struct Data<A: Hashable & Codable, B: Hashable & Codable>: CaseIterable, Hashable, Codable {
public var field0: A
public var field1: B

public init(field0: A, field1: B) {
self.field0 = field0
self.field1 = field1
}
}
5 changes: 5 additions & 0 deletions .golden/swiftRecord0DuplicateRecordFieldSpec/golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ public struct Data0 {
public var field0: Int
/// not a duplicate
public var field1: Int?

public init(field0: Int, field1: Int?) {
self.field0 = field0
self.field1 = field1
}
}
5 changes: 5 additions & 0 deletions .golden/swiftRecord0SumOfProductDocSpec/golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ public struct Record0: CaseIterable, Hashable, Codable {
public var record0Field0: Int
/// The first field of record 0
public var record0Field1: Int

public init(record0Field0: Int, record0Field1: Int) {
self.record0Field0 = record0Field0
self.record0Field1 = record0Field1
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
public struct Record0: Codable {
public var record0Field0: Int
public var record0Field1: Int

public init(record0Field0: Int, record0Field1: Int) {
self.record0Field0 = record0Field0
self.record0Field1 = record0Field1
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
public struct Record0: Codable {
public var record0Field0: Int
public var record0Field1: Int

public init(record0Field0: Int, record0Field1: Int) {
self.record0Field0 = record0Field0
self.record0Field1 = record0Field1
}
}
5 changes: 5 additions & 0 deletions .golden/swiftRecord1DuplicateRecordFieldSpec/golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ public struct Data1 {
public var field0: String
/// not a duplicate
public var field2: String?

public init(field0: String, field2: String?) {
self.field0 = field0
self.field2 = field2
}
}
5 changes: 5 additions & 0 deletions .golden/swiftRecord1SumOfProductDocSpec/golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ public struct Record1: CaseIterable, Hashable, Codable {
public var record1Field0: Int
/// The first field of record 1
public var record1Field1: Int

public init(record1Field0: Int, record1Field1: Int) {
self.record1Field0 = record1Field0
self.record1Field1 = record1Field1
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
public struct Record1: Codable {
public var record1Field0: Int
public var record1Field1: Int

public init(record1Field0: Int, record1Field1: Int) {
self.record1Field0 = record1Field0
self.record1Field1 = record1Field1
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
public struct Record1: Codable {
public var record1Field0: Int
public var record1Field1: Int

public init(record1Field0: Int, record1Field1: Int) {
self.record1Field0 = record1Field0
self.record1Field1 = record1Field1
}
}
4 changes: 4 additions & 0 deletions .golden/swiftStrictFieldsCheck-RecordA/golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public struct RecordA {
public var fieldA: String

public init(fieldA: String) {
self.fieldA = fieldA
}
}
4 changes: 4 additions & 0 deletions .golden/swiftStrictFieldsCheck-RecordB/golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public struct RecordB {
public var c: String

public init(c: String) {
self.c = c
}
}
4 changes: 4 additions & 0 deletions .golden/swiftTypeVariableSpec/golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public struct Data<A: Hashable & Codable>: CaseIterable, Hashable, Codable {
public var field0: A

public init(field0: A) {
self.field0 = field0
}
}
26 changes: 26 additions & 0 deletions src/Moat/Pretty/Swift.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ prettySwiftDataWith indent = \case
++ " {"
++ newlineNonEmpty structFields
++ prettyStructFields indents structFields structDeprecatedFields
++ prettyStructInitializer indents structFields structDeprecatedFields
++ newlineNonEmpty structPrivateTypes
++ prettyPrivateTypes indents structPrivateTypes
++ prettyTags indents structTags
Expand Down Expand Up @@ -287,6 +288,31 @@ prettyStructFields indents fields deprecatedFields = go fields
++ prettyField field
++ go fs

prettyStructInitializer :: String -> [Field] -> [(String, Maybe String)] -> String
prettyStructInitializer indents fields deprecatedFields =
case activeFields of
[] -> "" -- No initializer needed if there are no active fields
_ ->
"\n"
++ indents
++ "public init("
++ intercalate ", " (map prettyParam activeFields)
++ ") {\n"
++ concatMap (prettyAssignment indents) activeFields
++ indents
++ "}\n"
where
deprecatedFieldNames = map fst deprecatedFields
activeFields = filter (\(Field name _ _) -> name `notElem` deprecatedFieldNames) fields

prettyParam :: Field -> String
prettyParam (Field fieldName fieldType _) =
fieldName ++ ": " ++ prettyMoatType fieldType

prettyAssignment :: String -> Field -> String
prettyAssignment indentStr (Field fieldName _ _) =
indentStr ++ " self." ++ fieldName ++ " = " ++ fieldName ++ "\n"

prettyNewtypeField :: String -> Field -> String -> String
prettyNewtypeField indents (Field alias fieldType _) fieldName =
indents
Expand Down

0 comments on commit fa9d21b

Please sign in to comment.