using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
using System.ComponentModel.DataAnnotations;
namespace FileFlows.BasicNodes.Functions;
///
/// Tests if an input value is matched by a variable
///
public class VariableMatch : Node
{
///
public override int Inputs => 1;
///
public override int Outputs => 2;
///
public override FlowElementType Type => FlowElementType.Logic;
///
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/variable-match";
///
public override string Icon => "fas fa-equals";
///
public override bool FailureNode => true;
///
/// Gets or sets the variable to match
///
public ObjectReference Variable { get; set; }
///
/// Gets or sets the variable to match
///
[Required]
[Combobox("VARIABLE_LIST", 1)]
public string VariableName { get; set; }
///
/// Gets or sets the value to match
///
[Required]
[TextVariable(2)]
public string Input { get; set; }
///
public override int Execute(NodeParameters args)
{
string variableName = VariableName?.EmptyAsNull() ?? Variable?.Name?.EmptyAsNull() ?? string.Empty;
if (string.IsNullOrWhiteSpace(variableName))
{
args.FailureReason = "No variable defined to match against";
args.Logger?.ELog(args.FailureReason);
return -1;
}
string test = args.ReplaceVariables(Input, stripMissing: true);
args.Logger.ILog("Variable: " + variableName);
args.Logger.ILog("Test Value: " + test);
if (args.Variables.TryGetValue(variableName, out object variable) == false)
{
args.Logger?.ILog("Variable not found");
return 2;
}
var variableString = variable?.ToString();
args.Logger.ILog($"Variables[{variableName}]: " + variableString);
if (string.IsNullOrWhiteSpace(variableString))
{
args.Logger?.ILog("Variable had no appropriate string value");
return 2;
}
if (args.MathHelper.IsMathOperation(test) && double.TryParse(variableString, out var dbl))
{
args.Logger?.ILog("Testing math operation: " + test);
bool mathMatches = args.MathHelper.IsTrue(test, dbl);
return mathMatches ? 1 : 2;
}
bool matches = args.StringHelper.Matches(test, variableString);
return matches ? 1 : 2;
}
}