MCPcopy Create free account
hub / github.com/apache/fory / read

Method read

javascript/packages/core/lib/gen/struct.ts:815–873  ·  view source on GitHub ↗
(accessor: (expr: string) => string, refState: string)

Source from the content-addressed store, hash-verified

813 }
814
815 read(accessor: (expr: string) => string, refState: string): string {
816 const result = this.scope.uniqueName("result");
817 if (!this.typeInfo.options?.props || Object.keys(this.typeInfo.options.props).length === 0) {
818 return `
819 let ${result} = ${this.serializerExpr}.read(${refState});
820 ${accessor(result)};
821 `;
822 }
823 const hash = this.typeMeta.computeStructHash();
824 const directNumericObjectRead = this.readDirectNumericObject(accessor, refState);
825 if (directNumericObjectRead !== null) {
826 return `
827 ${!this.builder.resolver.isCompatible()
828? `
829 if(${this.builder.reader.readInt32()} !== ${hash}) {
830 throw new Error("Read class version is not consistent with ${hash} ")
831 }
832 `
833: ""}
834 ${directNumericObjectRead}
835 `;
836 }
837 return `
838 ${!this.builder.resolver.isCompatible()
839? `
840 if(${this.builder.reader.readInt32()} !== ${hash}) {
841 throw new Error("Read class version is not consistent with ${hash} ")
842 }
843 `
844: ""}
845 ${this.typeInfo.options!.withConstructor
846 ? `
847 const ${result} = new ${this.builder.getOptions("creator")}();
848 `
849 : `
850 const ${result} = {
851 ${this.sortedProps.map(({ key }) => {
852 if (shouldSkipCompatibleRead(this.typeInfo.options!.props![key])) {
853 return "";
854 }
855 return `${CodecBuilder.safePropName(key)}: null`;
856 }).filter(Boolean).join(",\n")}
857 };
858 `
859 }
860 ${this.maybeReference(result, refState)}
861 ${this.sortedProps.map(({ key, typeInfo }) => {
862 const InnerGeneratorClass = CodegenRegistry.get(typeInfo.typeId);
863 if (!InnerGeneratorClass) {
864 throw new Error(`${typeInfo.typeId} generator not exists`);
865 }
866 const innerGenerator = new InnerGeneratorClass(typeInfo, this.builder, this.scope);
867 return `
868 ${this.readField(key, typeInfo, expr => `${result}${CodecBuilder.safePropAccessor(key)} = ${expr}`, innerGenerator.readEmbed())}
869 `;
870 }).join(";\n")}
871 ${accessor(result)}
872 `;

Callers 1

readNoRefMethod · 0.95

Calls 14

readFieldMethod · 0.95
shouldSkipCompatibleReadFunction · 0.90
uniqueNameMethod · 0.80
getOptionsMethod · 0.80
safePropNameMethod · 0.80
safePropAccessorMethod · 0.80
getMethod · 0.65
readEmbedMethod · 0.65
keysMethod · 0.45
computeStructHashMethod · 0.45
isCompatibleMethod · 0.45

Tested by

no test coverage detected