-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(builtins): refactor , and plugins
- Loading branch information
Showing
3 changed files
with
160 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,52 @@ | ||
import {z, ZodType} from 'zod'; | ||
|
||
import { | ||
ERRORS, | ||
evaluateInput, | ||
evaluateConfig, | ||
evaluateArithmeticOutput, | ||
getParameterFromArithmeticExpression, | ||
validateArithmeticExpression, | ||
} from '@grnsft/if-core/utils'; | ||
import { | ||
mapConfigIfNeeded, | ||
mapOutputIfNeeded, | ||
} from '@grnsft/if-core/utils/helpers'; | ||
import { | ||
CoefficientConfig, | ||
ExecutePlugin, | ||
MappingParams, | ||
PluginParametersMetadata, | ||
PluginParams, | ||
} from '@grnsft/if-core/types'; | ||
import {z} from 'zod'; | ||
import {PluginFactory} from '@grnsft/if-core/interfaces'; | ||
import {ConfigParams, PluginParams} from '@grnsft/if-core/types'; | ||
|
||
import {validate} from '../../../common/util/validations'; | ||
|
||
import {STRINGS} from '../../config'; | ||
|
||
const {ConfigError} = ERRORS; | ||
const {MISSING_CONFIG} = STRINGS; | ||
export const Coefficient = PluginFactory({ | ||
metadata: { | ||
inputs: {}, | ||
outputs: {}, | ||
}, | ||
configValidation: z.object({ | ||
coefficient: z.number(), | ||
'input-parameter': z.string().min(1), | ||
'output-parameter': z.string().min(1), | ||
}), | ||
inputValidation: (input: PluginParams, config: ConfigParams) => { | ||
const inputData = { | ||
'input-parameter': input[config['input-parameter']], | ||
}; | ||
const validationSchema = z.record(z.string(), z.number()); | ||
validate(validationSchema, inputData); | ||
|
||
export const Coefficient = ( | ||
config: CoefficientConfig, | ||
parametersMetadata: PluginParametersMetadata, | ||
mapping: MappingParams | ||
): ExecutePlugin => { | ||
const metadata = { | ||
kind: 'execute', | ||
inputs: parametersMetadata?.inputs, | ||
outputs: parametersMetadata?.outputs, | ||
}; | ||
return input; | ||
}, | ||
implementation: async (inputs: PluginParams[], config: ConfigParams = {}) => { | ||
const inputParameter = config['input-parameter']; | ||
const outputParameter = config['output-parameter']; | ||
const coefficient = config['coefficient']; | ||
|
||
/** | ||
* Calculate the product of each input parameter. | ||
*/ | ||
const execute = (inputs: PluginParams[]) => { | ||
const safeConfig = validateConfig(); | ||
const { | ||
'input-parameter': inputParameter, | ||
'output-parameter': outputParameter, | ||
} = safeConfig; | ||
return inputs.map(input => { | ||
const calculatedConfig = evaluateConfig({ | ||
config: safeConfig, | ||
input, | ||
parametersToEvaluate: ['input-parameter', 'coefficient'], | ||
}); | ||
|
||
const safeInput = validateSingleInput(input, inputParameter); | ||
const coefficient = Number(calculatedConfig['coefficient']); | ||
const calculatedResult = calculateProduct( | ||
safeInput, | ||
calculatedConfig['input-parameter'], | ||
coefficient | ||
); | ||
|
||
const result = { | ||
...input, | ||
...safeInput, | ||
...evaluateArithmeticOutput(outputParameter, calculatedResult), | ||
[outputParameter]: calculateProduct(input, inputParameter, coefficient), | ||
}; | ||
|
||
return mapOutputIfNeeded(result, mapping); | ||
return result; | ||
}); | ||
}; | ||
|
||
/** | ||
* Checks for required fields in input. | ||
*/ | ||
const validateSingleInput = ( | ||
input: PluginParams, | ||
configInputParameter: string | ||
) => { | ||
const inputParameter = | ||
getParameterFromArithmeticExpression(configInputParameter); | ||
const evaluatedInput = evaluateInput(input); | ||
|
||
const inputData = { | ||
[inputParameter]: evaluatedInput[inputParameter], | ||
}; | ||
const validationSchema = z.record(z.string(), z.number()); | ||
validate(validationSchema, inputData); | ||
|
||
return evaluatedInput; | ||
}; | ||
|
||
/** | ||
* Calculates the product of the energy components. | ||
*/ | ||
const calculateProduct = ( | ||
input: PluginParams, | ||
inputParameter: string | number, | ||
coefficient: number | ||
) => | ||
(isNaN(Number(inputParameter)) ? input[inputParameter] : inputParameter) * | ||
coefficient; | ||
|
||
/** | ||
* Checks config value are valid. | ||
*/ | ||
const validateConfig = () => { | ||
if (!config) { | ||
throw new ConfigError(MISSING_CONFIG); | ||
} | ||
|
||
const mappedConfig = mapConfigIfNeeded(config, mapping); | ||
|
||
const configSchema = z | ||
.object({ | ||
coefficient: z.preprocess( | ||
value => validateArithmeticExpression('coefficient', value), | ||
z.number() | ||
), | ||
'input-parameter': z.string().min(1), | ||
'output-parameter': z.string().min(1), | ||
}) | ||
.refine(params => { | ||
Object.entries(params).forEach(([param, value]) => | ||
validateArithmeticExpression(param, value) | ||
); | ||
|
||
return true; | ||
}); | ||
|
||
return validate<z.infer<typeof configSchema>>( | ||
configSchema as ZodType<any>, | ||
mappedConfig | ||
); | ||
}; | ||
|
||
return { | ||
metadata, | ||
execute, | ||
}; | ||
}; | ||
}, | ||
allowArithmeticExpressions: ['input-parameter', 'coefficient'], | ||
}); | ||
|
||
/** | ||
* Calculates the product of the energy components. | ||
*/ | ||
const calculateProduct = ( | ||
input: PluginParams, | ||
inputParameter: string | number, | ||
coefficient: number | ||
) => | ||
(isNaN(Number(inputParameter)) ? input[inputParameter] : inputParameter) * | ||
coefficient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.