Skip to content

Commit

Permalink
feat: no-file-descriptor setting for outputSchema option (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukealvoeiro committed Jun 7, 2024
1 parent 58deee2 commit c54f06c
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=annotateFilesWithVersion=false`, the generated files will not contain the versions of `protoc` and `ts-proto` used to generate the file. This option is normally set to `true`, such that files list the versions used.

- With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.
- With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators. If outputSchema is instead specified to be `no-file-descriptor` then we do not include the file descriptor in the generated schema. This is useful if you are trying to minimize the size of the generated schema.

- With `--ts_proto_opt=outputTypeAnnotations=true`, each message will be given a `$type` field containing its fully-qualified name. You can use `--ts_proto_opt=outputTypeAnnotations=static-only` to omit it from the `interface` declaration, or `--ts_proto_opt=outputTypeAnnotations=optional` to make it an optional property on the `interface` definition. The latter option may be useful if you want to use the `$type` field for runtime type checking on responses from a server.

Expand Down
Binary file added integration/schema-no-file-descriptor/const-enum.bin
Binary file not shown.
13 changes: 13 additions & 0 deletions integration/schema-no-file-descriptor/const-enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

message DividerData {
enum DividerType {
DOUBLE = 0;
SINGLE = 1;
DASHED = 2;
DOTTED = 3;
}

DividerType type = 1;
map<string, DividerType> typeMap = 2;
}
281 changes: 281 additions & 0 deletions integration/schema-no-file-descriptor/const-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// source: const-enum.proto

/* eslint-disable */
import * as _m0 from "protobufjs/minimal";

export const protobufPackage = "";

export interface DividerData {
type: DividerData_DividerType;
typeMap: { [key: string]: DividerData_DividerType };
}

export enum DividerData_DividerType {
DOUBLE = 0,
SINGLE = 1,
DASHED = 2,
DOTTED = 3,
UNRECOGNIZED = -1,
}

export function dividerData_DividerTypeFromJSON(object: any): DividerData_DividerType {
switch (object) {
case 0:
case "DOUBLE":
return DividerData_DividerType.DOUBLE;
case 1:
case "SINGLE":
return DividerData_DividerType.SINGLE;
case 2:
case "DASHED":
return DividerData_DividerType.DASHED;
case 3:
case "DOTTED":
return DividerData_DividerType.DOTTED;
case -1:
case "UNRECOGNIZED":
default:
return DividerData_DividerType.UNRECOGNIZED;
}
}

export function dividerData_DividerTypeToJSON(object: DividerData_DividerType): string {
switch (object) {
case DividerData_DividerType.DOUBLE:
return "DOUBLE";
case DividerData_DividerType.SINGLE:
return "SINGLE";
case DividerData_DividerType.DASHED:
return "DASHED";
case DividerData_DividerType.DOTTED:
return "DOTTED";
case DividerData_DividerType.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
}
}

export interface DividerData_TypeMapEntry {
key: string;
value: DividerData_DividerType;
}

function createBaseDividerData(): DividerData {
return { type: 0, typeMap: {} };
}

export const DividerData = {
encode(message: DividerData, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.type !== 0) {
writer.uint32(8).int32(message.type);
}
Object.entries(message.typeMap).forEach(([key, value]) => {
DividerData_TypeMapEntry.encode({ key: key as any, value }, writer.uint32(18).fork()).ldelim();
});
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): DividerData {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseDividerData();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 8) {
break;
}

message.type = reader.int32() as any;
continue;
case 2:
if (tag !== 18) {
break;
}

const entry2 = DividerData_TypeMapEntry.decode(reader, reader.uint32());
if (entry2.value !== undefined) {
message.typeMap[entry2.key] = entry2.value;
}
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): DividerData {
return {
type: isSet(object.type) ? dividerData_DividerTypeFromJSON(object.type) : 0,
typeMap: isObject(object.typeMap)
? Object.entries(object.typeMap).reduce<{ [key: string]: DividerData_DividerType }>((acc, [key, value]) => {
acc[key] = dividerData_DividerTypeFromJSON(value);
return acc;
}, {})
: {},
};
},

toJSON(message: DividerData): unknown {
const obj: any = {};
if (message.type !== 0) {
obj.type = dividerData_DividerTypeToJSON(message.type);
}
if (message.typeMap) {
const entries = Object.entries(message.typeMap);
if (entries.length > 0) {
obj.typeMap = {};
entries.forEach(([k, v]) => {
obj.typeMap[k] = dividerData_DividerTypeToJSON(v);
});
}
}
return obj;
},

create<I extends Exact<DeepPartial<DividerData>, I>>(base?: I): DividerData {
return DividerData.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<DividerData>, I>>(object: I): DividerData {
const message = createBaseDividerData();
message.type = object.type ?? 0;
message.typeMap = Object.entries(object.typeMap ?? {}).reduce<{ [key: string]: DividerData_DividerType }>(
(acc, [key, value]) => {
if (value !== undefined) {
acc[key] = value as DividerData_DividerType;
}
return acc;
},
{},
);
return message;
},
};

function createBaseDividerData_TypeMapEntry(): DividerData_TypeMapEntry {
return { key: "", value: 0 };
}

export const DividerData_TypeMapEntry = {
encode(message: DividerData_TypeMapEntry, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.key !== "") {
writer.uint32(10).string(message.key);
}
if (message.value !== 0) {
writer.uint32(16).int32(message.value);
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): DividerData_TypeMapEntry {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseDividerData_TypeMapEntry();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}

message.key = reader.string();
continue;
case 2:
if (tag !== 16) {
break;
}

message.value = reader.int32() as any;
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): DividerData_TypeMapEntry {
return {
key: isSet(object.key) ? globalThis.String(object.key) : "",
value: isSet(object.value) ? dividerData_DividerTypeFromJSON(object.value) : 0,
};
},

toJSON(message: DividerData_TypeMapEntry): unknown {
const obj: any = {};
if (message.key !== "") {
obj.key = message.key;
}
if (message.value !== 0) {
obj.value = dividerData_DividerTypeToJSON(message.value);
}
return obj;
},

create<I extends Exact<DeepPartial<DividerData_TypeMapEntry>, I>>(base?: I): DividerData_TypeMapEntry {
return DividerData_TypeMapEntry.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<DividerData_TypeMapEntry>, I>>(object: I): DividerData_TypeMapEntry {
const message = createBaseDividerData_TypeMapEntry();
message.key = object.key ?? "";
message.value = object.value ?? 0;
return message;
},
};

type ProtoMetaMessageOptions = {
options?: { [key: string]: any };
fields?: { [key: string]: { [key: string]: any } };
oneof?: { [key: string]: { [key: string]: any } };
nested?: { [key: string]: ProtoMetaMessageOptions };
};

export interface ProtoMetadata {
references: { [key: string]: any };
dependencies?: ProtoMetadata[];
options?: {
options?: { [key: string]: any };
services?: {
[key: string]: { options?: { [key: string]: any }; methods?: { [key: string]: { [key: string]: any } } };
};
messages?: { [key: string]: ProtoMetaMessageOptions };
enums?: { [key: string]: { options?: { [key: string]: any }; values?: { [key: string]: { [key: string]: any } } } };
};
}

export const protoMetadata: ProtoMetadata = {
references: {
".DividerData": DividerData,
".DividerData.DividerType": DividerData_DividerType,
".DividerData.TypeMapEntry": DividerData_TypeMapEntry,
},
dependencies: [],
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin ? T
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

function isObject(value: any): boolean {
return typeof value === "object" && value !== null;
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
1 change: 1 addition & 0 deletions integration/schema-no-file-descriptor/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputSchema=no-file-descriptor
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { protoMetadata } from "./const-enum";

describe("schema-no-file-descriptor", () => {
test("the property doesn't exist", () => {
expect(protoMetadata).not.toHaveProperty("fileDescriptor");
expect(protoMetadata).toHaveProperty("references");
});
});
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type Options = {
unrecognizedEnumName: string;
unrecognizedEnumValue: number;
exportCommonSymbols: boolean;
outputSchema: boolean;
outputSchema: boolean | "no-file-descriptor";
onlyTypes: boolean;
emitImportedFiles: boolean;
useAbortSignal: boolean;
Expand Down
12 changes: 8 additions & 4 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour
};
export interface ProtoMetadata {
fileDescriptor: ${fileDescriptorProto};
references: { [key: string]: any };
${
ctx.options.outputSchema !== "no-file-descriptor" ? code`fileDescriptor: ${fileDescriptorProto};\n` : ""
}references: { [key: string]: any };
dependencies?: ProtoMetadata[];
options?: {
options?: { [key: string]: any };
Expand Down Expand Up @@ -181,8 +182,11 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour

chunks.push(code`
export const ${def("protoMetadata")}: ProtoMetadata = {
fileDescriptor: ${fileDescriptorProto}.fromPartial(${descriptor}),
references: { ${joinCode(references, { on: "," })} },
${
ctx.options.outputSchema !== "no-file-descriptor"
? code`fileDescriptor: ${fileDescriptorProto}.fromPartial(${descriptor}),\n`
: ""
}references: { ${joinCode(references, { on: "," })} },
dependencies: [${joinCode(dependencies, { on: "," })}],
${
fileOptions || messagesOptions.length > 0 || servicesOptions.length > 0 || enumsOptions.length > 0
Expand Down

0 comments on commit c54f06c

Please sign in to comment.