Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ async function main(processArgv: string[]) {
'Override the list of mixins to use. Semicolon-separated list of API names to mixin, e.g. google.longrunning.Operations. Use "none" to disable all mixins.',
)
.string('mixins')
.describe(
'compatibility-resources',
'A list of multi-pattern resources names which should also generate old single-pattern functions. Semicolon-separated list of resource names, e.g. secretmanager.googleapis.com/Secret;secretmanager.googleapis.com/SecretVersion',
)
.string('compatibility-resources')
.describe('protoc', 'Path to protoc binary')
.usage('Usage: $0 -I /path/to/googleapis')
.usage(' --output_dir /path/to/output_directory')
Expand All @@ -158,6 +163,7 @@ async function main(processArgv: string[]) {
const legacyProtoLoad = argv.legacyProtoLoad as boolean | undefined;
const restNumericEnums = argv.restNumericEnums as boolean | undefined;
const mixins = argv.mixins as string | undefined;
const compatibilityResources = argv.compatibilityResources as string | undefined;

// --protoc can be taken from environment or from the command line
let protocParameter = argv.protoc as string | string[] | undefined;
Expand Down Expand Up @@ -247,6 +253,9 @@ async function main(processArgv: string[]) {
if (mixins) {
protocCommand.push(`--typescript_gapic_opt="mixins=${mixins}"`);
}
if (compatibilityResources) {
protocCommand.push(`--typescript_gapic_opt="compatibility-resources=${compatibilityResources}"`);
}
protocCommand.push(...protoDirsArg);
protocCommand.push(...protoFiles);
protocCommand.push(`-I${commonProtoPath}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class Generator {
restNumericEnums?: boolean;
mixinsOverride?: string[];
format?: string | string[];
compatibilityResources?: string[];

private root: protobuf.Root;

Expand Down Expand Up @@ -245,6 +246,12 @@ export class Generator {
}
}

private readCompatibilityResources() {
if (this.paramMap['compatibility-resources']) {
this.compatibilityResources = this.paramMap['compatibility-resources'].split(';');
}
}

async initializeFromStdin() {
const inputBuffer = await getStdin();
const CodeGeneratorRequest = this.root.lookupType('CodeGeneratorRequest');
Expand Down Expand Up @@ -274,6 +281,7 @@ export class Generator {
this.readLegacyProtoLoad();
this.readRestNumericEnums();
this.readFormat();
this.readCompatibilityResources();
}
}

Expand Down Expand Up @@ -334,6 +342,7 @@ export class Generator {
legacyProtoLoad: this.legacyProtoLoad,
restNumericEnums: this.restNumericEnums,
mixinsOverridden: this.mixinsOverride !== undefined,
compatibilityResources: this.compatibilityResources,
});
return api;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Options {
legacyProtoLoad?: boolean;
restNumericEnums?: boolean;
mixinsOverridden?: boolean;
compatibilityResources?: string[];
}

export class Naming {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,16 @@ export function augmentService(parameters: AugmentServiceParameters) {
}
}
}
if (parameters.options.compatibilityResources) {
for (const type of parameters.options.compatibilityResources) {
const compatibilityResource = parameters.allResourceDatabase.getCompatibilityResourceByType(type)
if (compatibilityResource) {
uniqueResources[compatibilityResource.name] = compatibilityResource;
} else {
throw new Error('Resource "${type} has no compatibility pattern')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The error message is defined as a single-quoted string literal but contains a ${type} placeholder, which will not be interpolated. Additionally, it has an unclosed double quote. Use string concatenation or a template literal to correctly interpolate the type variable.

        throw new Error('Resource "' + type + '" has no compatibility pattern');

}
}
}
augmentedService.pathTemplates = Object.values(uniqueResources).sort(
(resourceA, resourceB) => {
// Path templates names can be cased differently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export interface ResourceDescriptor
export class ResourceDatabase {
patterns: {[pattern: string]: ResourceDescriptor};
types: {[type: string]: ResourceDescriptor};
compatibilityResources: {[type: string]: ResourceDescriptor}

constructor() {
this.patterns = {};
this.types = {};
this.compatibilityResources = {};
}

registerResource(
Expand Down Expand Up @@ -104,6 +106,18 @@ export class ResourceDatabase {
this.types[resource.type] = resourceDescriptor;
}
}
// Create the pattern we *would* have generated if it were single. Whether
// this is actually generated depends on the compatibility-resources
// option.
const pattern = patterns![0]
const name = arr![1];
const params = this.getParams(pattern);
let compatibilityResourceDescriptor = this.getResourceDescriptor(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The variable compatibilityResourceDescriptor is never reassigned, so it should be declared using const instead of let to adhere to TypeScript best practices.

Suggested change
let compatibilityResourceDescriptor = this.getResourceDescriptor(
const compatibilityResourceDescriptor = this.getResourceDescriptor(

name,
params,
resource,
);
this.compatibilityResources[resource.type] = compatibilityResourceDescriptor
}
}

Expand Down Expand Up @@ -180,6 +194,12 @@ export class ResourceDatabase {
return result;
}

getCompatibilityResourceByType(
type: string,
): ResourceDescriptor | undefined {
return this.compatibilityResources[type];
}

private getParams(pattern: string): string[] {
const params = pattern.match(/{[a-zA-Z_]+(?:=.*?)?}/g) || [];
const result = params.map(p => p.replace(/{([a-zA-Z_]+).*/, '$1'));
Expand Down
Loading