Skip to content

Added Encoding for Type Meta #1782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Encoded props parameter
  • Loading branch information
Forchapeatl authored Aug 7, 2024
commit ce41f3e90fddffc919ba515fa09d171e1c7b023f
14 changes: 10 additions & 4 deletions javascript/packages/fury/lib/gen/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const computeStringHash = (str: string) => {
return hash;
};


const computeStructHash = (description: TypeDescription) => {
let hash = 17;
for (const [, value] of Object.entries((<ObjectTypeDescription>description).options.props).sort()) {
Expand All @@ -81,10 +82,13 @@ class ObjectSerializerGenerator extends BaseSerializerGenerator {
const options = this.description.options;
const expectHash = computeStructHash(this.description);
const metaInformation = Buffer.from(computeMetaInformation(this.description));
const decodedInformation = decodeMetaInformation(metaInformation);
const propsInformation = Buffer.from(computeMetaInformation(options.props));

return `
${this.builder.writer.int32(expectHash)};
${this.builder.writer.buffer(`Buffer.from("${metaInformation.toString("base64")}", "base64")`)};
${this.builder.writer.buffer(`Buffer.from("${propsInformation.toString("base64")}", "base64")`)}
${Object.entries(options.props).sort().map(([key, inner]) => {
const InnerGeneratorClass = CodegenRegistry.get(inner.type);
if (!InnerGeneratorClass) {
Expand All @@ -100,23 +104,24 @@ class ObjectSerializerGenerator extends BaseSerializerGenerator {
const options = this.description.options;
const expectHash = computeStructHash(this.description);
const encodedMetaInformation = computeMetaInformation(this.description);
const encodedPropsInformation = computeMetaInformation(options.props);
const metaInformation = decodeMetaInformation(encodedMetaInformation);
const result = this.scope.uniqueName("result");
const pass = this.builder.reader.int32();
const handler = this.scope.declare("handler","");
//const handler = this.scope.declare("handler","");

return `
if (${this.builder.reader.int32()} !== ${expectHash}) {
throw new Error("got ${this.builder.reader.int32()} validate hash failed: ${this.safeTag()}. expect ${expectHash}");
}
if(${handler} == ""){
${handler} = ${this.builder.classResolver.getSerializerByTag(decodeMetaInformation(encodedMetaInformation))};
}
const ${result} = {
${Object.entries(options.props).sort().map(([key]) => {
return `${CodecBuilder.safePropName(key)}: null`;
}).join(",\n")}
};
${this.maybeReference(result, refState)}
${this.builder.reader.buffer(encodedMetaInformation.byteLength)}
${this.builder.reader.buffer(encodedPropsInformation.byteLength)}
${Object.entries(options.props).sort().map(([key, inner]) => {
const InnerGeneratorClass = CodegenRegistry.get(inner.type);
if (!InnerGeneratorClass) {
Expand All @@ -129,6 +134,7 @@ class ObjectSerializerGenerator extends BaseSerializerGenerator {
`;
}


private safeTag() {
return CodecBuilder.replaceBackslashAndQuote(this.description.options.tag);
}
Expand Down
Loading