From 07af0e2f2f2fa20c713d7ba5e682c46a3b1bf940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jakubowski?= Date: Sun, 17 Mar 2024 15:28:41 +0100 Subject: [PATCH] Improve usability of custom knobs- introduce nullability: nullable, noNullable, both --- src/data/custom_knob.ts | 39 ++++++++++++++++++- src/generators/file_content/base_generator.ts | 9 +++-- src/global.d.ts | 2 + src/providers/custom_knobs_provider.ts | 14 +++++-- src/util/extensions.ts | 4 ++ 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/data/custom_knob.ts b/src/data/custom_knob.ts index dfa590d..340cd1f 100644 --- a/src/data/custom_knob.ts +++ b/src/data/custom_knob.ts @@ -1,9 +1,44 @@ class CustomKnob { constructor( public type: string, - public nullable: boolean, + public nullabilitty: CustomKnobNullability, public value: string ) {} + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + static fromJson(json: any): CustomKnob { + const type = json.type; + const nullability = customKnobNullabilityFromString(json.nullability); + const value = json.value; + return new CustomKnob(type, nullability, value); + } + + get nullable(): boolean { + return this.nullabilitty != CustomKnobNullability.nonNullable; + } + + get nonNullable(): boolean { + return this.nullabilitty != CustomKnobNullability.nullable; + } +} + +enum CustomKnobNullability { + nullable, + nonNullable, + both, +} + +function customKnobNullabilityFromString(value: string): CustomKnobNullability { + switch (value) { + case "nullable": + return CustomKnobNullability.nullable; + case "nonNullable": + return CustomKnobNullability.nonNullable; + case "both": + return CustomKnobNullability.both; + default: + throw new Error(`Invalid CustomKnobNullability value: ${value}`); + } } -export { CustomKnob }; +export { CustomKnob, CustomKnobNullability }; diff --git a/src/generators/file_content/base_generator.ts b/src/generators/file_content/base_generator.ts index 7c034aa..71a0eb7 100644 --- a/src/generators/file_content/base_generator.ts +++ b/src/generators/file_content/base_generator.ts @@ -23,12 +23,13 @@ abstract class BaseFileContentGenerator implements FileContentGenerator { private applyCustomKnobs() { for (const customKnob of this.customKnobs) { - if (customKnob.nullable) { - this.knobForNullableType.set(customKnob.type, (fieldName) => + if (customKnob.nonNullable) { + this.knobForType.set(customKnob.type, (fieldName) => customKnob.value.replace("$fieldName", fieldName) ); - } else { - this.knobForType.set(customKnob.type, (fieldName) => + } + if (customKnob.nullable) { + this.knobForNullableType.set(customKnob.type, (fieldName) => customKnob.value.replace("$fieldName", fieldName) ); } diff --git a/src/global.d.ts b/src/global.d.ts index a83983c..f57b6c1 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -17,6 +17,8 @@ declare global { // eslint-disable-next-line @typescript-eslint/no-unused-vars interface Array { + whereNotNull(): Array; + whereType(): Array; } } diff --git a/src/providers/custom_knobs_provider.ts b/src/providers/custom_knobs_provider.ts index cb1e41c..67569f1 100644 --- a/src/providers/custom_knobs_provider.ts +++ b/src/providers/custom_knobs_provider.ts @@ -10,9 +10,17 @@ class CustomKnobsProvider { vscode.Uri.file(filePath) ); const fileContentString = Buffer.from(fileContent).toString("utf-8"); - const parsedContent = JSON.parse(fileContentString) as Array; - - return parsedContent; + const parsedContent = JSON.parse(fileContentString); + return parsedContent + .map((item: object) => { + try { + return CustomKnob.fromJson(item); + } catch (error) { + console.error("Error parsing custom knob:", error); + return null; + } + }) + .whereNotNull(); } catch (error) { console.error("Error reading JSON file:", error); return []; diff --git a/src/util/extensions.ts b/src/util/extensions.ts index ceb2a4d..8a2976f 100644 --- a/src/util/extensions.ts +++ b/src/util/extensions.ts @@ -29,6 +29,10 @@ String.prototype.substringUpToAndIncluding = function (text: string) { return this.substring(0, this.indexOf(text) + text.length); }; +Array.prototype.whereNotNull = function (): Array { + return this.filter((item) => item !== null); +}; + Array.prototype.whereType = function (): Array { return this.filter((element): element is T => !!element); };