private IAstNode ProcessReadWriteSimple(ReadWriteSimple readWriteSimple, int operationId)
        {
            IAstRefOrValue sourceValue = ReadSrcMappingValue(readWriteSimple, operationId);

            IAstRefOrValue convertedValue;

            if (readWriteSimple.NullSubstitutor != null && (ReflectionUtils.IsNullable(readWriteSimple.Source.MemberType) || !readWriteSimple.Source.MemberType.IsValueType))
            {
                convertedValue = new AstIfTernar(
                    ReflectionUtils.IsNullable(readWriteSimple.Source.MemberType)
                        ? (IAstValue) new AstExprNot(AstBuildHelper.ReadPropertyRV(new AstValueToAddr((IAstValue)sourceValue), readWriteSimple.Source.MemberType.GetProperty("HasValue")))
                        : new AstExprIsNull(sourceValue),
                    GetNullValue(readWriteSimple.NullSubstitutor),     // source is null
                    AstBuildHelper.CastClass(
                        ConvertMappingValue(
                            readWriteSimple,
                            operationId,
                            sourceValue
                            ),
                        readWriteSimple.Destination.MemberType
                        )
                    );
            }
            else
            {
                convertedValue =
                    ConvertMappingValue(
                        readWriteSimple,
                        operationId,
                        sourceValue
                        );
            }

            return(WriteMappingValue(readWriteSimple, operationId, convertedValue));
        }
Example #2
0
        private IAstRefOrValue ConvertMappingValue(
            ReadWriteSimple rwMapOp,
            int operationId,
            IAstRefOrValue sourceValue)
        {
            IAstRefOrValue convertedValue = sourceValue;

            if (rwMapOp.Converter != null)
            {
                convertedValue = AstBuildHelper.CallMethod(
                    rwMapOp.Converter.GetType().GetMethod("Invoke"),
                    new AstCastclassRef(
                        (IAstRef)AstBuildHelper.ReadMemberRV(
                            GetStoredObject(operationId, typeof(ReadWriteSimple)),
                            typeof(ReadWriteSimple).GetProperty("Converter")
                            ),
                        rwMapOp.Converter.GetType()
                        ),
                    new List <IAstStackItem>()
                {
                    sourceValue,
                    AstBuildHelper.ReadLocalRV(locState),
                }
                    );
            }
            else
            {
                if (rwMapOp.ShallowCopy && rwMapOp.Destination.MemberType == rwMapOp.Source.MemberType)
                {
                    convertedValue = sourceValue;
                }
                else
                {
                    var mi = staticConvertersManager.GetStaticConverter(rwMapOp.Source.MemberType, rwMapOp.Destination.MemberType);
                    if (mi != null)
                    {
                        convertedValue = AstBuildHelper.CallMethod(
                            mi,
                            null,
                            new List <IAstStackItem> {
                            sourceValue
                        }
                            );
                    }
                    else
                    {
                        convertedValue = ConvertByMapper(rwMapOp);
                    }
                }
            }

            return(convertedValue);
        }
    public void TestDestSrcReadOperation()
    {
        var message2 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                new[]
            {
                typeof(FromClass).GetMember(nameof(FromClass.Inner))[0],
                typeof(FromClass.InnerClass).GetMember(nameof(FromClass.InnerClass.GetMessage2))[0]
            }),
            Destination = new MemberDescriptor(
                new[] { typeof(ToClass).GetMember(nameof(ToClass.Message2))[0] })
        };

        var message = new DestSrcReadOperation
        {
            ValueProcessor = (f, t, o) =>
            {
                _testOutputHelper.WriteLine(f?.ToString());
                _testOutputHelper.WriteLine(t?.ToString());

                // _testOutputHelper.WriteLine(o?.ToString());
                // var source = f as FromClass;
                // var dest = t as ToClass;
                // dest.Message = source.Inner.Message;
            },
            Source = new MemberDescriptor(
                new[]
            {
                typeof(FromClass).GetMember(nameof(FromClass.Inner))[0],
                typeof(FromClass.InnerClass).GetMember(nameof(FromClass.Inner.Message))[0]
            }),
            Destination = new MemberDescriptor(
                new[] { typeof(ToClass).GetMember(nameof(ToClass.Message))[0] })
        };

        var mapper = Mapper.Default.GetMapper <FromClass, ToClass>(
            new CustomMapConfig {
            GetMappingOperationFunc = (from, to) => new IMappingOperation[] { message, message2 }
        });

        var b = new FromClass();
        var a = mapper.Map(b);

        // b.Inner.Message.ShouldBe(a.Message);
        // b.Inner.GetMessage2().ShouldBe(a.Message2);
    }
Example #4
0
    public void Test_EmitMapper_Map_ListObject(List <FromClass> listFrom)
    {
        _testOutputHelper.WriteLine(listFrom.Count.ToString());

        var rw1 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                typeof(FromClass).GetMember(nameof(FromClass.Inner))[0].AsEnumerable(
                    typeof(FromClass.InnerClass).GetMember(nameof(FromClass.Inner.Message))[0])),
            Destination = new MemberDescriptor(
                typeof(ToClass).GetMember(nameof(ToClass.Message))[0].AsEnumerable())
        };

        var rw2 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                typeof(FromClass).GetMember(nameof(FromClass.Inner))[0].AsEnumerable(
                    typeof(FromClass.InnerClass).GetMember(nameof(FromClass.InnerClass.GetMessage2))[0])),
            Destination = new MemberDescriptor(
                typeof(ToClass).GetMember(nameof(ToClass.Message2))[0].AsEnumerable())
        };

        var mapper = Mapper.Default.GetMapper <List <FromClass>, List <ToClass> >(
            new CustomMapConfig {
            GetMappingOperationFunc = (from, to) => rw1.AsEnumerable(rw2)
        });

        var tolist = mapper.Map(listFrom);

        using var f = listFrom.GetEnumerator();
        using var t = tolist.GetEnumerator();

        while (f.MoveNext() && t.MoveNext())
        {
            _testOutputHelper.WriteLine(t.Current.Message);
            f.Current.Inner.Message.ShouldBe(t.Current.Message);
            f.Current.Inner.GetMessage2().ShouldBe(t.Current.Message2);
        }
    }
Example #5
0
    public void TestFlattering1()
    {
        var rw1 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                new[]
            {
                typeof(Source).GetMember(nameof(Source.InnerSource))[0],
                typeof(Source.InnerSourceClass).GetMember(nameof(Source.InnerSource.Message))[0]
            }),
            Destination = new MemberDescriptor(
                new[] { typeof(Destination).GetMember(nameof(Destination.Message))[0] })
        };

        var rw2 = new ReadWriteSimple
        {
            Source = new MemberDescriptor(
                new[]
            {
                typeof(Source).GetMember(nameof(Source.InnerSource))[0],
                typeof(Source.InnerSourceClass).GetMember(nameof(Source.InnerSourceClass.GetMessage2))[0]
            }),
            Destination = new MemberDescriptor(
                new[] { typeof(Destination).GetMember(nameof(Destination.Message2))[0] })
        };

        var mapper = Mapper.Default.GetMapper <Source, Destination>(
            new CustomMapConfig {
            GetMappingOperationFunc = (from, to) => rw1.AsEnumerable(rw2)
        });

        var b      = new Source();
        var result = mapper.Map(b);

        b.InnerSource.Message.ShouldBe(result.Message);
        b.InnerSource.GetMessage2().ShouldBe(result.Message2);
    }
Example #6
0
        private IAstRefOrValue ConvertByMapper(ReadWriteSimple mapping)
        {
            IAstRefOrValue     convertedValue;
            ObjectsMapperDescr mapper = objectsMapperManager.GetMapperInt(
                mapping.Source.MemberType,
                mapping.Destination.MemberType,
                mappingConfigurator);
            int mapperId = AddObjectToStore(mapper);

            convertedValue = AstBuildHelper.CallMethod(
                typeof(ObjectsMapperBaseImpl).GetMethod(
                    "Map",
                    new Type[] { typeof(object), typeof(object), typeof(object) }
                    ),

                new AstReadFieldRef
            {
                fieldInfo    = typeof(ObjectsMapperDescr).GetField("mapper"),
                sourceObject = GetStoredObject(mapperId, mapper.GetType())
            },

                new List <IAstStackItem>()
            {
                AstBuildHelper.ReadMembersChain(
                    AstBuildHelper.ReadLocalRA(locFrom),
                    mapping.Source.MembersChain
                    ),
                AstBuildHelper.ReadMembersChain(
                    AstBuildHelper.ReadLocalRA(locTo),
                    mapping.Destination.MembersChain
                    ),
                (IAstRef)AstBuildHelper.ReadLocalRA(locState)
            }
                );
            return(convertedValue);
        }
Example #7
0
 private bool MappingItemTypeInList(IEnumerable <string> list, ReadWriteSimple mo)
 {
     return
         (TypeInList(list, mo.Destination.MemberType) ||
          TypeInList(list, mo.Source.MemberType));
 }
Example #8
0
 private bool MappingItemNameInList(IEnumerable <string> list, ReadWriteSimple mo)
 {
     return
         (list.Any(l => MatchMembers(l, mo.Destination.MemberInfo.Name)) ||
          list.Any(l => MatchMembers(l, mo.Source.MemberInfo.Name)));
 }