BaseObject() public static method

public static BaseObject ( object value ) : object
value object
return object
Example #1
0
        public static BsonDocument NewDocumentWithId(bool newId, object id, object input)
        {
            if (newId && id != null)
            {
                throw new PSInvalidOperationException("Parameters Id and NewId cannot be used together.");
            }

            if (newId)
            {
                return(new BsonDocument(BsonId.Element(new BsonObjectId(ObjectId.GenerateNewId()))));
            }

            if (id == null)
            {
                return(null);
            }

            id = Actor.BaseObject(id);
            if (!(id is ScriptBlock sb))
            {
                return(new BsonDocument(BsonId.Element(BsonValue.Create(id))));
            }

            var arr = Actor.InvokeScript(sb, input);

            if (arr.Count != 1)
            {
                throw new ArgumentException("-Id script must return a single object.");                 //! use this type
            }
            return(new BsonDocument(BsonId.Element(BsonValue.Create(arr[0].BaseObject))));
        }
Example #2
0
        public Dictionary(object value)
        {
            if (Environment.GetEnvironmentVariable("MdbcDictionaryLegacy") == "0")
            {
                throw new InvalidOperationException("Used deprecated Mdbc.Dictionary(object)");
            }

            value = Actor.BaseObject(value);
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (value is Dictionary that)
            {
                _document = (BsonDocument)that._document.DeepClone();
            }
            else
            {
                var bson = Actor.ToBsonValue(value);
                if (bson.BsonType == BsonType.Document)
                {
                    _document = bson.AsBsonDocument;
                }
                else
                {
                    _document = new BsonDocument(BsonId.Element(bson));
                }
            }
        }
Example #3
0
File: Api.cs Project: 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);
        }
Example #4
0
File: Api.cs Project: n0tspam/Mdbc
        public static FilterDefinition <BsonDocument> FilterDefinitionOfInputId(object value)
        {
            value = Actor.BaseObject(value, out PSObject custom);
            if (custom == null)
            {
                if (value is IConvertibleToBsonDocument cd)
                {
                    var doc = cd.ToBsonDocument();
                    if (doc.TryGetElement(BsonId.Name, out BsonElement elem))
                    {
                        return(new BsonDocument(elem));
                    }
                    else
                    {
                        throw new InvalidOperationException(Res.InputDocId);
                    }
                }

                if (value is IDictionary dic)
                {
                    if (!dic.Contains(BsonId.Name))
                    {
                        throw new InvalidOperationException(Res.InputDocId);
                    }

                    var id = dic[BsonId.Name];
                    return(FilterDefinitionOfId(id));
                }

                if (Actor.TypeIsDriverSerialized(value.GetType()))
                {
                    var cm = BsonClassMap.LookupClassMap(value.GetType());
                    var mm = cm.GetMemberMapForElement(BsonId.Name);
                    var id = mm.Getter(value);
                    return(FilterDefinitionOfId(id));
                }
            }

            var ps = custom ?? PSObject.AsPSObject(value);
            var pi = ps.Properties[BsonId.Name];

            if (pi == null)
            {
                throw new InvalidOperationException(Res.InputDocId);
            }
            else
            {
                return(FilterDefinitionOfId(pi.Value));
            }
        }
Example #5
0
        public Collection(object value)
        {
            value = Actor.BaseObject(value);
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (value is IEnumerable en && !(value is string))
            {
                if (en is Collection that)
                {
                    _array = (BsonArray)that._array.DeepClone();
                }
                else
                {
                    _array = new BsonArray();
                    foreach (var item in en)
                    {
                        _array.Add(Actor.ToBsonValue(item));
                    }
                }
            }
Example #6
0
File: Api.cs Project: n0tspam/Mdbc
 public static FilterDefinition <BsonDocument> FilterDefinitionOfId(object value)
 {
     value = Actor.BaseObject(value);
     return(Builders <BsonDocument> .Filter.Eq(BsonId.Name, BsonValue.Create(value)));
 }