/* 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
{
///
/// Encapsulates settings needed for path rendering.
///
public record struct PathRenderArgs(string PathPrefix = null, bool AllowScalarValueForArray = false)
{
}
///
/// Encapsulates settings needed for rendering Builder definitions.
///
/// The type of the document.
public record struct RenderArgs
{
private readonly IBsonSerializer _documentSerializer = default;
private readonly PathRenderArgs _pathRenderArgs = default;
private readonly string _subPathRoot = 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;
///
/// Initializes a new instance of the record.
///
/// The document serializer.
/// The serializer registry.
/// The path render arguments.
/// Value that specifies whether full dollar for should be rendered.
/// Value that specifies whether rendering a find operation.
/// Value that specifies whether rendering an $elemMatch.
/// The translation options.
public RenderArgs(
IBsonSerializer 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
}
///
/// Gets the document serializer.
///
public readonly IBsonSerializer DocumentSerializer
{
get => _documentSerializer;
init => _documentSerializer = Ensure.IsNotNull(value, nameof(value));
}
///
/// Gets the value indicating whether Render is being called for ElemMatch.
///
public readonly bool RenderForElemMatch { get => _renderForElemMatch; init => _renderForElemMatch = value; }
///
/// Gets the value indicating whether Render is being called for Find.
///
public readonly bool RenderForFind { get => _renderForFind; init => _renderForFind = value; }
///
/// Gets the path render arguments.
///
public readonly PathRenderArgs PathRenderArgs { get => _pathRenderArgs; init => _pathRenderArgs = value; }
///
/// Gets the sub-path that should be used for the root of the rendered document.
///
public readonly string SubPathRoot { get => _subPathRoot; init => _subPathRoot = value; }
///
/// Gets the value indicating whether full dollar form should be rendered.
///
public readonly bool RenderDollarForm { get => _renderDollarForm; init => _renderDollarForm = value; }
///
/// Gets the serializer registry.
///
public readonly IBsonSerializerRegistry SerializerRegistry
{
get => _serializerRegistry;
init => _serializerRegistry = Ensure.IsNotNull(value, nameof(value));
}
///
/// Gets the translation options used when translation Expressions to MQL.
///
public readonly ExpressionTranslationOptions TranslationOptions
{
get => _translationOptions;
init => _translationOptions = value;
}
///
/// Returns if it implements IBsonSerializer{T}
/// or resolves IBsonSerializer{T} from .
///
public readonly IBsonSerializer GetSerializer() =>
_documentSerializer as IBsonSerializer ?? SerializerRegistry.GetSerializer();
///
/// Creates a new RenderArgs with new document type {TNewDocument}
///
/// The new serializer.
///
/// A new RenderArgs{TNewDocument} instance.
///
public readonly RenderArgs WithNewDocumentType(IBsonSerializer serializer) =>
new(serializer, _serializerRegistry, _pathRenderArgs, _renderDollarForm, _renderForFind, _renderForElemMatch, _translationOptions);
}
}