-
Notifications
You must be signed in to change notification settings - Fork 24
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
feat: parse child_type resource #176
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7d1373
feat: parse child_type resource
alexander-fenster 925beba
Merge branch 'master' into child_type
alexander-fenster 38eec9b
fix: YAGNI: we will not need to get resource by name
alexander-fenster 51f119c
fix: no need to have this.names
alexander-fenster 3dd7c50
fix: typo in comments
alexander-fenster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import * as plugin from '../../../pbjs-genfiles/plugin'; | ||
|
||
export interface ResourceDescriptor | ||
extends plugin.google.api.IResourceDescriptor { | ||
name: string; | ||
params: string[]; | ||
} | ||
|
||
export class ResourceDatabase { | ||
private names: { [name: string]: ResourceDescriptor }; | ||
private patterns: { [pattern: string]: ResourceDescriptor }; | ||
private types: { [type: string]: ResourceDescriptor }; | ||
|
||
constructor() { | ||
this.names = {}; | ||
this.patterns = {}; | ||
this.types = {}; | ||
} | ||
|
||
registerResource( | ||
resource: plugin.google.api.IResourceDescriptor | undefined, | ||
errorLocation?: string | ||
) { | ||
if (!resource) { | ||
return; | ||
} | ||
|
||
if (!resource.type) { | ||
if (errorLocation) { | ||
console.warn( | ||
`Warning: ${errorLocation} refers to a resource which does not have a type: ${resource}` | ||
); | ||
} | ||
return; | ||
} | ||
|
||
const arr = resource.type.match(/\/([^.]+)$/); | ||
if (!arr?.[1]) { | ||
if (errorLocation) { | ||
console.warn( | ||
`Warning: ${errorLocation} refers to a resource which does not have a proper name: ${resource}` | ||
); | ||
} | ||
return; | ||
} | ||
const name = arr[1]; | ||
|
||
const pattern = resource.pattern; | ||
if (!pattern?.[0]) { | ||
if (errorLocation) { | ||
console.warn( | ||
`Warning: ${errorLocation} refers to a resource which does not have a proper pattern: ${resource}` | ||
); | ||
} | ||
return; | ||
} | ||
const params = pattern[0].match(/{[a-zA-Z]+}/g) || []; | ||
for (let i = 0; i < params.length; i++) { | ||
params[i] = params[i].replace('{', '').replace('}', ''); | ||
} | ||
|
||
const resourceDescriptor: ResourceDescriptor = Object.assign( | ||
{ | ||
name, | ||
params, | ||
}, | ||
resource | ||
); | ||
|
||
this.names[resourceDescriptor.name] = resourceDescriptor; | ||
this.patterns[pattern?.[0]] = resourceDescriptor; | ||
this.types[resourceDescriptor.type!] = resourceDescriptor; | ||
} | ||
|
||
getResourceByName( | ||
name: string | null | undefined, | ||
errorLocation?: string | ||
): ResourceDescriptor | undefined { | ||
if (!name) { | ||
return undefined; | ||
} | ||
if (!this.names[name]) { | ||
if (errorLocation) { | ||
console.warn( | ||
`Warning: ${errorLocation} refers to an unknown resource: ${name}` | ||
); | ||
} | ||
return undefined; | ||
} | ||
return this.names[name]; | ||
} | ||
|
||
getResourceByType( | ||
type: string | null | undefined, | ||
errorLocation?: string | ||
): ResourceDescriptor | undefined { | ||
if (!type) { | ||
return undefined; | ||
} | ||
if (!this.types[type]) { | ||
if (errorLocation) { | ||
console.warn( | ||
`Warning: ${errorLocation} refers to an unknown resource: ${type}` | ||
); | ||
} | ||
return undefined; | ||
} | ||
return this.types[type]; | ||
} | ||
|
||
getResourceByPattern( | ||
pattern: string | null | undefined, | ||
errorLocation?: string | ||
): ResourceDescriptor | undefined { | ||
if (!pattern) { | ||
return undefined; | ||
} | ||
if (!this.patterns[pattern]) { | ||
if (errorLocation) { | ||
console.warn( | ||
`Warning: ${errorLocation} refers to an unknown resource: ${pattern}` | ||
); | ||
} | ||
return undefined; | ||
} | ||
return this.patterns[pattern]; | ||
} | ||
|
||
getParentResourcesByChildType( | ||
childType: string | null | undefined, | ||
errorLocation?: string | ||
): ResourceDescriptor[] { | ||
// childType looks like "datacatalog.googleapis.com/EntryGroup" | ||
const result: ResourceDescriptor[] = []; | ||
|
||
if (!childType) { | ||
return result; | ||
} | ||
|
||
const childResource = this.getResourceByType(childType, errorLocation); | ||
if (!childResource) { | ||
return result; | ||
} | ||
|
||
const childPattern = childResource.pattern?.[0]; | ||
if (!childPattern) { | ||
return result; | ||
} | ||
|
||
let pattern = ''; | ||
for (const segment of childPattern.split('/')) { | ||
if (pattern !== '') { | ||
pattern += '/'; | ||
} | ||
pattern += segment; | ||
const parent = this.getResourceByPattern(pattern); | ||
if (parent) { | ||
result.push(parent); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: s/
know
/known