Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : Function Expression Defintions #59

Closed
pksorensen opened this issue Dec 23, 2021 · 2 comments · Fixed by #75
Closed

Feature : Function Expression Defintions #59

pksorensen opened this issue Dec 23, 2021 · 2 comments · Fixed by #75
Assignees
Labels

Comments

@pksorensen
Copy link
Member

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()]")
@pksorensen pksorensen added the enhancement New feature or request label Dec 23, 2021
thygesteffensen added a commit that referenced this issue Feb 28, 2022
Function Definition can be added to 'group' functions together to make it easier to write expressions with a lot of redundant function calls

Resolves #59
@thygesteffensen thygesteffensen linked a pull request Feb 28, 2022 that will close this issue
@github-actions
Copy link

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

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link

🎉 This issue has been resolved in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants