Skip to content

feat(dart): add uint struct support to the codegen system#3192

Merged
chaokunyang merged 3 commits intoapache:mainfrom
ayush00git:feat/dartStruct-codegen
Jan 23, 2026
Merged

feat(dart): add uint struct support to the codegen system#3192
chaokunyang merged 3 commits intoapache:mainfrom
ayush00git:feat/dartStruct-codegen

Conversation

@ayush00git
Copy link
Contributor

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:

@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:

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

if (anno != null){

After

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.

@ayush00git
Copy link
Contributor Author

Hey @chaokunyang
I'm done with integration of the unit struct annotations to the codegen system, i think fory now supports dart's uint annotations fully along with their struct implementations. Let me know if there are any changes, and do guide me through the docs of fory and fory-site where i could document the new features : )

@chaokunyang
Copy link
Collaborator

For docs, you shoudl add it to https://github.com/apache/fory/tree/main/docs/guide/dart, which will be sync to fory-siste automatically.

@chaokunyang
Copy link
Collaborator

But I think maybe we should first address #3132 first, before adding docs. If we add docs directly to https://github.com/apache/fory/tree/main/docs/guide/dart, it willeb reelases to users in next version. But currently fory dart is not stable for a release. We need to fully align with java

@ayush00git
Copy link
Contributor Author

But I think maybe we should first address #3132 first, before adding docs. If we add docs directly to https://github.com/apache/fory/tree/main/docs/guide/dart, it willeb reelases to users in next version. But currently fory dart is not stable for a release. We need to fully align with java

Okk, i'll wait for that issue to be resolved and then begin with the documentation part,
In the meantime, could you direct me to some other issues? I'm now smooth with Go, dart, JS part of fory

Copy link
Collaborator

@chaokunyang chaokunyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chaokunyang chaokunyang merged commit bf90e1d into apache:main Jan 23, 2026
56 checks passed
@ayush00git ayush00git deleted the feat/dartStruct-codegen branch January 31, 2026 05:51
@ayush00git
Copy link
Contributor Author

But I think maybe we should first address #3132 first, before adding docs. If we add docs directly to https://github.com/apache/fory/tree/main/docs/guide/dart, it willeb reelases to users in next version. But currently fory dart is not stable for a release. We need to fully align with java

Hey @chaokunyang
The dart xlang tests were added in #3322 . Can I go with the docs now? also i've asked you a related query about the runtime integration of idl parsers on slack. Do let me know how to continue.

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.

2 participants