Skip to content

Commit 950bf16

Browse files
authored
Merge pull request #16 from BlaiseD/master
Update to recreate the binary expression when the operands change.
2 parents a28b89b + 41c44a1 commit 950bf16

File tree

3 files changed

+181
-1
lines changed

3 files changed

+181
-1
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<Authors>Jimmy Bogard</Authors>
44
<LangVersion>latest</LangVersion>
5-
<VersionPrefix>1.0.4</VersionPrefix>
5+
<VersionPrefix>1.0.5-preview01</VersionPrefix>
66
<WarningsAsErrors>true</WarningsAsErrors>
77
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
88
</PropertyGroup>

src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ protected override Expression VisitLambda<T>(Expression<T> node)
105105
return mapped;
106106
}
107107

108+
protected override Expression VisitBinary(BinaryExpression node)
109+
{
110+
return DoVisitBinary(this.Visit(node.Left), this.Visit(node.Right), this.Visit(node.Conversion));
111+
112+
Expression DoVisitBinary(Expression newLeft, Expression newRight, Expression conversion)
113+
{
114+
if (newLeft != node.Left || newRight != node.Right || conversion != node.Conversion)
115+
{
116+
if (node.NodeType == ExpressionType.Coalesce && node.Conversion != null)
117+
return Expression.Coalesce(newLeft, newRight, conversion as LambdaExpression);
118+
else
119+
return Expression.MakeBinary
120+
(
121+
node.NodeType,
122+
newLeft,
123+
newRight,
124+
node.IsLiftedToNull,
125+
Expression.MakeBinary(node.NodeType, newLeft, newRight).Method
126+
);
127+
}
128+
129+
return node;
130+
}
131+
}
132+
108133
protected override Expression VisitUnary(UnaryExpression node)
109134
{
110135
switch (node.NodeType)

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapper.Structs.Tests.cs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,161 @@ public void Can_convert_return_type()
6262
output1.First().Year.ShouldBe(2000);
6363
output2.Year.ShouldBe(2000);
6464
}
65+
66+
[Fact]
67+
public void Replace_operator_when_operands_change()
68+
{
69+
var config = new MapperConfiguration(cfg => {
70+
cfg.AddExpressionMapping();
71+
cfg.CreateMap<Source, Dest>().ReverseMap();
72+
});
73+
74+
var mapper = config.CreateMapper();
75+
76+
Expression<Func<Source, bool>> expression = x => x == default(Source);
77+
78+
var mapped = mapper.MapExpression<Expression<Func<Dest, bool>>>(expression);
79+
}
80+
}
81+
82+
public struct Source
83+
{
84+
public Source(int i) { val = i; }
85+
public int val;
86+
87+
public static implicit operator int(Source i)
88+
{
89+
return i.val;
90+
}
91+
92+
public static implicit operator Source(int i)
93+
{
94+
return new Source(i);
95+
}
96+
97+
public static bool operator <(Source a, Source b)
98+
{
99+
return a.val < b.val;
100+
}
101+
102+
public static bool operator >(Source a, Source b)
103+
{
104+
return a.val > b.val;
105+
}
106+
107+
public static bool operator ==(Source a, Source b)
108+
{
109+
return a.val == b.val;
110+
}
111+
112+
public static bool operator ==(Source a, int b)
113+
{
114+
return a.val == b;
115+
}
116+
117+
public static bool operator ==(int a, Source b)
118+
{
119+
return a == b.val;
120+
}
121+
122+
public static bool operator !=(Source a, Source b)
123+
{
124+
return a.val != b.val;
125+
}
126+
127+
public static bool operator !=(Source a, int b)
128+
{
129+
return a.val != b;
130+
}
131+
132+
public static bool operator !=(int a, Source b)
133+
{
134+
return a != b.val;
135+
}
136+
137+
public override int GetHashCode()
138+
{
139+
return this.val.GetHashCode();
140+
}
141+
142+
public override bool Equals(object obj)
143+
{
144+
if (obj is Source)
145+
return this == (Source)obj;
146+
if (obj is int)
147+
return this == (Source)((int)obj);
148+
return false;
149+
}
150+
}
151+
152+
public struct Dest
153+
{
154+
public Dest(int i) { val = i; }
155+
public int val;
156+
157+
public static implicit operator int(Dest i)
158+
{
159+
return i.val;
160+
}
161+
162+
public static implicit operator Dest(int i)
163+
{
164+
return new Dest(i);
165+
}
166+
167+
public static bool operator <(Dest a, Dest b)
168+
{
169+
return a.val < b.val;
170+
}
171+
172+
public static bool operator >(Dest a, Dest b)
173+
{
174+
return a.val > b.val;
175+
}
176+
177+
public static bool operator ==(Dest a, Dest b)
178+
{
179+
return a.val == b.val;
180+
}
181+
182+
public static bool operator ==(Dest a, int b)
183+
{
184+
return a.val == b;
185+
}
186+
187+
public static bool operator ==(int a, Dest b)
188+
{
189+
return a == b.val;
190+
}
191+
192+
public static bool operator !=(Dest a, Dest b)
193+
{
194+
return a.val != b.val;
195+
}
196+
197+
public static bool operator !=(Dest a, int b)
198+
{
199+
return a.val != b;
200+
}
201+
202+
public static bool operator !=(int a, Dest b)
203+
{
204+
return a != b.val;
205+
}
206+
207+
public override int GetHashCode()
208+
{
209+
return this.val.GetHashCode();
210+
}
211+
212+
public override bool Equals(object obj)
213+
{
214+
if (obj is Dest)
215+
return this == (Dest)obj;
216+
if (obj is int)
217+
return this == (Dest)((int)obj);
218+
return false;
219+
}
65220
}
66221

67222
public struct Garage

0 commit comments

Comments
 (0)