private static object ParseFunction(string functionString, string inputJson, JArray array, JToken currentArrayElement) { try { object output = null; string functionName, argumentString; if (!TryParseFunctionNameAndArguments(functionString, out functionName, out argumentString)) { return(functionName); } string[] arguments = GetArguments(argumentString); object[] parameters = new object[arguments.Length + 1]; int i = 0; if (arguments != null && arguments.Length > 0) { foreach (string argument in arguments) { string trimmedArgument = argument; if (argument.Contains("#")) { trimmedArgument = argument.Trim(); } if (trimmedArgument.StartsWith("#")) { parameters[i] = ParseFunction(trimmedArgument, inputJson, array, currentArrayElement); } else { parameters[i] = trimmedArgument; } i++; } } parameters[i] = inputJson; if (functionName == "currentvalue" || functionName == "currentindex" || functionName == "lastindex" || functionName == "lastvalue") { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement }); } else if (functionName == "currentvalueatpath" || functionName == "lastvalueatpath") { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, arguments[0] }); } else if (functionName == "customfunction") { output = CallCustomFunction(parameters); } else if (JUSTContext.IsRegisteredCustomFunction(functionName)) { var methodInfo = JUSTContext.GetCustomMethod(functionName); output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true); } else if (Regex.IsMatch(functionName, ReflectionHelper.EXTERNAL_ASSEMBLY_REGEX)) { output = ReflectionHelper.CallExternalAssembly(functionName, parameters); } 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; output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, oParams); } else { if (currentArrayElement != null && functionName != "valueof") { parameters[i] = JsonConvert.SerializeObject(currentArrayElement); } output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, parameters); } return(output); } catch (Exception ex) { throw new Exception("Error while calling function : " + functionString + " - " + ex.Message, ex); } }
private static object ParseFunction(string functionString, string inputJson, JArray array, JToken currentArrayElement, JUSTContext localContext) { try { object output = null; string functionName, argumentString; if (!ExpressionHelper.TryParseFunctionNameAndArguments(functionString, out functionName, out argumentString)) { return(functionName); } string[] arguments = ExpressionHelper.GetArguments(argumentString); var listParameters = new List <object>(); if (functionName == "ifcondition") { var condition = ParseArgument(inputJson, array, currentArrayElement, arguments[0], localContext); var value = ParseArgument(inputJson, array, currentArrayElement, arguments[1], localContext); var index = condition.ToString().ToLower() == value.ToString().ToLower() ? 2 : 3; output = ParseArgument(inputJson, array, currentArrayElement, arguments[index], localContext); } else { int i = 0; for (; i < (arguments?.Length ?? 0); i++) { listParameters.Add(ParseArgument(inputJson, array, currentArrayElement, arguments[i], localContext)); } listParameters.Add(localContext ?? GlobalContext); var parameters = listParameters.ToArray(); if (functionName == "currentvalue" || functionName == "currentindex" || functionName == "lastindex" || functionName == "lastvalue") { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement }, true, localContext ?? GlobalContext); } else if (functionName == "currentvalueatpath" || functionName == "lastvalueatpath") { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, arguments[0] }, true, localContext ?? GlobalContext); } else if (functionName == "customfunction") { output = CallCustomFunction(parameters, localContext); } else if (localContext?.IsRegisteredCustomFunction(functionName) ?? false) { var methodInfo = localContext.GetCustomMethod(functionName); output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext); } else if (GlobalContext.IsRegisteredCustomFunction(functionName)) { var methodInfo = GlobalContext.GetCustomMethod(functionName); output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext); } else if (Regex.IsMatch(functionName, ReflectionHelper.EXTERNAL_ASSEMBLY_REGEX)) { output = ReflectionHelper.CallExternalAssembly(functionName, parameters, localContext ?? GlobalContext); } 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; output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, oParams, true, localContext ?? GlobalContext); } else { var input = ((JUSTContext)parameters.Last()).Input; if (currentArrayElement != null && functionName != "valueof") { ((JUSTContext)parameters.Last()).Input = JsonConvert.SerializeObject(currentArrayElement); } output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, parameters, true, localContext ?? GlobalContext); ((JUSTContext)parameters.Last()).Input = input; } } return(output); } catch (Exception ex) { throw new Exception("Error while calling function : " + functionString + " - " + ex.Message, ex); } }
private static object ParseFunction(string functionString, string inputJson, JArray array, JToken currentArrayElement, JUSTContext localContext) { try { object output; string functionName, argumentString; if (!ExpressionHelper.TryParseFunctionNameAndArguments(functionString, out functionName, out argumentString)) { return(functionName); } string[] arguments = ExpressionHelper.SplitArguments(argumentString); if (functionName == "ifcondition") { var condition = ParseArgument(inputJson, array, currentArrayElement, arguments[0], localContext); var value = ParseArgument(inputJson, array, currentArrayElement, arguments[1], localContext); var index = condition.ToString().ToLower() == value.ToString().ToLower() ? 2 : 3; output = ParseArgument(inputJson, array, currentArrayElement, arguments[index], localContext); } else { var listParameters = new List <object>(); int i = 0; for (; i < (arguments?.Length ?? 0); i++) { listParameters.Add(ParseArgument(inputJson, array, currentArrayElement, arguments[i], localContext)); } listParameters.Add(localContext ?? GlobalContext); var parameters = listParameters.ToArray(); if (new[] { "currentvalue", "currentindex", "lastindex", "lastvalue" }.Contains(functionName)) { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement }, true, localContext ?? GlobalContext); } else if (new[] { "currentvalueatpath", "lastvalueatpath" }.Contains(functionName)) { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, parameters[0] }, true, localContext ?? GlobalContext); } else if (functionName == "currentproperty") { output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, localContext ?? GlobalContext }, false, localContext ?? GlobalContext); } else if (functionName == "customfunction") { output = CallCustomFunction(parameters, localContext); } else if (localContext?.IsRegisteredCustomFunction(functionName) ?? false) { var methodInfo = localContext.GetCustomMethod(functionName); output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext); } else if (GlobalContext.IsRegisteredCustomFunction(functionName)) { var methodInfo = GlobalContext.GetCustomMethod(functionName); output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext); } else if (ReflectionHelper.IsExternalFunction(functionName)) { output = ReflectionHelper.CallExternalAssembly(functionName, parameters, localContext ?? GlobalContext); } else if (new[] { "xconcat", "xadd", "mathequals", "mathgreaterthan", "mathlessthan", "mathgreaterthanorequalto", "mathlessthanorequalto", "stringcontains", "stringequals" }.Contains(functionName)) { object[] oParams = new object[1]; oParams[0] = parameters; output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, oParams, true, localContext ?? GlobalContext); } else if (functionName == "applyover") { var contextInput = GetInputToken(localContext); var input = JToken.Parse(Transform(parameters[0].ToString(), contextInput.ToString(), localContext)); (localContext ?? GlobalContext).Input = input; output = ParseFunction(parameters[1].ToString().Trim(' ', '\''), inputJson, array, currentArrayElement, localContext ?? GlobalContext); (localContext ?? GlobalContext).Input = contextInput; } else { var input = ((JUSTContext)parameters.Last()).Input; if (currentArrayElement != null && functionName != "valueof") { ((JUSTContext)parameters.Last()).Input = currentArrayElement; } output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, parameters, true, localContext ?? GlobalContext); ((JUSTContext)parameters.Last()).Input = input; } } return(output); } catch (Exception ex) { //TODO Exception with full function string and specific function string (first and last level of recursion) throw new Exception("Error while calling function : " + functionString + " - " + ex.Message, ex); } }