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

Support Hashable #171

Open
sjmueller opened this issue Nov 23, 2022 · 2 comments
Open

Support Hashable #171

sjmueller opened this issue Nov 23, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@sjmueller
Copy link

sjmueller commented Nov 23, 2022

We have the requirement that all of our entities and enums implement Hashable. For the entities this is mostly achievable by setting our config.yaml:

entities:
  protocols: [Hashable, Codable]

However, two problems arise with this configuration:

  1. There's no way to set Hashable conformance to enums, which breaks all Entities with an enum property at compile time: Entity does not conform to Hashable. This can be worked around by manually extending all enums:
extension UserStatus: Hashable {}
  1. If an entity has a map property, then create-api will generate an enum AnyJSON which also doesn't conform to Hashable. However, using the same extension workaround yields compile time error:
Extension outside of file declaring enum 'AnyJSON' prevents automatic synthesis of 'hash(into:)' for protocol 'Hashable'

Potential Solution

Add a configuration option that allows conformance for enums, in the same way that is afforded for entities:

enums:
  protocols: [Hashable, Codable]

There's also the argument that Hashable should be universally used over Equatable by default in this library, since all Hashable constructs have Equatable conformance.

@aaronsky
Copy link

Likewise, given recent developments in Swift 5.8, I would like to see additional conformance to Sendable in enums and other places. Otherwise, it is impossible to add proper Sendable conformance through the entities.protocols configuration.

@liamnichols
Copy link
Member

Thanks for sharing this request! I agree that this would be a great addition to allowing us to customise enum generation 🙏 A similar configuration style was also proposed here: #130 (comment) so I think the enums option is a right approach to cover use cases like the one that @aaronsky raised with Sendable too.

It would be interesting to dig into Hashable by default, but there are some considerations to be had when using the generator to produce class types. We'd have to dig a bit further into that one, but in the meantime I think adding the enums.protocols support would be a good start 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants