Skip to content

Commit

Permalink
fix: Bump ts-proto-descriptors to restore any-less _unknownFields. (#810
Browse files Browse the repository at this point in the history
)
  • Loading branch information
stephenh authored Mar 26, 2023
1 parent 933d8eb commit de9c307
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 77 deletions.
6 changes: 3 additions & 3 deletions integration/generic-metadata/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,23 +318,23 @@ export const HeroServiceDefinition = {
requestStream: false,
responseType: Hero,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
findOneVillain: {
name: "FindOneVillain",
requestType: VillainById,
requestStream: false,
responseType: Villain,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
findManyVillain: {
name: "FindManyVillain",
requestType: VillainById,
requestStream: true,
responseType: Villain,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
},
} as const;
Expand Down
14 changes: 7 additions & 7 deletions integration/generic-service-definitions-and-services/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,31 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
serverStreaming: {
name: "ServerStreaming",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
clientStreaming: {
name: "ClientStreaming",
requestType: TestMessage,
requestStream: true,
responseType: TestMessage,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
bidiStreaming: {
name: "BidiStreaming",
requestType: TestMessage,
requestStream: true,
responseType: TestMessage,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
/** @deprecated */
deprecated: {
Expand All @@ -108,23 +108,23 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
idempotent: {
name: "Idempotent",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { idempotencyLevel: "IDEMPOTENT", _unknownFields: {} },
options: { idempotencyLevel: "IDEMPOTENT" },
},
noSideEffects: {
name: "NoSideEffects",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { idempotencyLevel: "NO_SIDE_EFFECTS", _unknownFields: {} },
options: { idempotencyLevel: "NO_SIDE_EFFECTS" },
},
},
} as const;
Expand Down
14 changes: 7 additions & 7 deletions integration/generic-service-definitions/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,31 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
serverStreaming: {
name: "ServerStreaming",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
clientStreaming: {
name: "ClientStreaming",
requestType: TestMessage,
requestStream: true,
responseType: TestMessage,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
bidiStreaming: {
name: "BidiStreaming",
requestType: TestMessage,
requestStream: true,
responseType: TestMessage,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
/** @deprecated */
deprecated: {
Expand All @@ -108,23 +108,23 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
idempotent: {
name: "Idempotent",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { idempotencyLevel: "IDEMPOTENT", _unknownFields: {} },
options: { idempotencyLevel: "IDEMPOTENT" },
},
noSideEffects: {
name: "NoSideEffects",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { idempotencyLevel: "NO_SIDE_EFFECTS", _unknownFields: {} },
options: { idempotencyLevel: "NO_SIDE_EFFECTS" },
},
},
} as const;
Expand Down
16 changes: 8 additions & 8 deletions integration/nice-grpc/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const TestDefinition = {
requestStream: false,
responseType: Empty,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
unaryStringValue: {
name: "UnaryStringValue",
Expand Down Expand Up @@ -212,23 +212,23 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
serverStreamingStringValue: {
name: "ServerStreamingStringValue",
requestType: StringValue,
requestStream: false,
responseType: StringValue,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
serverStreamingStruct: {
name: "ServerStreamingStruct",
requestType: Struct,
requestStream: false,
responseType: Struct,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
/** Client Streaming */
clientStreaming: {
Expand All @@ -237,15 +237,15 @@ export const TestDefinition = {
requestStream: true,
responseType: TestMessage,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
clientStreamingStringValue: {
name: "ClientStreamingStringValue",
requestType: StringValue,
requestStream: true,
responseType: StringValue,
responseStream: false,
options: { _unknownFields: {} },
options: {},
},
/** Bidi Streaming */
bidiStreaming: {
Expand All @@ -254,15 +254,15 @@ export const TestDefinition = {
requestStream: true,
responseType: TestMessage,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
bidiStreamingStringValue: {
name: "BidiStreamingStringValue",
requestType: StringValue,
requestStream: true,
responseType: StringValue,
responseStream: true,
options: { _unknownFields: {} },
options: {},
},
},
} as const;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"object-hash": "^1.3.1",
"protobufjs": "^6.11.3",
"ts-poet": "^6.4.1",
"ts-proto-descriptors": "1.7.1"
"ts-proto-descriptors": "1.8.0"
},
"packageManager": "[email protected]"
}
7 changes: 3 additions & 4 deletions src/generate-generic-service-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ function generateMethodOptions(ctx: Context, options: MethodOptions | undefined)
chunks.push(code`idempotencyLevel: 'NO_SIDE_EFFECTS',`);
}

if ("_unknownFields" in options) {
const msgUnknownFields: any = (options as any)["_unknownFields"];
if (options._unknownFields !== undefined) {
const unknownFieldsChunks: Code[] = [];

unknownFieldsChunks.push(code`{`);

for (const key of Object.keys(msgUnknownFields)) {
const values = msgUnknownFields[key] as Uint8Array[];
for (const key in options._unknownFields) {
const values = options._unknownFields[key];
const valuesChunks: Code[] = [];

for (const value of values) {
Expand Down
61 changes: 19 additions & 42 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,8 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour

let fileOptions: Code | undefined;
if (fileDesc.options) {
fileOptions = encodedOptionsToOptions(
ctx,
".google.protobuf.FileOptions",
(fileDesc.options as any)["_unknownFields"]
);
delete (fileDesc.options as any)["_unknownFields"];
fileOptions = encodedOptionsToOptions(ctx, ".google.protobuf.FileOptions", fileDesc.options._unknownFields);
delete fileDesc.options._unknownFields;
}

const messagesOptions: Code[] = [];
Expand All @@ -125,9 +121,9 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour
const methodOptions = encodedOptionsToOptions(
ctx,
".google.protobuf.MethodOptions",
(method.options as any)["_unknownFields"]
method.options._unknownFields
);
delete (method.options as any)["_unknownFields"];
delete method.options._unknownFields;
if (methodOptions) {
methodsOptions.push(code`'${method.name}': ${methodOptions}`);
}
Expand All @@ -136,12 +132,8 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour

let serviceOptions: Code | undefined;
if (service.options) {
serviceOptions = encodedOptionsToOptions(
ctx,
".google.protobuf.ServiceOptions",
(service.options as any)["_unknownFields"]
);
delete (service.options as any)["_unknownFields"];
serviceOptions = encodedOptionsToOptions(ctx, ".google.protobuf.ServiceOptions", service.options._unknownFields);
delete service.options._unknownFields;
}

if (methodsOptions.length > 0 || serviceOptions) {
Expand All @@ -162,9 +154,9 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour
const valueOptions = encodedOptionsToOptions(
ctx,
".google.protobuf.EnumValueOptions",
(value.options as any)["_unknownFields"]
value.options._unknownFields
);
delete (value.options as any)["_unknownFields"];
delete value.options._unknownFields;
if (valueOptions) {
valuesOptions.push(code`'${value.name}': ${valueOptions}`);
}
Expand All @@ -173,12 +165,8 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour

let enumOptions: Code | undefined;
if (Enum.options) {
enumOptions = encodedOptionsToOptions(
ctx,
".google.protobuf.EnumOptions",
(Enum.options as any)["_unknownFields"]
);
delete (Enum.options as any)["_unknownFields"];
enumOptions = encodedOptionsToOptions(ctx, ".google.protobuf.EnumOptions", Enum.options._unknownFields);
delete Enum.options._unknownFields;
}

if (valuesOptions.length > 0 || enumOptions) {
Expand Down Expand Up @@ -239,13 +227,14 @@ function getExtensionValue(ctx: Context, extension: FieldDescriptorProto, data:
function encodedOptionsToOptions(
ctx: Context,
extendee: string,
encodedOptions: { [key: number]: Uint8Array[] }
encodedOptions?: { [key: number]: Uint8Array[] }
): Code | undefined {
if (!encodedOptions) {
return undefined;
}
const resultOptions: Code[] = [];
for (const [key, value] of Object.entries(encodedOptions)) {
for (const key in encodedOptions) {
const value = encodedOptions[key];
const extension = extensionCache[extendee][parseInt(key, 10) >>> 3];
resultOptions.push(getExtensionValue(ctx, extension, value));
}
Expand All @@ -259,12 +248,8 @@ function resolveMessageOptions(ctx: Context, message: DescriptorProto): Code | u
const fieldsOptions: Code[] = [];
message.field.forEach((field) => {
if (field.options) {
const fieldOptions = encodedOptionsToOptions(
ctx,
".google.protobuf.FieldOptions",
(field.options as any)["_unknownFields"]
);
delete (field.options as any)["_unknownFields"];
const fieldOptions = encodedOptionsToOptions(ctx, ".google.protobuf.FieldOptions", field.options._unknownFields);
delete field.options._unknownFields;
if (fieldOptions) {
fieldsOptions.push(code`'${field.name}': ${fieldOptions}`);
}
Expand All @@ -274,12 +259,8 @@ function resolveMessageOptions(ctx: Context, message: DescriptorProto): Code | u
const oneOfsOptions: Code[] = [];
message.oneofDecl.forEach((oneOf) => {
if (oneOf.options) {
const oneOfOptions = encodedOptionsToOptions(
ctx,
".google.protobuf.OneofOptions",
(oneOf.options as any)["_unknownFields"]
);
delete (oneOf.options as any)["_unknownFields"];
const oneOfOptions = encodedOptionsToOptions(ctx, ".google.protobuf.OneofOptions", oneOf.options._unknownFields);
delete oneOf.options._unknownFields;
if (oneOfOptions) {
oneOfsOptions.push(code`'${oneOf.name}': ${oneOfOptions}`);
}
Expand All @@ -298,12 +279,8 @@ function resolveMessageOptions(ctx: Context, message: DescriptorProto): Code | u

let messageOptions: Code | undefined;
if (message.options) {
messageOptions = encodedOptionsToOptions(
ctx,
".google.protobuf.MessageOptions",
(message.options as any)["_unknownFields"]
);
delete (message.options as any)["_unknownFields"];
messageOptions = encodedOptionsToOptions(ctx, ".google.protobuf.MessageOptions", message.options._unknownFields);
delete message.options._unknownFields;
}

if (fieldsOptions.length > 0 || oneOfsOptions.length > 0 || nestedOptions.length > 0 || messageOptions) {
Expand Down
Loading

0 comments on commit de9c307

Please sign in to comment.