-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5552: Add support for $convert in LINQ #1659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 76 commits
b11abcb
e51d733
7484967
d604e2a
e46ebc6
9ff2347
3d55596
ebe1bf5
20763ce
64b0082
be829c9
4007671
9b40d91
9e6c11a
f1cc942
6b3fb54
f25e15b
73fe700
94651f8
82b3f02
6bfc029
23556b3
76dd221
6b6be70
311832d
34ef476
5f1732f
b17c354
37ab91e
54395c1
5e5701e
2b16ebb
40276ed
16887f9
c95d8a8
8c4dbf8
22691b4
cfff9b3
a341cfe
16ec0b1
698690f
10f05e6
98fd0e1
021f4f0
d7bca77
f94891c
0664eb1
e4c185b
3af159e
23ddc74
6829b66
b237d0d
555bbd9
fd9626c
403c02a
69cf61b
8e8eea5
6e87398
fb5509e
3ee0f42
705f5b3
2a0829b
53b886a
37cb373
9b88903
5fa5850
03c3974
9aeabb3
07de4e5
39904d0
e4fc2a7
dc2841e
276434d
23de869
d844cd2
ec52d7f
f8a5a66
abda353
e2f2f6a
afa7bdd
de05dd6
3b19233
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Serializers; | ||
|
||
namespace MongoDB.Driver | ||
{ | ||
/// <summary> | ||
/// Represents the options parameter for <see cref="Mql.Convert{TFrom, TTo}(TFrom, ConvertOptions{TTo})"/>. | ||
/// </summary> | ||
public abstract class ConvertOptions | ||
{ | ||
private ByteOrder? _byteOrder; | ||
private string _format; | ||
private BsonBinarySubType? _subType; | ||
|
||
/// <summary> | ||
/// The byteOrder parameter. | ||
/// </summary> | ||
public ByteOrder? ByteOrder | ||
{ | ||
get => _byteOrder; | ||
set => _byteOrder = value; | ||
} | ||
|
||
/// <summary> | ||
/// The format parameter. | ||
/// </summary> | ||
public string Format | ||
{ | ||
get => _format; | ||
set => _format = value; | ||
} | ||
|
||
/// <summary> | ||
/// The subType parameter. | ||
/// </summary> | ||
public BsonBinarySubType? SubType | ||
{ | ||
get => _subType; | ||
set => _subType = value; | ||
} | ||
|
||
internal abstract bool OnErrorWasSet(out object onError); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would make sense. |
||
|
||
internal abstract bool OnNullWasSet(out object onError); | ||
papafe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// <summary> | ||
/// Represents the options parameter for <see cref="Mql.Convert{TFrom, TTo}(TFrom, ConvertOptions{TTo})"/>. | ||
/// This class allows to set 'onError' and 'onNull'. | ||
/// </summary> | ||
/// <typeparam name="TTo"> The type of 'onError' and 'onNull'.</typeparam> | ||
public class ConvertOptions<TTo> : ConvertOptions | ||
{ | ||
private TTo _onError; | ||
private bool _onErrorWasSet; | ||
private TTo _onNull; | ||
private bool _onNullWasSet; | ||
private readonly IBsonSerializer _serializer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you forgot to remove the serializer here. |
||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConvertOptions{TTo}"/> class. | ||
/// </summary> | ||
public ConvertOptions() | ||
{ | ||
_serializer = StandardSerializers.TryGetSerializer(typeof(TTo), out var serializer) | ||
? serializer | ||
: BsonSerializer.LookupSerializer(typeof(TTo)); | ||
} | ||
|
||
/// <summary> | ||
/// The onError parameter. | ||
/// </summary> | ||
public TTo OnError | ||
{ | ||
get => _onError; | ||
set | ||
{ | ||
_onError = value; | ||
_onErrorWasSet = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The onNull parameter. | ||
/// </summary> | ||
public TTo OnNull | ||
{ | ||
get => _onNull; | ||
set | ||
{ | ||
_onNull = value; | ||
_onNullWasSet = true; | ||
} | ||
} | ||
|
||
internal override bool OnErrorWasSet(out object onError) | ||
{ | ||
onError = _onError; | ||
return _onErrorWasSet; | ||
} | ||
|
||
internal override bool OnNullWasSet(out object onNull) | ||
{ | ||
onNull = _onNull; | ||
return _onNullWasSet; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Represents the byte order of binary data when converting to/from numerical types using <see cref="Mql.Convert{TFrom, TTo}(TFrom, ConvertOptions{TTo})"/>. | ||
/// </summary> | ||
public enum ByteOrder | ||
{ | ||
/// <summary> | ||
/// Big endian order. | ||
/// </summary> | ||
BigEndian, | ||
/// <summary> | ||
/// Little endian order. | ||
/// </summary> | ||
LittleEndian, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using MongoDB.Bson; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast | ||
{ | ||
internal static class AstEnumExtensions | ||
{ | ||
public static string Render(this BsonType type) | ||
{ | ||
return type switch | ||
{ | ||
BsonType.Array => "array", | ||
BsonType.Binary => "binData", | ||
BsonType.Boolean => "bool", | ||
BsonType.DateTime => "date", | ||
BsonType.Decimal128 => "decimal", | ||
BsonType.Document => "object", | ||
BsonType.Double => "double", | ||
BsonType.Int32 => "int", | ||
BsonType.Int64 => "long", | ||
BsonType.JavaScript => "javascript", | ||
BsonType.JavaScriptWithScope => "javascriptWithScope", | ||
BsonType.MaxKey => "maxKey", | ||
BsonType.MinKey => "minKey", | ||
BsonType.Null => "null", | ||
BsonType.ObjectId => "objectId", | ||
BsonType.RegularExpression => "regex", | ||
BsonType.String => "string", | ||
BsonType.Symbol => "symbol", | ||
BsonType.Timestamp => "timestamp", | ||
BsonType.Undefined => "undefined", | ||
_ => throw new ArgumentException($"Unexpected BSON type: {type}.", nameof(type)) | ||
}; | ||
} | ||
|
||
public static string Render(this ByteOrder byteOrder) | ||
{ | ||
return byteOrder switch | ||
{ | ||
ByteOrder.BigEndian => "big", | ||
ByteOrder.LittleEndian => "little", | ||
_ => throw new ArgumentException($"Unexpected {nameof(ByteOrder)}: {byteOrder}.", nameof(byteOrder)) | ||
}; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.