ayush00git opened a new pull request, #3139:
URL: https://github.com/apache/fory/pull/3139
## Why?
As requested in Issue #3130, JavaScript implementation was missing support
for unsigned integer types that are already available in other Fory language
implementations (Rust, Go, C++, Python). This PR adds complete unsigned integer
support for JavaScript.
## What does this PR do?
This PR implements schema-based support for unsigned integer types in the
Fory JavaScript/TypeScript library:
### Added Types
- `uint8`
- `uint16`
- `uint32`
- `uint64`
- `var_uint32`
- `var_uint64`
- `tagged_uint64`
### Changes Made
#### 1. `lib/type.ts`
Added 7 unsigned integer types to the `InternalSerializerType` enum:
```typescript
export enum InternalSerializerType {
UINT8,
UINT16,
UINT32,
VAR_UINT32,
UINT64,
VAR_UINT64,
TAGGED_UINT64,
// ...
}
```
#### 2. `lib/typeInfo.ts`
**Added Type helper functions** for user-facing API:
```typescript
export const Type = {
uint8() {
return TypeInfo.fromNonParam(InternalSerializerType.UINT8 as const,
TypeId.UINT8);
},
uint16() {
return TypeInfo.fromNonParam(InternalSerializerType.UINT16 as const,
TypeId.UINT16);
},
// ...
}
```
**Updated TypeScript type hints** for proper type inference:
- Added unsigned types to HintInput number type union (returns `number` for
8/16/32-bit, `bigint` for 64-bit)
- Added unsigned types to HintResult type union for proper deserialization
type inference
#### 3. `lib/gen/number.ts`
Registered serializers for all 7 unsigned integer types using the existing
buildNumberSerializer pattern:
```typescript
CodegenRegistry.register(InternalSerializerType.UINT8,
buildNumberSerializer(
(builder, accessor) => builder.writer.uint8(accessor),
builder => builder.reader.uint8()
)
);
CodegenRegistry.register(InternalSerializerType.UINT16,
buildNumberSerializer(
(builder, accessor) => builder.writer.uint16(accessor),
builder => builder.reader.uint16()
)
);
//....
```
#### 4. `test/number.test.ts`
Added comprehensive tests for all unsigned types:
```typescript
test('should uint8 work', () => {
const fory = new Fory({ refTracking: true });
const serializer = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
a: Type.uint8()
})).serializer;
const input = fory.serialize({ a: 255 }, serializer);
const result = fory.deserialize(input);
expect(result).toEqual({ a: 255 });
});
test('should uint16 work', () => {
const fory = new Fory({ refTracking: true });
const serializer = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
a: Type.uint16()
})).serializer;
const input = fory.serialize({ a: 65535 }, serializer);
const result = fory.deserialize(input);
expect(result).toEqual({ a: 65535 });
});
\\...
```
### Usage Example
```typescript
import { Fory, Type } from '@apache-fory/fory';
const fory = new Fory();
const serializer = fory.registerSerializer(Type.struct({
typeName: 'Foo'
}, {
f1: Type.uint32(),
f2: Type.varUInt32(),
f3: Type.uint64()
})).serializer;
const data = { f1: 4294967295, f2: 1000, f3: 18446744073709551615n };
const bytes = fory.serialize(data, serializer);
const result = fory.deserialize(bytes);
```
## Related issues
- Closes #3130
## Does this PR introduce any user-facing change?
- [x] **Does this PR introduce any public API change?**
- **Yes**: Adds 7 new Type helper functions (`Type.uint8()`,
`Type.uint16()`, `Type.uint32()`, `Type.varUInt32()`, `Type.uint64()`,
`Type.varUInt64()`, `Type.taggedUInt64()`)
- These are **additive changes only** - no breaking changes to existing
APIs
- [x] **Does this PR introduce any binary protocol compatibility change?**
- **Yes**: Adds support for 7 new TypeId values (9-15) for unsigned
integers
- **Cross-language compatible**: Uses the same TypeId values as other Fory
implementations
- **Backward compatible**: Existing serialized data remains compatible
## Benchmark
N/A - this PR adds new functionality without modifying existing
serialization paths. The underlying writer/reader methods `uint8()`, `uint16()`
were already implemented and optimized; this PR only exposes them through the
type system.
---
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]