public void TestFixtureSetUp()
        {
            var datumConverterFactory = Substitute.For<IDatumConverterFactory>();

            var stringDatum = new Datum() {
                type = Datum.DatumType.R_STR,
                r_str = "Jackpot!",
            };
            var stringDatumConverter = Substitute.For<IDatumConverter<string>>();
            stringDatumConverter
                .ConvertObject("Jackpot!")
                .Returns(stringDatum);
            stringDatumConverter
                .ConvertDatum(Arg.Is<Datum>(d => d.type == stringDatum.type && d.r_str == stringDatum.r_str))
                .Returns("Jackpot!");

            testObject2Converter = DataContractDatumConverterFactory.Instance.Get<TestObject2>(datumConverterFactory);
            testObject4Converter = DataContractDatumConverterFactory.Instance.Get<TestObject4>(datumConverterFactory);
            testObjectStructEmitDefaultValueFieldConverter = DataContractDatumConverterFactory.Instance.Get<TestObjectStructEmitDefaultValueField>(datumConverterFactory);
            testObjectStructEmitDefaultValuePropertyConverter = DataContractDatumConverterFactory.Instance.Get<TestObjectStructEmitDefaultValueProperty>(datumConverterFactory);

            IDatumConverter<string> value;
            datumConverterFactory
                .TryGet<string>(datumConverterFactory, out value)
                .Returns(args => {
                        args[1] = stringDatumConverter;
                        return true;
                    });
        }
Beispiel #2
0
        public void TestFixtureSetUp()
        {
            var datumConverterFactory = Substitute.For <IDatumConverterFactory>();

            var stringDatum = new Datum()
            {
                type  = Datum.DatumType.R_STR,
                r_str = "Jackpot!",
            };
            var stringDatumConverter = Substitute.For <IDatumConverter <string> >();

            stringDatumConverter
            .ConvertObject("Jackpot!")
            .Returns(stringDatum);
            stringDatumConverter
            .ConvertDatum(Arg.Is <Datum>(d => d.type == stringDatum.type && d.r_str == stringDatum.r_str))
            .Returns("Jackpot!");

            testObject2Converter = DataContractDatumConverterFactory.Instance.Get <TestObject2>(datumConverterFactory);
            testObject4Converter = DataContractDatumConverterFactory.Instance.Get <TestObject4>(datumConverterFactory);
            testObjectStructEmitDefaultValueFieldConverter    = DataContractDatumConverterFactory.Instance.Get <TestObjectStructEmitDefaultValueField>(datumConverterFactory);
            testObjectStructEmitDefaultValuePropertyConverter = DataContractDatumConverterFactory.Instance.Get <TestObjectStructEmitDefaultValueProperty>(datumConverterFactory);

            IDatumConverter <string> value;

            datumConverterFactory
            .TryGet <string>(datumConverterFactory, out value)
            .Returns(args => {
                args[1] = stringDatumConverter;
                return(true);
            });
        }
        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 void TestFixtureSetUp()
 {
     datumConverter = new AggregateDatumConverterFactory(
         PrimitiveDatumConverterFactory.Instance,
         NullableDatumConverterFactory.Instance
     ).Get<int?>();
 }
Beispiel #5
0
 public void TestFixtureSetUp()
 {
     datumConverter = new AggregateDatumConverterFactory(
         PrimitiveDatumConverterFactory.Instance,
         NullableDatumConverterFactory.Instance
         ).Get <int?>();
 }
Beispiel #6
0
 public QueryEnumerator(Connection connection, IDatumConverterFactory datumConverterFactory, ISequenceQuery <T> queryObject)
 {
     this.connection            = connection;
     this.datumConverterFactory = datumConverterFactory;
     this.datumConverter        = datumConverterFactory.Get <T>();
     this.queryObject           = queryObject;
     this.stackTrace            = new StackTrace(true);
 }
Beispiel #7
0
 public QueryEnumerator(Connection connection, IQueryConverter queryConverter, ISequenceQuery <T> queryObject)
 {
     this.connection     = connection;
     this.queryConverter = queryConverter;
     this.datumConverter = queryConverter.Get <T>();
     this.queryObject    = queryObject;
     this.stackTrace     = new StackTrace(true);
 }
        private IDatumConverter GetConverter(Type type)
        {
            IDatumConverter valueConverter = null;

            if (datumConverterCache.TryGetValue(type, out valueConverter))
            {
                return(valueConverter);
            }

            datumConverterCache[type] = valueConverter = rootDatumConverterFactory.Get(type);
            return(valueConverter);
        }
 public NullableDatumConverter(IDatumConverterFactory rootDatumConverterFactory)
 {
     this.innerConverter = rootDatumConverterFactory.Get <T>(rootDatumConverterFactory);
 }
 public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
 {
     datumConverter = null;
     if (typeof(T).IsEnum)
     {
         datumConverter = EnumDatumConverter <T> .Instance.Value;
     }
     return(datumConverter != null);
 }
Beispiel #11
0
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            //I guess we could have some more specific checks here
            //but if we get here last in the NewtonsoftSerializer order, then
            //I suppose we can handle it if no preceding converters could handle it.


            //makes no difference to Newtonsoft, but base class constructor
            //checks this constraint and throws if it's not exactly a Value type converter...
            if (typeof(T).IsValueType)
            {
                datumConverter = new NewtonsoftValueDatumConverter <T>();
                return(true);
            }

            datumConverter = new NewtonsoftReferenceDatumConverter <T>();
            return(true);
        }
 public NamedValueDictionaryKeysDatumConverter(IDatumConverterFactory rootDatumConverterFactory)
 {
     arrayDatumConverter = rootDatumConverterFactory.Get <string[]>();
 }
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            datumConverter = null;

            if (typeof(T) == typeof(Uri))
            {
                datumConverter = (IDatumConverter <T>)UriDatumConverter.Instance.Value;
            }

            return(datumConverter != null);
        }
Beispiel #14
0
 public bool TryGet(Type datumType, IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter datumConverter)
 {
     return(delegatedDatumConverterFactory.TryGet(datumType, rootDatumConverterFactory, out datumConverter));
 }
 public void TestFixtureSetUp()
 {
     datumConverter = TimeSpanDatumConverterFactory.Instance.Get<TimeSpan>();
 }
 public abstract bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter);
            public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
            {
                datumConverter = null;
                if (rootDatumConverterFactory == null)
                {
                    throw new ArgumentNullException("rootDatumConverterFactory");
                }

                if (typeof(T) != typeof(ITestInterface))
                {
                    return(false);
                }

                datumConverter = (IDatumConverter <T>) new TestInterfaceDatumConverter();
                return(true);
            }
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            datumConverter = null;

            if (typeof(T) == typeof(Object))
            {
                datumConverter = (IDatumConverter <T>) new ObjectDatumConverter(rootDatumConverterFactory);
            }

            return(datumConverter != null);
        }
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            datumConverter = null;
            if (rootDatumConverterFactory == null)
            {
                throw new ArgumentNullException("rootDatumConverterFactory");
            }

            if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                Type converterType = typeof(NullableDatumConverter <>).MakeGenericType(typeof(T).GetGenericArguments()[0]);
                datumConverter = (IDatumConverter <T>)Activator.CreateInstance(converterType, rootDatumConverterFactory);
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
 {
     datumConverter = null;
     foreach (var factory in datumConverterFactories)
     {
         if (factory.TryGet <T>(rootDatumConverterFactory, out datumConverter))
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #21
0
 public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
 {
     if (typeof(T) == typeof(int))
     {
         datumConverter = Substitute.For <IDatumConverter <T> >();
         return(true);
     }
     else
     {
         datumConverter = null;
         return(false);
     }
 }
 public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
 {
     datumConverter = null;
     if (typeof(T) == typeof(Bound))
     {
         datumConverter = (IDatumConverter <T>)BoundEnumDatumConverter.Instance;
     }
     return(datumConverter != null);
 }
Beispiel #23
0
 public static bool TryGet(this IDatumConverterFactory datumConverterFactory, Type datumType, out IDatumConverter datumConverter)
 {
     return(datumConverterFactory.TryGet(datumType, datumConverterFactory, out datumConverter));
 }
Beispiel #24
0
 public void TestFixtureSetUp()
 {
     datumConverter = TimeSpanDatumConverterFactory.Instance.Get <TimeSpan>();
 }
Beispiel #25
0
 public static bool TryGet <T>(this IDatumConverterFactory datumConverterFactory, out IDatumConverter <T> datumConverter)
 {
     return(datumConverterFactory.TryGet <T>(datumConverterFactory, out datumConverter));
 }
        // This is really ugly, using a helper class and reflection to call the generic TryGet<T> method.  But,
        // I can't see any alternative due to the generic out parameter, and I'm making the assumptions that
        // (a) non-generic version of TryGet will be less frequently used than the generic version, and (b) the
        // generic version is easier to write, so the non-generic version should be the uglier one.
        public bool TryGet(Type datumType, IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter datumConverter)
        {
            var helperType   = typeof(GenericHelper <>).MakeGenericType(datumType);
            var helperMethod = helperType.GetMethod("TryGet", BindingFlags.Public | BindingFlags.Static);
            var retval       = (Tuple <bool, IDatumConverter>)helperMethod.Invoke(null, new object[] { this, rootDatumConverterFactory });

            datumConverter = retval.Item2;
            return(retval.Item1);
        }
Beispiel #27
0
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            if (rootDatumConverterFactory == null)
            {
                throw new ArgumentNullException("rootDatumConverterFactory");
            }

            datumConverter = null;

            if (typeof(CompoundIndexKey).IsAssignableFrom(typeof(T)))
            {
                var retval = Activator.CreateInstance(
                    typeof(CompoundIndexKeyDatumConverterShim <>).MakeGenericType(typeof(T)),
                    new object[] { rootDatumConverterFactory }
                    );
                datumConverter = (IDatumConverter <T>)retval;
            }

            return(datumConverter != null);
        }
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            datumConverter = null;

            if (rootDatumConverterFactory == null)
            {
                throw new ArgumentNullException("rootDatumConverterFactory");
            }
            if (!IsTypeSupported(typeof(T)))
            {
                return(false);
            }

            datumConverter = new TupleConverter <T>(rootDatumConverterFactory);
            return(true);
        }
Beispiel #29
0
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            if (rootDatumConverterFactory == null)
            {
                throw new ArgumentNullException("rootDatumConverterFactory");
            }

            datumConverter = null;

            var dataContractAttribute = typeof(T).GetCustomAttribute <DataContractAttribute>();

            if (dataContractAttribute == null)
            {
                return(false);
            }

            if (typeof(T).IsEnum)
            {
                // [DataContract] on an enum type isn't supported by this datum converter.  It possibly should be,
                // as using the [EnumValue] attribute allows users to assign arbitrary text values to enums. (#146)
                return(false);
            }

            Type datumConverterType = TypeCache <T> .Instance.Value;

            datumConverter = (IDatumConverter <T>)Activator.CreateInstance(datumConverterType, rootDatumConverterFactory);
            return(true);
        }
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            datumConverter = null;

            if (typeof(T) == typeof(string))
            {
                datumConverter = (IDatumConverter <T>)StringDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(char))
            {
                datumConverter = (IDatumConverter <T>)CharDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(bool))
            {
                datumConverter = (IDatumConverter <T>)BoolDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(double))
            {
                datumConverter = (IDatumConverter <T>)DoubleDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(int))
            {
                datumConverter = (IDatumConverter <T>)IntDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(uint))
            {
                datumConverter = (IDatumConverter <T>)UnsignedIntDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(long))
            {
                datumConverter = (IDatumConverter <T>)LongDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(ulong))
            {
                datumConverter = (IDatumConverter <T>)UnsignedLongDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(short))
            {
                datumConverter = (IDatumConverter <T>)ShortDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(ushort))
            {
                datumConverter = (IDatumConverter <T>)UnsignedShortDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(float))
            {
                datumConverter = (IDatumConverter <T>)FloatDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(decimal))
            {
                datumConverter = (IDatumConverter <T>)DecimalDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(byte))
            {
                datumConverter = (IDatumConverter <T>)ByteDatumConverter.Instance.Value;
            }
            else if (typeof(T) == typeof(sbyte))
            {
                datumConverter = (IDatumConverter <T>)SignedByteDatumConverter.Instance.Value;
            }

            return(datumConverter != null);
        }
        public override bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
        {
            datumConverter = null;
            if (rootDatumConverterFactory == null)
            {
                throw new ArgumentNullException("rootDatumConverterFactory");
            }

            if (typeof(T).IsGenericType &&
                typeof(T).GetGenericTypeDefinition() == typeof(Dictionary <,>) &&
                typeof(T).GetGenericArguments() [0] == typeof(string))
            {
                var specificType = typeof(NamedValueDictionaryDatumConverter <>).MakeGenericType(typeof(T).GetGenericArguments()[1]);
                var dc           = Activator.CreateInstance(specificType, new object[] { rootDatumConverterFactory });
                datumConverter = (IDatumConverter <T>)dc;
                return(true);
            }

            if (typeof(T).IsGenericType &&
                typeof(T).GetGenericTypeDefinition() == typeof(Dictionary <,> .KeyCollection) &&
                typeof(T).GetGenericArguments() [0] == typeof(string))
            {
                var specificType = typeof(NamedValueDictionaryKeysDatumConverter <>).MakeGenericType(typeof(T).GetGenericArguments()[1]);
                var dc           = Activator.CreateInstance(specificType, new object[] { rootDatumConverterFactory });
                datumConverter = (IDatumConverter <T>)dc;
                return(true);
            }

            if (typeof(T).IsGenericType &&
                typeof(T).GetGenericTypeDefinition() == typeof(Dictionary <,> .ValueCollection) &&
                typeof(T).GetGenericArguments()[0] == typeof(string))
            {
                var dictionaryConverterType = typeof(NamedValueDictionaryDatumConverter <>).MakeGenericType(typeof(T).GetGenericArguments()[1]);
                var dictionaryConverter     = Activator.CreateInstance(dictionaryConverterType, new object[] { rootDatumConverterFactory });

                var specificType = typeof(NamedValueDictionaryValuesDatumConverter <>).MakeGenericType(typeof(T).GetGenericArguments()[1]);
                var dc           = Activator.CreateInstance(specificType, new object[] { dictionaryConverter });
                datumConverter = (IDatumConverter <T>)dc;
                return(true);
            }

            return(false);
        }
Beispiel #32
0
 public bool TryGet <T>(IDatumConverterFactory rootDatumConverterFactory, out IDatumConverter <T> datumConverter)
 {
     return(delegatedDatumConverterFactory.TryGet <T>(rootDatumConverterFactory, out datumConverter));
 }
 public IListDatumConverter(IDatumConverterFactory rootDatumConverterFactory)
 {
     this.arrayDatumConverter = new ArrayDatumConverter <T[]>(rootDatumConverterFactory);
 }