Skip to content

Commit

Permalink
feat: service options unknown methods (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzeng0 authored Mar 18, 2023
1 parent 0e595c9 commit 994d0d0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
6 changes: 3 additions & 3 deletions integration/generic-metadata/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,23 +290,23 @@ export const HeroServiceDefinition = {
requestStream: false,
responseType: Hero,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
findOneVillain: {
name: "FindOneVillain",
requestType: VillainById,
requestStream: false,
responseType: Villain,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
findManyVillain: {
name: "FindManyVillain",
requestType: VillainById,
requestStream: true,
responseType: Villain,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
},
} 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 @@ -70,31 +70,31 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
serverStreaming: {
name: "ServerStreaming",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
clientStreaming: {
name: "ClientStreaming",
requestType: TestMessage,
requestStream: true,
responseType: TestMessage,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
bidiStreaming: {
name: "BidiStreaming",
requestType: TestMessage,
requestStream: true,
responseType: TestMessage,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
/** @deprecated */
deprecated: {
Expand All @@ -103,23 +103,23 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
idempotent: {
name: "Idempotent",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { idempotencyLevel: "IDEMPOTENT" },
options: { idempotencyLevel: "IDEMPOTENT", _unknownFields: {} },
},
noSideEffects: {
name: "NoSideEffects",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { idempotencyLevel: "NO_SIDE_EFFECTS" },
options: { idempotencyLevel: "NO_SIDE_EFFECTS", _unknownFields: {} },
},
},
} 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 @@ -70,31 +70,31 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
serverStreaming: {
name: "ServerStreaming",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
clientStreaming: {
name: "ClientStreaming",
requestType: TestMessage,
requestStream: true,
responseType: TestMessage,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
bidiStreaming: {
name: "BidiStreaming",
requestType: TestMessage,
requestStream: true,
responseType: TestMessage,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
/** @deprecated */
deprecated: {
Expand All @@ -103,23 +103,23 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
idempotent: {
name: "Idempotent",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { idempotencyLevel: "IDEMPOTENT" },
options: { idempotencyLevel: "IDEMPOTENT", _unknownFields: {} },
},
noSideEffects: {
name: "NoSideEffects",
requestType: TestMessage,
requestStream: false,
responseType: TestMessage,
responseStream: false,
options: { idempotencyLevel: "NO_SIDE_EFFECTS" },
options: { idempotencyLevel: "NO_SIDE_EFFECTS", _unknownFields: {} },
},
},
} 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 @@ -94,7 +94,7 @@ export const TestDefinition = {
requestStream: false,
responseType: Empty,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
unaryStringValue: {
name: "UnaryStringValue",
Expand Down Expand Up @@ -207,23 +207,23 @@ export const TestDefinition = {
requestStream: false,
responseType: TestMessage,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
serverStreamingStringValue: {
name: "ServerStreamingStringValue",
requestType: StringValue,
requestStream: false,
responseType: StringValue,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
serverStreamingStruct: {
name: "ServerStreamingStruct",
requestType: Struct,
requestStream: false,
responseType: Struct,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
/** Client Streaming */
clientStreaming: {
Expand All @@ -232,15 +232,15 @@ export const TestDefinition = {
requestStream: true,
responseType: TestMessage,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
clientStreamingStringValue: {
name: "ClientStreamingStringValue",
requestType: StringValue,
requestStream: true,
responseType: StringValue,
responseStream: false,
options: {},
options: { _unknownFields: {} },
},
/** Bidi Streaming */
bidiStreaming: {
Expand All @@ -249,15 +249,15 @@ export const TestDefinition = {
requestStream: true,
responseType: TestMessage,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
bidiStreamingStringValue: {
name: "BidiStreamingStringValue",
requestType: StringValue,
requestStream: true,
responseType: StringValue,
responseStream: true,
options: {},
options: { _unknownFields: {} },
},
},
} as const;
Expand Down
27 changes: 25 additions & 2 deletions src/generate-generic-service-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ function generateMethodDefinition(ctx: Context, methodDesc: MethodDescriptorProt
requestStream: ${methodDesc.clientStreaming},
responseType: ${outputType},
responseStream: ${methodDesc.serverStreaming},
options: ${generateMethodOptions(methodDesc.options)}
options: ${generateMethodOptions(ctx, methodDesc.options)}
}
`;
}

function generateMethodOptions(options: MethodOptions | undefined) {
function generateMethodOptions(ctx: Context, options: MethodOptions | undefined) {
const chunks: Code[] = [];

chunks.push(code`{`);
Expand All @@ -87,6 +87,29 @@ function generateMethodOptions(options: MethodOptions | undefined) {
} else if (options.idempotencyLevel === MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS) {
chunks.push(code`idempotencyLevel: 'NO_SIDE_EFFECTS',`);
}

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

unknownFieldsChunks.push(code`{`);

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

for (const value of values) {
valuesChunks.push(
code`${ctx.options.env == "node" ? "Buffer.from" : "new Uint8Array"}([${value.join(", ")}])`
);
}

unknownFieldsChunks.push(code`${key}: [\n${joinCode(valuesChunks, { on: "," }).toCodeString([])}\n],`);
}

unknownFieldsChunks.push(code`}`);
chunks.push(code`_unknownFields: ${joinCode(unknownFieldsChunks, { on: "\n" }).toCodeString([])}`);
}
}

chunks.push(code`}`);
Expand Down

0 comments on commit 994d0d0

Please sign in to comment.