delegateas / ExpressionEngine

Expression Engine for Power Automate expressions written in C# using Sprache!
MIT License
8 stars 3 forks source link

Feature : Function Expression Defintions #59

Closed pksorensen closed 2 years ago

pksorensen commented 2 years ago

We had the following expression

    "expression": "@and(equal(length(formvalue(logicalName())[idx()]['cvr']),8),isDigits(formvalue(logicalName())[idx()]['cvr']))"

and wanted to simplify the whole formvalue(logicalName())[idx()]['cvr'] into a currentCellValue function and coded this up:

public class CurrentCellValueFunction : Function
    {
        private readonly ILogger _logger;
        private readonly ManifestAccessor _validationContext;
        private readonly FormDataAccessor _formDataAccessor;
        private readonly FormValueFunction _formValueFunction;
        private readonly IdxFunction _idxFunction;
        private readonly LogicalNameFunction _logicalNameFunction;

        public CurrentCellValueFunction(
            ILogger<CurrentCellValueFunction> logger,
            ManifestAccessor validationContext,
            FormDataAccessor formDataAccessor,
            FormValueFunction formValueFunction,
            IdxFunction idxFunction,
            LogicalNameFunction logicalNameFunction) : base("currentCellValue")
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            _validationContext = validationContext ?? throw new ArgumentNullException(nameof(validationContext));
            _formDataAccessor = formDataAccessor ?? throw new ArgumentNullException(nameof(formDataAccessor));
            _formValueFunction = formValueFunction;
            _idxFunction = idxFunction;
            _logicalNameFunction = logicalNameFunction;
        }

        public override async ValueTask<ValueContainer> ExecuteFunction(params ValueContainer[] parameters)
        {
            _logger.LogDebug("{FunctionName} entered", nameof(CurrentCellValueFunction));

            try
            {  

                //currentCellValue = formvalue(logicalName())[idx()]['cvr']

                var logicalName = await _logicalNameFunction.ExecuteFunction();
                var idx = await _idxFunction.ExecuteFunction();
                var formvalue = await _formValueFunction.ExecuteFunction(logicalName);

                var currentCell = formvalue[idx.GetValue<int>()][_validationContext.CurrentAttributeLogicalName];

                return currentCell;

            }
            catch (Exception ex)
            {
                _logger.LogDebug(ex,"{FunctionName} failed", nameof(CurrentCellValueFunction));
                throw;
            }
        }
    }

however it bascially could have been automated in the framework to do Function expression Definitions:

services.AddFunctionExpresssionDefinition("currentCellValue", "@formvalue(logicalName())[idx()][attributeLogicalName()]")

or

services.AddFunctionExpresssionDefinition("currentCellValue", "formvalue(logicalName())[idx()][attributeLogicalName()]")
github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 4.0.0-dev.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 4.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: