Skip to content

Commit

Permalink
[INTERNAL] ResourceTagCollection: Add 'superCollection' parameter]
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Dec 2, 2021
1 parent 5b5f43e commit a7403e4
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/ResourceTagCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ const tagNamespaceRegExp = new RegExp("^[a-z][a-z0-9]*$"); // part before the co
const tagNameRegExp = new RegExp("^[A-Z][A-Za-z0-9]+$"); // part after the colon

class ResourceTagCollection {
constructor({allowedTags}) {
constructor({allowedTags, superCollection}) {
if (!allowedTags || !allowedTags.length) {
throw new Error(`Missing parameter 'allowedTags'`);
}

if (superCollection) {
this._superCollection = superCollection;
this._superTags = this._superCollection.getAcceptedTags();
} else {
this._superTags = [];
}

// No validation of tag names here since we might remove/ignore
// this parameter in the future and generally allow all tags
this._allowedTags = Object.freeze(allowedTags);
this._pathTags = {};
}

setTag(resource, tag, value = true) {
if (this._superTags.includes(tag)) {
return this._superCollection.setTag(resource, tag, value);
}

this._validateResource(resource);
this._validateTag(tag);
this._validateValue(value);
Expand All @@ -25,6 +37,10 @@ class ResourceTagCollection {
}

clearTag(resource, tag) {
if (this._superTags.includes(tag)) {
return this._superCollection.clearTag(resource, tag);
}

this._validateResource(resource);
this._validateTag(tag);

Expand All @@ -35,6 +51,10 @@ class ResourceTagCollection {
}

getTag(resource, tag) {
if (this._superTags.includes(tag)) {
return this._superCollection.getTag(resource, tag);
}

this._validateResource(resource);
this._validateTag(tag);

Expand All @@ -44,6 +64,10 @@ class ResourceTagCollection {
}
}

getAcceptedTags() {
return [...this._allowedTags, ...this._superTags];
}

_validateResource(resource) {
const path = resource.getPath();
if (!path) {
Expand Down

0 comments on commit a7403e4

Please sign in to comment.