using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace FrameworkBLL.ActionProcessor
{
///
/// Evaluates MEVENTTYPEACTIONDETAIL.EVALCONDITION expressions.
/// Supported syntax: {FieldName}==value or {FieldName}!=value
/// Returns true (pass) when expression is null/empty.
///
public interface IEvalConditionService
{
bool Evaluate(string? expression, IDictionary fields);
}
public sealed class EvalConditionService : IEvalConditionService
{
// Matches: {FieldName}==value or {FieldName}!=value
private static readonly Regex _regex = new(
@"^\{(?[^}]+)\}(?==|!=)(?.*)$",
RegexOptions.Compiled | RegexOptions.Singleline);
public bool Evaluate(string? expression, IDictionary fields)
{
if (string.IsNullOrWhiteSpace(expression))
return true;
var match = _regex.Match(expression.Trim());
if (!match.Success)
return true; // unknown syntax — pass through
var fieldName = match.Groups["field"].Value;
var op = match.Groups["op"].Value;
var expected = match.Groups["value"].Value;
fields.TryGetValue(fieldName, out var actual);
var actualStr = actual ?? string.Empty;
return op switch
{
"==" => string.Equals(actualStr, expected, StringComparison.OrdinalIgnoreCase),
"!=" => !string.Equals(actualStr, expected, StringComparison.OrdinalIgnoreCase),
_ => true
};
}
}
}