Skip to content

Commit

Permalink
Update NCalc to 5.2.7 and add iif alias
Browse files Browse the repository at this point in the history
  • Loading branch information
gumbarros committed Sep 2, 2024
1 parent dcfa0b9 commit 818a76e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PackageProjectUrl>https://www.github.com/JJConsulting/JJMasterData</PackageProjectUrl>
<RepositoryUrl>https://www.github.com/JJConsulting/JJMasterData</RepositoryUrl>
<PackageIcon>JJMasterData.png</PackageIcon>
<Version>4.3.4</Version>
<Version>4.3.5</Version>
<PackageReadmeFile>README.NuGet.md</PackageReadmeFile>
<PackageVersion>$(Version)</PackageVersion>
<AssemblyVersion>$(Version)</AssemblyVersion>
Expand Down
42 changes: 18 additions & 24 deletions src/Core/Configuration/Options/MasterDataCoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using JJMasterData.Commons.Util;
using NCalc;
using NCalc.Exceptions;

namespace JJMasterData.Core.Configuration.Options;

Expand All @@ -23,19 +25,19 @@ public sealed class MasterDataCoreOptions
/// </summary>
[Display(Name = "Audit Log Table Name")]
public string AuditLogTableName { get; set; } = "tb_masterdata_auditlog";
#if !NET

#if !NET
/// <summary>
/// Default value: null
/// </summary>
public string? MasterDataUrl { get; set; }

public bool EnableCultureProviderAtUrl { get; set; } = true;
#endif
#endif

[Display(Name = "Enable Data Dictionary Caching")]
public bool EnableDataDictionaryCaching { get; set; } = true;

/// <summary>
/// Default value: {ApplicationPath}/JJExportationFiles
/// </summary>
Expand All @@ -52,26 +54,18 @@ public sealed class MasterDataCoreOptions
| ExpressionOptions.OrdinalStringComparer
| ExpressionOptions.AllowNullOrEmptyExpressions
| ExpressionOptions.CaseInsensitiveStringComparer,
Functions = new Dictionary<string, ExpressionFunction>
{
{ "now", _ => DateTime.Now }
}.ToFrozenDictionary()
};

/// <summary>
/// Context of async expressions starting with "exp:". Declare here custom parameters and functions.
/// </summary>
public AsyncExpressionContext AsyncExpressionsContext { get; set; } = new()
{
Options = ExpressionOptions.IgnoreCaseAtBuiltInFunctions
| ExpressionOptions.AllowNullParameter
| ExpressionOptions.OrdinalStringComparer
| ExpressionOptions.AllowNullOrEmptyExpressions
| ExpressionOptions.CaseInsensitiveStringComparer,
Functions = new Dictionary<string, AsyncExpressionFunction>
Functions = new Dictionary<string, ExpressionFunction>(StringComparer.InvariantCultureIgnoreCase)
{
{ "now", _ => new(DateTime.Now) }
{ "now", _ => DateTime.Now },
{
"iif", args =>
{
if (args.Count() != 3)
throw new NCalcEvaluationException("iif() takes exactly 3 arguments.");
var conditional = StringManager.ParseBool(args[0].Evaluate());
return conditional ? args[1].Evaluate() : args[2].Evaluate();
}
}
}.ToFrozenDictionary()
};

}
7 changes: 3 additions & 4 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.8" />
<PackageReference Include="NCalc.MemoryCache" Version="5.2.6" />
<PackageReference Include="NCalc.DependencyInjection" Version="5.2.6" />
<PackageReference Include="NCalcAsync" Version="5.2.6" />
<PackageReference Include="NCalcSync" Version="5.2.6" />
<PackageReference Include="Fluid.Core" Version="2.11.1" />
<PackageReference Include="NCalcSync" Version="5.2.7" />
<PackageReference Include="NCalc.MemoryCache" Version="5.2.7" />
<PackageReference Include="NCalc.DependencyInjection" Version="5.2.7" />
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace JJMasterData.Core.DataManager.Expressions.Providers;

public sealed class DefaultExpressionProvider(
IExpressionFactory expressionFactory,
IAsyncExpressionFactory asyncExpressionFactory,
IServiceProvider serviceProvider,
IOptions<MasterDataCoreOptions> options) : ISyncExpressionProvider, IAsyncExpressionProvider
{
public string Prefix => "exp";
Expand All @@ -24,27 +22,12 @@ public sealed class DefaultExpressionProvider(
public object? Evaluate(string expression, Dictionary<string, object?> parsedValues)
{
var replacedExpression = ExpressionHelper.ReplaceExpression(expression, parsedValues);
var ncalcExpression = expressionFactory.Create(replacedExpression, options.Value.ExpressionContext with
{
StaticParameters = new Dictionary<string, object?>
{
{"serviceProvider", serviceProvider}
}
});
var ncalcExpression = expressionFactory.Create(replacedExpression, options.Value.ExpressionContext);
return ncalcExpression.Evaluate();
}

public ValueTask<object?> EvaluateAsync(string expression, Dictionary<string, object?> parsedValues)
{
var replacedExpression = ExpressionHelper.ReplaceExpression(expression, parsedValues);
var ncalcExpression = asyncExpressionFactory.Create(replacedExpression, options.Value.AsyncExpressionsContext with
{
StaticParameters = new Dictionary<string, object?>
{
{"serviceProvider", serviceProvider},
{"connectionId", ConnectionId}
}
});
return ncalcExpression.EvaluateAsync();
return new ValueTask<object?>(Evaluate(expression,parsedValues));
}
}

0 comments on commit 818a76e

Please sign in to comment.