/* 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; using MongoDB.Bson.Serialization; using MongoDB.Driver.Core.Misc; namespace MongoDB.Driver { /// /// Base class for updates. /// /// The type of the document. public abstract class UpdateDefinition { /// /// Renders the update to a . /// /// The document serializer. /// The serializer registry. /// A . public abstract BsonDocument Render(IBsonSerializer documentSerializer, IBsonSerializerRegistry serializerRegistry); /// /// Performs an implicit conversion from to . /// /// The document. /// /// The result of the conversion. /// public static implicit operator UpdateDefinition(BsonDocument document) { if (document == null) { return null; } return new BsonDocumentUpdateDefinition(document); } /// /// Performs an implicit conversion from to . /// /// The JSON string. /// /// The result of the conversion. /// public static implicit operator UpdateDefinition(string json) { if (json == null) { return null; } return new JsonUpdateDefinition(json); } } /// /// A based update. /// /// The type of the document. public sealed class BsonDocumentUpdateDefinition : UpdateDefinition { private readonly BsonDocument _document; /// /// Initializes a new instance of the class. /// /// The document. public BsonDocumentUpdateDefinition(BsonDocument document) { _document = Ensure.IsNotNull(document, nameof(document)); } /// /// Gets the document. /// public BsonDocument Document { get { return _document; } } /// public override BsonDocument Render(IBsonSerializer documentSerializer, IBsonSerializerRegistry serializerRegistry) { return _document; } } /// /// A JSON based update. /// /// The type of the document. public sealed class JsonUpdateDefinition : UpdateDefinition { private readonly string _json; /// /// Initializes a new instance of the class. /// /// The json. public JsonUpdateDefinition(string json) { _json = Ensure.IsNotNullOrEmpty(json, nameof(json)); } /// /// Gets the json. /// public string Json { get { return _json; } } /// public override BsonDocument Render(IBsonSerializer documentSerializer, IBsonSerializerRegistry serializerRegistry) { return BsonDocument.Parse(_json); } } /// /// An based update. /// /// The type of the document. public sealed class ObjectUpdateDefinition : UpdateDefinition { private readonly object _obj; /// /// Initializes a new instance of the class. /// /// The object. public ObjectUpdateDefinition(object obj) { _obj = Ensure.IsNotNull(obj, nameof(obj)); } /// /// Gets the object. /// public object Object { get { return _obj; } } /// public override BsonDocument Render(IBsonSerializer documentSerializer, IBsonSerializerRegistry serializerRegistry) { var serializer = serializerRegistry.GetSerializer(_obj.GetType()); return new BsonDocumentWrapper(_obj, serializer); } } }