Example #1
0
        private static bool NothingIsBeingMapped(MappingCreationContext context)
        {
            var mappingExpressions = context.GetMemberMappingExpressions();

            if (mappingExpressions.None())
            {
                return(true);
            }

            if (mappingExpressions[0].NodeType != Assign)
            {
                return(false);
            }

            var assignedValue = ((BinaryExpression)mappingExpressions[0]).Right;

            if (assignedValue.NodeType == Default)
            {
                return(true);
            }

            if (!mappingExpressions.HasOne())
            {
                return(false);
            }

            if (assignedValue == context.MapperData.TargetObject)
            {
                return(true);
            }

            if (assignedValue.NodeType != Coalesce)
            {
                return(false);
            }

            var valueCoalesce = (BinaryExpression)assignedValue;

            if ((valueCoalesce.Left != context.MapperData.TargetObject) ||
                (valueCoalesce.Right.NodeType != New))
            {
                return(false);
            }

            var objectNewing = (NewExpression)valueCoalesce.Right;

            return(objectNewing.Arguments.None() && (objectNewing.Type != typeof(object)));
        }
Example #2
0
        private static Expression GetMapToNullConditionOrNull(MappingCreationContext context)
        {
            if (context.MapperData.IsRoot || (context.MapToNullCondition != null))
            {
                return(context.MapToNullCondition);
            }

            var mappingExpressions = context.GetMemberMappingExpressions();

            if (mappingExpressions.None())
            {
                return(null);
            }

            var memberAssignments = mappingExpressions
                                    .Project(m =>
            {
                if (m.NodeType != Conditional)
                {
                    return(m);
                }

                var conditionalMapping = (ConditionalExpression)m;

                if (conditionalMapping.Test.IsNullableHasValueAccess() &&
                    (conditionalMapping.IfTrue.NodeType == Assign))
                {
                    return(conditionalMapping.IfTrue);
                }

                return(m);
            })
                                    .Filter(m => m.NodeType == Assign)
                                    .Cast <BinaryExpression>()
                                    .Filter(a => a.Left.NodeType == MemberAccess)
                                    .ToArray();

            if (memberAssignments.Length != 1)
            {
                return(null);
            }

            var assignedMember = (MemberExpression)memberAssignments[0].Left;

            var memberTypeIdentifier = context
                                       .MapperContext
                                       .GetIdentifierOrNull(assignedMember.Member.DeclaringType);

            if (!Equals(assignedMember.Member, memberTypeIdentifier?.MemberInfo))
            {
                return(null);
            }

            var nonNullableIdType = assignedMember.Type.GetNonNullableType();

            if ((nonNullableIdType == assignedMember.Type) || !nonNullableIdType.IsNumeric())
            {
                return(assignedMember.GetIsDefaultComparison());
            }

            return(Expression.Equal(
                       assignedMember.GetValueOrDefaultCall(),
                       0.ToConstantExpression(nonNullableIdType)));
        }
 private static bool NothingIsBeingMapped(MappingCreationContext context)
 => NothingIsBeingMapped(context.GetMemberMappingExpressions(), context);