diff --git a/doc/scenarioParameters.md b/doc/scenarioParameters.md index 44657dd43..b4f8e9af0 100644 --- a/doc/scenarioParameters.md +++ b/doc/scenarioParameters.md @@ -235,6 +235,32 @@ const PARAMETERS = { }; ``` +### Improve mapping between varTypes and factories + +You may have to define a specific input component regarding a specific parameter. +To do this, you can add a property to your parameter configuration: +- `extendedVarType` (optional): define extra information on varType to enhance mapping between varType <-> InputFactory + +Example: + +``` +const PARAMETERS = { + initial_stock_dataset: { + connectorId: 'C-xxxxxxxxxx', + defaultFileTypeFilter: '.zip,.csv,.json,.xls,.xlsx', + description: 'Initial stock dataset part' + extendedVarType: 'TABLE' + }, +}; +``` + +After that, you'll have to handle this `extendedVarType` properly by defining: +- a conversion method in `ConversionFromString.js` file +- a conversion method in `ConversionToString.js` file +- an entry in `CUSTOM_VAR_TYPES_EXTENSION` constant in `DefaultValues.js` file +- an entry in `CUSTOM_VAR_TYPES_FACTORIES_MAPPING` constant in `FactoriesMapping.js` file +- define your own `inputComponentsFactories` + ### Configure input components for Cypress tests If you want to add Cypress tests for your solution and test the scenario parameters input components, you can add a diff --git a/src/utils/ConfigUtils.js b/src/utils/ConfigUtils.js index eb1f8a6af..25a4c1731 100644 --- a/src/utils/ConfigUtils.js +++ b/src/utils/ConfigUtils.js @@ -28,6 +28,17 @@ export const addTranslationLabels = (config) => { _addTranslationParametersLabels(config); }; +const buildExtendedVarType = (varType, extension) => { + if (varType) { + if (extension) { + return varType + '-' + extension; + } + return varType; + } + return undefined; +}; + export const ConfigUtils = { addTranslationLabels, + buildExtendedVarType, }; diff --git a/src/utils/__test__/ConfigUtils.spec.js b/src/utils/__test__/ConfigUtils.spec.js new file mode 100644 index 000000000..218e52435 --- /dev/null +++ b/src/utils/__test__/ConfigUtils.spec.js @@ -0,0 +1,25 @@ +// Copyright (c) Cosmo Tech. +// Licensed under the MIT license. + +import { ConfigUtils } from '../ConfigUtils'; + +describe('buildExtendedVarType with possible values', () => { + test.each` + varType | extension | expectedRes + ${null} | ${null} | ${undefined} + ${null} | ${undefined} | ${undefined} + ${null} | ${''} | ${undefined} + ${null} | ${'extension'} | ${undefined} + ${''} | ${null} | ${undefined} + ${''} | ${undefined} | ${undefined} + ${''} | ${''} | ${undefined} + ${''} | ${'extension'} | ${undefined} + ${'varType'} | ${null} | ${'varType'} + ${'varType'} | ${undefined} | ${'varType'} + ${'varType'} | ${''} | ${'varType'} + ${'varType'} | ${'extension'} | ${'varType-extension'} + `('if "$varType" and "$extension" then "$expectedRes"', ({ varType, extension, expectedRes }) => { + const res = ConfigUtils.buildExtendedVarType(varType, extension); + expect(res).toStrictEqual(expectedRes); + }); +}); diff --git a/src/utils/scenarioParameters/factories/ScenarioParameterInputFactory.js b/src/utils/scenarioParameters/factories/ScenarioParameterInputFactory.js index 9a52344bf..74bf1de9f 100644 --- a/src/utils/scenarioParameters/factories/ScenarioParameterInputFactory.js +++ b/src/utils/scenarioParameters/factories/ScenarioParameterInputFactory.js @@ -3,18 +3,22 @@ import { DATASET_ID_VARTYPE } from '../../../services/config/ApiConstants'; import { VAR_TYPES_FACTORIES_MAPPING } from '../FactoriesMapping'; +import { ConfigUtils } from '../../ConfigUtils'; const create = (t, datasets, parameterData, parametersState, setParametersState, editMode) => { - const varTypeFactory = VAR_TYPES_FACTORIES_MAPPING[parameterData.varType]; + const parameterVarType = ConfigUtils.buildExtendedVarType(parameterData.varType, parameterData.extendedVarType); + + const varTypeFactory = VAR_TYPES_FACTORIES_MAPPING[parameterVarType]; + if (varTypeFactory === undefined) { - console.warn('No factory defined for varType ' + parameterData.varType); + console.warn('No factory defined for varType ' + parameterVarType); return null; } if (varTypeFactory === null) { return null; } - if (parameterData.varType === DATASET_ID_VARTYPE) { + if (parameterVarType === DATASET_ID_VARTYPE) { return varTypeFactory.create(t, datasets, parameterData, parametersState, setParametersState, editMode); } return varTypeFactory.create(t, parameterData, parametersState, setParametersState, editMode);