Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
leahecole committed Oct 22, 2024
1 parent 55da34f commit ff5d595
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 46 deletions.
92 changes: 56 additions & 36 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ const COMMON_PROTO_LIST = [
'google.type',
];

// services that are allowed to use Int32Value and UInt32Value protobuf wrapper types
// instead of "number" for pageSize/maxResults
// keyed by proto package name, e.g. "google.cloud.foo.v1".
const ENABLE_WRAPPER_TYPES_FOR_PAGE_SIZE = {
'google.cloud.bigquery.v2': true,
}
// services that are allowed to use Int32Value and UInt32Value protobuf wrapper types
// instead of "number" for pageSize/maxResults
// keyed by proto package name, e.g. "google.cloud.foo.v1".
const ENABLE_WRAPPER_TYPES_FOR_PAGE_SIZE = {
'google.cloud.bigquery.v2': true,
};
export interface MethodDescriptorProto
extends protos.google.protobuf.IMethodDescriptorProto {
autoPopulatedFields?: string[];
Expand Down Expand Up @@ -277,13 +277,13 @@ function streaming(method: MethodDescriptorProto) {
// returns true if the method has wrappers for UInt32Value enabled
// and is a paginated call with a maxResults parameter instead of pageSize
// as of its creation, this should only be true for BigQuery
function wrappersHasMaxResultsParameter(messages, method, wrappersAllowed){
function wrappersHasMaxResultsParameter(messages, method, wrappersAllowed) {
const inputType = messages[method.inputType!];
const hasMaxResults =
inputType &&
inputType.field &&
inputType.field.some(field => field.name === 'max_results');
if(wrappersAllowed && hasMaxResults){
if (wrappersAllowed && hasMaxResults) {
return true;
}
return false;
Expand Down Expand Up @@ -313,7 +313,6 @@ function pagingField(
return undefined;
}


const inputType = messages[method.inputType!];
const outputType = messages[method.outputType!];
const hasPageToken =
Expand All @@ -333,19 +332,19 @@ function pagingField(
// paginated resources to return.
const isPageSizeField = () => {
let fieldYes = false;
if (inputType && inputType.field){
inputType.field.some(
field =>{
if((field.name === 'page_size') ||
if (inputType && inputType.field) {
inputType.field.some(field => {
if (
field.name === 'page_size' ||
(diregapic && field.name === 'max_results') ||
(wrappersAllowed && field.name === 'max_results')){
fieldYes = true;
}
(wrappersAllowed && field.name === 'max_results')
) {
fieldYes = true;
}
);
});
}
return fieldYes;
}
};
const hasPageSize = isPageSizeField();

const hasNextPageToken =
Expand All @@ -361,7 +360,7 @@ function pagingField(
if (repeatedFields.length === 0) {
return undefined;
}

if (repeatedFields.length === 1) {
return repeatedFields[0];
}
Expand Down Expand Up @@ -401,7 +400,13 @@ function pagingFieldName(
diregapic?: boolean,
wrappersAllowed?: boolean // whether a service is allowed to use UInt32Value wrappers - generally this is only BigQuery
) {
const field = pagingField(messages, method, service, diregapic, wrappersAllowed);
const field = pagingField(
messages,
method,
service,
diregapic,
wrappersAllowed
);
return field?.name;
}

Expand All @@ -411,7 +416,13 @@ function pagingResponseType(
diregapic?: boolean,
wrappersAllowed?: boolean // whether a service is allowed to use UInt32Value wrappers - generally this is only BigQuery
) {
const field = pagingField(messages, method, undefined, diregapic, wrappersAllowed);
const field = pagingField(
messages,
method,
undefined,
diregapic,
wrappersAllowed
);
if (!field || !field.type) {
return undefined;
}
Expand All @@ -433,7 +444,13 @@ function ignoreMapPagingMethod(
diregapic?: boolean,
wrappersAllowed?: boolean // whether a service is allowed to use UInt32Value wrappers - generally this is only BigQuery
) {
const pagingfield = pagingField(messages, method, undefined, diregapic, wrappersAllowed);
const pagingfield = pagingField(
messages,
method,
undefined,
diregapic,
wrappersAllowed
);
const outputType = messages[method.outputType!];
if (!pagingfield?.type || !outputType.nestedType) {
return undefined;
Expand All @@ -456,13 +473,17 @@ function pagingMapResponseType(
diregapic?: boolean,
wrappersAllowed?: boolean // whether a service is allowed to use UInt32Value wrappers - generally this is only BigQuery
) {

const pagingfield = pagingField(messages, method, undefined, diregapic, wrappersAllowed);
const pagingfield = pagingField(
messages,
method,
undefined,
diregapic,
wrappersAllowed
);

const outputType = messages[method.outputType!];
if (!pagingfield?.type || (!diregapic) || !outputType.nestedType) {

return undefined;
if (!pagingfield?.type || !diregapic || !outputType.nestedType) {
return undefined;
}
const mapResponses = outputType.nestedType.filter(desProto => {
return desProto.options && desProto.options.mapEntry;
Expand Down Expand Up @@ -545,7 +566,8 @@ function augmentMethod(
// whether a service is allowed to use Int32Value and UInt32Value wrappers - generally this is only BigQuery
// this is used to determine factors about pagination fields and to allow users to pass a "number" instead of
// having to convert to a protobuf wrapper type to determine page size
const wrappersAllowed = ENABLE_WRAPPER_TYPES_FOR_PAGE_SIZE[parameters.service.packageName];
const wrappersAllowed =
ENABLE_WRAPPER_TYPES_FOR_PAGE_SIZE[parameters.service.packageName];
method = Object.assign(
{
longRunning: longrunning(parameters.service, method),
Expand Down Expand Up @@ -582,7 +604,6 @@ function augmentMethod(
method,
parameters.diregapic,
wrappersAllowed

),
ignoreMapPagingMethod: ignoreMapPagingMethod(
parameters.allMessages,
Expand All @@ -603,8 +624,12 @@ function augmentMethod(
),
retryableCodesName: defaultNonIdempotentRetryCodesName,
retryParamsName: defaultParametersName,
maxResultsParameter: wrappersHasMaxResultsParameter(parameters.allMessages, method, wrappersAllowed)
},
maxResultsParameter: wrappersHasMaxResultsParameter(
parameters.allMessages,
method,
wrappersAllowed
),
},
method
) as MethodDescriptorProto;
if (method.longRunning) {
Expand Down Expand Up @@ -652,12 +677,8 @@ function augmentMethod(
field.name!
);
paramComment.push(comment);


}



method.paramComment = paramComment;
}
if (method.methodConfig.retryPolicy?.retryableStatusCodes) {
Expand Down Expand Up @@ -1129,6 +1150,5 @@ export class Proto {
map[service.name!] = service;
return map;
}, {} as ServicesMap);

}
}
18 changes: 8 additions & 10 deletions typescript/test/unit/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,6 @@ describe('src/schema/proto.ts', () => {
);
assert.deepStrictEqual(proto.services['service'].paging.length, 0);
});

});
describe('should support pagination for allowlisted APIs that use UInt32 wrappers and max_results', () => {
it('should be page field if allowlisted with wrappers and use "max_results" as field name', () => {
Expand All @@ -1255,8 +1254,7 @@ describe('src/schema/proto.ts', () => {
fd.service[0].method[0] =
{} as protos.google.protobuf.MethodDescriptorProto;
fd.service[0].method[0].name = 'ListCats';
fd.service[0].method[0].outputType =
'.google.cloud.bigquery.v2.CatList';
fd.service[0].method[0].outputType = '.google.cloud.bigquery.v2.CatList';
fd.service[0].method[0].inputType =
'.google.cloud.bigquery.v2.ListCatsRequest';

Expand All @@ -1275,8 +1273,7 @@ describe('src/schema/proto.ts', () => {
fd.messageType[0].field[0].name = 'next_page_token';
fd.messageType[0].field[0].label = 3; // LABEL_REPEATED
fd.messageType[0].field[0].type = 11; // TYPE_MESSAGE
fd.messageType[0].field[0].typeName =
'.google.cloud.bigquery.v2.Cat';
fd.messageType[0].field[0].typeName = '.google.cloud.bigquery.v2.Cat';
fd.messageType[1].field = [
{} as protos.google.protobuf.FieldDescriptorProto,
];
Expand Down Expand Up @@ -1310,7 +1307,10 @@ describe('src/schema/proto.ts', () => {
proto.services['CatService'].method[0].pagingFieldName,
'next_page_token'
);
assert.deepStrictEqual(proto.services['CatService'].paging[0].name, 'ListCats');
assert.deepStrictEqual(
proto.services['CatService'].paging[0].name,
'ListCats'
);
assert.deepStrictEqual(
proto.services['CatService'].paging[0].inputType,
'.google.cloud.bigquery.v2.ListCatsRequest'
Expand All @@ -1337,8 +1337,7 @@ describe('src/schema/proto.ts', () => {
fd.service[0].method[0] =
{} as protos.google.protobuf.MethodDescriptorProto;
fd.service[0].method[0].name = 'ListCats';
fd.service[0].method[0].outputType =
'.google.cloud.felines.v2.CatList';
fd.service[0].method[0].outputType = '.google.cloud.felines.v2.CatList';
fd.service[0].method[0].inputType =
'.google.cloud.felines.v2.ListCatsRequest';

Expand All @@ -1357,8 +1356,7 @@ describe('src/schema/proto.ts', () => {
fd.messageType[0].field[0].name = 'next_page_token';
fd.messageType[0].field[0].label = 3; // LABEL_REPEATED
fd.messageType[0].field[0].type = 11; // TYPE_MESSAGE
fd.messageType[0].field[0].typeName =
'.google.cloud.bigquery.v2.Cat';
fd.messageType[0].field[0].typeName = '.google.cloud.bigquery.v2.Cat';
fd.messageType[1].field = [
{} as protos.google.protobuf.FieldDescriptorProto,
];
Expand Down

0 comments on commit ff5d595

Please sign in to comment.