feat(dart): Introduce id based enum serialization#3482
feat(dart): Introduce id based enum serialization#3482yash-agarwa-l wants to merge 8 commits intoapache:mainfrom
Conversation
analyze if the input is valid and pass to spec update EnumSpec to support idToValue map for id-based enum serialization
30a3919 to
1c56dfe
Compare
|
Firstly, Thank you sm for the clarification about this in the discussions, it took some time to complete it.
|
|
Could we also support this pattern? @ ForyEnum()
enum UserRole {
guest(0),
member(1),
admin(2);
@ForyEnumId()
final int code;
const UserRole(this.code);
} |
|
|
||
| part '../generated/enum_id_foo.g.dart'; | ||
|
|
||
| @foryEnum |
There was a problem hiding this comment.
Yes, we should do it as per conventions, but the existing files also follow the same pattern. If you prefer, shall I make a separate PR for that first before this one?
Yes, I had this in mind initially too. I went with per-value annotation because I feel the id is a metadata for values and since Dart enums don't support private fields, so the |
Why?
The Dart module, only supports ordinal enum serialization, where each enum
value is serialized as its position index (0, 1, 2...), which breaks in different
cases like adding or reordering values. This adds optional id based serialization
with so that we can provide unique id to every while preserving ordinal serialization
as the default fallback.
What does this PR do?
- partial @ForyEnumId usage
- duplicate IDs
Related issues
AI Contribution Checklist
AI Usage Disclosure
AI assistance was used to suggest tests and for better warnings in enum_analyzer.
dart/packages/fory/lib/src/codegen/analyze/impl/struct/enum_analyzer_impl.dart
dart/packages/fory-test/test/
Does this PR introduce any user-facing change?
@ForyEnumIdis a new annotation users will write in their codeForyEnumIdclass is publicly accessibleBenchmark
General struct performance remained similar because those benchmarks do not exercise enum serialization paths.