Skip to content
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

Add message branding with types #147

Merged
merged 6 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
38 changes: 38 additions & 0 deletions packages/runtime/spec/reflection-get-type.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {MessageType, ScalarType, reflectionGetType, reflectionIsProtoMessage} from '../src';

describe('reflectionGetType', () => {
interface MyMessage {
stringField: string;
}

const MyMessage: MessageType<MyMessage> = new MessageType<MyMessage>('.test.MyMessage', [
{no: 1, name: 'string_field', kind: "scalar", T: ScalarType.STRING},
]);

it('allows to extract type after .create', () => {
const msg = MyMessage.create({
stringField: "hello world",
});

expect(reflectionIsProtoMessage(msg)).toBeTrue();
expect(reflectionGetType(msg)).toEqual(MyMessage);
});

it('allows to extract type after .clone', () => {
const msg = MyMessage.create({
stringField: "hello world",
});

const cloned = MyMessage.clone(msg);

expect(reflectionIsProtoMessage(cloned)).toBeTrue();
expect(reflectionGetType(cloned)).toEqual(MyMessage);
});

it('returns error if provided a non-message', () => {
const msg: MyMessage = { stringField: "foo" };

expect(reflectionIsProtoMessage(msg)).toBeFalse();
expect(() => reflectionGetType(msg)).toThrowError(/msg is not a protobuf message/);
});
});
3 changes: 3 additions & 0 deletions packages/runtime/spec/reflection-merge-partial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
normalizeFieldInfo,
reflectionCreate,
reflectionMergePartial,
REFLECTION_BRAND,
RepeatType,
ScalarType,
UnknownMessage
Expand Down Expand Up @@ -142,10 +143,12 @@ describe('reflectionMergePartial()', () => {

it('keeps target array instance', () => {
let target: UnknownMessage = {
[REFLECTION_BRAND]: type,
arr: [1,2,3]
};
let targetArr = target.arr;
let source = {
[REFLECTION_BRAND]: type,
arr: []
};
reflectionMergePartial(type, source, target);
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ 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 {REFLECTION_BRAND} from './reflection-brand';
export {reflectionGetType, reflectionIsProtoMessage} from './reflection-get-type';

// Oneof helpers
export {isOneofGroup, setOneofValue, getOneofValue, clearOneofValue, getSelectedOneofValue} from './oneof';
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/message-type-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {FieldInfo, MessageInfo} from "./reflection-info";
import type {BinaryReadOptions, BinaryWriteOptions, IBinaryReader, IBinaryWriter} from "./binary-format-contract";
import type {JsonValue} from "./json-typings";
import type {JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions} from "./json-format-contract";
import {REFLECTION_BRAND} from './reflection-brand';
odashevskii-plaid marked this conversation as resolved.
Show resolved Hide resolved

/**
* Similar to `Partial<T>`, but recursive, and keeps `oneof` groups
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime/src/reflection-brand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** The symbol used as a key on message objects to store the message type. */
export const REFLECTION_BRAND = Symbol('protobufReflection');
odashevskii-plaid marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 4 additions & 2 deletions packages/runtime/src/reflection-create.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type {MessageInfo} from "./reflection-info";
import {reflectionScalarDefault} from "./reflection-scalar-default";
import type {UnknownMessage, UnknownOneofGroup} from "./unknown-types";

import { REFLECTION_BRAND } from './reflection-brand';

/**
* Creates an instance of the generic message, using the field
* information.
*/
export function reflectionCreate(info: MessageInfo): any {
let msg: UnknownMessage = {};
let msg: UnknownMessage = {
[REFLECTION_BRAND]: info,
};
for (let field of info.fields) {
timostamm marked this conversation as resolved.
Show resolved Hide resolved
let name = field.localName;
if (field.opt)
Expand Down
25 changes: 25 additions & 0 deletions packages/runtime/src/reflection-get-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {REFLECTION_BRAND} from './reflection-brand';
import type {IMessageType} from './message-type-contract';
import type {UnknownMessage} from './unknown-types';

/**
* Check if the provided object is a proto message.
*/
export function reflectionIsProtoMessage<T extends object>(msg: T): boolean {
const msgType = (msg as UnknownMessage)[REFLECTION_BRAND];

return msgType != null;
}

/**
* Get a message type from a proto message.
*/
export function reflectionGetType<T extends object>(msg: T): IMessageType<T> {
const msgType = (msg as UnknownMessage)[REFLECTION_BRAND];

if (msgType == null) {
throw Error('reflectionGetType: msg is not a protobuf message');
}
timostamm marked this conversation as resolved.
Show resolved Hide resolved

return msgType as IMessageType<T>;
}
3 changes: 3 additions & 0 deletions packages/runtime/src/unknown-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { REFLECTION_BRAND } from './reflection-brand';

/**
* A message of unknown type.
*/
export interface UnknownMessage {
[REFLECTION_BRAND]?: any;
odashevskii-plaid marked this conversation as resolved.
Show resolved Hide resolved
[k: string]:
| UnknownScalar
| UnknownEnum
Expand Down