public void Assign_Complex_With_Two_Items_In_List()
        {
            var config = new Dictionary <string, object>
            {
                { "Property1", "Value1" },
                { "Property Two", "2" },
                { "Property3", new List <object>
                  {
                      new { Id = 31, Name = "Property3_1" },
                      new { Id = 32, Name = "Property3_2" },
                  } }
            };

            var result = ObjectsMapper.MapInto <OtherClassEnumTyped>(config);

            // TODO: like below
            //var result = _mapper.MapInto<OtherClassEnumTyped>(config);

            Assert.Equal("Value1", result.Property1);
            Assert.Equal("2", result.Property2);

            Assert.Equal("Property3_1", result.Property3.First().Name);
            Assert.Equal(31, result.Property3.First().Id);

            Assert.Equal("Property3_2", result.Property3.Last().Name);
            Assert.Equal(32, result.Property3.Last().Id);
        }
        public void MapFrom_Config_Into_Obj_With_List_of_ParticularType_Prop()
        {
            var config = new Dictionary <string, object>
            {
                { "Property1", "Value1" },
                { "Property Two", "Value2" },
                { "Property3", new []
                  {
                      new { Id = 11, Name = "Property31" },
                      new { Id = 12, Name = "Property32" },
                  } }
            };

            var result = ObjectsMapper.MapInto <OtherClassEnumTyped>(config);

            // TODO: like below
            //var result = _mapper.MapInto<OtherClassEnumTyped>(config);

            Assert.Equal("Value1", result.Property1);
            Assert.Equal("Value2", result.Property2);
            Assert.Equal(11, result.Property3.First().Id);
            Assert.Equal(12, result.Property3.Last().Id);
            Assert.Equal("Property31", result.Property3.First().Name);
            Assert.Equal("Property32", result.Property3.Last().Name);
        }
        public void MapInto_Particular_Obj_From_Dynamic()
        {
            var resource = new
            {
                Id   = 11,
                Name = "Property3"
            };

            var result = ObjectsMapper.MapInto <OtherInner>(resource);

            Assert.Equal(11, result.Id);
            Assert.Equal("Property3", result.Name);
        }
Beispiel #4
0
        public void Not_It_1_Typed_Inner()
        {
            var filePathLeft   = Path.Combine(GetDataFilesPath(), "json2.json");
            var fileExistsLeft = File.Exists(filePathLeft);

            Assert.True(fileExistsLeft);

            using (StreamReader sr = File.OpenText(filePathLeft))
            {
                var objJSON = JsonConvert.DeserializeObject <Dictionary <string, object> >(sr.ReadToEnd());
                Assert.NotNull(objJSON);

                // search for types where keys are equal to value.Name property if it property exists and values is JObject
                var svc = new ObjectsMapperService();

                // todo: check this type of determination in Calculator svc
                var anonymousKeys = svc.GetNamesOfPropertiesWhichAreJObjects(objJSON);
                Assert.True(anonymousKeys.Count() == 3);

                var listOfJObjects = new List <TypedInner>(); //remove it to have mapping in the loop

                foreach (var key in anonymousKeys)
                {
                    // TODO: check it for type with diff types of prop (bool, string, int, double, guid)
                    // in case we catch exception we need to use object mapper class
                    // also check for bool type in mapping for cases we use config QtyChanged and CostChanged

                    var converted  = JsonConvert.DeserializeObject <ExpandoObject>(objJSON[key].ToString());
                    var currentVal = ObjectsMapper.MapInto <TypedInner>(converted);

                    Assert.NotNull(currentVal);
                    Assert.NotEmpty(currentVal.Name);

                    listOfJObjects.Add(currentVal);
                }

                Assert.True(listOfJObjects.Count == 3);
                Assert.True(listOfJObjects.Last().Class);
                Assert.True(listOfJObjects.Last().Amount == 33.3d);
                //new ObjectsMapper().Map<TestClass, Inner>((ExpandoObject)objJSON, targetLeft);
                // TODO: like var target = ObjectsMapper.MapInto<TestClass>(objJSON);
            }
        }
        public void MapFrom_Config_Into_Obj_With_ParticularType_Prop()
        {
            var config = new Dictionary <string, object>
            {
                { "Property1", "Value1" },
                { "Property Two", "Value2" },
                { "Property3", new
                  {
                      Id   = 11,
                      Name = "Property3"
                  } }
            };

            var result = ObjectsMapper.MapInto <OtherClassInnerTyped>(config);

            Assert.Equal("Value1", result.Property1);
            Assert.Equal("Value2", result.Property2);
            Assert.Equal(11, result.Property3.Id);
            Assert.Equal("Property3", result.Property3.Name);
        }
        public void MapInto_Particular_Obj_With_Nested_One_From_Dynamic()
        {
            var resource = new {
                Property1 = "Value1",
                Property2 = "Value2",
                Property3 = new {
                    Id   = 11,
                    Name = "Property3"
                }
            };

            var result = ObjectsMapper.MapInto <OtherClassInnerTyped>(resource);

            // TODO: like below
            //var result = _mapper.MapInto<OtherClassInnerTyped>(config);

            Assert.Equal("Value1", result.Property1);
            Assert.Equal("Value2", result.Property2);
            Assert.Equal(11, result.Property3.Id);
            Assert.Equal("Property3", result.Property3.Name);
        }
Beispiel #7
0
        public void Handle_Objects_In_The_Loop_LeftPriority()
        {
            var module_1_left = new Some {
                Id = Guid.NewGuid(), Name = "Module_1"
            };
            var module_1_right = new Some {
                Id = Guid.Empty, Name = "Module_2"
            };

            var module_2_left = new Some {
                Id = Guid.NewGuid(), Name = string.Empty
            };
            var module_2_right = new Some {
                Id = Guid.NewGuid(), Name = "Module_2"
            };


            List <Some> leftSet = new List <Some> {
                module_1_left, module_2_left
            };
            List <Some> rightSet = new List <Some> {
                module_1_right, module_2_right
            };

            var resultSetMerged = new List <Some>();

            for (int i = 0; i < leftSet.Count(); i++)
            {
                dynamic mixedObject = Mixer.MixObjects(leftSet[i], rightSet[i]);

                var target = ObjectsMapper.MapInto <Some>(mixedObject);

                resultSetMerged.Add(target);
            }

            Assert.Equal("Module_1", resultSetMerged[0].Name);
            Assert.Equal("Module_2", resultSetMerged[1].Name);
            Assert.NotEqual(Guid.Empty, resultSetMerged[0].Id);
            Assert.NotEqual(module_2_right.Id, resultSetMerged[1].Id);
        }