internal IFirestoreInternalConverter GetConverter(BclType targetType)
        {
            IFirestoreInternalConverter customConverter = null;

            _customConverters?.TryGetValue(targetType, out customConverter);
            return(customConverter ?? ConverterCache.GetConverter(targetType));
        }
        /// <summary>
        /// Deserializes from a Firestore Value proto to a .NET type.
        /// </summary>
        /// <param name="db">The database to use when deserializing DocumentReferences. Must not be null.</param>
        /// <param name="value">The value to deserialize. Must not be null.</param>
        /// <param name="targetType">The target type. The method tries to convert to this type. If the type is
        /// object, it uses the default representation of the value.</param>
        /// <returns>The deserialized value</returns>
        internal static object Deserialize(FirestoreDb db, Value value, BclType targetType)
        {
            GaxPreconditions.CheckNotNull(db, nameof(db));
            GaxPreconditions.CheckNotNull(value, nameof(value));

            // If we're asked for a Value, just clone it, even for null values.
            // (We need to clone to avoid aliasing: modifying the result of serialization
            // should not affect the original data.)
            if (targetType == typeof(Value))
            {
                return(value.Clone());
            }
            if (targetType == typeof(object))
            {
                targetType = GetTargetType(value);
            }

            BclType underlyingType = Nullable.GetUnderlyingType(targetType);

            if (value.ValueTypeCase == Value.ValueTypeOneofCase.NullValue)
            {
                return(!targetType.GetTypeInfo().IsValueType || underlyingType != null
                    ? (object)null
                    : throw new ArgumentException($"Unable to convert null value to {targetType.GetTypeInfo().FullName}"));
            }

            // We deserialize to T and Nullable<T> the same way for all non-null values. Use the converter
            // associated with the non-nullable version of the target type.
            BclType nonNullableTargetType = underlyingType ?? targetType;

            return(ConverterCache.GetConverter(nonNullableTargetType).DeserializeValue(db, value));
        }
示例#3
0
    public void Execute()
    {
        msCoreReferenceFinder = new MsCoreReferenceFinder
                                        {
                                            AssemblyResolver = ModuleDefinition.AssemblyResolver,
                                        };
        msCoreReferenceFinder.Execute();
        var comparisonFinder = new DefaultStringComparisonFinder
                                   {
                                       ModuleWeaver = this,
                                       MsCoreReferenceFinder = msCoreReferenceFinder,
                                   };
        comparisonFinder.Execute();
        converterCache = new ConverterCache
                             {
                                 MsCoreReferenceFinder = msCoreReferenceFinder,
                                 ModuleDefinition = ModuleDefinition,
                                 DefaultStringComparisonFinder = comparisonFinder
                             };
        converterCache.Execute();

        foreach (var type in ModuleDefinition.GetTypes())
        {
            if (type.IsInterface)
            {
                continue;
            }
            if (type.IsEnum)
            {
                continue;
            }
            ProcessType(type);
        }
    }
        public void TryGetStringDictionaryValueType(BclType input, BclType expectedElementType)
        {
            var actual = ConverterCache.TryGetStringDictionaryValueType(input, out var actualElementType);

            Assert.Equal(expectedElementType != null, actual);
            Assert.Equal(expectedElementType, actualElementType);
        }
示例#5
0
        /// <summary>
        /// Generic Get for a type.
        /// <remarks>
        /// It works as long as there is a converter for the type to
        /// convert from string.
        /// </remarks>
        /// </summary>
        /// <returns>The element converted to its type or the default
        /// if it didn't exist or was empty.</returns>
        public static T Get <T>(this XElement source, XName name, T @default)
        {
            T      value;
            string sValue = GetString(source, name, null);

            if (string.IsNullOrEmpty(sValue))
            {
                return(@default);
            }
            if (ConverterCache <T> .TryParse(sValue, out value))
            {
                return(value);
            }
            if (ParseCache <T> .TryParse(sValue, out value))
            {
                return(value);
            }
            if (ConstructorCache <T> .TryInvoke(source, sValue, out value))
            {
                return(value);
            }
            // Throwing an exception helps you improve your parsing if the default
            // methods provided by the type creator are not sufficient.  You can
            // create a constructor for your class or a parsing class for types that
            // will handle the string value or the XElement itself.
            throw new NotSupportedException("Can't parse XElement value to type:" + typeof(T).Name);
            //return @default;
        }
示例#6
0
    public override void Execute()
    {
        FindCoreReferences();
        var comparisonFinder = new DefaultStringComparisonFinder
        {
            ModuleWeaver = this
        };

        comparisonFinder.Execute();
        converterCache = new ConverterCache
        {
            ModuleWeaver     = this,
            ModuleDefinition = ModuleDefinition,
            DefaultStringComparisonFinder = comparisonFinder
        };
        converterCache.Execute();

        foreach (var type in ModuleDefinition.GetTypes())
        {
            if (type.IsInterface)
            {
                continue;
            }

            if (type.IsEnum)
            {
                continue;
            }

            ProcessType(type);
        }
    }
示例#7
0
    public void Execute()
    {
        msCoreReferenceFinder = new MsCoreReferenceFinder
        {
            AssemblyResolver = ModuleDefinition.AssemblyResolver,
        };
        msCoreReferenceFinder.Execute();
        var comparisonFinder = new DefaultStringComparisonFinder
        {
            ModuleWeaver          = this,
            MsCoreReferenceFinder = msCoreReferenceFinder,
        };

        comparisonFinder.Execute();
        converterCache = new ConverterCache
        {
            MsCoreReferenceFinder         = msCoreReferenceFinder,
            ModuleDefinition              = ModuleDefinition,
            DefaultStringComparisonFinder = comparisonFinder
        };
        converterCache.Execute();

        foreach (var type in ModuleDefinition.GetTypes())
        {
            if (type.IsInterface)
            {
                continue;
            }
            if (type.IsEnum)
            {
                continue;
            }
            ProcessType(type);
        }
    }
示例#8
0
        public static T[] Query <T>(this MySqlDriver driver, FormattableString query)
        {
            driver.Open();
            var reader = driver.Query(query);

            var converter = ConverterCache <T> .GetConverter(reader);

            var array = ThreadStaticArrayPool <T> .GetArray();

            var count = 0;

            while (reader.Read())
            {
                if (count == array.Length)
                {
                    Array.Resize(ref array, checked ((int)(count * 1.5)));
                }

                var v = converter(reader);
                array[count++] = v;
            }

            if (array == ThreadStaticArrayPool <T> .GetArray())
            {
                var result = new T[count];
                Array.Copy(array, result, count);
                Array.Clear(array, 0, count); // null clear
                return(result);
            }
            else
            {
                Array.Resize(ref array, count);
                return(array);
            }
        }
 internal static object DeserializeMap(FirestoreDb db, IDictionary <string, Value> values, BclType targetType)
 {
     if (targetType == typeof(object))
     {
         targetType = typeof(Dictionary <string, object>);
     }
     return(ConverterCache.GetConverter(targetType).DeserializeMap(db, values));
 }
示例#10
0
        /// <summary>
        /// Serializes a map-based input to a dictionary of fields to values.
        /// This is effectively the map-only part of <see cref="Serialize"/>, but without wrapping the
        /// result in a Value.
        /// </summary>
        internal static Dictionary <string, Value> SerializeMap(object value)
        {
            GaxPreconditions.CheckNotNull(value, nameof(value));
            var map = new Dictionary <string, Value>();

            ConverterCache.GetConverter(value.GetType()).SerializeMap(value, map);
            return(map);
        }
示例#11
0
 /// <summary>
 /// Serializes a single input to a Value.
 /// </summary>
 /// <remarks>
 /// It's important that this always clones any mutable values - which is really only
 /// relevant when the input is already a proto. That allows the caller to then mutate the result
 /// where appropriate.
 /// </remarks>
 /// <param name="value">The value to serialize.</param>
 /// <returns>A Firestore Value proto.</returns>
 internal static Value Serialize(object value)
 {
     if (value == null)
     {
         return(new Value {
             NullValue = wkt::NullValue.NullValue
         });
     }
     return(ConverterCache.GetConverter(value.GetType()).Serialize(value));
 }
示例#12
0
        public static IEnumerable <T> QueryEnumerable <T>(this MySqlDriver driver, FormattableString query)
        {
            driver.Open();
            var reader    = driver.Query(query);
            var converter = ConverterCache <T> .GetConverter(reader);

            while (reader.Read())
            {
                var v = converter(reader);
                yield return(v);
            }
        }
示例#13
0
        public void SetValues(ISensor sensor, IReadingValues reading)
        {
            ReadingValuesConverter <IReadingValues, ReadingValues> converter = null;

            if (null != sensor)
            {
                int n;
                sensorNameLabel.Text = (Int32.TryParse(sensor.Name, out n) && 0 <= n && n < 26)
                                        ? ((char)((byte)('A') + (byte)n)).ToString()
                                        : sensor.Name;
                if (null != ConverterCache && sensor is ISensorInfo)
                {
                    var si = sensor as ISensorInfo;
                    converter = ConverterCache.Get(
                        si.TemperatureUnit, TemperatureUnit,
                        si.SpeedUnit, SpeedUnit,
                        si.PressureUnit, PressureUnit
                        );
                }
            }
            else
            {
                sensorNameLabel.Text = "Sensor";
            }

            const string na = "N/A";

            if (null != reading && reading.IsValid)
            {
                var data = null == converter
                                    ? reading
                                    : converter.Convert(reading)
                ;

                string tempText;
                if (data.IsTemperatureValid)
                {
                    tempText = data.Temperature.ToString("F1") + ' ' + UnitUtility.GetFriendlyName(TemperatureUnit);
                }
                else
                {
                    tempText = na;
                }

                string presText;
                if (data.IsPressureValid)
                {
                    var presUnit = PressureUnit;
                    presText = Math.Round(data.Pressure, presUnit == PressureUnit.Millibar ? 1 : 2).ToString()
                               + ' '
                               + UnitUtility.GetFriendlyName(presUnit)
                    ;
                }
                else
                {
                    presText = na;
                }

                string speedText;
                if (data.IsWindSpeedValid)
                {
                    speedText = data.WindSpeed.ToString("F2") + ' ' + UnitUtility.GetFriendlyName(SpeedUnit);
                }
                else
                {
                    speedText = na;
                }

                string dirText;
                if (data.IsWindDirectionValid)
                {
                    dirText = Math.Round(data.WindDirection).ToString()
                              + "\xb0 "
                              + Vector2D.CardinalDirection.DegreesToBestCardinalName(reading.WindDirection)
                    ;
                }
                else
                {
                    dirText = na;
                }

                tempValue.Text     = tempText;
                pressureValue.Text = presText;
                humidityValue.Text = data.IsHumidityValid
                                        ? (Math.Round(data.Humidity * 100.0, 1).ToString() + '%')
                                        : na
                ;
                windSpeedValue.Text = speedText;
                windDirValue.Text   = dirText;
            }
            else
            {
                tempValue.Text      = na;
                pressureValue.Text  = na;
                humidityValue.Text  = na;
                windSpeedValue.Text = na;
                windDirValue.Text   = na;
            }
        }
        [InlineData(typeof(Dictionary <int, int>), null)] // Implements IEnumerable<T>, but List<T> isn't compatible.
        public void TryGetListTypeArgument(BclType input, BclType expectedType)
        {
            var actualType = ConverterCache.TryGetListType(input);

            Assert.Equal(expectedType, actualType);
        }
示例#15
0
 /// <summary>
 /// Constructor for the <c>ConverterFactory</c> object.
 /// This will create an internal cache which is used to cache all
 /// instantiations made by the factory. Caching the converters
 /// ensures there is no overhead with instantiations.
 /// </summary>
 public ConverterFactory() {
    this.cache = new ConverterCache();
 }
示例#16
0
 /// <summary>
 /// Constructor for the <c>Registry</c> object. This is used
 /// to create a registry between classes and the converters that
 /// should be used to serialize and deserialize the instances. All
 /// converters are instantiated once and cached for reuse.
 /// </summary>
 public Registry() {
    this.binder = new RegistryBinder();
    this.cache = new ConverterCache();
 }