示例#1
0
        public void CanInterpolate()
        {
            Assert.Equal("hello world", InterpolationHelper.InterpolateString("{{Data1 +\" \" + Data2}}", new Dictionary <string, object>()
            {
                { "Data1", "hello" },
                { "Data2", "world" }
            }));

            Assert.Equal("2", InterpolationHelper.InterpolateString("{{4 - Data1 - Data2 }}", new Dictionary <string, object>()
            {
                { "Data1", 1 },
                { "Data2", 1 }
            }));

            Assert.Equal("3", InterpolationHelper.InterpolateString("{{Data1.Count()}}", new Dictionary <string, object>()
            {
                { "Data1", new List <int>()
                  {
                      1, 2, 3
                  } },
                { "Data2", 1 }
            }));

            Assert.Equal("1", InterpolationHelper.InterpolateString("{{Data1[0]}}", new Dictionary <string, object>()
            {
                { "Data1", new List <int>()
                  {
                      1, 2, 3
                  } },
                { "Data2", 1 }
            }));
            //it refers to itself.
            Assert.Equal("6", InterpolationHelper.InterpolateString("{{Data1.Sum(it)}}", new Dictionary <string, object>()
            {
                { "Data1", new List <int>()
                  {
                      1, 2, 3
                  } },
                { "Data2", 1 }
            }));

            Assert.Equal("5", InterpolationHelper.InterpolateString("{{Data1.Sum(Item1)}}",
                                                                    new Dictionary <string, object>()
            {
                { "Data1", new List <(int Value, int no)>()
                  {
                      (5, 0)
                  } }
            }));
        public async Task <ExternalServiceData> ExecuteWitchcraftToComputeExternalService()
        {
            var data   = GetData();
            var recipe = await _recipeManager.GetRecipe(data.RecipeId);

            if (recipe == null)
            {
                return(null);
            }

            var actionData = new Dictionary <string, (object data, string json)>();

            if (!string.IsNullOrEmpty(data.RecipeActionId))
            {
                var recipeAction = recipe.RecipeActions.SingleOrDefault(action =>
                                                                        action.Id.Equals(data.RecipeActionId, StringComparison.InvariantCultureIgnoreCase));
                if (recipeAction == null)
                {
                    return(null);
                }

                var dispatchResult = await _actionDispatcher.Dispatch(actionData,
                                                                      recipeAction);

                actionData.Add("PreviousAction", (dispatchResult.First().Data, dispatchResult.First().DataJson));
                actionData.Add("ActionData0", (dispatchResult.First().Data, dispatchResult.First().DataJson));
            }

            if (!string.IsNullOrEmpty(data.RecipeActionGroupId))
            {
                var recipeActionGroup = recipe.RecipeActionGroups.SingleOrDefault(action =>
                                                                                  action.Id.Equals(data.RecipeActionGroupId, StringComparison.InvariantCultureIgnoreCase));
                if (recipeActionGroup == null)
                {
                    return(null);
                }

                await _actionDispatcher.Dispatch(actionData,
                                                 recipeActionGroup);
            }

            var value = InterpolationHelper.InterpolateString(data.Value,
                                                              actionData.ToDictionary(pair => pair.Key, pair => pair.Value.data));

            return(await _externalServiceManager.GetExternalServiceData(value, _data.UserId));
        }