Skip to content
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

swift: add Codable implementation for sum-of-product types #77

Merged
merged 1 commit into from
Feb 13, 2024

Conversation

tadfisher
Copy link
Collaborator

Support sum-of-product types in Swift by generating Codable implementations.

There are a couple of dimensions here, so I attempted to cover them all in tests:

  • encoding style: TaggedObjectStyle, TaggedFlatObjectStyle
  • case constructor: no fields, single unnamed field (linked record type), multiple named fields (only seen in TaggedFlatObjectStyle with record constructors)

In order to support "unknown" cases instead of throwing an error, the decoding logic needs to know the "unknown" field name at codegen time, so we are strongly nudged into making "unknown enum cases" a supported feature. This is done by adding enumUnknownCase :: Maybe String to Options and a corresponding enumEnumUnknownCase to MoatEnum. Only sum-of-product decoding takes this into account when decoding; the unknown case is generated for all enums as it doesn't hurt anything, but clients will have to implement their own Codable extensions to fall back to the unknown case for bare ("C-style") enums. We could implement that if desired, but I left that for a future improvement.

@tadfisher tadfisher force-pushed the tad/swift-sum-types branch from a7a5fc0 to bb72de5 Compare February 3, 2024 02:11
@tadfisher
Copy link
Collaborator Author

@tadfisher tadfisher marked this pull request as ready for review February 5, 2024 22:16
@josephsumabat
Copy link
Contributor

LGTM from the Haskell end. My swift knowledge is limited but based on our internal implementation this all seems reasonable to me!

@tadfisher tadfisher merged commit 84c138d into master Feb 13, 2024
21 checks passed
@tadfisher tadfisher deleted the tad/swift-sum-types branch February 13, 2024 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants