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 global type registry #371

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/plugin/src/code-gen/message-type-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class MessageTypeGenerator extends GeneratorBase {
MyMessage = this.imports.type(source, descriptor),
Message$Type = ts.createIdentifier(this.imports.type(source, descriptor) + '$Type'),
MessageType = ts.createIdentifier(this.imports.name(source, "MessageType", this.options.runtimeImportPath)),
RegisterTypeFunc = ts.createIdentifier(this.imports.name(source, "registerType", this.options.runtimeImportPath)),
interpreterType = this.interpreter.getMessageType(descriptor),
classDecMembers: ts.ClassElement[] = [],
classDecSuperArgs: ts.Expression[] = [ // arguments to the MessageType CTOR
Expand Down Expand Up @@ -146,9 +147,16 @@ export class MessageTypeGenerator extends GeneratorBase {
);


// registerType("messageId");
const registerType = ts.createExpressionStatement(
ts.createCall(RegisterTypeFunc, undefined, [ts.createIdentifier(MyMessage)])
);


// add to our file
source.addStatement(classDec);
source.addStatement(exportConst);
source.addStatement(registerType);


// add comments
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime/spec/json-format-contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('jsonWriteOptions()', () => {
enumAsInteger: true,
useProtoFieldName: false,
prettySpaces: 0,
typeRegistry: [],
});
expect(jsonWriteOptions({
useProtoFieldName: true,
Expand All @@ -27,6 +28,7 @@ describe('jsonWriteOptions()', () => {
enumAsInteger: false,
useProtoFieldName: true,
prettySpaces: 99,
typeRegistry: [],
});
expect(jsonWriteOptions({
emitDefaultValues: true,
Expand All @@ -38,6 +40,7 @@ describe('jsonWriteOptions()', () => {
enumAsInteger: true,
useProtoFieldName: true,
prettySpaces: 99,
typeRegistry: [],
});
expect(jsonWriteOptions({
typeRegistry: [],
Expand Down
8 changes: 7 additions & 1 deletion packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export {PbLong, PbULong} from './pb-long';

// JSON format contracts, options for reading and writing, for example
export {
JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions, jsonReadOptions, jsonWriteOptions, mergeJsonOptions
JsonReadOptions,
JsonWriteOptions,
JsonWriteStringOptions,
jsonReadOptions,
jsonWriteOptions,
mergeJsonOptions,
registerType,
} from './json-format-contract';

// Message type contract
Expand Down
20 changes: 18 additions & 2 deletions packages/runtime/src/json-format-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ export interface JsonWriteStringOptions extends JsonWriteOptions {
prettySpaces: number;
}

/**
* Globally stores message types to read and write `google.protobuf.Any` from
* and to JSON format.
*/
const typeRegistry: IMessageType<any>[] = [];

/**
* Registers a message type to the global type registry. Registered types are
* used reading and writing `google.protobuf.Any` from and to JSON format.
*/
export function registerType(t: IMessageType<any>) {
typeRegistry.push(t)
}

const defaultsWrite: Readonly<JsonWriteStringOptions> = {
emitDefaultValues: false,
enumAsInteger: false,
Expand All @@ -80,14 +94,16 @@ const defaultsWrite: Readonly<JsonWriteStringOptions> = {
* Make options for reading JSON data from partial options.
*/
export function jsonReadOptions(options?: Partial<JsonReadOptions>): Readonly<JsonReadOptions> {
return options ? {...defaultsRead, ...options} : defaultsRead;
const opts = options ? {...defaultsRead, ...options} : defaultsRead;
return mergeJsonOptions({typeRegistry}, opts) as Readonly<JsonReadOptions>;
}

/**
* Make options for writing JSON data from partial options.
*/
export function jsonWriteOptions(options?: Partial<JsonWriteStringOptions>): JsonWriteStringOptions {
return options ? {...defaultsWrite, ...options} : defaultsWrite;
const opts = options ? {...defaultsWrite, ...options} : defaultsWrite;
return mergeJsonOptions({typeRegistry}, opts) as JsonWriteStringOptions;
}


Expand Down
35 changes: 15 additions & 20 deletions packages/test-generated/spec/google.protobuf.any.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ describe('google.protobuf.Any', function () {

let scalarMsgAny = Any.pack(scalarMsg, ScalarValuesMessage);

it('throws without type registry', function () {
expect(() => Any.toJson(scalarMsgAny)).toThrow();
});

it('throws when type not in registry', function () {
expect(
() => Any.toJson(scalarMsgAny, {typeRegistry: [StructMessage]})
).toThrow();
});

it('creates expected JSON', function () {
let registry = [StructMessage, ScalarValuesMessage];
let json = Any.toJson(scalarMsgAny, {typeRegistry: registry});
Expand All @@ -100,6 +90,16 @@ describe('google.protobuf.Any', function () {
});
});

it('creates expected JSON with global type registry', function () {
let json = Any.toJson(scalarMsgAny);
expect(json).toEqual({
"@type": "type.googleapis.com/spec.ScalarValuesMessage",
doubleField: 0.5,
stringField: "hello",
boolField: true,
});
});

it('puts specialized JSON representation into the "value" property', function () {
let duration = makeDuration(1);
let durationAny = Any.pack(duration, Duration);
Expand Down Expand Up @@ -128,22 +128,17 @@ describe('google.protobuf.Any', function () {
boolField: true,
};

it('throws without type registry', function () {
expect(() => Any.fromJson(scalarMsgAnyJson)).toThrow();
});

it('throws when type not in registry', function () {
expect(
() => Any.fromJson(scalarMsgAnyJson, {typeRegistry: [StructMessage]})
).toThrow();
});

it('can read JSON', function () {
let registry = [StructMessage, ScalarValuesMessage];
let scalarMsgAny = Any.fromJson(scalarMsgAnyJson, {typeRegistry: registry});
expect(scalarMsgAny).toEqual(Any.pack(scalarMsg, ScalarValuesMessage));
});

it('can read JSON with global type registry', function () {
let scalarMsgAny = Any.fromJson(scalarMsgAnyJson);
expect(scalarMsgAny).toEqual(Any.pack(scalarMsg, ScalarValuesMessage));
});

it('can read specialized JSON', function () {
let duration = makeDuration(1);
let durationAny = Any.pack(duration, Duration);
Expand Down