fixing issue with jsonelements inside variables when passing to function code

This commit is contained in:
reven
2022-01-26 15:31:04 +13:00
parent 3b68481cf1
commit 4678de7ea7
2 changed files with 37 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ namespace FileFlows.BasicNodes.Functions
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Text.Json;
public class Function : Node
{
@@ -52,6 +53,17 @@ namespace FileFlows.BasicNodes.Functions
// so Variables.file?.Orig.Name, will be replaced to Variables["file.Orig.Name"]
// since its just a dictionary key value
string keyRegex = @"Variables(\?)?\." + k.Replace(".", @"(\?)?\.");
object? value = args.Variables[k];
if (value is JsonElement jElement)
{
if (jElement.ValueKind == JsonValueKind.String)
value = jElement.GetString();
if (jElement.ValueKind == JsonValueKind.Number)
value = jElement.GetInt64();
}
tcode = Regex.Replace(tcode, keyRegex, "Variables['" + k + "']");
}

View File

@@ -403,6 +403,31 @@ return 2; // it isn't so call output 2";
}
[TestMethod]
public void Function_FileNameStringVariable()
{
Function pm = new Function();
var logger = new TestLogger();
var args = new FileFlows.Plugin.NodeParameters(@"D:\videos\unprocessed\movie h264.mkv", logger, false, string.Empty);
pm.Code = @"
let newName = Variables.file.Name;
if (newName.indexOf('h264') > 0)
newName = newName.replace('h264', 'h265');
else if (newName.indexOf('hevc') > 0)
newName = newName.replace('hevc', 'h265');
else
newName += ' h265';
if (newName == Variables.file.Name)
return 2;
Variables.NewName = newName;
return 1;
; ";
var result = pm.Execute(args);
Assert.AreEqual(1, result);
Assert.AreEqual("movie h265", args.Variables["NewName"]);
}
}
}