/* Copyright 2016-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 System.Linq.Expressions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Linq.Translators;
namespace MongoDB.Driver
{
///
/// An aggregation expression.
///
/// The type of the source.
/// The type of the result.
public abstract class AggregateExpressionDefinition
{
#region static
// public implicit conversions
///
/// Performs an implicit conversion from to .
///
/// The expression.
///
/// The result of the conversion.
///
public static implicit operator AggregateExpressionDefinition(BsonValue expression)
{
Ensure.IsNotNull(expression, nameof(expression));
return new BsonValueAggregateExpressionDefinition(expression);
}
///
/// Performs an implicit conversion from to .
///
/// The expression.
///
/// The result of the conversion.
///
public static implicit operator AggregateExpressionDefinition(string expression)
{
Ensure.IsNotNullOrEmpty(expression, nameof(expression));
if (expression[0] == '{')
{
return new BsonValueAggregateExpressionDefinition(BsonDocument.Parse(expression));
}
else
{
return new BsonValueAggregateExpressionDefinition(new BsonString(expression));
}
}
#endregion
///
/// Renders the aggregation expression.
///
/// The source serializer.
/// The serializer registry.
/// The rendered aggregation expression.
public abstract BsonValue Render(IBsonSerializer sourceSerializer, IBsonSerializerRegistry serializerRegistry);
}
///
/// A based aggregate expression.
///
/// The type of the source.
/// The type of the result.
///
public sealed class BsonValueAggregateExpressionDefinition : AggregateExpressionDefinition
{
// private fields
private readonly BsonValue _expression;
// constructors
///
/// Initializes a new instance of the class.
///
/// The expression.
public BsonValueAggregateExpressionDefinition(BsonValue expression)
{
_expression = Ensure.IsNotNull(expression, nameof(expression));
}
// public methods
///
public override BsonValue Render(IBsonSerializer sourceSerializer, IBsonSerializerRegistry serializerRegistry)
{
return _expression;
}
}
///
/// A based aggregate expression.
///
/// The type of the source.
/// The type of the result.
///
public sealed class ExpressionAggregateExpressionDefinition : AggregateExpressionDefinition
{
// private fields
private readonly Expression> _expression;
private readonly ExpressionTranslationOptions _translationOptions;
// constructors
///
/// Initializes a new instance of the class.
///
/// The expression.
/// The translation options.
public ExpressionAggregateExpressionDefinition(Expression> expression, ExpressionTranslationOptions translationOptions)
{
_expression = Ensure.IsNotNull(expression, nameof(expression));
_translationOptions = translationOptions; // can be null
}
// public methods
///
public override BsonValue Render(IBsonSerializer sourceSerializer, IBsonSerializerRegistry serializerRegistry)
{
return AggregateExpressionTranslator.Translate(_expression, sourceSerializer, serializerRegistry, _translationOptions);
}
}
}