Skip to content

Commit

Permalink
fix(builtins): rename global-config to config in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Aug 12, 2024
1 parent 7bdc885 commit cea711a
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 155 deletions.
21 changes: 9 additions & 12 deletions src/if-run/builtins/coefficient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {validate} from '../../../common/util/validations';
import {STRINGS} from '../../config';

const {GlobalConfigError} = ERRORS;
const {MISSING_GLOBAL_CONFIG} = STRINGS;
const {MISSING_CONFIG} = STRINGS;

export const Coefficient = (
globalConfig: CoefficientConfig,
config: CoefficientConfig,
parametersMetadata: PluginParametersMetadata
): ExecutePlugin => {
const metadata = {
Expand All @@ -28,7 +28,7 @@ export const Coefficient = (
* Calculate the product of each input parameter.
*/
const execute = (inputs: PluginParams[]) => {
const safeGlobalConfig = validateGlobalConfig();
const safeGlobalConfig = validateConfig();
const inputParameter = safeGlobalConfig['input-parameter'];
const outputParameter = safeGlobalConfig['output-parameter'];
const coefficient = safeGlobalConfig['coefficient'];
Expand Down Expand Up @@ -66,23 +66,20 @@ export const Coefficient = (
) => input[inputParameter] * coefficient;

/**
* Checks global config value are valid.
* Checks config value are valid.
*/
const validateGlobalConfig = () => {
if (!globalConfig) {
throw new GlobalConfigError(MISSING_GLOBAL_CONFIG);
const validateConfig = () => {
if (!config) {
throw new GlobalConfigError(MISSING_CONFIG);
}

const globalConfigSchema = z.object({
const configSchema = z.object({
coefficient: z.number(),
'input-parameter': z.string().min(1),
'output-parameter': z.string().min(1),
});

return validate<z.infer<typeof globalConfigSchema>>(
globalConfigSchema,
globalConfig
);
return validate<z.infer<typeof configSchema>>(configSchema, config);
};

return {
Expand Down
21 changes: 9 additions & 12 deletions src/if-run/builtins/copy-param/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {validate} from '../../../common/util/validations';

import {STRINGS} from '../../config';

const {MISSING_GLOBAL_CONFIG} = STRINGS;
const {MISSING_CONFIG} = STRINGS;
const {GlobalConfigError} = ERRORS;
// keep-existing: true/false (whether to remove the parameter you are copying from)
// from-param: the parameter you are copying from (e.g. cpu/name)
// to-field: the parameter you are copying to (e.g. cpu/processor-name)

export const Copy = (
globalConfig: Record<string, any>,
config: Record<string, any>,
parametersMetadata: PluginParametersMetadata
): ExecutePlugin => {
const metadata = {
Expand All @@ -27,23 +27,20 @@ export const Copy = (
};

/**
* Checks global config value are valid.
* Checks config value are valid.
*/
const validateGlobalConfig = () => {
if (!globalConfig) {
throw new GlobalConfigError(MISSING_GLOBAL_CONFIG);
const validateConfig = () => {
if (!config) {
throw new GlobalConfigError(MISSING_CONFIG);
}

const globalConfigSchema = z.object({
const configSchema = z.object({
'keep-existing': z.boolean(),
from: z.string().min(1),
to: z.string().min(1),
});

return validate<z.infer<typeof globalConfigSchema>>(
globalConfigSchema,
globalConfig
);
return validate<z.infer<typeof configSchema>>(configSchema, config);
};

/**
Expand All @@ -70,7 +67,7 @@ export const Copy = (
};

const execute = (inputs: PluginParams[]) => {
const safeGlobalConfig = validateGlobalConfig();
const safeGlobalConfig = validateConfig();
const keepExisting = safeGlobalConfig['keep-existing'] === true;
const from = safeGlobalConfig['from'];
const to = safeGlobalConfig['to'];
Expand Down
23 changes: 10 additions & 13 deletions src/if-run/builtins/csv-lookup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
FILE_READ_FAILED,
MISSING_CSV_COLUMN,
NO_QUERY_DATA,
MISSING_GLOBAL_CONFIG,
MISSING_CONFIG,
} = STRINGS;

const {
Expand All @@ -33,7 +33,7 @@ const {
} = ERRORS;

export const CSVLookup = (
globalConfig: any,
config: any,
parametersMetadata: PluginParametersMetadata
): ExecutePlugin => {
const metadata = {
Expand Down Expand Up @@ -191,13 +191,13 @@ export const CSVLookup = (
};

/**
* 1. Validates global config.
* 1. Validates config.
* 2. Tries to retrieve given file (with url or local path).
* 3. Parses given CSV.
* 4. Filters requested information from CSV.
*/
const execute = async (inputs: PluginParams[]) => {
const safeGlobalConfig = validateGlobalConfig();
const safeGlobalConfig = validateConfig();
const {filepath, query, output} = safeGlobalConfig;

const file = await retrieveFile(filepath);
Expand Down Expand Up @@ -227,14 +227,14 @@ export const CSVLookup = (
};

/**
* Checks for `filepath`, `query` and `output` fields in global config.
* Checks for `filepath`, `query` and `output` fields in config.
*/
const validateGlobalConfig = () => {
if (!globalConfig) {
throw new GlobalConfigError(MISSING_GLOBAL_CONFIG);
const validateConfig = () => {
if (!config) {
throw new GlobalConfigError(MISSING_CONFIG);
}

const globalConfigSchema = z.object({
const configSchema = z.object({
filepath: z.string(),
query: z.record(z.string(), z.string()),
output: z
Expand All @@ -243,10 +243,7 @@ export const CSVLookup = (
.or(z.array(z.array(z.string()))),
});

return validate<z.infer<typeof globalConfigSchema>>(
globalConfigSchema,
globalConfig
);
return validate<z.infer<typeof configSchema>>(configSchema, config);
};

return {
Expand Down
16 changes: 8 additions & 8 deletions src/if-run/builtins/divide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {validate} from '../../../common/util/validations';
import {STRINGS} from '../../config';

const {GlobalConfigError, MissingInputDataError} = ERRORS;
const {MISSING_GLOBAL_CONFIG, MISSING_INPUT_DATA, ZERO_DIVISION} = STRINGS;
const {MISSING_CONFIG, MISSING_INPUT_DATA, ZERO_DIVISION} = STRINGS;

export const Divide = (
globalConfig: ConfigParams,
config: ConfigParams,
parametersMetadata: PluginParametersMetadata
): ExecutePlugin => {
const metadata = {
Expand All @@ -28,7 +28,7 @@ export const Divide = (
* Calculate the division of each input parameter.
*/
const execute = (inputs: PluginParams[]) => {
const safeGlobalConfig = validateGlobalConfig();
const safeGlobalConfig = validateConfig();
const {numerator, denominator, output} = safeGlobalConfig;

return inputs.map((input, index) => {
Expand All @@ -46,11 +46,11 @@ export const Divide = (
};

/**
* Checks global config value are valid.
* Checks config value are valid.
*/
const validateGlobalConfig = () => {
if (!globalConfig) {
throw new GlobalConfigError(MISSING_GLOBAL_CONFIG);
const validateConfig = () => {
if (!config) {
throw new GlobalConfigError(MISSING_CONFIG);
}

const schema = z.object({
Expand All @@ -59,7 +59,7 @@ export const Divide = (
output: z.string(),
});

return validate<z.infer<typeof schema>>(schema, globalConfig);
return validate<z.infer<typeof schema>>(schema, config);
};

/**
Expand Down
15 changes: 6 additions & 9 deletions src/if-run/builtins/exponent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import {validate} from '../../../common/util/validations';

export const Exponent = (
globalConfig: ExponentConfig,
config: ExponentConfig,
parametersMetadata: PluginParametersMetadata
): ExecutePlugin => {
const metadata = {
Expand All @@ -19,19 +19,16 @@ export const Exponent = (
};

/**
* Checks global config value are valid.
* Checks config value are valid.
*/
const validateGlobalConfig = () => {
const globalConfigSchema = z.object({
const validateConfig = () => {
const configSchema = z.object({
'input-parameter': z.string().min(1),
exponent: z.number(),
'output-parameter': z.string().min(1),
});

return validate<z.infer<typeof globalConfigSchema>>(
globalConfigSchema,
globalConfig
);
return validate<z.infer<typeof configSchema>>(configSchema, config);
};

/**
Expand All @@ -55,7 +52,7 @@ export const Exponent = (
'input-parameter': inputParameter,
exponent,
'output-parameter': outputParameter,
} = validateGlobalConfig();
} = validateConfig();

return inputs.map(input => {
validateSingleInput(input, inputParameter);
Expand Down
43 changes: 17 additions & 26 deletions src/if-run/builtins/interpolation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ import {validate} from '../../../common/util/validations';
import {STRINGS} from '../../config';

const {GlobalConfigError} = ERRORS;
const {
MISSING_GLOBAL_CONFIG,
X_Y_EQUAL,
ARRAY_LENGTH_NON_EMPTY,
WITHIN_THE_RANGE,
} = STRINGS;
const {MISSING_CONFIG, X_Y_EQUAL, ARRAY_LENGTH_NON_EMPTY, WITHIN_THE_RANGE} =
STRINGS;

export const Interpolation = (
globalConfig: ConfigParams,
config: ConfigParams,
parametersMetadata: PluginParametersMetadata
): ExecutePlugin => {
const metadata = {
Expand Down Expand Up @@ -68,7 +64,7 @@ export const Interpolation = (
config: ConfigParams,
input: PluginParams
) => {
const parameter = input[globalConfig['input-parameter']];
const parameter = input[config['input-parameter']];
const xPoints: number[] = config.x;
const yPoints: number[] = config.y;

Expand Down Expand Up @@ -98,7 +94,7 @@ export const Interpolation = (
config: ConfigParams,
input: PluginParams
) => {
const parameter = input[globalConfig['input-parameter']];
const parameter = input[config['input-parameter']];
const xPoints: number[] = config.x;
const yPoints: number[] = config.y;
const spline: any = new Spline(xPoints, yPoints);
Expand All @@ -113,7 +109,7 @@ export const Interpolation = (
config: ConfigParams,
input: PluginParams
) => {
const parameter = input[globalConfig['input-parameter']];
const parameter = input[config['input-parameter']];
const xPoints: number[] = config.x;
const yPoints: number[] = config.y;

Expand All @@ -133,12 +129,12 @@ export const Interpolation = (
};

/**
* Validates global config parameters.
* Validates config parameters.
* Sorts elements of `x` and `y`.
*/
const validateConfig = () => {
if (!globalConfig) {
throw new GlobalConfigError(MISSING_GLOBAL_CONFIG);
if (!config) {
throw new GlobalConfigError(MISSING_CONFIG);
}

const schema = z
Expand All @@ -156,16 +152,11 @@ export const Interpolation = (
message: ARRAY_LENGTH_NON_EMPTY,
});

const defaultMethod = globalConfig.method ?? Method.LINEAR;
const updatedConfig = Object.assign(
{},
{method: defaultMethod},
globalConfig,
{
x: sortPoints(globalConfig.x),
y: sortPoints(globalConfig.y),
}
);
const defaultMethod = config.method ?? Method.LINEAR;
const updatedConfig = Object.assign({}, {method: defaultMethod}, config, {
x: sortPoints(config.x),
y: sortPoints(config.y),
});

return validate<z.infer<typeof schema>>(schema, updatedConfig);
};
Expand All @@ -179,7 +170,7 @@ export const Interpolation = (
* Validates inputes parameters.
*/
const validateInput = (input: PluginParams, index: number) => {
const inputParameter = globalConfig['input-parameter'];
const inputParameter = config['input-parameter'];
const schema = z
.object({
timestamp: z.string().or(z.date()),
Expand All @@ -188,8 +179,8 @@ export const Interpolation = (
})
.refine(
data =>
data[inputParameter] >= globalConfig.x[0] &&
data[inputParameter] <= globalConfig.x[globalConfig.x.length - 1],
data[inputParameter] >= config.x[0] &&
data[inputParameter] <= config.x[config.x.length - 1],
{
message: WITHIN_THE_RANGE,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {STRINGS} from '../../../config';
import {Generator} from '../interfaces';

const {GlobalConfigError} = ERRORS;
const {MISSING_GLOBAL_CONFIG} = STRINGS;
const {MISSING_CONFIG} = STRINGS;

export const CommonGenerator = (config: ConfigParams): Generator => {
/**
Expand All @@ -16,7 +16,7 @@ export const CommonGenerator = (config: ConfigParams): Generator => {
*/
const validateConfig = (config: object) => {
if (!config || Object.keys(config).length === 0) {
throw new GlobalConfigError(MISSING_GLOBAL_CONFIG);
throw new GlobalConfigError(MISSING_CONFIG);
}

return structuredClone(config);
Expand Down
Loading

0 comments on commit cea711a

Please sign in to comment.