Exemplo n.º 1
0
        public void ConvertDatum()
        {
            var datum = new Datum()
            {
                type  = Datum.DatumType.R_NUM,
                r_num = 60.123,
            };
            var ts = datumConverter.ConvertDatum(datum);

            Assert.That(ts, Is.EqualTo(TimeSpan.FromSeconds(60.123)));
        }
Exemplo n.º 2
0
        public void ConvertDatum()
        {
            var datum = new Datum()
            {
                type  = Datum.DatumType.R_NUM,
                r_num = 5
            };
            var value = datumConverter.ConvertDatum(datum);

            Assert.That(value.HasValue);
        }
Exemplo n.º 3
0
        public void ConvertDatumToObjectToDatum_Simple()
        {
            var datum1 = new RethinkDb.Spec.Datum()
            {
                type  = RethinkDb.Spec.Datum.DatumType.R_STR,
                r_str = "http://www.example.com/"
            };
            var obj    = converter.ConvertDatum(datum1);
            var datum2 = converter.ConvertObject(obj);

            Assert.That(datum2.type, Is.EqualTo(datum1.type));
            Assert.That(datum2.r_str, Is.EqualTo(datum1.r_str));
        }
        public override Dictionary <string, TValue> ConvertDatum(Spec.Datum datum)
        {
            if (datum.type == Spec.Datum.DatumType.R_NULL)
            {
                return(null);
            }

            if (datum.type != RethinkDb.Spec.Datum.DatumType.R_OBJECT)
            {
                throw new NotSupportedException("Attempted to convert Datum to named-value dictionary, but Datum was unsupported type " + datum.type);
            }

            Dictionary <string, TValue> retval = new Dictionary <string, TValue>();

            IDatumConverter valueConverter = null;

            if (typeof(TValue) != typeof(object))
            {
                valueConverter = rootDatumConverterFactory.Get <TValue>();
            }

            foreach (var kvp in datum.r_object)
            {
                IDatumConverter thisValueConverter = valueConverter;
                if (thisValueConverter == null)
                {
                    Type valueType = rootDatumConverterFactory.GetBestNativeTypeForDatum(kvp.val);
                    thisValueConverter = GetConverter(valueType);
                }
                retval[kvp.key] = (TValue)thisValueConverter.ConvertDatum(kvp.val);
            }

            return(retval);
        }
 public override IList <T> ConvertDatum(Datum datum)
 {
     if (datum.type == Spec.Datum.DatumType.R_NULL)
     {
         return(null);
     }
     return(new List <T>(arrayDatumConverter.ConvertDatum(datum)));
 }
 public Nullable <T> ConvertDatum(Spec.Datum datum)
 {
     if (datum.type == Spec.Datum.DatumType.R_NULL)
     {
         return(new Nullable <T>());
     }
     else
     {
         return(new Nullable <T>(innerConverter.ConvertDatum(datum)));
     }
 }
        public override IGroupingDictionary <TKey, TValue> ConvertDatum(Datum datum)
        {
            if (datum.type == Datum.DatumType.R_NULL)
            {
                return(null);
            }
            else if (datum.type == Datum.DatumType.R_OBJECT)
            {
                var keys = datum.r_object.ToDictionary(kvp => kvp.key, kvp => kvp.val);

                Datum typeDatum;
                if (!keys.TryGetValue("$reql_type$", out typeDatum))
                {
                    throw new NotSupportedException("Object without $reql_type$ key cannot be converted to a dictionary");
                }
                if (typeDatum.type != Datum.DatumType.R_STR || typeDatum.r_str != "GROUPED_DATA")
                {
                    throw new NotSupportedException("Object without $reql_type$ = GROUPED_DATA cannot be converted to a dictionary");
                }

                Datum dataDatum;
                if (!keys.TryGetValue("data", out dataDatum))
                {
                    throw new NotSupportedException("Object without data key cannot be converted to a dictionary");
                }
                if (dataDatum.type != Datum.DatumType.R_ARRAY)
                {
                    throw new NotSupportedException("Object's data key must be an array type");
                }

                var retval = new GroupingDictionary <TKey, TValue>(dataDatum.r_array.Count);
                foreach (var item in dataDatum.r_array)
                {
                    if (item.type != Datum.DatumType.R_ARRAY || item.r_array.Count != 2)
                    {
                        throw new NotSupportedException("GROUPED_DATA data is expected to contain array elements of two items, a key and a value");
                    }
                    var key   = keyTypeConverter.ConvertDatum(item.r_array[0]);
                    var value = valueTypeConverter.ConvertDatum(item.r_array[1]);
                    retval[key] = value;
                }

                return(retval);
            }
            else
            {
                throw new NotSupportedException("Attempted to cast Datum to array, but Datum was unsupported type " + datum.type);
            }
        }
Exemplo n.º 8
0
 public override T ConvertDatum(Spec.Datum datum)
 {
     if (datum.type == Spec.Datum.DatumType.R_NULL)
     {
         return(default(T));
     }
     else if (datum.type == Spec.Datum.DatumType.R_ARRAY)
     {
         var retval = Array.CreateInstance(typeof(T).GetElementType(), datum.r_array.Count);
         for (int i = 0; i < datum.r_array.Count; i++)
         {
             retval.SetValue(arrayTypeConverter.ConvertDatum(datum.r_array [i]), i);
         }
         return((T)Convert.ChangeType(retval, typeof(T)));
     }
     else
     {
         throw new NotSupportedException("Attempted to cast Datum to array, but Datum was unsupported type " + datum.type);
     }
 }
Exemplo n.º 9
0
        public void FieldDataContractConvertDatum()
        {
            var datum = new Datum()
            {
                type = Datum.DatumType.R_OBJECT
            };

            datum.r_object.Add(new Datum.AssocPair()
            {
                key = "name",
                val = new Datum()
                {
                    type  = Datum.DatumType.R_STR,
                    r_str = "Jackpot!",
                }
            });

            var obj = testObject2Converter.ConvertDatum(datum);

            Assert.That(obj, Is.Not.Null);
            Assert.That(obj.Id, Is.EqualTo(0));
            Assert.That(obj.Name, Is.EqualTo("Jackpot!"));
        }