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

Method readField

javascript/packages/core/lib/gen/struct.ts:587–658  ·  view source on GitHub ↗
(fieldName: string, fieldTypeInfo: TypeInfo, assignStmt: (expr: string) => string, embedGenerator: SerializerGenerator)

Source from the content-addressed store, hash-verified

585 }
586
587 readField(fieldName: string, fieldTypeInfo: TypeInfo, assignStmt: (expr: string) => string, embedGenerator: SerializerGenerator) {
588 const { nullable = false, dynamic, trackingRef } = fieldTypeInfo;
589 const refMode = toRefMode(trackingRef, nullable);
590 const assignCompatible = (expr: string) => assignStmt(compatibleReadTargetExpr(fieldTypeInfo, expr));
591 if (shouldSkipCompatibleRead(fieldTypeInfo)) {
592 const discard = (expr: string) => `${expr};`;
593 if (this.builder.resolver.isMonomorphic(fieldTypeInfo, dynamic)) {
594 if (refMode == RefMode.TRACKING || refMode === RefMode.NULL_ONLY) {
595 return embedGenerator.readRefWithoutTypeInfo(discard);
596 }
597 if (isDepthFreeField(fieldTypeInfo)) {
598 return embedGenerator.read(discard, "false");
599 }
600 return embedGenerator.readWithDepth(discard, "false");
601 }
602 if (refMode == RefMode.TRACKING || refMode === RefMode.NULL_ONLY) {
603 return embedGenerator.readRef(discard);
604 }
605 return embedGenerator.readNoRef(discard, "false");
606 }
607 const scalarAction = getCompatibleScalarReadAction(fieldTypeInfo);
608 if (scalarAction) {
609 const readValue = compatibleScalarFieldReadExpr(
610 scalarAction.remoteTypeId,
611 scalarAction.localTypeId,
612 this.builder,
613 );
614 if (readValue === null) {
615 throw new Error(
616 `Unsupported compatible scalar conversion from ${scalarAction.remoteTypeId} to ${scalarAction.localTypeId}`,
617 );
618 }
619 if (scalarAction.remoteNullable !== true) {
620 return assignStmt(readValue);
621 }
622 const refFlag = this.scope.uniqueName("refFlag");
623 return `
624 const ${refFlag} = ${this.builder.reader.readInt8()};
625 switch (${refFlag}) {
626 case ${RefFlags.NotNullValueFlag}:
627 ${assignStmt(readValue)}
628 break;
629 case ${RefFlags.NullFlag}:
630 ${assignStmt("null")}
631 break;
632 default:
633 throw new Error("Invalid reference flag for compatible scalar field ${CodecBuilder.replaceBackslashAndQuote(fieldName)}");
634 }
635 `;
636 }
637 let stmt = "";
638 // polymorphic type
639 if (this.builder.resolver.isMonomorphic(fieldTypeInfo, dynamic)) {
640 if (refMode == RefMode.TRACKING || refMode === RefMode.NULL_ONLY) {
641 stmt = `
642 ${embedGenerator.readRefWithoutTypeInfo(assignCompatible)}
643 `;
644 } else if (isDepthFreeField(fieldTypeInfo)) {

Callers 1

readMethod · 0.95

Calls 14

shouldSkipCompatibleReadFunction · 0.90
toRefModeFunction · 0.85
isDepthFreeFieldFunction · 0.85
uniqueNameMethod · 0.80
readMethod · 0.65
readWithDepthMethod · 0.65
readRefMethod · 0.65
readNoRefMethod · 0.65

Tested by

no test coverage detected