Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions JUST.net/DataTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ private object EvaluateFunction(string functionString, string inputJson, JArray
}
}

listParameters.Add(new JUSTContext(inputJson));
var parameters = listParameters.ToArray();
var convertParameters = true;
if (new[] { "concat", "xconcat", "currentproperty" }.Contains(functionName))
{
Expand All @@ -167,44 +165,44 @@ private object EvaluateFunction(string functionString, string inputJson, JArray

if (functionName == "loop")
{
output = GetLoopResult(parameters, loopArgumentString);
output = GetLoopResult(listParameters.Concat(new object[] { JToken.Parse(inputJson), Context }).ToArray(), loopArgumentString, Context);
}
else if (functionName == "currentvalue" || functionName == "currentindex" || functionName == "lastindex"
|| functionName == "lastvalue")
output = Caller("JUST.Transformer`1", functionName, new object[] { array, currentArrayElement });
else if (functionName == "currentvalueatpath" || functionName == "lastvalueatpath")
output = Caller("JUST.Transformer`1", functionName, new object[] { array, currentArrayElement, arguments[0], new JUSTContext() });
else if (functionName == "customfunction")
output = CallCustomFunction(parameters);
output = CallCustomFunction(listParameters.Concat(new object[] { JToken.Parse(inputJson), Context }).ToArray());
else if (Context?.IsRegisteredCustomFunction(functionName) ?? false)
{
var methodInfo = Context.GetCustomMethod(functionName);
output = ReflectionHelper.InvokeCustomMethod<T>(methodInfo, parameters, convertParameters, Context);
output = ReflectionHelper.InvokeCustomMethod<T>(methodInfo, listParameters.Concat(new object[] { JToken.Parse(inputJson), Context }).ToArray(), convertParameters, Context);
}
else if (functionName == "xconcat" || functionName == "xadd" || functionName == "mathequals" || functionName == "mathgreaterthan" || functionName == "mathlessthan"
|| functionName == "mathgreaterthanorequalto"
|| functionName == "mathlessthanorequalto" || functionName == "stringcontains" ||
functionName == "stringequals")
{
object[] oParams = new object[1];
oParams[0] = parameters;
oParams[0] = listParameters.Concat(new object[] { Context }).ToArray();
output = Caller("JUST.Transformer`1", functionName, oParams);
}
else
output = Caller("JUST.Transformer`1", functionName, parameters);
output = Caller("JUST.Transformer`1", functionName, listParameters.Concat(new object[] { JToken.Parse(inputJson), Context }).ToArray());
}
return output;
}

private string GetLoopResult(object[] parameters,string loopArgumentString)
private string GetLoopResult(object[] parameters,string loopArgumentString, JUSTContext context)
{
string returnString = string.Empty;

if (parameters.Length < 2)
if (parameters.Length < 3)
throw new Exception("Incorrect number of parameters for function #Loop");

var context = (JUSTContext)parameters[parameters.Length - 1];
JToken token = context.Input;
//var context = (JUSTContext)parameters[parameters.Length - 1];
JToken token = (JToken)parameters[parameters.Length - 2];
JToken selectedToken = context.Resolve<T>(token).Select(parameters[0].ToString());

if (selectedToken.Type != JTokenType.Array)
Expand All @@ -214,7 +212,7 @@ private string GetLoopResult(object[] parameters,string loopArgumentString)

string seperator = Environment.NewLine;

if (parameters.Length == 3)
if (parameters.Length == 4)
seperator = parameters[1].ToString();

foreach (JToken arrToken in selectedToken.Children())
Expand Down
4 changes: 2 additions & 2 deletions JUST.net/ExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace JUST
{
internal static class ExceptionHelper
{
internal static void HandleException(Exception ex, EvaluationMode evaluationMode)
internal static void HandleException(Exception ex, bool IsStrictMode)
{
if ( (evaluationMode & EvaluationMode.Strict) == EvaluationMode.Strict)
if (IsStrictMode)
{
if (ex.InnerException != null)
{
Expand Down
10 changes: 10 additions & 0 deletions JUST.net/IContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using JUST;
using JUST.net.Selectables;
using Newtonsoft.Json.Linq;

public interface IContext{
char SplitGroupChar { get; }
int DefaultDecimalPlaces { get; }
bool IsStrictMode();
T Resolve<T>(JToken token) where T: ISelectableToken;
}
16 changes: 9 additions & 7 deletions JUST.net/JUSTContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ public enum EvaluationMode : short
Strict = 4
}

public class JUSTContext
public class JUSTContext : IContext
{
private Dictionary<string, MethodInfo> _customFunctions = new Dictionary<string, MethodInfo>();
private int _defaultDecimalPlaces = 28;

internal JToken Input;

public EvaluationMode EvaluationMode = EvaluationMode.FallbackToDefault;
private char _escapeChar = '/'; //do not use backslash, it is already the escape char in JSON
private char _splitGroupChar = ':';
Expand Down Expand Up @@ -88,12 +86,16 @@ public JUSTContext(IEnumerable<CustomFunction> customFunctions)
}
}

internal JUSTContext(string inputJson)
internal JUSTContext(JUSTContext context)
{
Input = JToken.Parse(inputJson);
this.EvaluationMode = context.EvaluationMode;
this.EscapeChar = context.EscapeChar;
this.DefaultDecimalPlaces = context.DefaultDecimalPlaces;
this.SplitGroupChar = context.SplitGroupChar;
this._customFunctions = context._customFunctions;
}

internal bool IsStrictMode()
public bool IsStrictMode()
{
return (EvaluationMode & EvaluationMode.Strict) == EvaluationMode.Strict;
}
Expand Down Expand Up @@ -148,7 +150,7 @@ internal bool IsRegisteredCustomFunction(string aliasOrName)
return _customFunctions.ContainsKey(aliasOrName);
}

internal T Resolve<T>(JToken token) where T: ISelectableToken
public T Resolve<T>(JToken token) where T: ISelectableToken
{
T instance = Activator.CreateInstance<T>();
instance.Token = token;
Expand Down
Loading