From 7ea703440db81902c3ba0839db4024b0d41c070e Mon Sep 17 00:00:00 2001 From: Xiaozhen Liu Date: Fri, 31 Jan 2020 14:11:59 -0800 Subject: [PATCH] fix: add api resource option to resourceDatabase (#211) * add api resource option to resourceDatabase * lint Co-authored-by: Alexander Fenster --- typescript/src/schema/proto.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/typescript/src/schema/proto.ts b/typescript/src/schema/proto.ts index 8f290a759..69cddc009 100644 --- a/typescript/src/schema/proto.ts +++ b/typescript/src/schema/proto.ts @@ -528,6 +528,21 @@ function augmentService( const uniqueResources: { [name: string]: ResourceDescriptor } = {}; for (const property of Object.keys(messages)) { const errorLocation = `service ${service.name} message ${property}`; + // take the option['.google.api.resource'] of the message as resource, add it to resourceDatabase id it's not there. + const descriptorProto = messages[property]; + if ( + descriptorProto.options && + descriptorProto.options['.google.api.resource'] + ) { + const resource = descriptorProto.options['.google.api.resource']; + if (!resourceDatabase.getResourceByType(resource.type)) { + resourceDatabase.registerResource(resource); + const registeredResource = resourceDatabase.getResourceByType( + resource.type + )!; + uniqueResources[registeredResource.name] = registeredResource; + } + } for (const fieldDescriptor of messages[property].field ?? []) { // note: ResourceDatabase can accept `undefined` values, so we happily use optional chaining here. const resourceReference =