This is an automated email from the ASF dual-hosted git repository.
wangweipeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new aef46ad6d feat(JavaScript): Align testcases (#3321)
aef46ad6d is described below
commit aef46ad6da266b59081f8b971648c3dc28c6ae93
Author: weipeng <[email protected]>
AuthorDate: Wed Feb 11 08:20:19 2026 +0800
feat(JavaScript): Align testcases (#3321)
---
javascript/packages/fory/lib/gen/collection.ts | 5 +-
javascript/packages/fory/lib/gen/map.ts | 88 +++++++++++++-------------
javascript/test/crossLanguage.test.ts | 29 +++------
3 files changed, 56 insertions(+), 66 deletions(-)
diff --git a/javascript/packages/fory/lib/gen/collection.ts
b/javascript/packages/fory/lib/gen/collection.ts
index 808d92546..16cc6689e 100644
--- a/javascript/packages/fory/lib/gen/collection.ts
+++ b/javascript/packages/fory/lib/gen/collection.ts
@@ -130,12 +130,13 @@ class CollectionAnySerializer {
} else {
const serializer =
this.fory.typeResolver.getSerializerByData(item);
this.fory.binaryWriter.int8(RefFlags.NotNullValueFlag);
- serializer!.write(item);
+ serializer!.writeNoRef(item);
}
}
} else {
for (const item of value) {
- serializer!.write(item);
+ const serializer = this.fory.typeResolver.getSerializerByData(item);
+ serializer!.writeNoRef(item);
}
}
}
diff --git a/javascript/packages/fory/lib/gen/map.ts
b/javascript/packages/fory/lib/gen/map.ts
index e7f2a2d19..372ac5c73 100644
--- a/javascript/packages/fory/lib/gen/map.ts
+++ b/javascript/packages/fory/lib/gen/map.ts
@@ -37,25 +37,23 @@ const MapFlags = {
DECL_ELEMENT_TYPE: 0b100,
};
-class MapHeadUtil {
- private static IS_NULL = 0b10;
- private static TRACKING_REF = 0b01;
- static elementInfo(typeId: number, isNull: 0 | 1, trackRef: 0 | 1) {
- return (typeId << 16) | (isNull << 1) | trackRef;
+class ElementInfo {
+ constructor(public serializer: Serializer | null, public isNull: boolean,
public trackRef: boolean) {
}
- static isNull(info: number) {
- return info & this.IS_NULL;
- }
-
- static trackingRef(info: number) {
- return info & this.TRACKING_REF;
+ equalTo(other: ElementInfo | null) {
+ if (other === null) {
+ return false;
+ }
+ return this.serializer === other.serializer
+ && this.isNull === other.isNull
+ && this.trackRef === other.trackRef;
}
}
class MapChunkWriter {
- private preKeyInfo = 0;
- private preValueInfo = 0;
+ private preKeyInfo: ElementInfo | null = null;
+ private preValueInfo: ElementInfo | null = null;
private chunkSize = 0;
private chunkOffset = 0;
@@ -65,22 +63,22 @@ class MapChunkWriter {
}
- private getHead(keyInfo: number, valueInfo: number) {
+ private getHead(keyInfo: ElementInfo, valueInfo: ElementInfo) {
let flag = 0;
- if (MapHeadUtil.isNull(valueInfo)) {
+ if (valueInfo.isNull) {
flag |= MapFlags.HAS_NULL;
}
- if (MapHeadUtil.trackingRef(valueInfo)) {
+ if (valueInfo.trackRef) {
flag |= MapFlags.TRACKING_REF;
}
if (this.valueSerializer) {
flag |= MapFlags.DECL_ELEMENT_TYPE;
}
flag <<= 3;
- if (MapHeadUtil.isNull(keyInfo)) {
+ if (keyInfo.isNull) {
flag |= MapFlags.HAS_NULL;
}
- if (MapHeadUtil.trackingRef(keyInfo)) {
+ if (keyInfo.trackRef) {
flag |= MapFlags.TRACKING_REF;
}
if (this.keySerializer) {
@@ -89,7 +87,7 @@ class MapChunkWriter {
return flag;
}
- private writeHead(keyInfo: number, valueInfo: number, withOutSize = false) {
+ private writeHead(keyInfo: ElementInfo, valueInfo: ElementInfo, withOutSize
= false) {
// KV header
const header = this.getHead(keyInfo, valueInfo);
// chunkSize default 0 | KV header
@@ -102,12 +100,6 @@ class MapChunkWriter {
} else {
this.chunkOffset = 0;
}
- if (this.keySerializer) {
- this.keySerializer.writeTypeInfo(null);
- }
- if (this.valueSerializer) {
- this.valueSerializer.writeTypeInfo(null);
- }
return header;
}
@@ -115,8 +107,8 @@ class MapChunkWriter {
return this.chunkSize === 0 || this.chunkSize === 1;
}
- next(keyInfo: number, valueInfo: number) {
- if (keyInfo & MapFlags.HAS_NULL || valueInfo & MapFlags.HAS_NULL) {
+ next(keyInfo: ElementInfo, valueInfo: ElementInfo) {
+ if (keyInfo.isNull || valueInfo.isNull) {
this.endChunk();
this.header = this.writeHead(keyInfo, valueInfo, true);
this.preKeyInfo = keyInfo;
@@ -126,8 +118,8 @@ class MapChunkWriter {
// max size of chunk is 255
if (this.chunkSize == 255
|| this.chunkOffset == 0
- || this.preKeyInfo !== keyInfo
- || this.preValueInfo !== valueInfo
+ || keyInfo.equalTo(this.preKeyInfo)
+ || valueInfo.equalTo(this.preValueInfo)
) {
// new chunk
this.endChunk();
@@ -149,16 +141,8 @@ class MapChunkWriter {
}
class MapAnySerializer {
- private keySerializer: Serializer | null = null;
- private valueSerializer: Serializer | null = null;
+ constructor(private fory: Fory, private keySerializer: Serializer | null,
private valueSerializer: Serializer | null) {
- constructor(private fory: Fory, keySerializerId: null | number,
valueSerializerId: null | number) {
- if (keySerializerId !== null) {
- fory.typeResolver.getSerializerById(keySerializerId);
- }
- if (valueSerializerId !== null) {
- fory.typeResolver.getSerializerById(valueSerializerId);
- }
}
private writeFlag(header: number, v: any) {
@@ -187,8 +171,8 @@ class MapAnySerializer {
const valueSerializer = this.valueSerializer !== null ?
this.valueSerializer : this.fory.typeResolver.getSerializerByData(v);
const header = mapChunkWriter.next(
- MapHeadUtil.elementInfo(keySerializer?.getTypeId() ?? 0, k == null ? 1
: 0, keySerializer?.needToWriteRef() ? 1 : 0),
- MapHeadUtil.elementInfo(valueSerializer?.getTypeId() ?? 0, v == null ?
1 : 0, valueSerializer?.needToWriteRef() ? 1 : 0)
+ new ElementInfo(keySerializer || null, k == null,
keySerializer?.needToWriteRef() || false),
+ new ElementInfo(valueSerializer || null, v == null,
valueSerializer?.needToWriteRef() || false)
);
const keyHeader = header & 0b111;
const valueHeader = (header >> 3);
@@ -396,8 +380,16 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
if (!this.isAny()) {
return this.writeSpecificType(accessor);
}
- return `new (${anySerializer})(${this.builder.getForyName()},
${this.typeInfo.options.key.typeId !== TypeId.UNKNOWN ?
this.typeInfo.options.key.typeId : null
- }, ${this.typeInfo.options.value.typeId !== TypeId.UNKNOWN ?
this.typeInfo.options.value.typeId : null
+ const innerSerializer = (innerTypeInfo: TypeInfo) => {
+ return this.scope.declare(
+ "map_inner_ser",
+ TypeId.isNamedType(innerTypeInfo.typeId)
+ ?
this.builder.typeResolver.getSerializerByName(CodecBuilder.replaceBackslashAndQuote(innerTypeInfo.named!))
+ : this.builder.typeResolver.getSerializerById(innerTypeInfo.typeId,
innerTypeInfo.userTypeId)
+ );
+ };
+ return `new (${anySerializer})(${this.builder.getForyName()},
${this.typeInfo.options.key.typeId !== TypeId.UNKNOWN ?
innerSerializer(this.typeInfo.options.key) : null
+ }, ${this.typeInfo.options.value.typeId !== TypeId.UNKNOWN ?
innerSerializer(this.typeInfo.options.value) : null
}).write(${accessor})`;
}
@@ -486,8 +478,16 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
if (!this.isAny()) {
return this.readSpecificType(accessor, refState);
}
- return accessor(`new (${anySerializer})(${this.builder.getForyName()},
${this.typeInfo.options.key.typeId !== TypeId.UNKNOWN ?
(this.typeInfo.options.key.typeId) : null
- }, ${this.typeInfo.options.value.typeId !== TypeId.UNKNOWN ?
(this.typeInfo.options.value.typeId) : null
+ const innerSerializer = (innerTypeInfo: TypeInfo) => {
+ return this.scope.declare(
+ "map_inner_ser",
+ TypeId.isNamedType(innerTypeInfo.typeId)
+ ?
this.builder.typeResolver.getSerializerByName(CodecBuilder.replaceBackslashAndQuote(innerTypeInfo.named!))
+ : this.builder.typeResolver.getSerializerById(innerTypeInfo.typeId,
innerTypeInfo.userTypeId)
+ );
+ };
+ return accessor(`new (${anySerializer})(${this.builder.getForyName()},
${this.typeInfo.options.key.typeId !== TypeId.UNKNOWN ?
innerSerializer(this.typeInfo.options.key) : null
+ }, ${this.typeInfo.options.value.typeId !== TypeId.UNKNOWN ?
innerSerializer(this.typeInfo.options.value) : null
}).read(${refState})`);
}
diff --git a/javascript/test/crossLanguage.test.ts
b/javascript/test/crossLanguage.test.ts
index 6fab9a1ce..a21925f71 100644
--- a/javascript/test/crossLanguage.test.ts
+++ b/javascript/test/crossLanguage.test.ts
@@ -761,35 +761,29 @@ describe("bool", () => {
});
test("test_polymorphic_list", () => {
- if (Boolean("1")) { return; }
const fory = new Fory({
compatible: true
});
// Define Animal interface implementations
@Type.struct(302, {
- age: Type.int32(),
+ age: Type.varInt32(),
name: Type.string()
})
class Dog {
age: number = 0;
+ @ForyField({ nullable: true })
name: string | null = null;
-
- getAge() { return this.age; }
- speak() { return "Woof"; }
}
fory.registerSerializer(Dog);
@Type.struct(303, {
- age: Type.int32(),
- lives: Type.int32()
+ age: Type.varInt32(),
+ lives: Type.varInt32()
})
class Cat {
age: number = 0;
lives: number = 0;
-
- getAge() { return this.age; }
- speak() { return "Meow"; }
}
fory.registerSerializer(Cat);
@@ -819,6 +813,7 @@ describe("bool", () => {
// Serialize each deserialized item back
for (const item of deserializedData) {
const serializedData = fory.serialize(item);
+ const s = fory.deserialize(serializedData)
writer.buffer(serializedData);
}
@@ -826,35 +821,29 @@ describe("bool", () => {
});
test("test_polymorphic_map", () => {
- if (Boolean("1")) { return; }
const fory = new Fory({
compatible: true
});
// Define Animal interface implementations
@Type.struct(302, {
- age: Type.int32(),
+ age: Type.varInt32(),
name: Type.string()
})
class Dog {
age: number = 0;
+ @ForyField({ nullable: true})
name: string | null = null;
-
- getAge() { return this.age; }
- speak() { return "Woof"; }
}
fory.registerSerializer(Dog);
@Type.struct(303, {
- age: Type.int32(),
- lives: Type.int32()
+ age: Type.varInt32(),
+ lives: Type.varInt32()
})
class Cat {
age: number = 0;
lives: number = 0;
-
- getAge() { return this.age; }
- speak() { return "Meow"; }
}
fory.registerSerializer(Cat);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]