Skip to content

Commit

Permalink
feat: option to request REST numeric enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster committed Sep 7, 2022
1 parent 17fdab5 commit 6118474
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rules_typescript_gapic/typescript_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def typescript_gapic_library(
diregapic = None,
handwritten_layer = None,
legacy_proto_load = None,
rest_numeric_enums = None,
extra_protoc_parameters = [],
extra_protoc_file_parameters = {},
**kwargs):
Expand All @@ -47,6 +48,8 @@ def typescript_gapic_library(
plugin_args_dict["handwritten-layer"] = "true"
if legacy_proto_load:
plugin_args_dict["legacy-proto-load"] = "true"
if rest_numeric_enums:
plugin_args_dict["rest-numeric-enums"] = "true"

file_args = {} # note: keys are filenames, values are parameter name, aligned with the prior art
for key, value in extra_protoc_file_parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ export class {{ service.name }}Client {
const fallback = opts?.fallback ?? (typeof window !== 'undefined' && typeof window?.fetch === 'function');
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts);
{%- endif %}
{%- if api.restNumericEnums %}

// Request numeric enum values if REST transport is used.
opts.numericEnums = true;
{%- endif %}

// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
if (servicePath !== staticMembers.{{ id.get("servicePath") }} && !('scopes' in opts)) {
Expand Down
11 changes: 11 additions & 0 deletions typescript/src/gapic-generator-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ yargs.describe(
);
yargs.boolean('legacy-proto-load');
yargs.alias('legacy-proto-load', 'legacy_proto_load');
yargs.describe(
'rest_numeric_enums',
'The generated library will pass and accept enum values as numbers when using the HTTP/1.1 REST transport.'
);
yargs.boolean('rest-numeric-enums');
yargs.alias('rest-numeric-enums', 'rest_numeric_enums');
yargs.describe('protoc', 'Path to protoc binary');
yargs.usage('Usage: $0 -I /path/to/googleapis');
yargs.usage(' --output_dir /path/to/output_directory');
Expand All @@ -128,6 +134,7 @@ export interface IArguments {
diregapic?: boolean;
handwrittenLayer?: boolean;
legacyProtoLoad?: boolean;
restNumericEnums?: boolean;
_: string[];
$0: string;
}
Expand All @@ -147,6 +154,7 @@ const transport = argv.transport as string | undefined;
const diregapic = argv.diregapic as boolean | undefined;
const handwrittenLayer = argv.handwrittenLayer as boolean | undefined;
const legacyProtoLoad = argv.legacyProtoLoad as boolean | undefined;
const restNumericEnums = argv.restNumericEnums as boolean | undefined;

// --protoc can be passed from BUILD.bazel and overridden from the command line
let protocParameter = argv.protoc as string | string[] | undefined;
Expand Down Expand Up @@ -215,6 +223,9 @@ if (transport && transport === 'rest') {
if (legacyProtoLoad) {
protocCommand.push('--typescript_gapic_opt="legacy-proto-load"');
}
if (restNumericEnums) {
protocCommand.push('--typescript_gapic_opt="rest-numeric-enums"');
}
protocCommand.push(...protoDirsArg);
protocCommand.push(...protoFiles);
protocCommand.push(`-I${commonProtoPath}`);
Expand Down
9 changes: 9 additions & 0 deletions typescript/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class Generator {
rest?: boolean;
diregapic?: boolean;
legacyProtoLoad?: boolean;
restNumericEnums?: boolean;

constructor() {
this.request = protos.google.protobuf.compiler.CodeGeneratorRequest.create();
Expand Down Expand Up @@ -214,6 +215,12 @@ export class Generator {
}
}

private readRestNumericEnums() {
if (this.paramMap['rest-numeric-enums'] === 'true') {
this.restNumericEnums = true;
}
}

async initializeFromStdin() {
const inputBuffer = await getStdin();
this.request = protos.google.protobuf.compiler.CodeGeneratorRequest.decode(
Expand All @@ -231,6 +238,7 @@ export class Generator {
this.readDiregapic();
this.readHandwrittenLayer();
this.readLegacyProtoLoad();
this.readRestNumericEnums();
}
}

Expand Down Expand Up @@ -272,6 +280,7 @@ export class Generator {
diregapic: this.diregapic,
handwrittenLayer: this.handwrittenLayer,
legacyProtoLoad: this.legacyProtoLoad,
restNumericEnums: this.restNumericEnums,
});
return api;
}
Expand Down
2 changes: 2 additions & 0 deletions typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class API {
diregapic?: boolean;
handwrittenLayer?: boolean;
legacyProtoLoad: boolean;
restNumericEnums: boolean;

static isIgnoredService(
fd: protos.google.protobuf.IFileDescriptorProto
Expand Down Expand Up @@ -94,6 +95,7 @@ export class API {
this.rest = options.rest;
this.diregapic = options.diregapic ?? false;
this.legacyProtoLoad = options.legacyProtoLoad ?? false;
this.restNumericEnums = options.restNumericEnums ?? false;

const [allResourceDatabase, resourceDatabase] = getResourceDatabase(
fileDescriptors
Expand Down
1 change: 1 addition & 0 deletions typescript/src/schema/naming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Options {
diregapic?: boolean;
handwrittenLayer?: boolean;
legacyProtoLoad?: boolean;
restNumericEnums?: boolean;
}

export class Naming {
Expand Down
1 change: 1 addition & 0 deletions typescript/test/unit/baselines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('Baseline tests', () => {
serviceYaml: 'google/showcase/v1beta1/showcase_v1beta1.yaml',
template: 'typescript_gapic;typescript_packing_test',
metadata: true,
restNumericEnums: true,
});

runBaselineTest({
Expand Down
5 changes: 5 additions & 0 deletions typescript/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface BaselineOptions {
legacyProtoLoad?: boolean;
transport?: string;
diregapic?: boolean;
restNumericEnums?: boolean;
}

const cwd = process.cwd();
Expand Down Expand Up @@ -85,6 +86,7 @@ export function runBaselineTest(options: BaselineOptions) {
? path.join(protosDirRoot, options.serviceYaml.split('/').join(path.sep))
: undefined;
const legacyProtoLoad = options.legacyProtoLoad ?? false;
const restNumericEnums = options.restNumericEnums ?? false;
it(options.baselineName, async function () {
this.timeout(60000);
if (fs.existsSync(outputDir)) {
Expand Down Expand Up @@ -123,6 +125,9 @@ export function runBaselineTest(options: BaselineOptions) {
if (legacyProtoLoad) {
commandLine += ' --legacy-proto-load';
}
if (restNumericEnums) {
commandLine += ' --rest-numeric-enums';
}
if (options.transport && options.transport === 'rest') {
commandLine += ' --transport=rest';
}
Expand Down

0 comments on commit 6118474

Please sign in to comment.