forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstConvertExpression.cs
102 lines (94 loc) · 3.69 KB
/
AstConvertExpression.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* 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.Driver.Core.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
{
internal sealed class AstConvertExpression : AstExpression
{
private readonly ByteOrder? _byteOrder;
private readonly string _format;
private readonly AstExpression _input;
private readonly AstExpression _onError;
private readonly AstExpression _onNull;
private readonly BsonBinarySubType? _subType;
private readonly AstExpression _to;
public AstConvertExpression(
AstExpression input,
AstExpression to,
BsonBinarySubType? subType = null,
ByteOrder? byteOrder = null,
string format = null,
AstExpression onError = null,
AstExpression onNull = null)
{
_input = Ensure.IsNotNull(input, nameof(input));
_to = Ensure.IsNotNull(to, nameof(to));
_subType = subType;
_byteOrder = byteOrder;
_format = format;
_onError = onError;
_onNull = onNull;
}
public ByteOrder? ByteOrder => _byteOrder;
public string Format => _format;
public AstExpression Input => _input;
public override AstNodeType NodeType => AstNodeType.ConvertExpression;
public AstExpression OnError => _onError;
public AstExpression OnNull => _onNull;
public BsonBinarySubType? SubType => _subType;
public AstExpression To => _to;
public override AstNode Accept(AstNodeVisitor visitor)
{
return visitor.VisitConvertExpression(this);
}
public override BsonValue Render()
{
return new BsonDocument
{
{ "$convert", new BsonDocument
{
{ "input", _input.Render() },
{ "to", _to.Render(), _subType == null },
{ "to", () => new BsonDocument
{
{ "type", _to.Render() },
{ "subtype", (int)_subType!.Value},
},
_subType != null
},
{ "onError", () => _onError.Render(), _onError != null },
{ "onNull", () => _onNull.Render(), _onNull != null },
{ "format", () => _format, _format != null },
{ "byteOrder", () => _byteOrder!.Value.Render(), _byteOrder != null }
}
}
};
}
public AstConvertExpression Update(
AstExpression input,
AstExpression to,
AstExpression onError,
AstExpression onNull)
{
if (input == _input && to == _to && onError == _onError && onNull == _onNull)
{
return this;
}
return new AstConvertExpression(input, to, _subType, _byteOrder, _format, onError, onNull);
}
}
}