Skip to content

Commit ebb34db

Browse files
committed
CSHARP-823: added support for with xor.
1 parent 37255a5 commit ebb34db

File tree

2 files changed

+165
-1
lines changed

2 files changed

+165
-1
lines changed

MongoDB.Driver/Builders/UpdateBuilder.cs

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ public static UpdateBuilder BitwiseOr(string name, long value)
156156
return new UpdateBuilder().BitwiseOr(name, value);
157157
}
158158

159+
/// <summary>
160+
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
161+
/// </summary>
162+
/// <param name="name">The name of the element to be modified.</param>
163+
/// <param name="value">The value to be xor-ed with the current value.</param>
164+
/// <returns>The builder (so method calls can be chained).</returns>
165+
public static UpdateBuilder BitwiseXor(string name, int value)
166+
{
167+
return new UpdateBuilder().BitwiseXor(name, value);
168+
}
169+
170+
/// <summary>
171+
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
172+
/// </summary>
173+
/// <param name="name">The name of the element to be modified.</param>
174+
/// <param name="value">The value to be xor-ed with the current value.</param>
175+
/// <returns>The builder (so method calls can be chained).</returns>
176+
public static UpdateBuilder BitwiseXor(string name, long value)
177+
{
178+
return new UpdateBuilder().BitwiseXor(name, value);
179+
}
180+
159181
/// <summary>
160182
/// Combines several UpdateBuilders into a single UpdateBuilder.
161183
/// </summary>
@@ -853,6 +875,32 @@ public UpdateBuilder BitwiseOr(string name, long value)
853875
return this;
854876
}
855877

878+
/// <summary>
879+
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
880+
/// </summary>
881+
/// <param name="name">The name of the element to be modified.</param>
882+
/// <param name="value">The value to be xor-ed with the current value.</param>
883+
/// <returns>The builder (so method calls can be chained).</returns>
884+
public UpdateBuilder BitwiseXor(string name, int value)
885+
{
886+
if (name == null) { throw new ArgumentNullException("name"); }
887+
BitwiseOperation(name, "xor", value);
888+
return this;
889+
}
890+
891+
/// <summary>
892+
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
893+
/// </summary>
894+
/// <param name="name">The name of the element to be modified.</param>
895+
/// <param name="value">The value to be xor-ed with the current value.</param>
896+
/// <returns>The builder (so method calls can be chained).</returns>
897+
public UpdateBuilder BitwiseXor(string name, long value)
898+
{
899+
if (name == null) { throw new ArgumentNullException("name"); }
900+
BitwiseOperation(name, "xor", value);
901+
return this;
902+
}
903+
856904
/// <summary>
857905
/// Combines another UpdateBuilder into this one.
858906
/// </summary>
@@ -1752,6 +1800,32 @@ public static UpdateBuilder<TDocument> BitwiseOr(Expression<Func<TDocument, long
17521800
return new UpdateBuilder<TDocument>().BitwiseOr(memberExpression, value);
17531801
}
17541802

1803+
/// <summary>
1804+
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
1805+
/// </summary>
1806+
/// <param name="memberExpression">The member expression.</param>
1807+
/// <param name="value">The value to be xor-ed with the current value.</param>
1808+
/// <returns>
1809+
/// The builder (so method calls can be chained).
1810+
/// </returns>
1811+
public static UpdateBuilder<TDocument> BitwiseXor(Expression<Func<TDocument, int>> memberExpression, int value)
1812+
{
1813+
return new UpdateBuilder<TDocument>().BitwiseXor(memberExpression, value);
1814+
}
1815+
1816+
/// <summary>
1817+
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
1818+
/// </summary>
1819+
/// <param name="memberExpression">The member expression.</param>
1820+
/// <param name="value">The value to be xor-ed with the current value.</param>
1821+
/// <returns>
1822+
/// The builder (so method calls can be chained).
1823+
/// </returns>
1824+
public static UpdateBuilder<TDocument> BitwiseXor(Expression<Func<TDocument, long>> memberExpression, long value)
1825+
{
1826+
return new UpdateBuilder<TDocument>().BitwiseXor(memberExpression, value);
1827+
}
1828+
17551829
/// <summary>
17561830
/// Combines several UpdateBuilders into a single UpdateBuilder.
17571831
/// </summary>
@@ -2215,7 +2289,7 @@ public UpdateBuilder<TDocument> BitwiseOr(Expression<Func<TDocument, int>> membe
22152289
/// Sets the named element to the bitwise or of its value with another value (see $bit with "or").
22162290
/// </summary>
22172291
/// <param name="memberExpression">The member expression.</param>
2218-
/// <param name="value">The value to be and-ed with the current value.</param>
2292+
/// <param name="value">The value to be or-ed with the current value.</param>
22192293
/// <returns>
22202294
/// The builder (so method calls can be chained).
22212295
/// </returns>
@@ -2232,6 +2306,48 @@ public UpdateBuilder<TDocument> BitwiseOr(Expression<Func<TDocument, long>> memb
22322306
return this;
22332307
}
22342308

2309+
/// <summary>
2310+
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
2311+
/// </summary>
2312+
/// <param name="memberExpression">The member expression.</param>
2313+
/// <param name="value">The value to be xor-ed with the current value.</param>
2314+
/// <returns>
2315+
/// The builder (so method calls can be chained).
2316+
/// </returns>
2317+
public UpdateBuilder<TDocument> BitwiseXor(Expression<Func<TDocument, int>> memberExpression, int value)
2318+
{
2319+
if (memberExpression == null)
2320+
{
2321+
throw new ArgumentNullException("memberExpression");
2322+
}
2323+
2324+
var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
2325+
var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
2326+
_updateBuilder = _updateBuilder.BitwiseXor(serializationInfo.ElementName, value);
2327+
return this;
2328+
}
2329+
2330+
/// <summary>
2331+
/// Sets the named element to the bitwise xor of its value with another value (see $bit with "xor").
2332+
/// </summary>
2333+
/// <param name="memberExpression">The member expression.</param>
2334+
/// <param name="value">The value to be xor-ed with the current value.</param>
2335+
/// <returns>
2336+
/// The builder (so method calls can be chained).
2337+
/// </returns>
2338+
public UpdateBuilder<TDocument> BitwiseXor(Expression<Func<TDocument, long>> memberExpression, long value)
2339+
{
2340+
if (memberExpression == null)
2341+
{
2342+
throw new ArgumentNullException("memberExpression");
2343+
}
2344+
2345+
var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
2346+
var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
2347+
_updateBuilder = _updateBuilder.BitwiseXor(serializationInfo.ElementName, value);
2348+
return this;
2349+
}
2350+
22352351
/// <summary>
22362352
/// Combines another UpdateBuilder into this one.
22372353
/// </summary>

MongoDB.DriverUnitTests/Builders/UpdateBuilderTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,54 @@ public void TestBitwiseOrLongTwice()
231231
Assert.AreEqual(expected, update.ToJson());
232232
}
233233

234+
[Test]
235+
public void TestBitwiseXorInt()
236+
{
237+
var update = Update.BitwiseXor("name", 1);
238+
var expected = "{ '$bit' : { 'name' : { 'xor' : 1 } } }".Replace("'", "\"");
239+
Assert.AreEqual(expected, update.ToJson());
240+
}
241+
242+
[Test]
243+
public void TestBitwiseXorInt_Typed()
244+
{
245+
var update = Update<Test>.BitwiseXor(t => t.X, 1);
246+
var expected = "{ '$bit' : { 'x' : { 'xor' : 1 } } }".Replace("'", "\"");
247+
Assert.AreEqual(expected, update.ToJson());
248+
}
249+
250+
[Test]
251+
public void TestBitwiseXorIntTwice()
252+
{
253+
var update = Update.BitwiseXor("x", 1).BitwiseXor("y", 2);
254+
var expected = "{ '$bit' : { 'x' : { 'xor' : 1 }, 'y' : { 'xor' : 2 } } }".Replace("'", "\"");
255+
Assert.AreEqual(expected, update.ToJson());
256+
}
257+
258+
[Test]
259+
public void TestBitwiseXorLong()
260+
{
261+
var update = Update.BitwiseXor("name", 1L);
262+
var expected = "{ '$bit' : { 'name' : { 'xor' : NumberLong(1) } } }".Replace("'", "\"");
263+
Assert.AreEqual(expected, update.ToJson());
264+
}
265+
266+
[Test]
267+
public void TestBitwiseXorLong_Typed()
268+
{
269+
var update = Update<Test>.BitwiseXor(t => t.XL, 1L);
270+
var expected = "{ '$bit' : { 'xl' : { 'xor' : NumberLong(1) } } }".Replace("'", "\"");
271+
Assert.AreEqual(expected, update.ToJson());
272+
}
273+
274+
[Test]
275+
public void TestBitwiseXorLongTwice()
276+
{
277+
var update = Update.BitwiseXor("x", 1L).BitwiseXor("y", 2L);
278+
var expected = "{ '$bit' : { 'x' : { 'xor' : NumberLong(1) }, 'y' : { 'xor' : NumberLong(2) } } }".Replace("'", "\"");
279+
Assert.AreEqual(expected, update.ToJson());
280+
}
281+
234282
[Test]
235283
public void TestCombineIncSet()
236284
{

0 commit comments

Comments
 (0)