Skip to content

Commit

Permalink
Generate message type assignment in speed mode
Browse files Browse the repository at this point in the history
  • Loading branch information
be9 committed Sep 1, 2021
1 parent 512289a commit 483882e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
38 changes: 34 additions & 4 deletions packages/plugin/src/message-type-extensions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export class Create implements CustomMethodGenerator {
// const message = { boolField: false, ... };
this.makeMessageVariable(source, descriptor),

// (message as unknown as MessageTypeContainer<ScalarValuesMessage>)[MESSAGE_TYPE] = this;
this.makeTypeAssignment(source, descriptor),

// if (value !== undefined)
// reflectionMergePartial<ScalarValuesMessage>(message, value, this);
this.makeMergeIf(source, descriptor),
Expand Down Expand Up @@ -70,7 +73,7 @@ export class Create implements CustomMethodGenerator {
}


makeMessageVariable(source: TypescriptFile,descriptor: DescriptorProto) {
makeMessageVariable(source: TypescriptFile, descriptor: DescriptorProto) {
let messageType = this.interpreter.getMessageType(descriptor);
let defaultMessage = messageType.create();
return ts.createVariableStatement(
Expand All @@ -87,16 +90,43 @@ export class Create implements CustomMethodGenerator {
}


makeMergeIf(source: TypescriptFile,descriptor: DescriptorProto) {
const MessageInterface = this.imports.type(source,descriptor);
makeTypeAssignment(source: TypescriptFile, descriptor: DescriptorProto) {
const
MessageTypeContainer = this.imports.name(source, 'MessageTypeContainer', this.options.runtimeImportPath, true),
MESSAGE_TYPE = this.imports.name(source, 'MESSAGE_TYPE', this.options.runtimeImportPath),
MessageInterface = this.imports.type(source, descriptor);
;

return ts.createExpressionStatement(
ts.createAssignment(
ts.createElementAccess(
ts.createAsExpression(
ts.createAsExpression(
ts.createIdentifier("message"),
ts.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),
),
ts.createTypeReferenceNode(MessageTypeContainer, [
ts.createTypeReferenceNode(MessageInterface, undefined),
]),
),
ts.createIdentifier(MESSAGE_TYPE)
),
ts.createThis(),
),
);
}


makeMergeIf(source: TypescriptFile, descriptor: DescriptorProto) {
const MessageInterface = this.imports.type(source, descriptor);
return ts.createIf(
ts.createBinary(
ts.createIdentifier("value"),
ts.createToken(ts.SyntaxKind.ExclamationEqualsEqualsToken),
ts.createIdentifier("undefined")
),
ts.createExpressionStatement(ts.createCall(
ts.createIdentifier(this.imports.name(source,'reflectionMergePartial', this.options.runtimeImportPath)),
ts.createIdentifier(this.imports.name(source, 'reflectionMergePartial', this.options.runtimeImportPath)),
[ts.createTypeReferenceNode(
MessageInterface,
undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export {
} from './json-format-contract';

// Message type contract
export {IMessageType, PartialMessage} from './message-type-contract';
export {IMessageType, PartialMessage, MESSAGE_TYPE} from './message-type-contract';

// Message type implementation via reflection
export {MessageType} from './message-type';
Expand Down Expand Up @@ -78,7 +78,7 @@ export {ReflectionBinaryReader} from './reflection-binary-reader';
export {ReflectionBinaryWriter} from './reflection-binary-writer';
export {ReflectionJsonReader} from './reflection-json-reader';
export {ReflectionJsonWriter} from './reflection-json-writer';
export {containsMessageType, getMessageType} from './reflection-contains-message-type';
export {containsMessageType, getMessageType, MessageTypeContainer} from './reflection-contains-message-type';

// Oneof helpers
export {isOneofGroup, setOneofValue, getOneofValue, clearOneofValue, getSelectedOneofValue} from './oneof';
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime/src/reflection-contains-message-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {IMessageType, MESSAGE_TYPE} from './message-type-contract';

interface MessageTypeContainer<T extends object> {
/**
* The interface that models storing type in a symbol property.
*/
export interface MessageTypeContainer<T extends object> {
[MESSAGE_TYPE]: IMessageType<T>;
}

Expand Down

0 comments on commit 483882e

Please sign in to comment.