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

Generate non-exhaustive enums #94

Open
shilangyu opened this issue Jul 10, 2024 · 0 comments
Open

Generate non-exhaustive enums #94

shilangyu opened this issue Jul 10, 2024 · 0 comments

Comments

@shilangyu
Copy link
Contributor

Problem

Currently the generated enums assume to be exhaustive. This is not forward compatible and can break applications severely when a new enum variant is added.

Solution

This issue is about always generating an extra enum variant unknown. Then, all unknown enum values should be deserialized into that. Additionally a new config field should be added that allows the bring back the old behavior. This will be needed for compatibility with existing users.

Implementation

Consider an enum E with variants a and b. The most obvious solution would be to generate the following enum:

enum E {
	a,
	b,
	unknown
}

The issue here is that now the client is allowed to send E.unknown to the backend, which should not be allowed. We can make it a bit more type safe. Instead we can generate the following:

sealed class EBase {}

enum E implements EBase {
	a,
	b
}

final class UnknownEnumVariant implements EBase {
	const UnknownEnumVariant._();
}

UnknownEnumVariant would be shared by all enums. The private constructor makes it not constructable outside of the contracts file. This makes the types more complicated, but makes the API safe.

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

No branches or pull requests

1 participant