Skip to content

Commit

Permalink
refactor: use optional chaining (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster committed Nov 22, 2019
1 parent cac5ca4 commit 71733ab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 74 deletions.
4 changes: 2 additions & 2 deletions typescript/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Generator {
}

private async readGrpcServiceConfig(map: OptionsMap) {
if (map && map['grpc-service-config']) {
if (map?.['grpc-service-config']) {
const filename = map['grpc-service-config'];
if (!fs.existsSync(filename)) {
throw new Error(`File ${filename} cannot be opened.`);
Expand All @@ -100,7 +100,7 @@ export class Generator {
}

private async readPublishPackageName(map: OptionsMap) {
if (map && map['package-name']) {
if (map?.['package-name']) {
this.publishName = map['package-name'];
}
}
Expand Down
12 changes: 6 additions & 6 deletions typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ function getResourceMap(
}, {} as MessagesMap);
for (const property of Object.keys(messages)) {
const m = messages[property];
if (m && m.options) {
if (m?.options) {
const option = m.options;
if (option && option['.google.api.resource']) {
if (option?.['.google.api.resource']) {
const opt = option['.google.api.resource'];
const oneResource = option[
'.google.api.resource'
] as ResourceDescriptor;
if (opt.type) {
const arr = opt.type.match(/\/([^.]+)$/);
if (arr && arr[1]) {
if (arr?.[1]) {
oneResource.name = arr[1];
}
} else {
Expand All @@ -150,7 +150,7 @@ function getResourceMap(
continue;
}
const pattern = opt.pattern;
if (pattern && pattern[0]) {
if (pattern?.[0]) {
const params = pattern[0].match(/{[a-zA-Z]+}/g) || [];
for (let i = 0; i < params.length; i++) {
params[i] = params[i].replace('{', '').replace('}', '');
Expand All @@ -161,7 +161,7 @@ function getResourceMap(
resourceMap[opt.type!] = oneResource;
} else if (oneResource.name) {
console.warn(
'In file ' +
'Warning: in file ' +
fd.name +
' message ' +
property +
Expand All @@ -170,7 +170,7 @@ function getResourceMap(
);
} else {
console.warn(
'In file ' +
'Warning: in file ' +
fd.name +
' message ' +
property +
Expand Down
10 changes: 3 additions & 7 deletions typescript/src/schema/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,14 @@ export class CommentsMap {
return this.comments;
}
getServiceComment(serviceName: string) {
return this.comments[serviceName]
? this.comments[serviceName].comments
: [];
return this.comments[serviceName]?.comments || [];
}
getMethodComments(serviceName: string, methodName: string) {
const key = serviceName + ':' + methodName;
return this.comments[key] ? this.comments[key].comments : [];
return this.comments[key]?.comments || [];
}
getParamComments(messageName: string, fieldName: string): Comment {
const key = messageName + ':' + fieldName;
return this.comments[key]
? this.comments[key]
: { paramName: '', paramType: '', comments: [] };
return this.comments[key] ?? { paramName: '', paramType: '', comments: [] };
}
}
91 changes: 32 additions & 59 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,24 @@ export interface EnumsMap {
// methods of the given service, to use in templates.

function longrunning(method: MethodDescriptorProto) {
if (method.options && method.options['.google.longrunning.operationInfo']) {
if (method.options?.['.google.longrunning.operationInfo']) {
return method.options['.google.longrunning.operationInfo']!;
}
return undefined;
}

function toFullyQualifiedName(packageName: string, messageName: string) {
function toFullyQualifiedName(
packageName: string,
messageName: string | null | undefined
) {
if (!messageName) {
return undefined;
}
if (messageName.includes('.')) {
if (!messageName.startsWith('.')) {
return `.${messageName}`;
} else return messageName;
}
return messageName;
}
return `.${packageName}.${messageName}`;
}
Expand All @@ -226,34 +233,20 @@ function longRunningResponseType(
packageName: string,
method: MethodDescriptorProto
) {
if (
method.options &&
method.options['.google.longrunning.operationInfo'] &&
method.options['.google.longrunning.operationInfo'].responseType
) {
return toFullyQualifiedName(
packageName,
method.options['.google.longrunning.operationInfo'].responseType
);
}
return undefined;
return toFullyQualifiedName(
packageName,
method.options?.['.google.longrunning.operationInfo']?.responseType
);
}

function longRunningMetadataType(
packageName: string,
method: MethodDescriptorProto
) {
if (
method.options &&
method.options['.google.longrunning.operationInfo'] &&
method.options['.google.longrunning.operationInfo'].metadataType
) {
return toFullyQualifiedName(
packageName,
method.options['.google.longrunning.operationInfo'].metadataType
);
}
return undefined;
return toFullyQualifiedName(
packageName,
method.options?.['.google.longrunning.operationInfo']?.metadataType
);
}

// convert from input interface to message name
Expand Down Expand Up @@ -301,31 +294,24 @@ function pagingField(messages: MessagesMap, method: MethodDescriptorProto) {
}

function pagingFieldName(messages: MessagesMap, method: MethodDescriptorProto) {
const repeatedFields = pagingField(messages, method);
if (repeatedFields && repeatedFields.name) {
return repeatedFields.name;
} else {
return undefined;
}
const field = pagingField(messages, method);
return field?.name;
}

function pagingResponseType(
messages: MessagesMap,
method: MethodDescriptorProto
) {
const repeatedFields = pagingField(messages, method);
if (repeatedFields && repeatedFields.typeName) {
return repeatedFields.typeName; //.google.showcase.v1beta1.EchoResponse
}
return undefined;
const field = pagingField(messages, method);
return field?.typeName; //.google.showcase.v1beta1.EchoResponse
}

export function getHeaderParams(rule: plugin.google.api.IHttpRule): string[] {
const message =
rule.post || rule.delete || rule.get || rule.put || rule.patch;
if (message) {
const res = message.match(/{(.*?)=/);
return res && res[1] ? res[1].split('.') : [];
return res?.[1] ? res[1].split('.') : [];
}
return [];
}
Expand Down Expand Up @@ -391,11 +377,7 @@ function augmentMethod(
},
method
) as MethodDescriptorProto;
if (
method.inputType &&
messages[method.inputType] &&
messages[method.inputType].field
) {
if (method.inputType && messages[method.inputType]?.field) {
const paramComment: Comment[] = [];
const inputType = messages[method.inputType!];
const inputmessageName = toMessageName(method.inputType);
Expand All @@ -408,10 +390,7 @@ function augmentMethod(
}
method.paramComment = paramComment;
}
if (
method.methodConfig.retryPolicy &&
method.methodConfig.retryPolicy.retryableStatusCodes
) {
if (method.methodConfig.retryPolicy?.retryableStatusCodes) {
method.retryableCodesName = service.retryableCodeMap.getRetryableCodesName(
method.methodConfig.retryPolicy.retryableStatusCodes
);
Expand Down Expand Up @@ -450,7 +429,7 @@ function augmentMethod(
if (method.methodConfig.timeout) {
method.timeoutMillis = milliseconds(method.methodConfig.timeout);
}
if (method.options && method.options['.google.api.http']) {
if (method.options?.['.google.api.http']) {
const httpRule = method.options['.google.api.http'];
method.headerRequestParams = getHeaderParams(httpRule);
} else method.headerRequestParams = [];
Expand Down Expand Up @@ -499,10 +478,7 @@ function augmentService(

augmentedService.hostname = '';
augmentedService.port = 0;
if (
augmentedService.options &&
augmentedService.options['.google.api.defaultHost']
) {
if (augmentedService.options?.['.google.api.defaultHost']) {
const match = augmentedService.options['.google.api.defaultHost'].match(
/^(.*):(\d+)$/
);
Expand All @@ -512,28 +488,25 @@ function augmentService(
}
}
augmentedService.oauthScopes = [];
if (
augmentedService.options &&
augmentedService.options['.google.api.oauthScopes']
) {
if (augmentedService.options?.['.google.api.oauthScopes']) {
augmentedService.oauthScopes = augmentedService.options[
'.google.api.oauthScopes'
].split(',');
}
augmentedService.pathTemplates = [];
for (const property of Object.keys(messages)) {
const m = messages[property];
if (m && m.field) {
if (m?.field) {
const fields = m.field;
for (const fieldDescriptor of fields) {
if (fieldDescriptor && fieldDescriptor.options) {
if (fieldDescriptor?.options) {
const option = fieldDescriptor.options;
if (option && option['.google.api.resourceReference']) {
if (option?.['.google.api.resourceReference']) {
const resourceReference = option['.google.api.resourceReference'];
const type = resourceReference.type;
if (!type || !resourceMap[type.toString()]) {
console.warn(
'In service proto ' +
'Warning: in service proto ' +
service.name +
' message ' +
property +
Expand Down

0 comments on commit 71733ab

Please sign in to comment.