Skip to content

Conversation

@ayush00git
Copy link
Contributor

@ayush00git ayush00git commented Jan 18, 2026

Why?

While unsigned integer types (UInt8, UInt16, UInt32) were added to Fory in PR #3144 with their primitive serializers and wrapper classes, they were not integrated into the framework's type system for struct serialization. This meant users could serialize individual UInt values but could not use them as fields in structs annotated with @ForyClass.

For example:

  • A struct with UInt16 port field would fail during code generation
  • Cross-language data structures using unsigned integers couldn't be represented in Dart structs
  • The type system didn't recognize UInt types as valid field types for serialization

What does this PR do?

1. Registers Unsigned Integer Types in DartTypeEnum

Added UInt8, UInt16, and UInt32 to the type enumeration in dart_type.dart:

UINT8(UInt8, true, 'UInt8', 'package', 'fory/src/datatype/uint8.dart', ObjType.UINT8, true, 'dart:core@UInt8'),
UINT16(UInt16, true, 'UInt16', 'package', 'fory/src/datatype/uint16.dart', ObjType.UINT16, true, 'dart:core@UInt16'),
UINT32(UInt32, true, 'UInt32', 'package', 'fory/src/datatype/uint32.dart', ObjType.UINT32, true, 'dart:core@UInt32'),

This enables the code generator to recognize unsigned types during static analysis and generate proper serialization metadata.

2. Integrates Serializers into SerializerPool

Registered the serializers in the framework initialization:

type2Ser[UInt8]!.ser = UInt8Serializer.cache.getSerializer(conf);
type2Ser[UInt16]!.ser = UInt16Serializer.cache.getSerializer(conf);
type2Ser[UInt32]!.ser = UInt32Serializer.cache.getSerializer(conf);

This ensures serializers are available when the framework starts and can be used for struct field serialization.

3. Adds Comprehensive Test Suite for Struct Serialization

Created test entity and test cases:

@ForyClass(promiseAcyclic: true)
class UIntStruct with _$UIntStructFory {
  final UInt8 age;
  final UInt16 port;
  final UInt32 count;

  const UIntStruct(this.age, this.port, this.count);
}

Test coverage:

  • Normal value serialization/deserialization
  • Maximum boundary values (255, 65535, 4294967295)
  • Minimum boundary values (0, 0, 0)
  • Round-trip serialization integrity

Run tests:

cd packages/fory-test
dart test test/struct_test/uint_struct_test.dart

Related issues

Completes the unsigned integer types support initiated in PR #3144.

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?

    • Dart: UInt8, UInt16, UInt32 can now be used as fields in @ForyClass structs
    • The wrapper classes themselves were already public (added in feat: add unsigned number for dart #3144)
    • This PR enables their use in code generation and struct serialization contexts
  • Does this PR introduce any binary protocol compatibility change?

    • No changes to binary encoding format
    • Uses existing serializers from feat: add unsigned number for dart #3144 (UInt8: 1 byte, UInt16: 2 bytes LE, UInt32: 4 bytes LE)
    • Type IDs remain the same: UINT8 (40), UINT16 (41), UINT32 (42)

Benchmark

N/A, The serializers themselves were already implemented in #3144. This PR only adds registration overhead during framework initialization, which is negligible..

@ayush00git
Copy link
Contributor Author

Hey @chaokunyang
Have a look, the struct serializer support for the uint types is now implemented and tested, I've tested the changes locally and they're working fine...

@ayush00git
Copy link
Contributor Author

Hey @chaokunyang
Could you please review this PR, i am beginning my work on integrating the dart uint annotations to the code generation system, as we discussed in the PR #3144

@chaokunyang chaokunyang merged commit 4b53e0a into apache:main Jan 19, 2026
54 checks passed
@ayush00git ayush00git deleted the feat/struct-serializer branch January 21, 2026 05:37
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