feat(dart): add uint annotation types to the fory's codegen system#3181
Merged
chaokunyang merged 11 commits intoapache:mainfrom Jan 22, 2026
Merged
feat(dart): add uint annotation types to the fory's codegen system#3181chaokunyang merged 11 commits intoapache:mainfrom
chaokunyang merged 11 commits intoapache:mainfrom
Conversation
Contributor
Author
|
Hey @chaokunyang |
Collaborator
|
@ayush00git Please add tests for feature pull request. |
Contributor
Author
|
Hey @chaokunyang |
chaokunyang
reviewed
Jan 22, 2026
dart/packages/fory/lib/src/codegen/analyze/impl/field/field_analyzer_impl.dart
Show resolved
Hide resolved
Contributor
Author
|
Hey @chaokunyang |
Contributor
Author
|
Hey @chaokunyang |
2 tasks
chaokunyang
pushed a commit
that referenced
this pull request
Jan 23, 2026
## Why? While the uint annotation types were integrated into the code generation system in the PR #3181 , there were two remaining issues: 1. **No annotated struct support and test coverage for annotated structs**: The annotation system was integrated but not tested with actual struct fields using the annotations 2. **Null check error in key_annotation_analyzer**: When fields didn't have `@ForyKey` annotations, the code generator crashed with a null check error at `key_annotation_analyzer.dart` ## What does this PR do? ### 1. Created Test Entity with Annotated Uint Fields Added `uint_annotated_struct.dart` demonstrating all uint annotation variants: ```dart @ForyClass(promiseAcyclic: true) class UIntAnnotatedStruct with _$UIntAnnotatedStructFory { @uint8type() final int age; @Uint16Type() final int port; @Uint32Type() final int count; @Uint32Type(encoding: UintEncoding.varint) final int varCount; @Uint64Type() final int id; @Uint64Type(encoding: UintEncoding.varint) final int varId; @Uint64Type(encoding: UintEncoding.tagged) final int taggedId; } ``` ### 2. Added Comprehensive Test Suite Created `uint_annotated_struct_test.dart` with test coverage for: * Basic serialization/deserialization with annotated fields * Max value handling for uint8 (255), uint16 (65535), uint32 (4294967295) * Min value handling (0 for all types) * Varint encoding efficiency verification **Run tests:** ```bash cd dart/packages/fory-test dart run build_runner build --delete-conflicting-outputs dart test test/struct_test/uint_annotated_struct_test.dart ``` ### 3. Fixed Null Check Error in Key Annotation Analyzer **Problem:** The `key_annotation_analyzer.dart` was attempting to access annotation fields even when no `@ForyKey` annotation was present, causing a null check operator error. **Solution:** Added additional check to ensure we only access annotation fields when a `@ForyKey` annotation is actually found: Before ```dart if (anno != null){ ``` After ```dart if (getMeta && anno != null){ ``` This ensures that: - We only try to read annotation fields when `getMeta` is true (meaning we found a `@ForyKey` annotation) - Fields without `@ForyKey` annotations correctly default to `includeFromFory: true` and `includeToFory: true` - Code generation no longer crashes on structs with unannotated fields ## Related issues Completes the uint annotation testing and fixes a critical bug that prevented code generation from working with fields lacking `@ForyKey` annotations. ## Does this PR introduce any user-facing change? * [ ] Does this PR introduce any public API change? * No new APIs - only adds test coverage and fixes a bug * [ ] Does this PR introduce any binary protocol compatibility change? * No changes to binary encoding format * Bug fix only affects code generation, not runtime serialization ## Benchmark N/A - This PR adds test coverage and fixes a code generation bug. No runtime performance impact.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
While unsigned integer type annotations (
@Uint8Type,@Uint16Type,@Uint32Type,@Uint64Type) were added in PR #3144, they were not integrated into the code generation system. This meant:@Uint8Type() int agein struct fieldsWhat does this PR do?
1. Created Uint Annotation Analyzer
Added
uint_annotation_analyzer.dartto detect and parse uint type annotations during code generation:Supported annotations:
@Uint8Type()→ObjType.UINT8@Uint16Type()→ObjType.UINT16@Uint32Type()→ObjType.UINT32@Uint32Type(encoding: UintEncoding.varint)→ObjType.VAR_UINT32@Uint64Type()→ObjType.UINT64@Uint64Type(encoding: UintEncoding.varint)→ObjType.VAR_UINT64@Uint64Type(encoding: UintEncoding.tagged)→ObjType.TAGGED_UINT642. Extended Type Identifier System
Updated
analysis_type_identifier.dartto recognize uint annotation types:3. Integrated Annotation-Based Type Override
Modified
type_analyzer_impl.dartto support annotation-based type override:4. Updated Field Analyzer
Modified
field_analyzer_impl.dartto check for uint annotations:Related issues
Completes the unsigned integer annotation types support initiated in PR #3144 by integrating the annotations into the code generation system.
Does this PR introduce any user-facing change?
Does this PR introduce any public API change?
@Uint8Type(),@Uint16Type(),@Uint32Type(),@Uint64Type()annotations on nativeintfields in@ForyClassstructsencodingparameter for uint32/uint64@Uint8Type() int ageinstead ofUInt8 ageDoes this PR introduce any binary protocol compatibility change?
Benchmark
N/A - This PR only adds annotation processing during code generation (build-time). No runtime performance impact.