Skip to content

Commit

Permalink
Improve usability of custom knobs- introduce nullability: nullable, n…
Browse files Browse the repository at this point in the history
…oNullable, both
  • Loading branch information
FirentisTFW committed Mar 17, 2024
1 parent b5db86b commit 07af0e2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
39 changes: 37 additions & 2 deletions src/data/custom_knob.ts
Original file line number Diff line number Diff line change
@@ -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 };
9 changes: 5 additions & 4 deletions src/generators/file_content/base_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ declare global {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Array<T> {
whereNotNull(): Array<T>;

whereType<T>(): Array<T>;
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/providers/custom_knobs_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomKnob>;

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 [];
Expand Down
4 changes: 4 additions & 0 deletions src/util/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ String.prototype.substringUpToAndIncluding = function (text: string) {
return this.substring(0, this.indexOf(text) + text.length);
};

Array.prototype.whereNotNull = function <T>(): Array<T> {
return this.filter((item) => item !== null);
};

Array.prototype.whereType = function <T>(): Array<T> {
return this.filter((element): element is T => !!element);
};
Expand Down

0 comments on commit 07af0e2

Please sign in to comment.