-
-
Notifications
You must be signed in to change notification settings - Fork 43
Encode enum cases without associated values #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,6 +150,7 @@ extension EnumSwitcherVariable { | |
| generatedCode | ||
| } | ||
| } else { | ||
| preSyntax("\(values.first!)") | ||
| "break" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: You can add logic to conditionally include this |
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -802,4 +802,103 @@ struct CodedAtEnumTests { | |
| #expect(json["int"] as? Int == 42) | ||
| } | ||
| } | ||
|
|
||
| struct WithoutAssociatedVariables { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: You can add test case for your original example as well, without the |
||
| @Codable | ||
| @CodedAt("type") | ||
| enum Foo { | ||
| case foo | ||
| } | ||
|
|
||
| @Test | ||
| func expansion() throws { | ||
| assertMacroExpansion( | ||
| """ | ||
| @Codable | ||
| @CodedAt("type") | ||
| enum Foo { | ||
| case foo | ||
| } | ||
| """, | ||
| expandedSource: | ||
| """ | ||
| enum Foo { | ||
| case foo | ||
| } | ||
|
|
||
| extension Foo: Decodable { | ||
| init(from decoder: any Decoder) throws { | ||
| var typeContainer: KeyedDecodingContainer<CodingKeys>? | ||
| let container = try? decoder.container(keyedBy: CodingKeys.self) | ||
| if let container = container { | ||
| typeContainer = container | ||
| } else { | ||
| typeContainer = nil | ||
| } | ||
| if let typeContainer = typeContainer { | ||
| let typeString: String? | ||
| do { | ||
| typeString = try typeContainer.decodeIfPresent(String.self, forKey: CodingKeys.type) ?? nil | ||
| } catch { | ||
| typeString = nil | ||
| } | ||
| if let typeString = typeString { | ||
| switch typeString { | ||
| case "foo": | ||
| self = .foo | ||
| return | ||
| default: | ||
| break | ||
| } | ||
| } | ||
| } | ||
| let context = DecodingError.Context( | ||
| codingPath: decoder.codingPath, | ||
| debugDescription: "Couldn't match any cases." | ||
| ) | ||
| throw DecodingError.typeMismatch(Self.self, context) | ||
| } | ||
| } | ||
|
|
||
| extension Foo: Encodable { | ||
| func encode(to encoder: any Encoder) throws { | ||
| let container = encoder.container(keyedBy: CodingKeys.self) | ||
| var typeContainer = container | ||
| switch self { | ||
| case .foo: | ||
| try typeContainer.encode("foo", forKey: CodingKeys.type) | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension Foo { | ||
| enum CodingKeys: String, CodingKey { | ||
| case type = "type" | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| func decodingFromJSON() throws { | ||
| let jsonStr = """ | ||
| { | ||
| "type": "foo" | ||
| } | ||
| """ | ||
| let jsonData = try #require(jsonStr.data(using: .utf8)) | ||
| let decoded = try JSONDecoder().decode(Foo.self, from: jsonData) | ||
| #expect(decoded == Foo.foo) | ||
| } | ||
|
|
||
| @Test | ||
| func encodingToJSON() throws { | ||
| let original = Foo.foo | ||
| let encoded = try JSONEncoder().encode(original) | ||
| let json = try JSONSerialization.jsonObject(with: encoded) as? [String: Any] | ||
| #expect(json?["type"] as? String == "foo") | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will cause unused variable warnings in case of your original example. Can you address that in someway? May be passing some flag to this
preSyntaxclosure and customizing the output based on this flag?