Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use optional chaining for routing parameters #1138

Merged
merged 1 commit into from
May 23, 2022
Merged
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
14 changes: 7 additions & 7 deletions baselines/routingtest/src/v1/test_service_client.ts.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class TestServiceClient {
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if((typeof request.name !== "undefined") && RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.name!)){
if ((typeof request.name !== "undefined") && RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.name!)){
Object.assign(routingParameter, { database: request.name!.match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)'))![0]})}

options.otherArgs.headers[
Expand Down Expand Up @@ -423,10 +423,10 @@ export class TestServiceClient {
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if((typeof request.name !== "undefined") && RegExp('[^/]+').test(request.name!)){
if ((typeof request.name !== "undefined") && RegExp('[^/]+').test(request.name!)){
Object.assign(routingParameter, { name: request.name!.match(RegExp('[^/]+'))![0]})}

if((typeof request.name !== "undefined") && RegExp('(?<routing_id>(?:/.*)?)').test(request.name!)){
if ((typeof request.name !== "undefined") && RegExp('(?<routing_id>(?:/.*)?)').test(request.name!)){
Object.assign(routingParameter, { routing_id: request.name!.match(RegExp('(?<routing_id>.*)'))![0]})}

options.otherArgs.headers[
Expand Down Expand Up @@ -506,13 +506,13 @@ export class TestServiceClient {
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if((typeof request.parentId !== "undefined") && RegExp('(?<database>projects)/[^/]+').test(request.parentId!)){
if ((typeof request.parentId !== "undefined") && RegExp('(?<database>projects)/[^/]+').test(request.parentId!)){
Object.assign(routingParameter, { database: request.parentId!.match(RegExp('(?<database>projects/[^/]+)'))![0]})}

if((typeof request.nest1.nest2.nameId !== "undefined") && RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.nest1.nest2.nameId!)){
Object.assign(routingParameter, { database: request.nest1.nest2.nameId!.match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)'))![0]})}
if ((typeof request.nest1?.nest2?.nameId !== "undefined") && RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.nest1?.nest2?.nameId!)){
Object.assign(routingParameter, { database: request.nest1?.nest2?.nameId!.match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)'))![0]})}

if((typeof request.anotherParentId !== "undefined") && RegExp('(?<routing_id>projects)/[^/]+/databases/[^/]+/documents').test(request.anotherParentId!)){
if ((typeof request.anotherParentId !== "undefined") && RegExp('(?<routing_id>projects)/[^/]+/databases/[^/]+/documents').test(request.anotherParentId!)){
Object.assign(routingParameter, { routing_id: request.anotherParentId!.match(RegExp('(?<routing_id>projects/[^/]+)'))![0]})}

options.otherArgs.headers[
Expand Down
4 changes: 2 additions & 2 deletions templates/typescript_gapic/_util.njk
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ request.{{ oneComment.paramName.toCamelCase() }}
let routingParameter = {};
{%- for paramArray in method.dynamicRoutingRequestParams %}
{%- for param in paramArray %}
if((typeof request.{{ param.fieldRetrieve }} !== "undefined") && RegExp('{{ param.messageRegex | safe }}').test(request.{{ param.fieldRetrieve }}!)){
Object.assign(routingParameter, { {{ param.fieldSend }}: request.{{ param.fieldRetrieve }}!.match(RegExp('{{ param.namedSegment | safe }}'))![0]})}
if ((typeof request.{{ param.fieldRetrieve.join('?.') }} !== "undefined") && RegExp('{{ param.messageRegex | safe }}').test(request.{{ param.fieldRetrieve.join('?.') }}!)){
Copy link
Contributor

Choose a reason for hiding this comment

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

So, the old behavior we just had the object access baked into the string, and now we actually use an array type?

Seems like a good refactor to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the generator keeps the parts as an array, and we only merge them with ?. in the template.

Object.assign(routingParameter, { {{ param.fieldSend }}: request.{{ param.fieldRetrieve.join('?.') }}!.match(RegExp('{{ param.namedSegment | safe }}'))![0]})}
{% endfor -%}
{%- endfor %}
options.otherArgs.headers[
Expand Down
9 changes: 6 additions & 3 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ export function getDynamicHeaderRequestParams(
// messageRegex is the regex of the path template that the message field should match.
// namedSegment is the regex capture of the named value of the field.
export interface DynamicRoutingParameters {
fieldRetrieve: string;
fieldRetrieve: string[];
fieldSend: string;
messageRegex: string;
namedSegment: string;
Expand All @@ -673,19 +673,22 @@ export interface DynamicRoutingParameters {
// The field to be retrieved needs to be converted into camelCase
export function convertFieldToCamelCase(field: string) {
const camelCaseFields: string[] = [];
if (field === '') {
return camelCaseFields;
}
const fieldsToRetrieve = field.split('.');
fieldsToRetrieve.forEach(field => {
camelCaseFields.push(field.toCamelCase());
});
return camelCaseFields.join('.');
return camelCaseFields;
}

// This parses a single Routing Parameter and returns a MapRoutingParameters interface.
export function getSingleRoutingHeaderParam(
rule: protos.google.api.IRoutingParameter
): DynamicRoutingParameters {
let dynamicRoutingRule: DynamicRoutingParameters = {
fieldRetrieve: '',
fieldRetrieve: [],
fieldSend: '',
messageRegex: '',
namedSegment: '',
Expand Down
52 changes: 27 additions & 25 deletions typescript/test/unit/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('src/schema/proto.ts', () => {
const expectedRoutingParameters: DynamicRoutingParameters[][] = [
[
{
fieldRetrieve: '',
fieldRetrieve: [],
fieldSend: '',
messageRegex: '',
namedSegment: '',
Expand Down Expand Up @@ -160,19 +160,19 @@ describe('src/schema/proto.ts', () => {
const expectedRoutingParameters: DynamicRoutingParameters[][] = [
[
{
fieldRetrieve: 'name',
fieldRetrieve: ['name'],
fieldSend: 'routing_id',
messageRegex: '(?<routing_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<routing_id>projects/[^/]+)',
},
{
fieldRetrieve: 'database',
fieldRetrieve: ['database'],
fieldSend: 'routing_id',
messageRegex: '(?<routing_id>(?:/.*)?)',
namedSegment: '(?<routing_id>.*)',
},
{
fieldRetrieve: 'database',
fieldRetrieve: ['database'],
fieldSend: 'routing_id',
messageRegex:
'(?<routing_id>projects)/[^/]+/databases/[^/]+/documents/[^/]+(?:/.*)?',
Expand All @@ -199,15 +199,15 @@ describe('src/schema/proto.ts', () => {
const expectedRoutingParameters: DynamicRoutingParameters[][] = [
[
{
fieldRetrieve: 'name',
fieldRetrieve: ['name'],
fieldSend: 'routing_id',
messageRegex: '(?<routing_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<routing_id>projects/[^/]+)',
},
],
[
{
fieldRetrieve: 'appProfileId',
fieldRetrieve: ['appProfileId'],
fieldSend: 'profile_id',
messageRegex: '(?<profile_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<profile_id>projects/[^/]+)',
Expand Down Expand Up @@ -238,13 +238,13 @@ describe('src/schema/proto.ts', () => {
const expectedRoutingParameters: DynamicRoutingParameters[][] = [
[
{
fieldRetrieve: 'name',
fieldRetrieve: ['name'],
fieldSend: 'routing_id',
messageRegex: '(?<routing_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<routing_id>projects/[^/]+)',
},
{
fieldRetrieve: 'name',
fieldRetrieve: ['name'],
fieldSend: 'routing_id',
messageRegex:
'test/(?<routing_id>projects)/[^/]+/databases/[^/]+/documents/[^/]+(?:/.*)?',
Expand All @@ -253,7 +253,7 @@ describe('src/schema/proto.ts', () => {
],
[
{
fieldRetrieve: 'appProfileId',
fieldRetrieve: ['appProfileId'],
fieldSend: 'profile_id',
messageRegex: '(?<profile_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<profile_id>projects/[^/]+)',
Expand All @@ -269,19 +269,21 @@ describe('src/schema/proto.ts', () => {

describe('should return a string set to camelCase', () => {
it('should return this to camelCase', () => {
assert.deepStrictEqual(
convertFieldToCamelCase('name.name2.name3'),
'name.name2.name3'
);
assert.deepStrictEqual(convertFieldToCamelCase(''), '');
assert.deepStrictEqual(convertFieldToCamelCase('parent_id'), 'parentId');
assert.deepStrictEqual(
convertFieldToCamelCase('app_profile_id'),
'appProfileId'
);
assert.deepStrictEqual(convertFieldToCamelCase('name.name2.name3'), [
'name',
'name2',
'name3',
]);
assert.deepStrictEqual(convertFieldToCamelCase(''), []);
assert.deepStrictEqual(convertFieldToCamelCase('parent_id'), [
'parentId',
]);
assert.deepStrictEqual(convertFieldToCamelCase('app_profile_id'), [
'appProfileId',
]);
assert.deepStrictEqual(
convertFieldToCamelCase('name.parent_id.another_parent_id'),
'name.parentId.anotherParentId'
['name', 'parentId', 'anotherParentId']
);
});
});
Expand All @@ -293,7 +295,7 @@ describe('src/schema/proto.ts', () => {
pathTemplate: 'test/database',
};
const expectedRoutingParameters: DynamicRoutingParameters = {
fieldRetrieve: '',
fieldRetrieve: [],
fieldSend: '',
messageRegex: '',
namedSegment: '',
Expand All @@ -306,7 +308,7 @@ describe('src/schema/proto.ts', () => {
it('works with no parameters', () => {
const routingRule: protos.google.api.IRoutingParameter = {};
const expectedRoutingParameters: DynamicRoutingParameters = {
fieldRetrieve: '',
fieldRetrieve: [],
fieldSend: '',
messageRegex: '',
namedSegment: '',
Expand All @@ -321,7 +323,7 @@ describe('src/schema/proto.ts', () => {
field: 'name',
};
const expectedRoutingParameters: DynamicRoutingParameters = {
fieldRetrieve: 'name',
fieldRetrieve: ['name'],
fieldSend: 'name',
messageRegex: '[^/]+',
namedSegment: '[^/]+',
Expand All @@ -337,7 +339,7 @@ describe('src/schema/proto.ts', () => {
pathTemplate: '{routing_id=**}',
};
const expectedRoutingParameters: DynamicRoutingParameters = {
fieldRetrieve: 'appProfileId.parentId',
fieldRetrieve: ['appProfileId', 'parentId'],
fieldSend: 'routing_id',
messageRegex: '(?<routing_id>(?:/.*)?)',
namedSegment: '(?<routing_id>.*)',
Expand All @@ -353,7 +355,7 @@ describe('src/schema/proto.ts', () => {
pathTemplate: '{routing_id=projects/*}/**',
};
const expectedRoutingParameters: DynamicRoutingParameters = {
fieldRetrieve: 'appProfileId',
fieldRetrieve: ['appProfileId'],
fieldSend: 'routing_id',
messageRegex: '(?<routing_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<routing_id>projects/[^/]+)',
Expand Down