Skip to content

Commit

Permalink
Merge pull request #211 from takenet/feature/630646-changing-copy-arg…
Browse files Browse the repository at this point in the history
…uments-method

630646 changing copy arguments method
  • Loading branch information
danteveloso authored Jun 4, 2024
2 parents 2a92907 + b31e9e5 commit ca8a46b
Showing 1 changed file with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Esprima;
using HandlebarsDotNet;
using Lime.Protocol;
using Newtonsoft.Json;
Expand Down Expand Up @@ -29,9 +29,9 @@ public override async Task ExecuteAsync(IContext context, ExecuteTemplateSetting
try
{
var arguments = await GetScriptArgumentsAsync(context, settings, cancellationToken);
var obj = CopyArgumentsToObject(arguments);
var dict = CopyArgumentsToDictionary(arguments);
var template = _handlebars.Compile(settings.Template);
result = template(obj);
result = template(dict);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -78,28 +78,24 @@ private async Task SetScriptResultAsync(
await context.SetVariableAsync(settings.OutputVariable, result, cancellationToken);
}
}

private JObject CopyArgumentsToObject(JObject arguments)
private Dictionary<string, object> CopyArgumentsToDictionary(JObject arguments)
{
var obj = new JObject();
foreach (var property in arguments.Properties())
var obj = new Dictionary<string, object>();
foreach (var property in arguments.Properties())
{
try
{
var deserializedJson = JsonConvert.DeserializeObject<JToken>(property.Value.ToString(), JsonSerializerSettingsContainer.Settings);
if (deserializedJson != null)
{
obj.Add(property.Name, deserializedJson);
}
} catch (Exception ex)
var deserializedJson = JsonConvert.DeserializeObject(property.Value.ToString(), JsonSerializerSettingsContainer.Settings);
obj[property.Name] = deserializedJson ?? property.Value.ToString();
}
catch (Exception ex)
{
if (ex is JsonReaderException)
{
obj[property.Name] = property.Value;
}
}
}

return obj;
}
}
Expand Down

0 comments on commit ca8a46b

Please sign in to comment.