Ejemplo n.º 1
0
        /// <summary>
        /// execute indicator
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        public bool?Indicate(ArgumentProvider argument)
        {
            var actual   = argument.Format(Actual);
            var expected = Expected.Select(x => argument.Format(x));

            var match = expected.Any(x => string.Equals(actual, x, StringComparison.CurrentCultureIgnoreCase));

            if (Not)
            {
                match = !match;
            }

            if (match)
            {
                if (IsSuccess)
                {
                    return(true);
                }
                else if (IsFail)
                {
                    return(false);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <PeekListActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list    = JsonHelper.GetArray(listJson, setting.Source);
                var results = new List <JToken>();
                foreach (var listItem in list)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("sourceItem");
                    tmpArgument.PutPrivate("sourceItem", listItem.ToString());
                    if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument) ?? false) == true)
                    {
                        string targetValue = tmpArgument.Format(setting.Target, true);
                        if (setting.AsString)
                        {
                            results.Add(targetValue);
                        }
                        else
                        {
                            var valueObj = JsonHelper.TryGetObject(targetValue, setting.Target);
                            if (valueObj == null)
                            {
                                results.Add(targetValue);
                            }
                            else
                            {
                                targetValue = JsonConvert.SerializeObject(valueObj);
                                results.Add(valueObj);
                            }
                        }
                    }
                }

                var resultStr = JsonConvert.SerializeObject(results);
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", resultStr }
                    },
                    Data = resultStr
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }
Ejemplo n.º 3
0
        public void TestEventBody()
        {
            WorkingArguments workingArguments = new WorkingArguments();
            ArgumentProvider argProvider      = new ArgumentProvider(workingArguments);

            argProvider.PutPrivate("lastCheckResult", "{\"data\":null,\"success\":false,\"code\":null,\"message\":\"Cannot get any products by style number: [20200122].\",\"exception\":null}");
            argProvider.PutPrivate("now", "2019/12/11T09:00:00.000Z");
            argProvider.PutPrivate("input.merchantOrderId", "merchantOrderId");
            var targetStr = "{\"message\":\"{\\\"name\\\":\\\"InvalidOrder\\\",\\\"time\\\":\\\"2019/12/11T09:00:00.000Z\\\",\\\"data\\\":{\\\"orderId\\\":\\\"merchantOrderId\\\",\\\"messages\\\":{\\\"data\\\":null,\\\"success\\\":false,\\\"code\\\":null,\\\"message\\\":\\\"Cannot get any products by style number: [20200122].\\\",\\\"exception\\\":null}}}\"}";

            var setProcessor = new SetValueProcessor();
            var setSetting1  = new SetValueActionSetting
            {
                Set = new Dictionary <string, string> {
                    { "orderId", "{{input.merchantOrderId}}" },
                    { "messages", "{{lastCheckResult}}" }
                }
            };
            var res1 = setProcessor.Execute(JsonConvert.SerializeObject(setSetting1), argProvider).Result;

            argProvider.PutPrivate("event_msg_data", res1.Data);

            var setSetting2 = new SetValueActionSetting
            {
                Set = new Dictionary <string, string> {
                    { "name", "InvalidOrder" },
                    { "time", "{{now}}" },
                    { "data", "{{event_msg_data}}" }
                }
            };
            var res2 = setProcessor.Execute(JsonConvert.SerializeObject(setSetting2), argProvider).Result;

            argProvider.PutPrivate("event_msg", res2.Data);

            var setSetting3 = new SetValueActionSetting
            {
                Set = new Dictionary <string, string> {
                    { "message", "{{event_msg}}" }
                },
                AsString = true
            };
            var res3 = setProcessor.Execute(JsonConvert.SerializeObject(setSetting3), argProvider).Result;

            argProvider.PutPrivate("event_body", res3.Data);


            var resultStr = argProvider.Format("{{event_body}}");

            Assert.AreEqual(targetStr, resultStr);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <MapListActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list    = JsonHelper.GetArray(listJson, setting.Source);
                var results = new List <Dictionary <string, string> >();
                foreach (var listItem in list)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("mapItem");
                    tmpArgument.PutPrivate("mapItem", listItem.ToString());
                    if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument) ?? false) == true)
                    {
                        Dictionary <string, string> result = new Dictionary <string, string>();
                        foreach (var rule in setting.Output)
                        {
                            result.Add(rule.Key, tmpArgument.Format(rule.Value, true));
                        }
                        results.Add(result);
                    }
                }

                var resultStr = JsonConvert.SerializeObject(results);
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", resultStr }
                    },
                    Data = resultStr
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <BooleanProcessActionSetting>(actionSetting);

            try
            {
                bool arg = bool.Parse(argument.Format(setting.Arg));
                bool res = false;
                switch (setting.Action.ToLower())
                {
                case "not":
                case "!":
                    res = !arg;
                    break;

                default:
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Fail = true,
                        Message = $"Action ({setting.Action}) is not known"
                    }));
                }
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", res.ToString() }
                    },
                    Data = res.ToString()
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message
                }));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <GroupListActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list   = JsonHelper.GetArray(listJson, setting.Source);
                var result = new List <Dictionary <string, string> >();
                Dictionary <string, JArray> groupData = new Dictionary <string, JArray>();
                foreach (var listItem in list)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("loopItem");
                    tmpArgument.PutPrivate("loopItem", listItem.ToString());
                    var key = tmpArgument.Format(setting.Key, true);
                    if (groupData.ContainsKey(key))
                    {
                        groupData[key].Add(listItem);
                    }
                    else
                    {
                        groupData[key] = new JArray {
                            listItem
                        };
                    }
                }
                foreach (var kvp in groupData)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("groupKey");
                    tmpArgument.ClearKey("groupItems");
                    tmpArgument.PutPrivate("groupKey", kvp.Key.ToString());
                    tmpArgument.PutPrivate("groupItems", kvp.Value.ToString());
                    Dictionary <string, string> results = new Dictionary <string, string>();
                    foreach (var rule in setting.Output)
                    {
                        results.Add(rule.Key, tmpArgument.Format(rule.Value, true));
                    }
                    result.Add(results);
                }

                var resultStr = JsonConvert.SerializeObject(result);
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", resultStr }
                    },
                    Data = resultStr
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <MathProcessActionSetting>(actionSetting);

            try
            {
                decimal arg1 = decimal.Parse(argument.Format(setting.Arg1));
                decimal arg2 = decimal.Parse(argument.Format(setting.Arg2));
                decimal res  = 0;
                switch (setting.Action.ToLower())
                {
                case "+":
                    res = arg1 + arg2;
                    break;

                case "-":
                    res = arg1 - arg2;
                    break;

                case "*":
                    res = arg1 * arg2;
                    break;

                case "/":
                    if (arg2 == 0)
                    {
                        return(Task.FromResult(new ActionExecuteResult
                        {
                            Fail = true,
                            Message = $"You cannot divide by zero!"
                        }));
                    }
                    res = arg1 / arg2;
                    break;

                case "%":
                    if (arg2 == 0)
                    {
                        return(Task.FromResult(new ActionExecuteResult
                        {
                            Fail = true,
                            Message = $"You cannot divide by zero!"
                        }));
                    }
                    res = arg1 % arg2;
                    break;

                case "abs":
                    res = Math.Abs(arg1);
                    break;

                case "ceiling":
                    res = Math.Ceiling(arg1);
                    break;

                case "floor":
                    res = Math.Floor(arg1);
                    break;

                default:
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Fail = true,
                        Message = $"Action ({setting.Action}) is not known"
                    }));
                }
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", res.ToString() }
                    },
                    Data = res.ToString()
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message
                }));
            }
        }
Ejemplo n.º 8
0
        private async Task <OperationResult <string> > CallHttp(ArgumentProvider argument)
        {
            var url     = argument.Format(Settings.Url);
            var call    = HttpProvider.CallUrl(url);
            var headers = new List <KeyValuePair <string, string> >();

            if (Settings.Headers?.Any() == true)
            {
                foreach (var kvp in Settings.Headers)
                {
                    var headerValue = argument.Format(kvp.Value);
                    headers.Add(new KeyValuePair <string, string>(kvp.Key, headerValue));
                    call.WithHeader(kvp.Key, headerValue);
                }
            }
            call.AcceptStatusCodes(Settings.SuccessStatusCodes.Select(x => (HttpStatusCode)x).ToArray());
            string body = string.Empty;

            if (!string.IsNullOrEmpty(Settings.Body))
            {
                body = argument.Format(Settings.Body);
                call.WithJsonBody(body.ToJsonObject());
            }
            if (Settings.FormData?.Any() == true)
            {
                var formData = new List <KeyValuePair <string, string> >();
                foreach (var kvp in Settings.FormData)
                {
                    var formValue = argument.Format(kvp.Value);
                    formData.Add(new KeyValuePair <string, string>(kvp.Key, formValue));
                    call.WithHttpParameter(kvp.Key, formValue);
                }
                body = JsonConvert.SerializeObject(formData);
            }
            var executionData = new
            {
                Settings.Method,
                url,
                headers,
                body
            };

            argument.PutPrivate(nameof(executionData), JsonConvert.SerializeObject(executionData));

            if ("get".Equals(Settings.Method, StringComparison.CurrentCultureIgnoreCase))
            {
                return(await call.GetAsync());
            }
            else if ("post".Equals(Settings.Method, StringComparison.CurrentCultureIgnoreCase))
            {
                return(await call.PostAsync <string>());
            }
            else if ("put".Equals(Settings.Method, StringComparison.CurrentCultureIgnoreCase))
            {
                return(await call.PutAsync <string>());
            }
            else if ("delete".Equals(Settings.Method, StringComparison.CurrentCultureIgnoreCase))
            {
                return(await call.DeleteAsync <string>());
            }

            return(new OperationResult <string>
            {
                Success = false,
                Code = Messages.UnsupportedHttpMethod.Code,
                Message = Messages.UnsupportedHttpMethod.Message
            });
        }
Ejemplo n.º 9
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <CompareNumberActionSetting>(actionSetting);

            try
            {
                var arg1 = argument.Format(setting.Arg1);
                var arg2 = argument.Format(setting.Arg2);
                if (decimal.TryParse(arg1, out decimal d1) &&
                    decimal.TryParse(arg2, out decimal d2))
                {
                    bool result = false;
                    switch (setting.Comparer.ToLower())
                    {
                    case "greater":
                    case ">":
                        result = d1 > d2;
                        break;

                    case "greaterorequal":
                    case ">=":
                        result = d1 >= d2;
                        break;

                    case "equal":
                    case "=":
                        result = d1 == d2;
                        break;

                    case "less":
                    case "<":
                        result = d1 < d2;
                        break;

                    case "lessorequal":
                    case "<=":
                        result = d1 <= d2;
                        break;

                    case "notequal":
                    case "<>":
                    case "!=":
                        result = d1 != d2;
                        break;

                    default:
                        return(Task.FromResult(new ActionExecuteResult
                        {
                            Fail = true,
                            Message = $"Comparer ({setting.Comparer}) is not known"
                        }));
                    }
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Success = true,
                        Output = new Dictionary <string, string> {
                            { "result", result.ToString() }
                        },
                        Data = result.ToString()
                    }));
                }
                else
                {
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Fail = true,
                        Message = $"Cannot compare {arg1} and {arg2}"
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message
                }));
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <SetValueActionSetting>(actionSetting);
            var results = new Dictionary <string, JToken>();

            foreach (var kvp in setting.Set)
            {
                var             target     = argument.Format(kvp.Key, true);
                ExpressionModel expression = new ExpressionModel(string.Empty, target);
                var             currentKey = expression.CurrentKey;
                if (expression.IsArray)
                {
                    currentKey = expression.ArrayKey;
                }
                var obj = argument.Get(currentKey, true);
                if (obj != null && !(JsonHelper.IsObject(obj) || JsonHelper.IsArray(obj)))
                {
                    obj = null;
                }
                if (obj == null)
                {
                    string targetValue = argument.Format(kvp.Value, true);
                    if (setting.AsString)
                    {
                        results.Add(target, targetValue);
                    }
                    else
                    {
                        var valueObj = JsonHelper.TryGetObject(targetValue, kvp.Value);
                        var valueArr = JsonHelper.TryGetArray(targetValue, kvp.Value);
                        if (valueObj != null)
                        {
                            targetValue = JsonConvert.SerializeObject(valueObj);
                            results.Add(target, valueObj);
                        }
                        else if (valueArr != null)
                        {
                            targetValue = JsonConvert.SerializeObject(valueArr);
                            results.Add(target, valueArr);
                        }
                        else
                        {
                            results.Add(target, targetValue);
                        }
                    }
                    argument.PutPrivate(target, targetValue);
                }
                else
                {
                    JToken token = null;
                    JToken res   = null;
                    if (expression.IsArray)
                    {
                        JArray arr = JsonHelper.GetArray(obj, currentKey);
                        res = JsonHelper.FindToken(arr[expression.Index], expression.SubExpression.Key, currentKey);
                        if (res == null)
                        {
                            arr = FillArrayToCount(arr, expression.Index ?? 0);
                            if (arr[expression.Index] == null)
                            {
                                arr[expression.Index] = CreateObject(new JObject(), expression.SubExpression);
                                res = JsonHelper.FindToken(arr[expression.Index], expression.SubExpression.Key, currentKey);
                            }
                        }
                        token = arr;
                    }
                    else
                    {
                        JObject o = JsonHelper.GetObject(obj, currentKey);
                        res = JsonHelper.FindToken(o, expression.SubExpression.Key, currentKey);
                        if (res == null)
                        {
                            o   = CreateObject(o, expression.SubExpression);
                            res = JsonHelper.FindToken(o, expression.SubExpression.Key, currentKey);
                        }
                        token = o;
                    }

                    if (res?.Parent != null)
                    {
                        var prop     = (JProperty)res.Parent;
                        var newValue = argument.Format(kvp.Value, true);
                        if (setting.AsString)
                        {
                            prop.Value = newValue;
                        }
                        else if (JsonHelper.IsArray(newValue))
                        {
                            JArray newArr = JsonHelper.GetArray(newValue, kvp.Value);
                            prop.Value = newArr;
                        }
                        else if (JsonHelper.IsObject(newValue))
                        {
                            JObject newObj = JsonHelper.GetObject(newValue, kvp.Value);
                            prop.Value = newObj;
                        }
                        else
                        {
                            prop.Value = newValue;
                        }
                    }
                    argument.PutPrivate(currentKey, JsonConvert.SerializeObject(token));
                    results.Add(currentKey, token);
                }
            }

            return(Task.FromResult(new ActionExecuteResult
            {
                Success = true,
                Data = JsonConvert.SerializeObject(results)
            }));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <CompareActionSetting>(actionSetting);

            try
            {
                var arg1 = argument.Format(setting.Arg1);
                var arg2 = argument.Format(setting.Arg2);
                var args = setting.Args?.Select(x => argument.Format(x));
                if (setting.IgnoreCase)
                {
                    arg1 = arg1?.ToLower();
                    arg2 = arg2?.ToLower();
                    args = args?.Select(x => x?.ToLower());
                }
                bool result = false;
                switch (setting.Comparer.ToLower())
                {
                case "equal":
                case "=":
                    result = arg1 == arg2;
                    break;

                case "notequal":
                case "<>":
                case "!=":
                    result = arg1 != arg2;
                    break;

                case "in":
                    result = args?.Contains(arg1) ?? false;
                    break;

                case "notin":
                    result = !(args?.Contains(arg1) ?? false);
                    break;

                default:
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Fail = true,
                        Message = $"Comparer ({setting.Comparer}) is not known"
                    }));
                }
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", result.ToString() }
                    },
                    Data = result.ToString()
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message
                }));
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <CompareListActionSetting>(actionSetting);

            try
            {
                var    listStr       = argument.Format(setting.Source);
                var    list          = JsonHelper.GetArray(listStr, setting.Source);
                string methodSetting = JsonConvert.SerializeObject(setting.ActionSetting);

                bool result = false;
                switch (setting.Comparer)
                {
                case "any":
                    result = list.Any(x =>
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", x.ToString());

                        return(ExecuteAction(setting.Action, methodSetting, tmpArgument).Result?.Success == true);
                    });
                    break;

                case "all":
                    result = list.All(x =>
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", x.ToString());

                        return(ExecuteAction(setting.Action, methodSetting, tmpArgument).Result?.Success == true);
                    });
                    break;

                case "notany":
                    result = !list.Any(x =>
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", x.ToString());

                        return(ExecuteAction(setting.Action, methodSetting, tmpArgument).Result?.Success == true);
                    });
                    break;

                case "notall":
                    result = !list.All(x =>
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", x.ToString());

                        return(ExecuteAction(setting.Action, methodSetting, tmpArgument).Result?.Success == true);
                    });
                    break;

                default:
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Fail = true,
                        Message = $"Comparer ({setting.Comparer}) is not known"
                    }));
                }
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", result.ToString() }
                    },
                    Data = result.ToString()
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message
                }));
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <ExpandActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list   = JsonHelper.GetArray(listJson, setting.Source);;
                var result = new List <Dictionary <string, JToken> >();
                foreach (var listItem in list)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("sourceItem");
                    tmpArgument.PutPrivate("sourceItem", listItem.ToString());
                    var target = setting.Target;
                    if (target.StartsWith("{{") && target.EndsWith("}}"))
                    {
                        target = target.Substring(2, target.Length - 4);
                    }

                    var targetJson = tmpArgument.Format($"{{{{sourceItem.{target}}}}}", true);
                    var targetList = JsonHelper.GetArray(targetJson, target);
                    foreach (var targetItem in targetList)
                    {
                        var tmpArgument2 = new ArgumentProvider(tmpArgument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("targetItem");
                        tmpArgument2.PutPrivate("targetItem", targetItem.ToString());

                        if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument2) ?? false) == true)
                        {
                            Dictionary <string, JToken> results = new Dictionary <string, JToken>();
                            foreach (var rule in setting.Output)
                            {
                                string targetValue = tmpArgument2.Format(rule.Value, true);
                                if (JsonHelper.IsObject(targetValue))
                                {
                                    results.Add(rule.Key, JsonHelper.TryGetObject(targetValue, rule.Value));
                                }
                                else if (JsonHelper.IsArray(targetValue))
                                {
                                    results.Add(rule.Key, JsonHelper.TryGetArray(targetValue, rule.Value));
                                }
                                else
                                {
                                    results.Add(rule.Key, targetValue);
                                }
                            }
                            result.Add(results);
                        }
                    }
                }

                var resultStr = JsonConvert.SerializeObject(result);
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", resultStr }
                    },
                    Data = resultStr
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <AggregateActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list = JsonHelper.GetArray(listJson, setting.Source);
                if (string.Equals("sum", setting?.Action, StringComparison.CurrentCultureIgnoreCase))
                {
                    // sum
                    var sum = 0;
                    foreach (var listItem in list)
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", listItem.ToString());
                        if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument) ?? false) == true)
                        {
                            var targetJson = tmpArgument.Format(setting.Target, true);
                            sum += targetJson.ToNullableInt() ?? 0;
                        }
                    }
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Success = true,
                        Output = new Dictionary <string, string> {
                            { "result", $"{sum}" }
                        },
                        Data = $"{sum}"
                    }));
                }
                else if (string.Equals("count", setting?.Action, StringComparison.CurrentCultureIgnoreCase))
                {
                    // count
                    var count = 0;
                    foreach (var listItem in list)
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", listItem.ToString());
                        if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument) ?? false) == true)
                        {
                            count++;
                        }
                    }
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Success = true,
                        Output = new Dictionary <string, string> {
                            { "result", $"{count}" }
                        },
                        Data = $"{count}"
                    }));
                }

                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = false,
                    Output = new Dictionary <string, string> {
                        { "result", "Unknow action" }
                    },
                    Data = "Unknow action"
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }