-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathRenderArgs.cs
134 lines (121 loc) · 5.94 KB
/
RenderArgs.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 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.Serialization;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver
{
/// <summary>
/// Encapsulates settings needed for path rendering.
/// </summary>
public record struct PathRenderArgs(string PathPrefix = null, bool AllowScalarValueForArray = false)
{
}
/// <summary>
/// Encapsulates settings needed for rendering Builder definitions.
/// </summary>
/// <typeparam name="TDocument">The type of the document.</typeparam>
public record struct RenderArgs<TDocument>
{
private readonly IBsonSerializer<TDocument> _documentSerializer = default;
private readonly PathRenderArgs _pathRenderArgs = default;
private readonly bool _renderDollarForm = default;
private readonly bool _renderForElemMatch = false;
private readonly bool _renderForFind = false;
private readonly IBsonSerializerRegistry _serializerRegistry = default;
private readonly ExpressionTranslationOptions _translationOptions = default;
/// <summary>
/// Initializes a new instance of the <see cref="RenderArgs{TDocument}"/> record.
/// </summary>
/// <param name="documentSerializer">The document serializer.</param>
/// <param name="serializerRegistry">The serializer registry.</param>
/// <param name="pathRenderArgs">The path render arguments.</param>
/// <param name="renderDollarForm">Value that specifies whether full dollar for should be rendered.</param>
/// <param name="renderForFind">Value that specifies whether rendering a find operation.</param>
/// <param name="renderForElemMatch">Value that specifies whether rendering an $elemMatch.</param>
/// <param name="translationOptions">The translation options.</param>
public RenderArgs(
IBsonSerializer<TDocument> documentSerializer,
IBsonSerializerRegistry serializerRegistry,
PathRenderArgs pathRenderArgs = default,
bool renderDollarForm = default,
bool renderForFind = false,
bool renderForElemMatch = false,
ExpressionTranslationOptions translationOptions = null)
{
DocumentSerializer = documentSerializer;
PathRenderArgs = pathRenderArgs;
SerializerRegistry = serializerRegistry;
RenderDollarForm = renderDollarForm;
_renderForFind = renderForFind;
_renderForElemMatch = renderForElemMatch;
_translationOptions = translationOptions; // can be null
}
/// <summary>
/// Gets the document serializer.
/// </summary>
public readonly IBsonSerializer<TDocument> DocumentSerializer
{
get => _documentSerializer;
init => _documentSerializer = Ensure.IsNotNull(value, nameof(value));
}
/// <summary>
/// Gets the value indicating whether Render is being called for ElemMatch.
/// </summary>
public readonly bool RenderForElemMatch { get => _renderForElemMatch; init => _renderForElemMatch = value; }
/// <summary>
/// Gets the value indicating whether Render is being called for Find.
/// </summary>
public readonly bool RenderForFind { get => _renderForFind; init => _renderForFind = value; }
/// <summary>
/// Gets the path render arguments.
/// </summary>
public readonly PathRenderArgs PathRenderArgs { get => _pathRenderArgs; init => _pathRenderArgs = value; }
/// <summary>
/// Gets the value indicating whether full dollar form should be rendered.
/// </summary>
public readonly bool RenderDollarForm { get => _renderDollarForm; init => _renderDollarForm = value; }
/// <summary>
/// Gets the serializer registry.
/// </summary>
public readonly IBsonSerializerRegistry SerializerRegistry
{
get => _serializerRegistry;
init => _serializerRegistry = Ensure.IsNotNull(value, nameof(value));
}
/// <summary>
/// Gets the translation options used when translation Expressions to MQL.
/// </summary>
public readonly ExpressionTranslationOptions TranslationOptions
{
get => _translationOptions;
init => _translationOptions = value;
}
/// <summary>
/// Returns <see cref="DocumentSerializer"/> if it implements <c>IBsonSerializer{T}</c>
/// or resolves <c>IBsonSerializer{T}</c> from <see cref="SerializerRegistry"/>.
/// </summary>
public readonly IBsonSerializer<T> GetSerializer<T>() =>
_documentSerializer as IBsonSerializer<T> ?? SerializerRegistry.GetSerializer<T>();
/// <summary>
/// Creates a new RenderArgs with new document type {TNewDocument}
/// </summary>
/// <param name="serializer">The new serializer.</param>
/// <returns>
/// A new RenderArgs{TNewDocument} instance.
/// </returns>
public readonly RenderArgs<TNewDocument> WithNewDocumentType<TNewDocument>(IBsonSerializer<TNewDocument> serializer) =>
new(serializer, _serializerRegistry, _pathRenderArgs, _renderDollarForm, _renderForFind, _renderForElemMatch, _translationOptions);
}
}