ToBsonDocumentFromDictionary() static private method

static private ToBsonDocumentFromDictionary ( BsonDocument source, IDictionary dictionary, Mdbc.DocumentInput input, IEnumerable properties, int depth ) : BsonDocument
source BsonDocument
dictionary IDictionary
input Mdbc.DocumentInput
properties IEnumerable
depth int
return BsonDocument
示例#1
0
文件: Api.cs 项目: n0tspam/Mdbc
        /// <summary>
        /// Null, empty string, JSON, IConvertibleToBsonDocument, IDictionary.
        /// </summary>
        // Used for optional -Filter, -Sort, -Project. They may be omitted,
        // nulls or empty strings (e.g. nulls converted to strings by PS).
        static bool TryBsonDocumentOrNull(object value, out BsonDocument result)
        {
            if (value == null)
            {
                result = null;
                return(true);
            }

            value = Actor.BaseObject(value);

            if (value is string json)
            {
                result = json.Length == 0 ? null : MyJson.ToBsonDocument(json);
                return(true);
            }

            //! before IDictionary, mind Mdbc.Dictionary
            if (value is IConvertibleToBsonDocument cd)
            {
                result = cd.ToBsonDocument();
                return(true);
            }

            //! after IConvertibleToBsonDocument
            if (value is IDictionary dictionary)
            {
                result = Actor.ToBsonDocumentFromDictionary(dictionary);
                return(true);
            }

            result = null;
            return(false);
        }
示例#2
0
文件: Api.cs 项目: n0tspam/Mdbc
        /// <summary>
        /// JSON, IConvertibleToBsonDocument, IDictionary.
        /// </summary>
        static bool TryBsonDocument(object value, out BsonDocument result)
        {
            // unwrap, it may be PSObject item of PS array, see _191103_084410
            value = BaseObjectNotNull(value);

            if (value is string json)
            {
                result = MyJson.ToBsonDocument(json);
                return(true);
            }

            //! before IDictionary, mind Mdbc.Dictionary
            if (value is IConvertibleToBsonDocument cd)
            {
                result = cd.ToBsonDocument();
                return(true);
            }

            //! after IConvertibleToBsonDocument
            if (value is IDictionary dictionary)
            {
                result = Actor.ToBsonDocumentFromDictionary(dictionary);
                return(true);
            }

            result = null;
            return(false);
        }
示例#3
0
文件: Api.cs 项目: n0tspam/Mdbc
        public static PipelineDefinition <BsonDocument, BsonDocument> PipelineDefinition(object value)
        {
            value = BaseObjectNotNull(value);

            // JSON first because it looks very convenient for pipelines
            if (value is string json)
            {
                var bson = MyJson.ToBsonValue(json);
                switch (bson.BsonType)
                {
                case BsonType.Array:
                    return(bson.AsBsonArray.Select(x => x.AsBsonDocument).ToList());

                case BsonType.Document:
                    return(new BsonDocument[] { bson.AsBsonDocument });

                default:
                    throw new ArgumentException($"JSON: expected array or document, found {bson.BsonType}.");
                }
            }

            //! before IDictionary, mind Mdbc.Dictionary
            if (value is IConvertibleToBsonDocument cd)
            {
                return new BsonDocument[] { cd.ToBsonDocument() }
            }
            ;

            //! after IConvertibleToBsonDocument
            if (value is IDictionary dictionary)
            {
                return new BsonDocument[] { Actor.ToBsonDocumentFromDictionary(dictionary) }
            }
            ;

            // PS arrays or other collections
            if (value is IEnumerable en)
            {
                var list = new List <BsonDocument>();

                foreach (var it in en)
                {
                    list.Add(BsonDocument(it));
                }
                return(list);
            }

            // either PipelineDefinition (unlikely but possible) or "Cannot cast X to Y"
            return((PipelineDefinition <BsonDocument, BsonDocument>)value);
        }
示例#4
0
        public Dictionary(IDictionary document)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document is Dictionary that)
            {
                _document = (BsonDocument)that._document.DeepClone();
            }
            else
            {
                _document = Actor.ToBsonDocumentFromDictionary(document);
            }
        }
示例#5
0
文件: Api.cs 项目: n0tspam/Mdbc
        /// <summary>
        /// JSON, IConvertibleToBsonDocument, IDictionary.
        /// </summary>
        internal static BsonDocument BsonDocument(object value)
        {
            // unwrap, it may be PSObject item of PS array, see _191103_084410
            value = BaseObjectNotNull(value);

            if (value is string json)
            {
                return(MyJson.ToBsonDocument(json));
            }

            //! before IDictionary, mind Mdbc.Dictionary
            if (value is IConvertibleToBsonDocument cd)
            {
                return(cd.ToBsonDocument());
            }

            //! after IConvertibleToBsonDocument
            if (value is IDictionary dictionary)
            {
                return(Actor.ToBsonDocumentFromDictionary(dictionary));
            }

            throw new InvalidOperationException(Res.CannotConvert2(value.GetType(), nameof(BsonDocument)));
        }