Esempio n. 1
0
        /// <inheritdoc />
        public dynamic Objectify(
            JToken graphsonObject, Type type, IGraphTypeSerializer serializer, IGenericSerializer genericSerializer)
        {
            if (!Utils.IsTuple(type))
            {
                throw new InvalidOperationException($"Can not deserialize a tuple to {type.FullName}.");
            }

            var values = (JArray)graphsonObject["value"];

            var genericArguments = type.GetGenericArguments();

            if (genericArguments.Length != values.Count)
            {
                throw new InvalidOperationException(
                          "Could not deserialize tuple, number of elements don't match " +
                          $"(expected {genericArguments.Length} but the server returned {values.Count}).");
            }
            var tupleValues = new object[values.Count];

            for (var i = 0; i < tupleValues.Length; i++)
            {
                tupleValues[i] = serializer.FromDb(values[i], genericArguments[i], false);
            }

            return(Activator.CreateInstance(type, tupleValues));
        }
Esempio n. 2
0
        public static Dictionary <string, dynamic> GetDefinitionByValue(
            IGenericSerializer genericSerializer, dynamic obj)
        {
            var objType  = (Type)obj.GetType();
            var typeCode = genericSerializer.GetCqlType(objType, out var typeInfo);

            return(GetDefinitionByType(new Dictionary <string, dynamic>(), genericSerializer, typeCode, typeInfo));
        }
Esempio n. 3
0
        /// <inheritdoc />
        public dynamic Objectify(
            JToken graphsonObject, Type type, IGraphTypeSerializer serializer, IGenericSerializer genericSerializer)
        {
            var keyspace = serializer.FromDb <string>(graphsonObject["keyspace"]);
            var name     = serializer.FromDb <string>(graphsonObject["name"]);
            var values   = (JArray)graphsonObject["value"];

            var  targetTypeIsDictionary = false;
            Type elementType            = null;

            if (type.GetTypeInfo().IsGenericType &&
                (type.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary <,>) ||
                 type.GetGenericTypeDefinition() == typeof(Dictionary <,>) ||
                 type.GetGenericTypeDefinition() == typeof(IDictionary <,>)))
            {
                targetTypeIsDictionary = true;
                var genericArgs = type.GetTypeInfo().GetGenericArguments();
                if (genericArgs[0] != typeof(string))
                {
                    throw new InvalidOperationException(
                              "Deserializing UDT to Dictionary is only supported when the dictionary key is of type \"string\".");
                }
                elementType = genericArgs[1];
            }

            UdtMap udtMap = null;
            bool   readToDictionary;

            if (targetTypeIsDictionary)
            {
                readToDictionary = true;
            }
            else
            {
                udtMap = genericSerializer.GetUdtMapByName($"{keyspace}.{name}");
                if (udtMap != null)
                {
                    readToDictionary = false;
                }
                else
                {
                    readToDictionary = true;
                    elementType      = typeof(object);
                }
            }

            var obj = readToDictionary
                ? ToDictionary(serializer, elementType, (JArray)graphsonObject["definition"], values)
                : ToObject(serializer, udtMap, values);

            if (!serializer.ConvertFromDb(obj, type, out var result))
            {
                throw new InvalidOperationException($"Could not convert UDT from type {obj.GetType().FullName} to {type.FullName}");
            }

            return(result);
        }
 public void SetUp()
 {
     sleepTime = 1000;
     assemblyResolver = MockRepository.GenerateMock<IAssemblyResolver>();
     config = new SqlToGraphiteConfig(assemblyResolver, log);
     reader = MockRepository.GenerateMock<IConfigReader>();
     cache = MockRepository.GenerateMock<ICache>();
     sleep = MockRepository.GenerateMock<ISleep>();
     log = MockRepository.GenerateMock<ILog>();
     configPersister = MockRepository.GenerateMock<IConfigPersister>();
     genericSerializer = MockRepository.GenerateMock<IGenericSerializer>();
     repository = new ConfigRepository(this.reader, this.cache, this.sleep, this.log, this.sleepTime, this.configPersister, this.genericSerializer);
 }
Esempio n. 5
0
        private static Dictionary <string, dynamic> GetDefinitionByType(
            Dictionary <string, dynamic> dictionary, IGenericSerializer genericSerializer, ColumnTypeCode typeCode, IColumnInfo typeInfo)
        {
            if (typeInfo is UdtColumnInfo udtTypeInfo)
            {
                var udtMap = genericSerializer.GetUdtMapByName(udtTypeInfo.Name);
                if (udtMap == null)
                {
                    throw new InvalidOperationException($"Could not find UDT mapping for {udtTypeInfo.Name}");
                }
                return(GetUdtTypeDefinition(dictionary, genericSerializer.GetUdtMapByName(udtTypeInfo.Name), genericSerializer));
            }

            dictionary.Add("cqlType", typeCode.ToString().ToLower());

            if (typeInfo is TupleColumnInfo tupleColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    tupleColumnInfo.Elements.Select(c => GetDefinitionByType(genericSerializer, c.TypeCode, c.TypeInfo)));
            }

            if (typeInfo is MapColumnInfo mapColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] {
                    GetDefinitionByType(genericSerializer, mapColumnInfo.KeyTypeCode, mapColumnInfo.KeyTypeInfo),
                    GetDefinitionByType(genericSerializer, mapColumnInfo.ValueTypeCode, mapColumnInfo.ValueTypeInfo)
                });
            }

            if (typeInfo is ListColumnInfo listColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] { GetDefinitionByType(
                                genericSerializer, listColumnInfo.ValueTypeCode, listColumnInfo.ValueTypeInfo) });
            }

            if (typeInfo is SetColumnInfo setColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] { GetDefinitionByType(
                                genericSerializer, setColumnInfo.KeyTypeCode, setColumnInfo.KeyTypeInfo) });
            }

            return(dictionary);
        }
Esempio n. 6
0
        /// <inheritdoc />
        public bool TryDictify(
            dynamic objectData,
            IGraphSONWriter serializer,
            IGenericSerializer genericSerializer,
            out dynamic result)
        {
            if (objectData == null)
            {
                result = null;
                return(false);
            }

            var type = (Type)objectData.GetType();
            var map  = genericSerializer.GetUdtMapByType(type);

            if (map == null)
            {
                result = null;
                return(false);
            }

            var dict = GetUdtTypeDefinition(map, genericSerializer);

            var value  = (object)objectData;
            var values = new List <object>();

            foreach (var field in map.Definition.Fields)
            {
                object fieldValue      = null;
                var    prop            = map.GetPropertyForUdtField(field.Name);
                var    fieldTargetType = genericSerializer.GetClrTypeForGraph(field.TypeCode, field.TypeInfo);
                if (prop != null)
                {
                    fieldValue = prop.GetValue(value, null);
                    if (!fieldTargetType.GetTypeInfo().IsAssignableFrom(prop.PropertyType.GetTypeInfo()))
                    {
                        fieldValue = UdtMap.TypeConverter.ConvertToDbFromUdtFieldValue(prop.PropertyType,
                                                                                       fieldTargetType,
                                                                                       fieldValue);
                    }
                }

                values.Add(fieldValue);
            }

            dict.Add("value", values.Select(serializer.ToDict));
            result = GraphSONUtil.ToTypedValue("UDT", dict, "dse");
            return(true);
        }
Esempio n. 7
0
 public static Dictionary <string, dynamic> GetUdtTypeDefinition(
     Dictionary <string, dynamic> dictionary, UdtMap map, IGenericSerializer genericSerializer)
 {
     dictionary.Add("cqlType", "udt");
     dictionary.Add("keyspace", map.Keyspace);
     dictionary.Add("name", map.IgnoreCase ? map.UdtName.ToLowerInvariant() : map.UdtName);
     dictionary.Add("definition", map.Definition.Fields.Select(
                        c => GetDefinitionByType(
                            new Dictionary <string, dynamic> {
         { "fieldName", c.Name }
     },
                            genericSerializer,
                            c.TypeCode,
                            c.TypeInfo)));
     return(dictionary);
 }
Esempio n. 8
0
        public bool TryDictify(
            dynamic objectData,
            IGraphSONWriter serializer,
            IGenericSerializer genericSerializer,
            out dynamic result)
        {
            if (objectData == null)
            {
                result = null;
                return(false);
            }

            var tupleType = (Type)objectData.GetType();

            if (!Utils.IsTuple(tupleType))
            {
                result = null;
                return(false);
            }

            var tupleTypeInfo = tupleType.GetTypeInfo();
            var subtypes      = tupleTypeInfo.GetGenericArguments();
            var data          = new List <object>();

            for (var i = 1; i <= subtypes.Length; i++)
            {
                var prop = tupleTypeInfo.GetProperty("Item" + i);
                if (prop != null)
                {
                    data.Add(prop.GetValue(objectData, null));
                }
            }

            var dict = new Dictionary <string, dynamic>
            {
                { "cqlType", "tuple" },
                {
                    "definition",
                    data.Select(elem => ComplexTypeDefinitionHelper.GetDefinitionByValue(genericSerializer, elem))
                },
                { "value", data.Select(d => serializer.ToDict(d)) }
            };

            result = GraphSONUtil.ToTypedValue("Tuple", dict, "dse");
            return(true);
        }
 public ConfigRepository(IConfigReader configReader, ICache cache, ISleep sleep, ILog log,
     int errorReadingConfigSleepTime, IGenericSerializer genericSerializer)
 {
     this.configReader = configReader;
     this.cache = cache;
     this.sleep = sleep;
     ConfigRepository.log = log;
     this.errorReadingConfigSleepTime = errorReadingConfigSleepTime;
     this.genericSerializer = genericSerializer;
     clientList = new GraphiteClients();
     var dir = new DirectoryImpl();
     if (ar == null)
     {
         ar = new AssemblyResolver(dir, log);
     }
     this.masterConfig = new SqlToGraphiteConfig(ar, log);
     this.Hash = "NotSet";
 }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T">Have to be serializeable with custom genericSerializer</typeparam>
        /// <param name="obj"></param>
        /// <param name="commandName"></param>
        /// <param name="genericSerializer"></param>
        public static BinaryProtocol FromData <T>(T obj, string commandName, IGenericSerializer <T, byte[]> genericSerializer) where T : new()
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            if (string.IsNullOrEmpty(commandName))
            {
                throw new ArgumentNullException(nameof(commandName));
            }
            if (genericSerializer == null)
            {
                throw new ArgumentNullException(nameof(genericSerializer));
            }

            var body = genericSerializer.Serialize(obj);

            return(new BinaryProtocol(body, commandName));
        }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the Yandex.Money.Api.Sdk.Requests.InstanceIdRequest class.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="jsonSerializer"></param>
 public InstanceIdRequest(IHttpClient client, IGenericSerializer <TResult> jsonSerializer) : base(client, jsonSerializer)
 {
 }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of the Yandex.Money.Api.Sdk.Requests.ProcessPaymentRequest class.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="jsonSerializer"></param>
 public ProcessPaymentRequest(IHttpClient client, IGenericSerializer <TResult> jsonSerializer)
     : base(client, jsonSerializer)
 {
 }
        internal static ColumnTypeCode GetCqlTypeForPrimitive(this IGenericSerializer serializer, Type type)
        {
            IColumnInfo dummyInfo;

            return(serializer.GetCqlType(type, out dummyInfo));
        }
Esempio n. 14
0
        /// <summary>
        /// Default ctor which allows to replace standard serializer.
        /// </summary>
        /// <param name="jsonSerializer"></param>
        protected JsonRequest(IGenericSerializer <TResult> jsonSerializer)
        {
            Argument.NotNull(jsonSerializer, "Serailizer instance is required.");

            _serializer = jsonSerializer;
        }
Esempio n. 15
0
 public Serializer(ProtocolVersion version, IGenericSerializer serializer)
 {
     ProtocolVersion = version;
     _serializer     = serializer;
 }
 public ConfigRepository(IConfigReader configReader, ICache cache, ISleep sleep, ILog log,
     int errorReadingConfigSleepTime, IConfigPersister configPersister, IGenericSerializer genericSerializer)
     : this(configReader, cache, sleep, log, errorReadingConfigSleepTime, genericSerializer)
 {
     this.configPersister = configPersister;
 }
Esempio n. 17
0
 /// <summary>
 /// basic constructor for http requests that return results in json format
 /// </summary>
 /// <param name="client"></param>
 /// <param name="jsonSerializer"></param>
 public JsonRequest(IHttpClient client, IGenericSerializer <TResult> jsonSerializer)
     : base(client)
 {
     _jsonSerializer = jsonSerializer;
 }
 /// <summary>
 /// Initializes a new instance of the Yandex.Money.Api.Sdk.Requests.AccountInfoRequest class.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="jsonSerializer">the serializer instance</param>
 public AccountInfoRequest(IHttpClient client, IGenericSerializer <TResult> jsonSerializer) : base(client, jsonSerializer)
 {
 }
 public ConfigPersister(IConfigWriter configWriter, IGenericSerializer genericSerializer)
 {
     this.configWriter = configWriter;
     this.genericSerializer = genericSerializer;
 }
 /// <summary>
 /// Deserialize a TSerialized to a TComplex async
 /// </summary>
 /// <typeparam name="TSerialized">The Serialized Type</typeparam>
 /// <typeparam name="TComplex">The Deserialized Type</typeparam>
 /// <param name="serializer">The Serializer instance</param>
 /// <param name="input">The TSerialized Input</param>
 /// <returns>The TComplex Output Task</returns>
 public static async Task <TComplex> DeserializeAsync <TComplex, TSerialized>(this IGenericSerializer <TSerialized> serializer, TSerialized input)
 {
     return(await Task.Run(() => {
         return serializer.Deserialize <TComplex>(input);
     }).ConfigureAwait(false));
 }
 public static BinaryProtocol FromData <T>(T obj, IGenericSerializer <T, byte[]> genericSerializer) where T : new()
 {
     return(FromData(obj, obj.GetType().Name, genericSerializer));
 }
Esempio n. 22
0
 /// <summary>
 /// Initializes a new instance of the Yandex.Money.Api.Sdk.Requests.OperationHistoryRequest class.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="jsonSerializer"></param>
 public OperationHistoryRequest(IHttpClient client, IGenericSerializer <TResult> jsonSerializer)
     : base(client, jsonSerializer)
 {
 }
Esempio n. 23
0
 private Dictionary <string, dynamic> GetUdtTypeDefinition(UdtMap map, IGenericSerializer genericSerializer)
 {
     return(ComplexTypeDefinitionHelper.GetUdtTypeDefinition(
                new Dictionary <string, dynamic>(), map, genericSerializer));
 }
Esempio n. 24
0
 /// <summary>
 /// Default ctor for JsonRequest.
 /// </summary>
 protected JsonRequest()
 {
     _serializer = new JsonSerializer <TResult>();
 }
Esempio n. 25
0
 /// <summary>
 /// Initializes a new instance of the Yandex.Money.Api.Sdk.Requests.TokenRequest class.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="jsonSerializer"></param>
 public TokenRequest(IHttpClient client, IGenericSerializer <TResult> jsonSerializer)
     : base(client, jsonSerializer)
 {
 }
Esempio n. 26
0
 private static Dictionary <string, dynamic> GetDefinitionByType(
     IGenericSerializer genericSerializer, ColumnTypeCode typeCode, IColumnInfo typeInfo)
 {
     return(GetDefinitionByType(new Dictionary <string, dynamic>(), genericSerializer, typeCode, typeInfo));
 }
Esempio n. 27
0
 /// <summary>
 /// Initializes a new instance of the Yandex.Money.Api.Sdk.Requests.IncomingTransferAcceptRequest class.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="jsonSerializer"></param>
 public IncomingTransferAcceptRequest(IHttpClient client, IGenericSerializer <TResult> jsonSerializer)
     : base(client, jsonSerializer)
 {
 }
Esempio n. 28
0
 /// <summary>
 /// Initializes a new instance of the Yandex.Money.Api.Sdk.Requests.RequestExternalPaymentRequest class.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="jsonSerializer"></param>
 public RequestExternalPaymentRequest(IHttpClient client, IGenericSerializer <TResult> jsonSerializer)
     : base(client, jsonSerializer)
 {
 }