Filter out unknown catalog entities (#2816)

* Filter out unknown catelog entities

- Keep raw data

- But filter unknown types until a category is registered

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix unit tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* simplify tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove getOrDefault, consolodate ExtendedMap

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-05-20 08:50:51 +03:00
1 parent 5ed4537979
commit b61ba7ef71
4 files changed
+71 -49

No files matched your search

+23 -17
View File
@@ -19,26 +19,38 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { action, computed, observable, toJS } from "mobx";
import { action, computed, observable } from "mobx";
import { Disposer, ExtendedMap } from "../utils";
import { CatalogCategory, CatalogEntityData, CatalogEntityKindData } from "./catalog-entity";
export class CatalogCategoryRegistry {
@observable protected categories: CatalogCategory[] = [];
protected categories = observable.set<CatalogCategory>();
@action add(category: CatalogCategory) {
this.categories.push(category);
@action add(category: CatalogCategory): Disposer {
this.categories.add(category);
return () => this.categories.delete(category);
}
@action remove(category: CatalogCategory) {
this.categories = this.categories.filter((cat) => cat.apiVersion !== category.apiVersion && cat.kind !== category.kind);
@computed private get groupKindLookup(): Map<string, Map<string, CatalogCategory>> {
// ExtendedMap has the convenience methods `getOrInsert` and `strictSet`
const res = new ExtendedMap<string, ExtendedMap<string, CatalogCategory>>();
for (const category of this.categories) {
res
.getOrInsert(category.spec.group, ExtendedMap.new)
.strictSet(category.spec.names.kind, category);
}
return res;
}
@computed get items() {
return toJS(this.categories);
return Array.from(this.categories);
}
getForGroupKind<T extends CatalogCategory>(group: string, kind: string) {
return this.categories.find((c) => c.spec.group === group && c.spec.names.kind === kind) as T;
getForGroupKind<T extends CatalogCategory>(group: string, kind: string): T | undefined {
return this.groupKindLookup.get(group)?.get(kind) as T;
}
getEntityForData(data: CatalogEntityData & CatalogEntityKindData) {
@@ -60,17 +72,11 @@ export class CatalogCategoryRegistry {
return new specVersion.entityClass(data);
}
getCategoryForEntity<T extends CatalogCategory>(data: CatalogEntityData & CatalogEntityKindData) {
getCategoryForEntity<T extends CatalogCategory>(data: CatalogEntityData & CatalogEntityKindData): T | undefined {
const splitApiVersion = data.apiVersion.split("/");
const group = splitApiVersion[0];
const category = this.categories.find((category) => {
return category.spec.group === group && category.spec.names.kind === data.kind;
});
if (!category) return null;
return category as T;
return this.getForGroupKind(group, data.kind);
}
}
+29 -14
View File
@@ -22,19 +22,17 @@
import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx";
export class ExtendedMap<K, V> extends Map<K, V> {
constructor(protected getDefault: () => V, entries?: readonly (readonly [K, V])[] | null) {
super(entries);
static new<K, V>(entries?: readonly (readonly [K, V])[] | null): ExtendedMap<K, V> {
return new ExtendedMap<K, V>(entries);
}
getOrInsert(key: K, val: V): V {
if (this.has(key)) {
return this.get(key);
}
return this.set(key, val).get(key);
}
getOrInsertWith(key: K, getVal: () => V): V {
/**
* Get the value behind `key`. If it was not pressent, first insert the value returned by `getVal`
* @param key The key to insert into the map with
* @param getVal A function that returns a new instance of `V`.
* @returns The value in the map
*/
getOrInsert(key: K, getVal: () => V): V {
if (this.has(key)) {
return this.get(key);
}
@@ -42,12 +40,29 @@ export class ExtendedMap<K, V> extends Map<K, V> {
return this.set(key, getVal()).get(key);
}
getOrDefault(key: K): V {
/**
* Set the value associated with `key` iff there was not a previous value
* @throws if `key` already in map
* @returns `this` so that `strictSet` can be chained
*/
strictSet(key: K, val: V): this {
if (this.has(key)) {
return this.get(key);
throw new TypeError("Duplicate key in map");
}
return this.set(key, this.getDefault()).get(key);
return this.set(key, val);
}
/**
* Get the value associated with `key`
* @throws if `key` did not a value associated with it
*/
strictGet(key: K): V {
if (!this.has(key)) {
throw new TypeError("key not in map");
}
return this.get(key);
}
}