Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
feat: cascade configuration loading
Browse files Browse the repository at this point in the history
i wonder if `{ ...fallback, ...organization, ...repository }[key];` is a valid solution
  • Loading branch information
0x4007 committed Aug 20, 2023
1 parent 3cc60c3 commit a6ef97a
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommentElementPricing } from "../types";
import { CommentElementPricingConfiguration } from "../types";
import { CommandsConfiguration, Label, OrganizationConfiguration, RepositoryConfiguration } from "./private";

interface AllConfigs {
Expand All @@ -25,83 +25,73 @@ type getsCommandSetting = "command-settings";
type getsLabels = "time-labels" | "priority-labels";

export const fromConfig = {
getNumber: function getNumberFromConfig(key: getsNumber, { repository, organization, fallback: defaultConfiguration }: AllConfigs): number {
getNumber: function getNumberFromConfig(key: getsNumber, { repository, organization, fallback }: AllConfigs): number {
if (repository && repository[key] && !Number.isNaN(repository[key])) {
return Number(repository[key]);
} else if (organization && organization[key] && !Number.isNaN(organization[key])) {
return Number(organization[key]);
} else {
console.error(`config parser: "${key}" from imported configs failed to parse as Number`);
return Number(defaultConfiguration[key]);
return Number(fallback[key]);
}
},
getLabels: function getLabelsFromConfig(key: getsLabels, { repository, organization, fallback: defaultConfiguration }: AllConfigs): Label[] | undefined {
getLabels: function getLabelsFromConfig(key: getsLabels, { repository, organization, fallback }: AllConfigs): Label[] | undefined {
if (repository && repository[key] && Array.isArray(repository[key]) && (repository[key] as []).length > 0) {
return repository[key];
} else if (organization && organization[key] && Array.isArray(organization[key]) && (organization[key] as []).length > 0) {
return organization[key];
} else {
console.error(`config parser: "${key}" from imported configs failed to parse as Labels`);
return defaultConfiguration[key];
return fallback[key];
}
},
getCommentItemPrice: function getCommentItemPriceFromConfig(
key: getsCommentElementPricing,
{ repository, organization, fallback: defaultConfiguration }: AllConfigs
): CommentElementPricing | undefined {
if (repository && repository[key]) {
return repository[key];
} else if (organization && organization[key]) {
return organization[key];
} else {
console.error(`config parser: "${key}" from imported configs failed to parse as CommentItemPrice`);
return defaultConfiguration[key];
}
{ repository, organization, fallback }: AllConfigs
): CommentElementPricingConfiguration | undefined {
return { ...fallback, ...organization, ...repository }[key];
},
getCommandSettings: function getCommandSettingsFromConfig(
key: getsCommandSetting,
{ repository, organization, fallback: defaultConfiguration }: AllConfigs
{ repository, organization, fallback }: AllConfigs
): CommandsConfiguration[] | undefined {
if (repository && repository[key]) {
return repository[key];
} else if (organization && organization[key]) {
return organization[key];
} else {
console.error(`config parser: "${key}" from imported configs failed to parse as CommandSettings`);
return defaultConfiguration[key];
return fallback[key];
}
},
getBoolean: function getBooleanFromConfig(key: getsBoolean, { repository, organization, fallback: defaultConfiguration }: AllConfigs): boolean | undefined {
getBoolean: function getBooleanFromConfig(key: getsBoolean, { repository, organization, fallback }: AllConfigs): boolean | undefined {
if (repository && repository[key] && typeof repository[key] === "boolean") {
return repository[key];
} else if (organization && organization[key] && typeof organization[key] === "boolean") {
return organization[key];
} else {
console.error(`config parser: "${key}" from imported configs failed to parse as Boolean`);
return defaultConfiguration[key];
return fallback[key];
}
},
getString: function getStringFromConfig(key: getsString, { repository, organization, fallback: defaultConfiguration }: AllConfigs): string | undefined {
getString: function getStringFromConfig(key: getsString, { repository, organization, fallback }: AllConfigs): string | undefined {
if (repository && repository[key] && typeof repository[key] === "string") {
return repository[key];
} else if (organization && organization[key] && typeof organization[key] === "string") {
return organization[key];
} else {
console.error(`config parser: "${key}" from imported configs failed to parse as String`);
return defaultConfiguration[key];
return fallback[key];
}
},
getStrings: function getStringsFromConfig(
key: getsArrayOfStrings,
{ repository, organization, fallback: defaultConfiguration }: AllConfigs
): string[] | undefined {
getStrings: function getStringsFromConfig(key: getsArrayOfStrings, { repository, organization, fallback }: AllConfigs): string[] | undefined {
if (repository && repository[key]) {
return repository[key];
} else if (organization && organization[key]) {
return organization[key];
} else {
console.error(`config parser: "${key}" from imported configs failed to parse as Strings`);
return defaultConfiguration[key];
return fallback[key];
}
},
};
Expand Down

0 comments on commit a6ef97a

Please sign in to comment.