Exemplo n.º 1
0
        public unsafe void TestReadOnlyDictionary()
        {
            var now         = DateTimeOffset.UtcNow;
            var nestedArray = new[] { 1L, 2L, 3L };
            var nestedDict  = new Dictionary <string, object> {
                ["foo"] = "bar"
            };
            var masterData = new Dictionary <string, object>
            {
                ["date"]  = now,
                ["array"] = nestedArray,
                ["dict"]  = nestedDict
            };

            var flData = new FLSliceResult();

            Db.InBatch(() =>
            {
                flData = masterData.FLEncode();
            });

            try {
                var context = new DocContext(Db, null);
                using (var mRoot = new MRoot(context)) {
                    mRoot.Context.Should().BeSameAs(context);
                    FLDoc *fleeceDoc = Native.FLDoc_FromResultData(flData,
                                                                   FLTrust.Trusted,
                                                                   Native.c4db_getFLSharedKeys(Db.c4db), FLSlice.Null);
                    var flValue          = Native.FLDoc_GetRoot(fleeceDoc);
                    var mDict            = new MDict(new MValue(flValue), mRoot);
                    var deserializedDict = new DictionaryObject(mDict, false);

                    deserializedDict["bogus"].Blob.Should().BeNull();
                    deserializedDict["date"].Date.Should().Be(now);
                    deserializedDict.GetDate("bogus").Should().Be(DateTimeOffset.MinValue);
                    deserializedDict.GetArray("array").Should().Equal(1L, 2L, 3L);
                    deserializedDict.GetArray("bogus").Should().BeNull();
                    deserializedDict.GetDictionary("dict").Should().BeEquivalentTo(nestedDict);
                    deserializedDict.GetDictionary("bogus").Should().BeNull();

                    var dict = deserializedDict.ToDictionary();
                    dict["array"].As <IList>().Should().Equal(1L, 2L, 3L);
                    dict["dict"].As <IDictionary <string, object> >().Should().BeEquivalentTo(nestedDict);
                    var isContain = mDict.Contains("");
                    isContain.Should().BeFalse();
                    Native.FLDoc_Release(fleeceDoc);
                }
            } finally {
                Native.FLSliceResult_Release(flData);
            }
        }
Exemplo n.º 2
0
        public unsafe void TestReadOnlyDictionary()
        {
            var now         = DateTimeOffset.UtcNow;
            var nestedArray = new[] { 1L, 2L, 3L };
            var nestedDict  = new Dictionary <string, object> {
                ["foo"] = "bar"
            };
            var masterData = new Dictionary <string, object>
            {
                ["date"]  = now,
                ["array"] = nestedArray,
                ["dict"]  = nestedDict
            };

            var flData = new FLSliceResult();

            Db.InBatch(() =>
            {
                flData = masterData.FLEncode();
            });

            try {
                var context = new DocContext(Db, null);
                using (var mRoot = new MRoot(context)) {
                    mRoot.Context.Should().BeSameAs(context);
                    var flValue          = NativeRaw.FLValue_FromTrustedData((FLSlice)flData);
                    var mDict            = new MDict(new MValue(flValue), mRoot);
                    var deserializedDict = new DictionaryObject(mDict, false);

                    deserializedDict["bogus"].Blob.Should().BeNull();
                    deserializedDict["date"].Date.Should().Be(now);
                    deserializedDict.GetDate("bogus").Should().Be(DateTimeOffset.MinValue);
                    deserializedDict.GetArray("array").Should().Equal(1L, 2L, 3L);
                    deserializedDict.GetArray("bogus").Should().BeNull();
                    deserializedDict.GetDictionary("dict").Should().BeEquivalentTo(nestedDict);
                    deserializedDict.GetDictionary("bogus").Should().BeNull();

                    var dict = deserializedDict.ToDictionary();
                    dict["array"].As <IList>().Should().Equal(1L, 2L, 3L);
                    dict["dict"].As <IDictionary <string, object> >().ShouldBeEquivalentTo(nestedDict);
                }
            } finally {
                Native.FLSliceResult_Free(flData);
            }
        }
        protected override DictionaryObject DoPrediction(DictionaryObject input)
        {
            var numbers = input.GetArray("numbers");

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

            var output = new MutableDictionaryObject();

            output.SetLong("sum", numbers.Cast <long>().Sum());
            output.SetLong("min", numbers.Cast <long>().Min());
            output.SetLong("max", numbers.Cast <long>().Max());
            output.SetDouble("avg", numbers.Cast <long>().Average());
            return(output);
        }