Enable Computed Cfgu Properties
rannn505 opened this issue · comments
Suggestion
Introducing the capability for computed values in Cfgu declarations, specifically for properties like default
and required
. This feature will allow users to provide expressions for these properties, enabling dynamic evaluation based on the context or other configuration values.
When running commands using a Schema with computed Cfgu properties, the expressions will be evaluated to determine the actual value of these properties. This enhancement opens up possibilities for more flexible and context-sensitive configuration setups.
For instance, it could allow for the creation of conditional properties like required-if
or computed-default
. Consider the following JSON schema example:
{
"ENABLE_FEAT": {
"type": "Boolean",
"default": "{{#$}}return '{{CONFIGU_SET.path}}' == 'production'{{/$}}"
},
"FEAT_CONF": {
"type": "String",
"required": "{{#$}}return {{ENABLE_FEAT}}{{/$}}"
}
...
}
In this example:
- The
ENABLE_FEAT
boolean property dynamically sets its default value based on whether theCONFIGU_SET.path
is 'production'. - The
FEAT_CONF
string property becomes required ifENABLE_FEAT
is true.
Motivation
The ability to define computed properties significantly enhances the flexibility and adaptability of the Cfgu declaration. It allows for context-dependent settings, enabling configurations to adapt to different environments or conditions automatically. This feature is particularly beneficial in complex deployment scenarios where configuration needs may vary significantly based on the environment, application state, or other dynamic factors. By implementing computed properties, Configu will offer a more powerful and versatile solution for configuration management, meeting diverse and evolving user needs.
Context
potential implementation for JS (mustache functions api and JS Function constructor):
const Mustache = require('mustache');
const view = {
title: "Joe",
calc: () => ( 7 + 7 + 7 ),
threshold: 11,
eval: function () {
return function (text, render) {
return Function(render(text))();
}
}
};
const output = Mustache.render("{{#eval}} return {{calc}} > {{threshold}} && '{{title}}' === 'Ran' {{/eval}}", view);
- https://github.com/TomFrost/Jexl
- https://github.com/mozilla/mozjexl
- https://mathjs.org/docs/expressions/index.html
- https://github.com/silentmatt/expr-eval
- https://www.npmjs.com/package/expr-eval-fork
- https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-concept-adaptive-expressions?view=azure-bot-service-4.0&tabs=arithmetic
- https://github.com/formulajs/formulajs
- https://github.com/P0lip/simple-eval
Currently except for adaptive-expressions
all others are not maintained or have security issues.