public void AnswerQuestion()
        {
            var n1 = new Node <int>(1);
            var n2 = new Node <int>(2);
            var n3 = new Node <int>(3);
            var n4 = new Node <int>(4);
            var n5 = new Node <int>(5);
            var n6 = new Node <int>(6);
            var n7 = new Node <int>(7);
            var n8 = new Node <int>(8);

            n2.AddChild(n1);
            n2.AddChild(n3);
            n4.AddChild(n2);
            n5.AddChild(n4);
            n5.AddChild(n6);
            n7.AddChild(n8);
            n5.AddChild(n7);

            var tree = new GenericTree <int>();

            tree.Root = n5;

            tree.PrintByLevel();
        }
        public GenericTree ConvertToGenericTree(TextTree tree)
        {
            var gTree = new GenericTree(tree.tid);

            gTree.root = ConvertToGenericNode(tree, 0);
            return(gTree);
        }
예제 #3
0
        public void testException(string arg)
        {
            GenericTree input1 = new GenericTree();

            input1.setName("thing");
            input1.setParamType(TypeEnum.PRIMITIVE_TYPE);
            input1.setThrfitType("STRING");

            List <GenericTree> inputGenericTrees = new List <GenericTree>
            {
                input1
            };

            // 参数值
            List <Object> inputVals = new List <object>();

            inputVals.Add(arg);

            //出参
            GenericTree output = null;

            string method = SERVICE_NAME + "testException";

            GenericNode genericNode = new GenericNode();

            genericNode.setInputs(inputGenericTrees);
            genericNode.setMethodName(method);
            genericNode.setValues(inputVals);
            genericNode.setOutput(output);


            object obj = genericAnalyser.syncProcess(protocol, genericNode);

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }
예제 #4
0
        private static GenericTree toGenericTree(JObject input)
        {
            tidx += 1;

            var tree = new GenericTree(
                $"INPUT-TREE-#{tidx}"
                );

            tree.root = toGenericNode(input);

            return(tree);
        }
        public void secondtestString()
        {
            //入参
            GenericTree input1 = new GenericTree();

            input1.setName("thing");
            input1.setParamType(TypeEnum.PRIMITIVE_TYPE);
            input1.setThrfitType("STRING");

            List <GenericTree> inputGenericTrees = new List <GenericTree>
            {
                input1
            };

            // 参数值
            List <Object> inputVals = new List <object>();

            inputVals.Add("xiejianjun");


            //出参
            GenericTree output = new GenericTree();

            output.setParamType(TypeEnum.PRIMITIVE_TYPE);
            output.setThrfitType("string");
            output.setName("returnModel");

            string method = "SecondService:secondtestString";

            GenericNode genericNode = new GenericNode();

            genericNode.setInputs(inputGenericTrees);
            genericNode.setMethodName(method);
            genericNode.setValues(inputVals);
            genericNode.setOutput(output);

            protocol.Transport.Open();

            object obj = genericAnalyser.syncProcess(protocol, genericNode);

            protocol.Transport.Close();

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }
예제 #6
0
        private void readMap(TProtocol iprot, Dictionary <string, GenericTree> genericTreeDict, Dictionary <object, object> result)
        {
            TMap _map0 = iprot.ReadMapBegin();

            GenericTree key   = genericTreeDict[PARAMINFO_COLLECTION_MAP_KEY];
            GenericTree value = genericTreeDict[PARAMINFO_COLLECTION_MAP_VALUE];

            if (key == null || value == null)
            {
                Console.WriteLine("key or value is not found in GenericNode !");
                iprot.ReadMapEnd();
                return;
            }

            //TODO:默认key是简单类型
            for (int i = 0; i < _map0.Count; ++i)
            {
                object obj_key = doProtocolMethod(iprot, key.getThrfitType(), null, READ);
                if (value.getParamType() == TypeEnum.PRIMITIVE_TYPE)
                {
                    Object obj_value = doProtocolMethod(iprot, key.getThrfitType(), null, READ);
                    result.Add(obj_key, obj_value);
                }
                else if (value.getThrfitType() == "MAP")
                {
                    Dictionary <object, object> map_value = new Dictionary <object, object>();
                    readMap(iprot, value.getChildren(), map_value);

                    result.Add(obj_key, map_value);
                }
                else
                {
                    Dictionary <string, object> map_value = new Dictionary <string, object>();

                    List <GenericTree> children = value.getChildren().Values.ToList();

                    read(iprot, children, map_value);

                    result.Add(obj_key, map_value);
                }
            }
            iprot.ReadMapEnd();
        }
예제 #7
0
        public object recvMsg(TProtocol iprot, GenericTree genericTree, int seqid_)
        {
            TMessage msg = iprot.ReadMessageBegin();

            if (msg.Type == TMessageType.Exception)
            {
                TApplicationException x = TApplicationException.Read(iprot);
                iprot.ReadMessageEnd();
                throw x;
            }
            if (msg.SeqID != seqid_)
            {
                throw new TApplicationException(TApplicationException.ExceptionType.BadSequenceID, " failed: out of sequence response");
            }

            Dictionary <string, object> result = new Dictionary <string, object>();

            List <GenericTree> list = new List <GenericTree>();

            if (genericTree != null)
            {
                list.Add(genericTree);
            }

            read(iprot, list, result);
            iprot.ReadMessageEnd();
            //如果genericTree 是复杂类型则需要去掉Map的第一层 ex: 返回值是{a : {b:1,c:2}} 则真正的返回值应该是{b:1,c:2}
            //TODO:如果是集合情况也应该是这样处理
            if (genericTree != null && !isPrimitiveType(genericTree))
            {
                if (result.Count == 0)
                {
                    return(null);
                }
                object obj = result[genericTree.getName()];
                return(obj);
            }
            return(result);
        }
예제 #8
0
        public void testMapMap(int hello)
        {
            GenericTree input1 = new GenericTree();

            input1.setName("thing");
            input1.setParamType(TypeEnum.COLLECTION_TYPE);
            input1.setThrfitType("MAP");

            GenericTree innerKey = new GenericTree();

            innerKey.setName("thing1");
            innerKey.setParamType(TypeEnum.PRIMITIVE_TYPE);
            innerKey.setThrfitType("I32");

            GenericTree innerValue = new GenericTree();

            innerValue.setName("thing2");
            innerValue.setParamType(TypeEnum.COLLECTION_TYPE);
            innerValue.setThrfitType("MAP");

            Dictionary <string, GenericTree> valueChildren = new Dictionary <string, GenericTree>();

            valueChildren.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_KEY, innerKey);
            valueChildren.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_VALUE, innerKey);

            innerValue.setChildren(valueChildren);

            Dictionary <string, GenericTree> children = new Dictionary <string, GenericTree>();

            children.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_KEY, innerKey);
            children.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_VALUE, innerValue);

            input1.setChildren(children);

            List <GenericTree> inputGenericTrees = new List <GenericTree>
            {
                innerKey
            };

            // 参数值
            List <Object> inputVals = new List <object>();

            inputVals.Add(hello);

            //出参
            GenericTree output = new GenericTree();

            output.setParamType(TypeEnum.COLLECTION_TYPE);
            output.setThrfitType("MAP");
            output.setName("returnModel");

            GenericTree outKey = new GenericTree();

            outKey.setName("thing");
            outKey.setParamType(TypeEnum.PRIMITIVE_TYPE);
            outKey.setThrfitType("I32");

            GenericTree outValue = new GenericTree();

            outValue.setName("thing");
            outValue.setParamType(TypeEnum.COLLECTION_TYPE);
            outValue.setThrfitType("MAP");

            Dictionary <string, GenericTree> outValueChildren = new Dictionary <string, GenericTree>();

            outValueChildren.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_KEY, outKey);
            outValueChildren.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_VALUE, outKey);

            outValue.setChildren(outValueChildren);

            Dictionary <string, GenericTree> outputChildren = new Dictionary <string, GenericTree>();

            outputChildren.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_KEY, outKey);
            outputChildren.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_VALUE, outValue);

            output.setChildren(outputChildren);

            string method = SERVICE_NAME + "testMapMap";

            GenericNode genericNode = new GenericNode();

            genericNode.setInputs(inputGenericTrees);
            genericNode.setMethodName(method);
            genericNode.setValues(inputVals);
            genericNode.setOutput(output);

            object obj = genericAnalyser.syncProcess(protocol, genericNode);

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }
예제 #9
0
        public void testMulti(sbyte arg0, int arg1, long arg2, Dictionary <short, string> arg3, Numberz arg4, long arg5)
        {
            // 返回 Xtruct

            GenericTree _arg0 = new GenericTree();

            _arg0.setName("arg0");
            _arg0.setParamType(TypeEnum.PRIMITIVE_TYPE);
            _arg0.setThrfitType("SBYTE");

            GenericTree _arg1 = new GenericTree();

            _arg1.setName("_arg1");
            _arg1.setParamType(TypeEnum.PRIMITIVE_TYPE);
            _arg1.setThrfitType("I32");

            GenericTree _arg2 = new GenericTree();

            _arg2.setName("_arg2");
            _arg2.setParamType(TypeEnum.PRIMITIVE_TYPE);
            _arg2.setThrfitType("I64");

            GenericTree _arg3 = new GenericTree();

            _arg3.setName("_arg3");
            _arg3.setParamType(TypeEnum.COLLECTION_TYPE);
            _arg3.setThrfitType("MAP");


            GenericTree innerKey = new GenericTree();

            innerKey.setName("innerKey");
            innerKey.setParamType(TypeEnum.PRIMITIVE_TYPE);
            innerKey.setThrfitType("I16");

            GenericTree innerValue = new GenericTree();

            innerValue.setName("innerValue");
            innerValue.setParamType(TypeEnum.PRIMITIVE_TYPE);
            innerValue.setThrfitType("STRING");

            Dictionary <string, GenericTree> children = new Dictionary <string, GenericTree>();

            children.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_KEY, innerKey);
            children.Add(TGenericClient.PARAMINFO_COLLECTION_MAP_VALUE, innerValue);

            _arg3.setChildren(children);



            GenericTree _arg4 = new GenericTree();

            _arg4.setName("_arg4");
            _arg4.setParamType(TypeEnum.PRIMITIVE_TYPE);
            _arg4.setThrfitType("I32");

            GenericTree _arg5 = new GenericTree();

            _arg5.setName("_arg5");
            _arg5.setParamType(TypeEnum.PRIMITIVE_TYPE);
            _arg5.setThrfitType("I64");

            List <GenericTree> inputGenericTrees = new List <GenericTree>
            {
                _arg0, _arg1, _arg2, _arg3, _arg4, _arg5
            };

            GenericTree input2 = new GenericTree();

            input2.setName("thing");
            input2.setParamType(TypeEnum.SYNTHETIC_TYPE);
            input2.setThrfitType("STRUCT");
            input2.setType("Xtruct");

            GenericTree prop11 = new GenericTree();

            prop11.setName("String_thing");
            prop11.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop11.setThrfitType("STRING");
            prop11.setParent(input2);


            GenericTree prop12 = new GenericTree();

            prop12.setName("Byte_thing");
            prop12.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop12.setThrfitType("SBYTE");
            prop12.setParent(input2);

            GenericTree prop13 = new GenericTree();

            prop13.setName("I32_thing");
            prop13.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop13.setThrfitType("I32");
            prop13.setParent(input2);

            GenericTree prop14 = new GenericTree();

            prop14.setName("I64_thing");
            prop14.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop14.setThrfitType("I64");
            prop14.setParent(input2);


            // 参数值
            List <Object> inputVals = new List <object>();


            inputVals.Add(arg0);
            inputVals.Add(arg1);
            inputVals.Add(arg2);
            inputVals.Add(arg3);
            inputVals.Add(arg4);
            inputVals.Add(arg5);

            //出参


            string method = SERVICE_NAME + "testMulti";

            GenericNode genericNode = new GenericNode();

            genericNode.setInputs(inputGenericTrees);
            genericNode.setMethodName(method);
            genericNode.setValues(inputVals);
            genericNode.setOutput(input2);

            object obj = genericAnalyser.syncProcess(protocol, genericNode);

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }
예제 #10
0
        //public Xtruct testMultiException(string arg0, string arg1)
        //{
        //    Console.WriteLine("testMultiException:" + arg0 + " " + arg1 + " ");

        //    Xtruct info = new Xtruct()
        //    {
        //        String_thing = arg0 + arg1
        //    };

        //    return info;
        //}

        public void testNest(Xtruct2 thing)
        {
            GenericTree input1 = new GenericTree();

            input1.setName("thing");
            input1.setParamType(TypeEnum.SYNTHETIC_TYPE);
            input1.setThrfitType("STRUCT");
            input1.setType("XStruct2");

            GenericTree prop1 = new GenericTree();

            prop1.setName("Byte_thing");
            prop1.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop1.setThrfitType("SBYTE");
            prop1.setParent(input1);

            GenericTree prop2 = new GenericTree();

            prop2.setName("Xstruct_thing");
            prop2.setParamType(TypeEnum.SYNTHETIC_TYPE);
            prop2.setThrfitType("STRUCT");
            prop2.setType("XStruct");


            GenericTree prop31 = new GenericTree();

            prop31.setName("String_thing");
            prop31.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop31.setThrfitType("STRING");
            prop31.setParent(prop2);

            GenericTree prop32 = new GenericTree();

            prop32.setName("Byte_thing");
            prop32.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop32.setThrfitType("SBYTE");
            prop32.setParent(prop2);

            GenericTree prop33 = new GenericTree();

            prop33.setName("I32_thing");
            prop33.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop33.setThrfitType("I32");
            prop33.setParent(prop2);

            GenericTree prop34 = new GenericTree();

            prop34.setName("I64_thing");
            prop34.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop34.setThrfitType("I64");
            prop34.setParent(prop2);

            prop2.setParent(input1);


            GenericTree prop3 = new GenericTree();

            prop3.setName("I32_thing");
            prop3.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop3.setThrfitType("I32");
            prop3.setParent(input1);


            List <GenericTree> inputGenericTrees = new List <GenericTree>
            {
                input1
            };

            // 参数值
            List <Object> inputVals = new List <object>();


            Dictionary <string, object> xstruct2 = new Dictionary <string, object>();

            xstruct2["Byte_thing"]    = thing.Byte_thing;
            xstruct2["I32_thing"]     = thing.I32_thing;
            xstruct2["Xstruct_thing"] = new Dictionary <string, object> {
                { "Byte_thing", thing.Struct_thing.Byte_thing }
                , { "String_thing", thing.Struct_thing.String_thing }
                , { "I32_thing", thing.Struct_thing.I32_thing }
                , { "I64_thing", thing.Struct_thing.I64_thing }
            };

            inputVals.Add(xstruct2);


            string method = SERVICE_NAME + "testNest";

            GenericNode genericNode = new GenericNode();

            genericNode.setInputs(inputGenericTrees);
            genericNode.setMethodName(method);
            genericNode.setValues(inputVals);
            genericNode.setOutput(input1);

            object obj = genericAnalyser.syncProcess(protocol, genericNode);

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }
예제 #11
0
 private bool isColletionMap(GenericTree genericTree)
 {
     return(genericTree.getThrfitType().ToUpper().Equals("MAP"));
 }
예제 #12
0
 private bool isPrimitiveType(GenericTree genericTree)
 {
     return(genericTree.getParamType().GetHashCode() == TypeEnum.PRIMITIVE_TYPE.GetHashCode());
 }
예제 #13
0
 private bool isColletionType(GenericTree genericTree)
 {
     return(genericTree.getParamType().GetHashCode() == TypeEnum.COLLECTION_TYPE.GetHashCode());
 }
예제 #14
0
        public void testInsanity(string argument)
        {
            // Dictionary<long, Dictionary<Numberz, Insanity>>

            GenericTree input1 = new GenericTree();

            input1.setName("argument");
            input1.setParamType(TypeEnum.SYNTHETIC_TYPE);
            input1.setThrfitType("STRUCT");

            Dictionary <string, GenericTree> insanity = new Dictionary <string, GenericTree>();

            GenericTree userMap = new GenericTree();

            userMap.setName("userMap");
            userMap.setParamType(TypeEnum.COLLECTION_TYPE);
            userMap.setThrfitType("MAP");

            GenericTree xtructs = new GenericTree();

            xtructs.setName("xtructs");
            xtructs.setParamType(TypeEnum.COLLECTION_TYPE);
            xtructs.setThrfitType("LIST");

            insanity.Add("userMap", userMap);
            insanity.Add("xtructs", xtructs);


            input1.setChildren(insanity);


            List <GenericTree> inputGenericTrees = new List <GenericTree>
            {
                input1
            };

            // 参数值
            List <Object> inputVals = new List <object>();

            inputVals.Add(argument);

            //出参
            GenericTree output = new GenericTree();

            output.setParamType(TypeEnum.PRIMITIVE_TYPE);
            output.setThrfitType("STRING");
            output.setName("returnModel");

            string method = SERVICE_NAME + "testInsanity";

            GenericNode genericNode = new GenericNode();

            genericNode.setInputs(inputGenericTrees);
            genericNode.setMethodName(method);
            genericNode.setValues(inputVals);
            genericNode.setOutput(output);

            object obj = genericAnalyser.syncProcess(protocol, genericNode);

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }
예제 #15
0
        private void read(TProtocol iprot, List <GenericTree> genericTrees, Dictionary <string, object> result)
        {
            TField schemeField;

            iprot.ReadStructBegin();

            //每一层的参数 按顺序逐个进行遍历 读取二进制序列返回值
            bool isStop = false;

            foreach (GenericTree genericTreeTmp in genericTrees)
            {
                GenericTree genericTree = new GenericTree();

                schemeField = iprot.ReadFieldBegin();
                if (schemeField.Type == TType.Stop)
                {
                    // Console.WriteLine("返回结果参数个数不对! type : stop 返回值可能为null");
                    isStop = true;
                    break;
                }

                if (genericTrees.Count > 1)
                {
                    genericTree = genericTrees[schemeField.ID - 1];
                }
                else
                {
                    genericTree = genericTreeTmp;
                }


                if (isPrimitiveType(genericTree))
                {
                    String key        = genericTree.getName();
                    String thriftType = genericTree.getThrfitType();
                    Object obj        = doProtocolMethod(iprot, thriftType, null, READ);
                    result.Add(key, obj);
                }
                else if (isColletionType(genericTree))
                {
                    if (schemeField.Type != TType.List && schemeField.Type != TType.Set && schemeField.Type != TType.Map)
                    {
                        Console.WriteLine("返回结果参数类型不是集合类型! : " + schemeField.Type);
                        break;
                        //TODO:若类型不匹配则跳过这段数据报文   TProtocolUtil.skip(iprot, schemeField.type);
                    }

                    //MAP单独处理
                    if (schemeField.Type == TType.Map)
                    {
                        TMap        _map0 = iprot.ReadMapBegin();
                        GenericTree key   = genericTree.getChildren()[PARAMINFO_COLLECTION_MAP_KEY];
                        GenericTree value = genericTree.getChildren()[PARAMINFO_COLLECTION_MAP_VALUE];
                        if (key == null || value == null)
                        {
                            Console.WriteLine("key or value is not found in GenericNode !");
                            break;
                        }
                        Dictionary <object, object> map = new Dictionary <object, object>(2 * _map0.Count);
                        //TODO:默认key是简单类型
                        for (int i = 0; i < _map0.Count; ++i)
                        {
                            object obj_key = doProtocolMethod(iprot, key.getThrfitType(), null, READ);
                            if (value.getParamType() == TypeEnum.PRIMITIVE_TYPE)
                            {
                                Object obj_value = doProtocolMethod(iprot, key.getThrfitType(), null, READ);
                                map.Add(obj_key, obj_value);
                            }
                            else if (value.getThrfitType() == "MAP")
                            {
                                Dictionary <object, object> map_value = new Dictionary <object, object>();
                                readMap(iprot, value.getChildren(), map_value);

                                map.Add(obj_key, map_value);
                            }
                            else
                            {
                                Dictionary <string, object> map_value = new Dictionary <string, object>();

                                List <GenericTree> children = value.getChildren().Values.ToList();

                                read(iprot, children, map_value);

                                map.Add(obj_key, map_value);
                            }
                        }
                        result.Add(genericTree.getName(), map);
                        iprot.ReadMapEnd();
                        iprot.ReadFieldEnd();
                        break;
                    }

                    TList _list0 = iprot.ReadListBegin();
                    //获取容器的孩子节点,如果集合是基础类型的 本次遍历就到这里
                    GenericTree child = genericTree.getChildren()[PARAMINFO_COLLECTION_INNER_KEY];

                    List <object> list = new List <object>();
                    if (isPrimitiveType(child))
                    {
                        for (int _i2 = 0; _i2 < _list0.Count; ++_i2)
                        {
                            string childThrfitType = child.getThrfitType();
                            list.Add(doProtocolMethod(iprot, childThrfitType, null, READ));
                        }
                        iprot.ReadListEnd();
                    }
                    else
                    {
                        //如果发现集合内部是复杂类型 则遍历_list0 内部就是复杂类型的结构参数
                        for (int _i2 = 0; _i2 < _list0.Count; ++_i2)
                        {
                            Dictionary <string, object> childResult = new Dictionary <string, object>();
                            //这里的child是复杂类型说明,应该直接拿到它的children 即它的子参数类型
                            List <GenericTree> children = child.getChildren().Values.ToList();
                            read(iprot, children, childResult);
                            list.Add(childResult);
                        }
                        iprot.ReadListEnd();
                    }
                    result[genericTree.getName()] = list;
                }
                else
                {
                    if (schemeField.Type != TType.Struct)
                    {
                        Console.WriteLine("返回结果参数类型不是复杂类型! : " + schemeField.Type);
                        break;
                        //TODO:若类型不匹配则跳过这段二进制序列   TProtocolUtil.skip(iprot, schemeField.type);
                    }
                    List <GenericTree>          children    = genericTree.getChildren().Values.ToList();
                    Dictionary <string, object> childResult = new Dictionary <string, object>();
                    //如果是第一层 即返回值类型本身,则本身没有key,应该直接将map传入下一层。通过map size是否为0判断是否未第一层
                    result[genericTree.getName()] = childResult;
                    read(iprot, children, childResult);
                }
                iprot.ReadFieldEnd();
            }

            if (!isStop)
            {
                schemeField = iprot.ReadFieldBegin();
                if (schemeField.Type != TType.Stop)
                {
                    Console.WriteLine("type is not stop : " + schemeField.Type);
                }
            }

            iprot.ReadStructEnd();
        }
예제 #16
0
        // protocol写入具体数据 基本类型直接写入 非基本类型需要递归进行写入
        // ex:oprot.writeString()
        // TODO:存在多次JSON decode enocde 性能问题!
        // TODO:value 为null如何处理

        private void writeVal(TProtocol oprot, GenericTree genericTree, object value, short id)
        {
            String thriftType = genericTree.getThrfitType();

            if (value == null)
            {
                return;
            }
            if (isPrimitiveType(genericTree))
            {
                oprot.IncrementRecursionDepth();
                try
                {
                    TField tField = getTField(genericTree.getName(), thriftType, id);
                    oprot.WriteFieldBegin(tField);
                    doProtocolMethod(oprot, thriftType, value, WRITE);
                    oprot.WriteFieldEnd();
                }
                finally
                {
                    oprot.DecrementRecursionDepth();
                }
            }
            else if (isColletionType(genericTree))
            {
                TField tField = getTField(genericTree.getName(), thriftType, id);
                oprot.WriteFieldBegin(tField);
                //TODO:假设目前集合只有List类型
                // String json = Newtonsoft.Json.JsonConvert.SerializeObject(value);
                if (isColletionMap(genericTree))
                {
                    GenericTree k = genericTree.getChildren()[PARAMINFO_COLLECTION_MAP_KEY];
                    GenericTree v = genericTree.getChildren()[PARAMINFO_COLLECTION_MAP_VALUE];
                    if (k == null || v == null)
                    {
                        Console.WriteLine("key or value is not found in GenericNode !");
                    }
                    else
                    {
                        IDictionary map = value as IDictionary;

                        oprot.WriteMapBegin(new TMap(TypeChange.TTypeMap[k.getThrfitType().ToUpper()]
                                                     , TypeChange.TTypeMap[v.getThrfitType().ToUpper()], map.Count));

                        foreach (DictionaryEntry entry in map)
                        {
                            //TODO:只支持key是简单类型
                            doProtocolMethod(oprot, k.getThrfitType(), entry.Key, WRITE);
                            if (v.getParamType() == TypeEnum.PRIMITIVE_TYPE)
                            {
                                doProtocolMethod(oprot, v.getThrfitType(), entry.Value, WRITE);
                            }
                            else
                            {
                                writeVal(oprot, v, entry.Value, id);
                            }
                        }
                        oprot.WriteMapEnd();
                    }
                }
                else
                {
                    // List<object> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(json);
                    IEnumerable list = value as IEnumerable;

                    int count = 0;
                    foreach (var item in list)
                    {
                        count++;
                    }

                    GenericTree child           = genericTree.getChildren()[PARAMINFO_COLLECTION_INNER_KEY];
                    String      childThriftType = child.getThrfitType();
                    oprot.WriteListBegin(new TList(TypeChange.TTypeMap[childThriftType.ToUpper()], count));
                    //获取容器的孩子节点,如果集合是基础类型的 本次遍历就到这里
                    if (isPrimitiveType(child))
                    {
                        foreach (object obj in list)
                        {
                            string childThrfitType = child.getThrfitType();
                            doProtocolMethod(oprot, childThrfitType, obj, WRITE);
                        }
                    }
                    else
                    {
                        foreach (object obj in list)
                        {
                            short ids = 1;
                            writeVal(oprot, child, obj, ids);
                        }
                    }
                    oprot.WriteListEnd();
                }

                oprot.WriteFieldEnd();
            }
            else
            {
                oprot.IncrementRecursionDepth();
                try
                {
                    TField field = new TField();
                    field.Name = genericTree.getName();
                    field.Type = TType.Struct;
                    field.ID   = id;
                    oprot.WriteFieldBegin(field);

                    String  structName  = genericTree.getType();
                    TStruct STRUCT_DESC = new TStruct(structName);
                    oprot.WriteStructBegin(STRUCT_DESC);

                    //TODO:这个map是有序的
                    Dictionary <String, GenericTree> children = genericTree.getChildren();

                    Dictionary <string, object> map = value as Dictionary <string, object>;

                    short ids = 1;
                    foreach (GenericTree child in children.Values)
                    {
                        //if (map.ContainsKey(child.getName()))
                        //{
                        writeVal(oprot, child, map[child.getName()], ids++);
                        //}
                    }
                    oprot.WriteFieldStop();
                    oprot.WriteStructEnd();

                    oprot.WriteFieldEnd();
                }
                finally
                {
                    oprot.DecrementRecursionDepth();
                }
            }
        }
예제 #17
0
 public virtual void AddChild(GenericTree <T> newChild)
 {
     this.children.Add(newChild);
 }
예제 #18
0
        /*
         * public THashSet<int> testSet(THashSet<int> thing)
         * {
         *  Console.WriteLine("testSet:" + thing.ToString());
         *
         *  return thing;
         * }
         */

        public void testStruct()
        {
            GenericTree input1 = new GenericTree();

            input1.setName("thing");
            input1.setParamType(TypeEnum.SYNTHETIC_TYPE);
            input1.setThrfitType("STRUCT");
            input1.setType("Xtruct");

            GenericTree prop1 = new GenericTree();

            prop1.setName("String_thing");
            prop1.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop1.setThrfitType("STRING");

            GenericTree prop2 = new GenericTree();

            prop2.setName("Byte_thing");
            prop2.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop2.setThrfitType("SBYTE");

            GenericTree prop3 = new GenericTree();

            prop3.setName("I32_thing");
            prop3.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop3.setThrfitType("I32");

            GenericTree prop4 = new GenericTree();

            prop4.setName("I64_thing");
            prop4.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop4.setThrfitType("I64");

            Dictionary <string, GenericTree> children = new Dictionary <string, GenericTree>();

            children.Add("String_thing", prop1);
            children.Add("Byte_thing", prop2);
            children.Add("I32_thing", prop3);
            children.Add("I64_thing", prop4);

            input1.setChildren(children);

            List <GenericTree> inputGenericTrees = new List <GenericTree>
            {
                input1
            };

            GenericTree input2 = new GenericTree();

            input2.setName("thing");
            input2.setParamType(TypeEnum.SYNTHETIC_TYPE);
            input2.setThrfitType("STRUCT");
            input2.setType("Xtruct");

            GenericTree prop11 = new GenericTree();

            prop11.setName("String_thing");
            prop11.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop11.setThrfitType("STRING");
            prop11.setParent(input2);


            GenericTree prop12 = new GenericTree();

            prop12.setName("Byte_thing");
            prop12.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop12.setThrfitType("SBYTE");

            GenericTree prop13 = new GenericTree();

            prop13.setName("I32_thing");
            prop13.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop13.setThrfitType("I32");

            GenericTree prop14 = new GenericTree();

            prop14.setName("I64_thing");
            prop14.setParamType(TypeEnum.PRIMITIVE_TYPE);
            prop14.setThrfitType("I64");

            Dictionary <string, GenericTree> children2 = new Dictionary <string, GenericTree>();

            children2.Add("String_thing", prop11);
            children2.Add("Byte_thing", prop12);
            children2.Add("I32_thing", prop13);
            children2.Add("I64_thing", prop14);

            input2.setChildren(children2);


            // 参数值
            List <Object> inputVals = new List <object>();



            Dictionary <string, object> row = new Dictionary <string, object>();

            row.Add("String_thing", "stringValue3");
            row.Add("Byte_thing", Convert.ToSByte(127));
            row.Add("I32_thing", 99999);
            row.Add("I64_thing", 9999900001111);

            inputVals.Add(row);

            //出参


            string method = SERVICE_NAME + "testStruct";

            GenericNode genericNode = new GenericNode();

            genericNode.setInputs(inputGenericTrees);
            genericNode.setMethodName(method);
            genericNode.setValues(inputVals);
            genericNode.setOutput(input2);

            object obj = genericAnalyser.syncProcess(protocol, genericNode);

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }