예제 #1
0
        /// <summary>根据Patch信息生成PatchInfo
        /// </summary>
        public PatchInfo PatchToInfo(Patch patch)
        {
            var patchInfo = new PatchInfo()
            {
                StoreType = StoreType.S3,
                Code      = patch.Code,
                Name      = patch.Name,
                State     = patch.State
            };

            if (patch.StoreType == (int)StoreType.S3)
            {
                var s3Patch    = (S3Patch)patch;
                var bucketDict = DictionaryEmit.ObjectToDictionary <BucketInfo>(s3Patch.Bucket);
                patchInfo.Expands.Add(bucketDict.ToList());
            }
            else if (patch.StoreType == (int)StoreType.FastDFS)
            {
                var fastDFSPatch = (FastDFSPatch)patch;
                foreach (var tracker in fastDFSPatch.Trackers)
                {
                    var trackerDict = DictionaryEmit.ObjectToDictionary <Tracker>(tracker);
                    patchInfo.Expands.Add(trackerDict.ToList());
                }
            }
            else
            {
                throw new ArgumentException("不是有效的Patch类型,无法转换成PatchInfo");
            }

            for (int i = 0; i < patchInfo.Expands.Count; i++)
            {
                for (int j = 0; j < patchInfo.Expands[i].Count; j++)
                {
                    var kv    = patchInfo.Expands[i][j];
                    var value = kv.Value.TrimEnd('\r').TrimEnd('\n');
                    patchInfo.Expands[i][j] = new KeyValuePair <string, string>(kv.Key, value);
                }
            }

            return(patchInfo);
        }
예제 #2
0
        public void ObjectToDictionary_DictionaryToObject_Test()
        {
            Assert.Throws <NotSupportedException>(() =>
            {
                DictionaryEmit.DictionaryToObject <List <int> >(new Dictionary <string, string>());
            });


            Assert.Throws <NotSupportedException>(() =>
            {
                DictionaryEmit.GetDictionaryFunc <List <DictionaryEmitClass1> >();
            });

            var o = new DictionaryEmitClass1()
            {
                Id       = 100,
                Name     = "zhangsan",
                Age      = 22,
                Birthday = new DateTime(2020, 3, 5)
            };

            var dict1 = DictionaryEmit.ObjectToDictionary <DictionaryEmitClass1>(o);

            Assert.Equal(o.Id.ToString(), dict1["Id"]);
            Assert.Equal(o.Name, dict1["Name"]);
            Assert.Equal(o.Age?.ToString(), dict1["Age"]);
            Assert.Equal(4, dict1.Count);


            var o1 = DictionaryEmit.DictionaryToObject <DictionaryEmitClass1>(dict1);

            Assert.Equal(o.Id, o1.Id);
            Assert.Equal(o.Name, o1.Name);
            Assert.Equal(o.Age, o1.Age);
            Assert.Equal(o.Birthday, o1.Birthday);
        }