-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from MercuryTechnologies/tad/swift-sum-types
swift: add Codable implementation for sum-of-product types
- Loading branch information
Showing
21 changed files
with
695 additions
and
15 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
.golden/kotlinEnumSumOfProductWithTaggedFlatObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@Serializable | ||
sealed class Enum : Parcelable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
.golden/kotlinRecord0SumOfProductWithTaggedFlatObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@Parcelize | ||
@Serializable | ||
data class Record0( | ||
val record0Field0: Int, | ||
val record0Field1: Int, | ||
) : Parcelable |
6 changes: 6 additions & 0 deletions
6
.golden/kotlinRecord1SumOfProductWithTaggedFlatObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@Parcelize | ||
@Serializable | ||
data class Record1( | ||
val record1Field0: Int, | ||
val record1Field1: Int, | ||
) : Parcelable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,48 @@ | ||
enum Enum: CaseIterable, Hashable, Codable { | ||
enum Enum: Hashable, Codable { | ||
case dataCons0(_ enumField0: Int, _ enumField1: Int) | ||
case dataCons1(_ enumField2: String, _ enumField3: String) | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case tag | ||
case enumField0 | ||
case enumField1 | ||
case enumField2 | ||
case enumField3 | ||
} | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let discriminator = try container.decode(String.self, forKey: .tag) | ||
switch discriminator { | ||
case "dataCons0": | ||
self = .dataCons0( | ||
try container.decode(Int.self, forKey: .enumField0), | ||
try container.decode(Int.self, forKey: .enumField1) | ||
) | ||
case "dataCons1": | ||
self = .dataCons1( | ||
try container.decode(String.self, forKey: .enumField2), | ||
try container.decode(String.self, forKey: .enumField3) | ||
) | ||
default: | ||
throw DecodingError.typeMismatch( | ||
CodingKeys.self, | ||
.init(codingPath: decoder.codingPath, debugDescription: "Can't decode unknown tag: Enum.\(discriminator)") | ||
) | ||
} | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
switch (self) { | ||
case let .dataCons0: | ||
try container.encode("dataCons0", forKey: .tag) | ||
try container.encode(enumField0, forKey: .enumField0) | ||
try container.encode(enumField1, forKey: .enumField1) | ||
case let .dataCons1: | ||
try container.encode("dataCons1", forKey: .tag) | ||
try container.encode(enumField2, forKey: .enumField2) | ||
try container.encode(enumField3, forKey: .enumField3) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.golden/swiftEnumSumOfProductWithLinkEnumInterfaceSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,36 @@ | ||
enum Enum: CaseIterable, Hashable, Codable { | ||
case dataCons0(Record0) | ||
case dataCons1(Record1) | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case tag | ||
} | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let discriminator = try container.decode(String.self, forKey: .tag) | ||
switch discriminator { | ||
case "dataCons0": | ||
self = .dataCons0(try Record0.init(from: decoder)) | ||
case "dataCons1": | ||
self = .dataCons1(try Record1.init(from: decoder)) | ||
default: | ||
throw DecodingError.typeMismatch( | ||
CodingKeys.self, | ||
.init(codingPath: decoder.codingPath, debugDescription: "Can't decode unknown tag: Enum.\(discriminator)") | ||
) | ||
} | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
switch (self) { | ||
case let .dataCons0(value): | ||
try container.encode("dataCons0", forKey: .tag) | ||
try value.encode(to: encoder) | ||
case let .dataCons1(value): | ||
try container.encode("dataCons1", forKey: .tag) | ||
try value.encode(to: encoder) | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.golden/swiftEnumSumOfProductWithTaggedFlatObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
enum Enum: Codable { | ||
case dataCons0(Record0) | ||
case dataCons1(Record1) | ||
case dataCons2 | ||
case _unknown | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case tag | ||
} | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let discriminator = try container.decode(String.self, forKey: .tag) | ||
switch discriminator { | ||
case "dataCons0": | ||
self = .dataCons0(try Record0.init(from: decoder)) | ||
case "dataCons1": | ||
self = .dataCons1(try Record1.init(from: decoder)) | ||
case "dataCons2": | ||
self = .dataCons2 | ||
default: | ||
self = ._unknown | ||
} | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
switch (self) { | ||
case let .dataCons0(value): | ||
try container.encode("dataCons0", forKey: .tag) | ||
try value.encode(to: encoder) | ||
case let .dataCons1(value): | ||
try container.encode("dataCons1", forKey: .tag) | ||
try value.encode(to: encoder) | ||
case .dataCons2: | ||
try container.encode("dataCons2", forKey: .tag) | ||
case ._unknown: | ||
throw EncodingError.invalidValue( | ||
self, | ||
.init(codingPath: encoder.codingPath, debugDescription: "Can't encode value: Enum._unknown") | ||
) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
.golden/swiftEnumSumOfProductWithTaggedObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
enum Enum: Codable { | ||
case dataCons0(Record0) | ||
case dataCons1(Record1) | ||
case dataCons2 | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case tag | ||
case contents | ||
} | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let discriminator = try container.decode(String.self, forKey: .tag) | ||
switch discriminator { | ||
case "dataCons0": | ||
self = .dataCons0(try container.decode(Record0.self, forKey: .contents)) | ||
case "dataCons1": | ||
self = .dataCons1(try container.decode(Record1.self, forKey: .contents)) | ||
case "dataCons2": | ||
self = .dataCons2 | ||
default: | ||
throw DecodingError.typeMismatch( | ||
CodingKeys.self, | ||
.init(codingPath: decoder.codingPath, debugDescription: "Can't decode unknown tag: Enum.\(discriminator)") | ||
) | ||
} | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
switch (self) { | ||
case let .dataCons0(contents): | ||
try container.encode("dataCons0", forKey: .tag) | ||
try container.encode(contents, forKey: .contents) | ||
case let .dataCons1(contents): | ||
try container.encode("dataCons1", forKey: .tag) | ||
try container.encode(contents, forKey: .contents) | ||
case .dataCons2: | ||
try container.encode("dataCons2", forKey: .tag) | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
.golden/swiftRecord0SumOfProductWithTaggedFlatObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Record0: Codable { | ||
var record0Field0: Int | ||
var record0Field1: Int | ||
} |
4 changes: 4 additions & 0 deletions
4
.golden/swiftRecord0SumOfProductWithTaggedObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Record0: Codable { | ||
var record0Field0: Int | ||
var record0Field1: Int | ||
} |
4 changes: 4 additions & 0 deletions
4
.golden/swiftRecord1SumOfProductWithTaggedFlatObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Record1: Codable { | ||
var record1Field0: Int | ||
var record1Field1: Int | ||
} |
4 changes: 4 additions & 0 deletions
4
.golden/swiftRecord1SumOfProductWithTaggedObjectStyleSpec/golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Record1: Codable { | ||
var record1Field0: Int | ||
var record1Field1: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,37 @@ | ||
enum CursorInput<A: Hashable & Codable>: CaseIterable, Hashable, Codable { | ||
case nextPage(A?) | ||
case previousPage(A) | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case direction | ||
case key | ||
} | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let discriminator = try container.decode(String.self, forKey: .direction) | ||
switch discriminator { | ||
case "nextPage": | ||
self = .nextPage(try container.decode(A?.self, forKey: .key)) | ||
case "previousPage": | ||
self = .previousPage(try container.decode(A.self, forKey: .key)) | ||
default: | ||
throw DecodingError.typeMismatch( | ||
CodingKeys.self, | ||
.init(codingPath: decoder.codingPath, debugDescription: "Can't decode unknown direction: CursorInput.\(discriminator)") | ||
) | ||
} | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
switch (self) { | ||
case let .nextPage(key): | ||
try container.encode("nextPage", forKey: .direction) | ||
try container.encode(key, forKey: .key) | ||
case let .previousPage(key): | ||
try container.encode("previousPage", forKey: .direction) | ||
try container.encode(key, forKey: .key) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.