pnp-scheduler/source/Configuration/ConfigurationTransformer.ts

55 lines
1.5 KiB
TypeScript

import {ChannelId} from "../types/DiscordTypes";
import {Nullable} from "../types/Nullable";
import {ArrayUtils} from "../Utilities/ArrayUtils";
import {ConfigurationModel} from "../Database/Models/ConfigurationModel";
export enum TransformerType {
Channel,
PermissionBoolean,
String,
Paragraph,
}
type ConfigurationTransformerItem = {
path: string[];
type: TransformerType,
}
export type TransformerResults =
ChannelId | boolean | string | null
export class ConfigurationTransformer {
constructor(
private readonly transformers: ConfigurationTransformerItem[]
) {
}
public getValue(configValue: ConfigurationModel): TransformerResults {
const transformerType = this.getTransformerType(configValue.key);
if (transformerType === undefined || transformerType === null) {
throw new Error(`Can't find transformer for ${configValue.key}`);
}
switch (transformerType) {
case TransformerType.Channel:
return <ChannelId>configValue.value;
case TransformerType.PermissionBoolean:
return configValue.value === '1';
case TransformerType.Paragraph:
case TransformerType.String:
return configValue.value;
}
return null;
}
public getTransformerType(configKey: string): Nullable<TransformerType> {
const path = configKey.split('.');
return this.transformers.find(
transformer => {
return ArrayUtils.arraysEqual(transformer.path, path);
}
)?.type;
}
}