コード例 #1
0
        public JContainer EvalJContainer(string exp)
        {
            if (string.IsNullOrEmpty(exp))
            {
                return(null);
            }

            var var = EvaluationFunctions.parseLanguageExpressionWithoutUpdate(exp);

            if (!var.IsJsonIdentifierExpression)
            {
                return(null);
            }

            var jsonIdentifierExpression = var as LanguageAST.LanguageExpression.JsonIdentifierExpression;

            if (jsonIdentifierExpression?.Item is LanguageAST.JsonIdentifierExpression.NameExpression nameExpression)
            {
                return(_env.JsonObjects[nameExpression.Item.Name]);
            }
            if (jsonIdentifierExpression?.Item is LanguageAST.JsonIdentifierExpression.IndexNestedNameExpression arrayExpression)
            {
                return(_env.JsonObjects[arrayExpression.Item.ObjectName]);
            }
            return(null);
        }
コード例 #2
0
        public List <string> GetIndexes(string exp)
        {
            var indexMap = new List <string>();

            if (!string.IsNullOrEmpty(exp))
            {
                var var = EvaluationFunctions.parseLanguageExpressionWithoutUpdate(exp);

                if (var.IsJsonIdentifierExpression)
                {
                    if (var is LanguageAST.LanguageExpression.JsonIdentifierExpression jsonIdentifierExpression)
                    {
                        BuildIndexMap(jsonIdentifierExpression.Item, exp, indexMap, null);
                    }
                }
                else
                {
                    if (var.IsRecordSetExpression && var is LanguageAST.LanguageExpression.RecordSetExpression recSetExpression)
                    {
                        GetIndexAddRecordSetIndexes(exp, indexMap, recSetExpression);
                    }
                }
            }
            return(indexMap.Where(s => !s.Contains(@"(*)")).ToList());
        }
コード例 #3
0
        public void AssignEvaluation_LanguageExpressionToJsonExpression_Scalar()
        {
            var parsed = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[@Child]]");
            var exp    = AssignEvaluation.languageExpressionToJsonIdentifier(parsed);

            Assert.IsNotNull(exp);
        }
コード例 #4
0
        public List <string> GetIndexes(string exp)
        {
            IList <string> indexMap = null;

            if (!string.IsNullOrEmpty(exp))
            {
                var parsedExpression = EvaluationFunctions.parseLanguageExpressionWithoutUpdate(exp, ShouldTypeCast.Yes);

                if (parsedExpression.IsJsonIdentifierExpression)
                {
                    if (parsedExpression is LanguageAST.LanguageExpression.JsonIdentifierExpression jsonIdentifierExpression)
                    {
                        indexMap = _buildIndexMap.Build(jsonIdentifierExpression.Item, exp);
                    }
                }
                else
                {
                    if (parsedExpression.IsRecordSetExpression && parsedExpression is LanguageAST.LanguageExpression.RecordSetExpression recSetExpression)
                    {
                        indexMap = GetIndexAddRecordSetIndexes(exp, recSetExpression);
                    }
                }
            }

            if (indexMap is null)
            {
                return(new List <string>());
            }

            return(indexMap.Where(s => !s.Contains(@"(*)")).ToList());
        }
コード例 #5
0
        public void AssignEvaluation_assignGivenAnArrayValueCreatesValidJson_StarIndex_MutateArray_AndAddAProperty()
        {
            var    env = CreateTestEnvWithData();
            JArray x   = new JArray();

            var result       = PublicFunctions.EvalEnvExpression("[[rec(1).a]]", 0, false, env);
            var secondResult = PublicFunctions.EvalEnvExpression("[[rec(2).a]]", 0, false, env);
            var thirdResult  = PublicFunctions.EvalEnvExpression("[[rec(3).a]]", 0, false, env);
            var parsed       = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[@Person.Child().Name]]");
            var parsed2      = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[@Person.Child(*).Name]]");
            var parsed3      = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[@Person.Age]]");
            var val          = (LanguageAST.LanguageExpression.JsonIdentifierExpression)parsed;
            var val2         = (LanguageAST.LanguageExpression.JsonIdentifierExpression)parsed2;
            var val3         = (LanguageAST.LanguageExpression.JsonIdentifierExpression)parsed3;
            // ReSharper disable once RedundantAssignment
            var env2 = AssignEvaluation.assignGivenAValue(env, result, val.Item);

            env2 = AssignEvaluation.assignGivenAValue(env2, secondResult, val.Item);
            env2 = AssignEvaluation.assignGivenAValue(env2, thirdResult, val2.Item);
            env2 = AssignEvaluation.assignGivenAValue(env2, result, val3.Item);
            Assert.IsTrue(env2.JsonObjects.ContainsKey("Person"));
            var obj = env2.JsonObjects["Person"];

            Assert.AreEqual(obj.ToString(), "{\r\n  \"Child\": [\r\n    {\r\n      \"Name\": \"Bob\"\r\n    },\r\n    {\r\n      \"Name\": \"Bob\"\r\n    }\r\n  ],\r\n  \"Age\": \"2\"\r\n}");
        }
コード例 #6
0
        public List <string> GetIndexes(string exp)
        {
            var indexMap = new List <string>();

            if (!string.IsNullOrEmpty(exp))
            {
                var var = EvaluationFunctions.parseLanguageExpressionWithoutUpdate(exp);

                if (var.IsJsonIdentifierExpression)
                {
                    var jsonIdentifierExpression = var as LanguageAST.LanguageExpression.JsonIdentifierExpression;
                    if (jsonIdentifierExpression != null)
                    {
                        BuildIndexMap(jsonIdentifierExpression.Item, exp, indexMap, null);
                    }
                }
                else if (var.IsRecordSetExpression)
                {
                    var recSetExpression = var as LanguageAST.LanguageExpression.RecordSetExpression;
                    if (recSetExpression != null)
                    {
                        var indexes = EvalRecordSetIndexes(@"[[" + recSetExpression.Item.Name + @"(*)]]", 0);
                        foreach (var index in indexes)
                        {
                            indexMap.Add(exp.Replace(@"(*).", $"({index})."));
                        }
                    }
                }
            }
            return(indexMap.Where(s => !s.Contains(@"(*)")).ToList());
        }
コード例 #7
0
        public JContainer EvalJContainer(string exp)
        {
            if (string.IsNullOrEmpty(exp))
            {
                return(null);
            }

            var parsedExpression = EvaluationFunctions.parseLanguageExpressionWithoutUpdate(exp, ShouldTypeCast.Yes);

            if (parsedExpression.IsJsonIdentifierExpression && parsedExpression is LanguageAST.LanguageExpression.JsonIdentifierExpression jsonIdentifierExpression)
            {
                if (jsonIdentifierExpression.Item is LanguageAST.JsonIdentifierExpression.NameExpression nameExpression)
                {
                    if (_env.JsonObjects.Count > 0)
                    {
                        return(_env.JsonObjects[nameExpression.Item.Name]);
                    }
                }

                if (jsonIdentifierExpression.Item is LanguageAST.JsonIdentifierExpression.IndexNestedNameExpression arrayExpression)
                {
                    return(_env.JsonObjects[arrayExpression.Item.ObjectName]);
                }
            }

            // BUG: if any other types are added to JsonIdentifierExpression this method will no longer be correct.
            return(null);
        }
コード例 #8
0
        public void Eval_JsonArrayComplex_Atom()
        {
            //------------Setup for test--------------------------
            var env = CreateEnvironmentWithData();
            //------------Execute Test---------------------------
            var res = EvaluationFunctions.evalJson(env, 0, false, EvaluationFunctions.parseLanguageExpressionWithoutUpdate("1"));

            //------------Assert Results-------------------------
            Assert.AreEqual("1", CommonFunctions.evalResultToString(res));
        }
コード例 #9
0
        public void Eval_JsonArrayComplex_ComplexExp()
        {
            //------------Setup for test--------------------------
            var env = CreateEnvironmentWithData();
            //------------Execute Test---------------------------
            var res = EvaluationFunctions.evalJson(env, 0, false, EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[@arrayObj().Name]] [[@arrayObj().Name]]", ShouldTypeCast.Yes));

            //------------Assert Results-------------------------
            Assert.AreEqual("bobe bobe", CommonFunctions.evalResultToString(res));
        }
コード例 #10
0
        public void AssignEvaluation_assignGivenAnArrayValueCreatesValidJson_Invalid()
        {
            var    env = CreateTestEnvWithData();
            JArray x   = new JArray();

            var result = PublicFunctions.EvalEnvExpression("[[rec(1).a]]", 0, false, env);
            var parsed = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[@Person.Child(1).Name]]");
            var val    = (LanguageAST.LanguageExpression.JsonIdentifierExpression)parsed;
            var env2   = AssignEvaluation.assignGivenAValue(env, result, val.Item);

            Assert.IsTrue(env2.JsonObjects.ContainsKey("Person"));
            Assert.AreEqual(env2.JsonObjects["Person"].ToString(), "{\r\n  \"Child\": [\r\n    {\r\n      \"Name\": \"2\"\r\n    }\r\n  ]\r\n}");
        }
コード例 #11
0
        public void BuildJObjectArrayIndexMapHelper_Build_GivenObjectWithChildThatHasListOfStrings_SelectNonExistentItem_ExpectNoIndexMap()
        {
            var env = new ExecutionEnvironment();

            env.AssignJson(new AssignValue("[[@ob]]", "{\"addresses\":[],\"user\":{\"name\":\"bob\", \"friends\":[{\"name\":\"joe\"},{\"name\":\"jef\"}]}}"), 0);

            var test = new BuildJObjectArrayIndexMapHelper(env);

            const string exp = "[[@ob.user.addresses(*).name]]";

            var parsedExpression = EvaluationFunctions.parseLanguageExpressionWithoutUpdate(exp, ShouldTypeCast.Yes) as LanguageAST.LanguageExpression.JsonIdentifierExpression;

            var indexMap = test.Build(parsedExpression.Item, exp);

            Assert.AreEqual(0, indexMap.Count);
        }
コード例 #12
0
        public void AssignEvaluation_assignGivenAnArrayValueCreatesValidJson_LastIndex_MutateArray_differentProperties()
        {
            var env = CreateTestEnvWithData();
            var x   = new JArray();

            var result       = PublicFunctions.EvalEnvExpression("[[rec(1).a]]", 0, false, env);
            var secondResult = PublicFunctions.EvalEnvExpression("[[rec(2).a]]", 0, false, env);
            var parsed       = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[@Person.Child().Name]]");
            var parsed2      = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[@Person.Child().Age]]");
            var val          = (LanguageAST.LanguageExpression.JsonIdentifierExpression)parsed;
            var val2         = (LanguageAST.LanguageExpression.JsonIdentifierExpression)parsed2;

            var env2 = AssignEvaluation.assignGivenAValue(env, result, val.Item);

            env2 = AssignEvaluation.assignGivenAValue(env2, secondResult, val2.Item);
            Assert.IsTrue(env2.JsonObjects.ContainsKey("Person"));
            Assert.AreEqual("{\r\n  \"Child\": [\r\n    {\r\n      \"Name\": \"2\",\r\n      \"Age\": \"4\"\r\n    }\r\n  ]\r\n}", env2.JsonObjects["Person"].ToString());
        }
コード例 #13
0
        public void GivenContainer_ExecutionEnvironmentBuildIndexMap_ShouldBuildMapForChildObj()
        {
            Assert.IsNotNull(_environment);
            _environment.AssignJson(new AssignValue(ChildNameExpression, "Sanele"), 0);
            var privateObj = new PrivateObject(_environment);
            var var        = EvaluationFunctions.parseLanguageExpressionWithoutUpdate(ChildNameExpression);
            var jsonIdentifierExpression = var as LanguageAST.LanguageExpression.JsonIdentifierExpression;
            var obj = new JArray(ChildNameExpression);

            if (jsonIdentifierExpression == null)
            {
                return;
            }
            var mapItems = new List <string>();

            object[] args = { jsonIdentifierExpression.Item, "", mapItems, obj };
            privateObj.Invoke("BuildIndexMap", args);
        }
コード例 #14
0
        public void BuildJObjectArrayIndexMapHelper_Build_GivenObjectWithListOfStrings_ExpectIndexForEachArrayItem()
        {
            var env = new ExecutionEnvironment();

            env.AssignJson(new AssignValue("[[@ob]]", "{\"addresses\":[],\"users\":[{\"name\":\"bob\"},{\"name\":\"joe\"},{\"name\":\"jef\"}],\"tags\":[]}"), 0);

            var          test = new BuildJObjectArrayIndexMapHelper(env);
            const string exp  = "[[@ob.users(*).name]]";

            var parsedExpression = EvaluationFunctions.parseLanguageExpressionWithoutUpdate(exp, ShouldTypeCast.Yes) as LanguageAST.LanguageExpression.JsonIdentifierExpression;

            var indexMap = test.Build(parsedExpression.Item, exp);

            Assert.AreEqual(3, indexMap.Count);
            Assert.AreEqual("[[@ob.users(1).name]]", indexMap[0]);
            Assert.AreEqual("[[@ob.users(2).name]]", indexMap[1]);
            Assert.AreEqual("[[@ob.users(3).name]]", indexMap[2]);
        }
コード例 #15
0
        public void AssignEvaluation_LanguageExpressionToJsonExpression()
        {
            var parsed = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[Child(1).Name]]");
            var exp    = AssignEvaluation.languageExpressionToJsonIdentifier(parsed);

            Assert.IsTrue(exp.IsIndexNestedNameExpression);
            var exp2  = (exp as LanguageAST.JsonIdentifierExpression.IndexNestedNameExpression).Item;
            var index = exp2.Index;

            Assert.IsTrue(index.IsIntIndex);
            var bob = (index as LanguageAST.Index.IntIndex).Item;

            Assert.AreEqual(1, bob);
            Assert.AreEqual("Child", exp2.ObjectName);
            Assert.IsTrue(exp2.Next.IsNameExpression);
            var x2 = (exp2.Next as LanguageAST.JsonIdentifierExpression.NameExpression).Item;

            Assert.AreEqual(x2.Name, "Name");
        }
コード例 #16
0
        public void Eval_RecSet_AsStringind()
        {
            //------------Setup for test--------------------------
            var env = CreateEnvironmentWithData();



            //------------Execute Test---------------------------
            var res = EvaluationFunctions.evalRecordSetAsString(env, ((LanguageAST.LanguageExpression.RecordSetExpression)EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[Rec(1).a]]")).Item);

            //------------Assert Results-------------------------
            Assert.AreEqual(CommonFunctions.atomtoString(res), "1");
        }
コード例 #17
0
 public void AssignEvaluation_LanguageExpressionToJsonExpression_Complex()
 {
     var parsed = EvaluationFunctions.parseLanguageExpressionWithoutUpdate("[[[[bob]]]]");
     var exp    = AssignEvaluation.languageExpressionToJsonIdentifier(parsed);
 }
コード例 #18
0
 public static LanguageAST.LanguageExpression ParseLanguageExpressionWithoutUpdate(string expression)
 {
     return(EvaluationFunctions.parseLanguageExpressionWithoutUpdate(expression));
 }
コード例 #19
0
 public static LanguageAST.LanguageExpression ParseLanguageExpressionWithoutUpdate(string expression) => EvaluationFunctions.parseLanguageExpressionWithoutUpdate(expression, ShouldTypeCast.Yes);