/* 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.Misc;
namespace MongoDB.Driver
{
///
/// A definition of a single field to be set.
///
/// The type of the document.
public abstract class SetFieldDefinition
{
///
/// Renders the SetFieldDefinition.
///
/// The render arguments.
/// The rendered SetFieldDefinition.
public abstract BsonElement Render(RenderArgs args);
}
///
/// A SetFieldDefinition that uses a field and a a constant to define the field to be set.
///
/// The type of the document.
/// The type of the field.
public sealed class ConstantSetFieldDefinition : SetFieldDefinition
{
// private fields
private readonly FieldDefinition _field;
private readonly TField _value;
// public constructors
///
/// Initializes an instance of ConstantSetFieldDefinition.
///
/// The field.
/// The value.
public ConstantSetFieldDefinition(FieldDefinition field, TField value)
{
_field = Ensure.IsNotNull(field, nameof(field));
_value = value;
}
// public methods
///
public override BsonElement Render(RenderArgs args)
{
var renderedField = _field.Render(args);
var serializedValue = SerializationHelper.SerializeValue(renderedField.ValueSerializer, _value);
return new BsonElement(renderedField.FieldName, serializedValue);
}
}
}