|
@@ -93,9 +93,10 @@ namespace InABox.Core
|
|
|
|
|
|
protected virtual Type? ReturnType { get; }
|
|
|
|
|
|
- public CoreExpression(string expressionString)
|
|
|
+ public CoreExpression(string expressionString, Type? returnType = null)
|
|
|
{
|
|
|
Expression = new Expression(expressionString, _context);
|
|
|
+ ReturnType = returnType;
|
|
|
if (!IsValid)
|
|
|
{
|
|
|
var expr = "\"" + expressionString + "\"";
|
|
@@ -117,6 +118,32 @@ namespace InABox.Core
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ public object? Evaluate(IVariableProvider provider)
|
|
|
+ {
|
|
|
+ var result = Expression.Evaluate(provider);
|
|
|
+ if(ReturnType != null)
|
|
|
+ {
|
|
|
+ return CoreUtils.ChangeType(result, ReturnType);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public Result<T, Exception> TryEvaluate<T>(IVariableProvider provider)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var result = Evaluate(provider);
|
|
|
+ if(result is T ret)
|
|
|
+ {
|
|
|
+ return Result.Ok(ret);
|
|
|
+ }
|
|
|
+ return Result.Ok<T>(default);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ return Result.Error(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static List<string> GetModelVariables(Type modelType)
|
|
|
{
|
|
|
var props = DatabaseSchema.Properties(modelType).Select(x => x.Name).ToList();
|
|
@@ -166,6 +193,17 @@ namespace InABox.Core
|
|
|
return default;
|
|
|
}
|
|
|
|
|
|
+ [return: MaybeNull]
|
|
|
+ public new TReturn Evaluate(IVariableProvider provider)
|
|
|
+ {
|
|
|
+ var result = base.Evaluate(provider);
|
|
|
+ if(result is TReturn ret)
|
|
|
+ {
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ return default;
|
|
|
+ }
|
|
|
+
|
|
|
public Result<TReturn, Exception> Evaluate(TModel model)
|
|
|
{
|
|
|
var values = new Dictionary<string, object?>();
|