public void GetPropertyOwner_ReturnsTypeDescriptor()
        {
            IDictionary<string, object> data = new Dictionary<string, object>();
            DictionaryTypeDescriptor typeDescriptor = new DictionaryTypeDescriptor(data);
            PropertyDescriptor propertyDescriptor = new MockPropertyDescriptor();

            Assert.That(typeDescriptor.GetPropertyOwner(propertyDescriptor), Is.EqualTo(typeDescriptor));
        }
        public void GetPropertyOwner_ReturnsTypeDescriptor()
        {
            var data               = new Dictionary <string, object>();
            var typeDescriptor     = new DictionaryTypeDescriptor(data);
            var propertyDescriptor = new MockPropertyDescriptor();

            typeDescriptor.GetPropertyOwner(propertyDescriptor).Should().Be(typeDescriptor);
        }
Esempio n. 3
0
        public ITypeDescriptor Create(Type type)
        {
            SerializerTypeInfo result;

            if (_typeOrDescriptor2Info.TryGetValue(type, out result))
            {
                return(result.Descriptor);
            }
            if (_typeOrDescriptor2InfoNew.TryGetValue(type, out result))
            {
                return(result.Descriptor);
            }
            ITypeDescriptor desc = null;

            if (!type.IsSubclassOf(typeof(Delegate)))
            {
                if (type.IsGenericType)
                {
                    if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IList <>)))
                    {
                        desc = new ListTypeDescriptor(this, type);
                    }
                    else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IDictionary <,>)))
                    {
                        desc = new DictionaryTypeDescriptor(this, type);
                    }
                }
                else if (type.IsArray)
                {
                    desc = new ListTypeDescriptor(this, type);
                }
                else if (type.IsEnum)
                {
                    desc = new EnumTypeDescriptor(this, type);
                }
                else
                {
                    desc = new ObjectTypeDescriptor(this, type);
                }
            }
            if (desc == null)
            {
                throw new BTDBException("Don't know how to serialize type " + type.ToSimpleName());
            }
            result = new SerializerTypeInfo
            {
                Id         = 0,
                Descriptor = desc
            };
            _typeOrDescriptor2InfoNew[desc] = result;
            _typeOrDescriptor2InfoNew[type] = result;
            desc.FinishBuildFromType(this);
            return(desc);
        }
        public void GetProperties_ReturnsPropertiesFromDataDictionary()
        {
            IDictionary<string, object> data = new Dictionary<string, object>();
            data["Property1"] = "value1";
            data["Property2"] = 2;

            DictionaryTypeDescriptor typeDescriptor = new DictionaryTypeDescriptor(data);

            PropertyDescriptorCollection properties = typeDescriptor.GetProperties();
            Assert.That(properties[0].Name, Is.EqualTo("Property1"));
            Assert.That(properties[1].Name, Is.EqualTo("Property2"));
        }
        public void GetProperties_TypeOmitted_UsesObjectType()
        {
            IDictionary<string, object> data = new Dictionary<string, object>();
            data["Property1"] = "value1";
            data["Property2"] = 2;

            DictionaryTypeDescriptor typeDescriptor = new DictionaryTypeDescriptor(data);

            PropertyDescriptorCollection properties = typeDescriptor.GetProperties();
            Assert.That(properties[0].Name, Is.EqualTo("Property1"));
            Assert.That(properties[0].PropertyType, Is.EqualTo(typeof(object)));
            Assert.That(properties[1].Name, Is.EqualTo("Property2"));
            Assert.That(properties[1].PropertyType, Is.EqualTo(typeof(object)));
        }
        public void GetProperties_ReturnsPropertiesFromDataDictionary()
        {
            var data = new Dictionary <string, object>();

            data["Property1"] = "value1";
            data["Property2"] = 2;

            var typeDescriptor = new DictionaryTypeDescriptor(data);

            var properties = typeDescriptor.GetProperties();

            properties[0].Name.Should().Be("Property1");
            properties[1].Name.Should().Be("Property2");
        }
        public void GetProperties_TypeOmitted_UsesObjectType()
        {
            var data = new Dictionary <string, object>();

            data["Property1"] = "value1";
            data["Property2"] = 2;

            var typeDescriptor = new DictionaryTypeDescriptor(data);

            var properties = typeDescriptor.GetProperties();

            properties[0].Name.Should().Be("Property1");
            properties[0].PropertyType.Should().Be(typeof(object));
            properties[1].Name.Should().Be("Property2");
            properties[1].PropertyType.Should().Be(typeof(object));
        }
        public void GetProperties_TypeIncluded_UsesSpecifiedType()
        {
            IDictionary<string, object> data = new Dictionary<string, object>();
            data["Property1"] = "value1";
            data["Property2"] = 2;

            IDictionary<string, Type> types = new Dictionary<string, Type>();
            types["Property1"] = typeof(string);
            types["Property2"] = typeof(int);

            DictionaryTypeDescriptor typeDescriptor = new DictionaryTypeDescriptor(data, types);

            PropertyDescriptorCollection properties = typeDescriptor.GetProperties();
            Assert.That(properties[0].Name, Is.EqualTo("Property1"));
            Assert.That(properties[0].PropertyType, Is.EqualTo(typeof(string)));
            Assert.That(properties[1].Name, Is.EqualTo("Property2"));
            Assert.That(properties[1].PropertyType, Is.EqualTo(typeof(int)));
        }
        public void GetProperties_TypeIncluded_UsesSpecifiedType()
        {
            var data = new Dictionary <string, object>();

            data["Property1"] = "value1";
            data["Property2"] = 2;

            var types = new Dictionary <string, Type>();

            types["Property1"] = typeof(string);
            types["Property2"] = typeof(int);

            var typeDescriptor = new DictionaryTypeDescriptor(data, types);

            var properties = typeDescriptor.GetProperties();

            properties[0].Name.Should().Be("Property1");
            properties[0].PropertyType.Should().Be(typeof(string));
            properties[1].Name.Should().Be("Property2");
            properties[1].PropertyType.Should().Be(typeof(int));
        }
        public void PropertySet_RaisesPropertyChangedEvent()
        {
            var data = new Dictionary <string, object>();

            data["Property1"] = "value1";
            data["Property2"] = "value2";

            string propertyChanged = null;

            var descriptor = new DictionaryTypeDescriptor(data);

            descriptor.PropertyChanged += (s, e) =>
            {
                propertyChanged = e.PropertyName;
            };

            var properties = descriptor.GetProperties();

            properties[0].SetValue(descriptor, "modified");

            data["Property1"].Should().Be("modified");
            propertyChanged.Should().Be("Property1");
        }
Esempio n. 11
0
 /// <summary>
 /// formats a dictionary type
 /// </summary>
 /// <param name="dictionaryType"></param>
 /// <returns></returns>
 public override string FormatDictionaryType(DictionaryTypeDescriptor dictionaryType)
 {
     return(TypeScriptHelper.FormatDictionaryType(dictionaryType.Key.FormatType(this), dictionaryType.Value.FormatType(this)));
 }
Esempio n. 12
0
        public void ProcessMetadataLog(ByteBuffer buffer)
        {
            var reader = new ByteBufferReader(buffer);
            var typeId = reader.ReadVInt32();

            while (typeId != 0)
            {
                var             typeCategory = (TypeCategory)reader.ReadUInt8();
                ITypeDescriptor descriptor;
                switch (typeCategory)
                {
                case TypeCategory.BuildIn:
                    throw new ArgumentOutOfRangeException();

                case TypeCategory.Class:
                    descriptor = new ObjectTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.List:
                    descriptor = new ListTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Dictionary:
                    descriptor = new DictionaryTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Enum:
                    descriptor = new EnumTypeDescriptor(this, reader);
                    break;

                case TypeCategory.Nullable:
                    descriptor = new NullableTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                while (-typeId - 1 >= _id2InfoNew.Count)
                {
                    _id2InfoNew.Add(null);
                }
                if (_id2InfoNew[-typeId - 1] == null)
                {
                    _id2InfoNew[-typeId - 1] = new DeserializerTypeInfo {
                        Id = typeId, Descriptor = descriptor
                    }
                }
                ;
                typeId = reader.ReadVInt32();
            }
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i].Descriptor.MapNestedTypes(d =>
                {
                    var placeHolderDescriptor = d as PlaceHolderDescriptor;
                    return(placeHolderDescriptor != null ? _id2InfoNew[-placeHolderDescriptor.TypeId - 1].Descriptor : d);
                });
            }
            // This additional cycle is needed to fill names of recursive structures
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i].Descriptor.MapNestedTypes(d => d);
            }
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                var infoForType = _id2InfoNew[i];
                for (var j = ReservedBuildinTypes; j < _id2Info.Count; j++)
                {
                    if (infoForType.Descriptor.Equals(_id2Info[j].Descriptor))
                    {
                        _remapToOld[infoForType.Descriptor] = _id2Info[j].Descriptor;
                        _id2InfoNew[i] = _id2Info[j];
                        infoForType    = _id2InfoNew[i];
                        break;
                    }
                }
                if (infoForType.Id < 0)
                {
                    infoForType.Id = _id2Info.Count;
                    _id2Info.Add(infoForType);
                    _typeOrDescriptor2Info[infoForType.Descriptor] = infoForType;
                }
            }
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i].Descriptor.MapNestedTypes(d =>
                {
                    ITypeDescriptor res;
                    return(_remapToOld.TryGetValue(d, out res) ? res : d);
                });
            }
            _id2InfoNew.Clear();
            _remapToOld.Clear();
        }

        Func <AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeDescriptor, object> LoaderFactory(ITypeDescriptor descriptor)
        {
            var loadAsType    = LoadAsType(descriptor);
            var methodBuilder = ILBuilder.Instance.NewMethod <Func <AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeDescriptor, object> >("DeserializerFor" + descriptor.Name);
            var il            = methodBuilder.Generator;

            descriptor.GenerateLoad(il, ilGen => ilGen.Ldarg(0), ilGen => ilGen.Ldarg(1), ilGen => ilGen.Ldarg(2), loadAsType);
            if (loadAsType.IsValueType)
            {
                il.Box(loadAsType);
            }
            else if (loadAsType != typeof(object))
            {
                il.Castclass(typeof(object));
            }
            il.Ret();
            return(methodBuilder.Create());
        }
Esempio n. 13
0
        public ITypeDescriptor Create(Type type)
        {
            if (_typeOrDescriptor2Info.TryGetValue(type, out var result))
            {
                return(result.Descriptor);
            }
            if (_typeOrDescriptor2InfoNew.TryGetValue(type, out result))
            {
                return(result.Descriptor);
            }
            ITypeDescriptor desc            = null;
            Type            typeAlternative = null;

            if (!type.IsSubclassOf(typeof(Delegate)))
            {
                if (type.IsGenericType)
                {
                    typeAlternative = type.SpecializationOf(typeof(IList <>));
                    if (typeAlternative != null)
                    {
                        if (type != typeAlternative)
                        {
                            if (_typeOrDescriptor2Info.TryGetValue(typeAlternative, out result))
                            {
                                _typeOrDescriptor2Info[type] = result;
                                return(result.Descriptor);
                            }

                            if (_typeOrDescriptor2InfoNew.TryGetValue(typeAlternative, out result))
                            {
                                _typeOrDescriptor2InfoNew[type] = result;
                                return(result.Descriptor);
                            }
                        }

                        desc = new ListTypeDescriptor(this, typeAlternative);
                    }
                    else
                    {
                        typeAlternative = type.SpecializationOf(typeof(IDictionary <,>));
                        if (typeAlternative != null)
                        {
                            if (type != typeAlternative)
                            {
                                if (_typeOrDescriptor2Info.TryGetValue(typeAlternative, out result))
                                {
                                    _typeOrDescriptor2Info[type] = result;
                                    return(result.Descriptor);
                                }

                                if (_typeOrDescriptor2InfoNew.TryGetValue(typeAlternative, out result))
                                {
                                    _typeOrDescriptor2InfoNew[type] = result;
                                    return(result.Descriptor);
                                }
                            }

                            desc = new DictionaryTypeDescriptor(this, typeAlternative);
                        }
                        else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IIndirect <>)))
                        {
                            return(null);
                        }
                        else if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            typeAlternative = type.SpecializationOf(typeof(Nullable <>));
                            if (typeAlternative != null)
                            {
                                if (type != typeAlternative)
                                {
                                    if (_typeOrDescriptor2Info.TryGetValue(typeAlternative, out result))
                                    {
                                        _typeOrDescriptor2Info[type] = result;
                                        return(result.Descriptor);
                                    }

                                    if (_typeOrDescriptor2InfoNew.TryGetValue(typeAlternative, out result))
                                    {
                                        _typeOrDescriptor2InfoNew[type] = result;
                                        return(result.Descriptor);
                                    }
                                }

                                desc = new NullableTypeDescriptor(this, typeAlternative);
                            }
                        }
                    }
                }
                else if (type.IsArray)
                {
                    typeAlternative = type.SpecializationOf(typeof(IList <>));
                    Debug.Assert(typeAlternative != null && type != typeAlternative);
                    if (_typeOrDescriptor2Info.TryGetValue(typeAlternative, out result))
                    {
                        _typeOrDescriptor2Info[type] = result;
                        return(result.Descriptor);
                    }

                    if (_typeOrDescriptor2InfoNew.TryGetValue(typeAlternative, out result))
                    {
                        _typeOrDescriptor2InfoNew[type] = result;
                        return(result.Descriptor);
                    }

                    desc = new ListTypeDescriptor(this, typeAlternative);
                }
                else if (type.IsEnum)
                {
                    desc = new EnumTypeDescriptor(this, type);
                }
                else if (type.IsValueType)
                {
                    throw new BTDBException($"Unsupported value type {type.Name}.");
                }
                else
                {
                    desc = new ObjectTypeDescriptor(this, type);
                }
            }

            if (desc == null)
            {
                throw new BTDBException("Don't know how to serialize type " + type.ToSimpleName());
            }
            result = new SerializerTypeInfo
            {
                Id         = 0,
                Descriptor = desc
            };
            _typeOrDescriptor2InfoNew[desc] = result;
            _typeOrDescriptor2InfoNew[type] = result;
            if (typeAlternative != null)
            {
                _typeOrDescriptor2InfoNew[typeAlternative] = result;
            }
            if (!desc.FinishBuildFromType(this))
            {
                _typeOrDescriptor2InfoNew.Remove(desc);
                _typeOrDescriptor2InfoNew.Remove(type);
                if (typeAlternative != null)
                {
                    _typeOrDescriptor2InfoNew.Remove(typeAlternative);
                }
                return(null);
            }

            return(desc);
        }
Esempio n. 14
0
        public void ProcessMetadataLog(ByteBuffer buffer)
        {
            var reader = new ByteBufferReader(buffer);
            var typeId = reader.ReadVInt32();

            while (typeId != 0)
            {
                var             typeCategory = (TypeCategory)reader.ReadUInt8();
                ITypeDescriptor descriptor;
                switch (typeCategory)
                {
                case TypeCategory.BuildIn:
                    throw new ArgumentOutOfRangeException();

                case TypeCategory.Class:
                    descriptor = new ObjectTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.List:
                    descriptor = new ListTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Dictionary:
                    descriptor = new DictionaryTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Enum:
                    descriptor = new EnumTypeDescriptor(this, reader);
                    break;

                case TypeCategory.Nullable:
                    descriptor = new NullableTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                while (-typeId - 1 >= _id2InfoNew.Count)
                {
                    _id2InfoNew.Add(null);
                }
                if (_id2InfoNew[-typeId - 1] == null)
                {
                    _id2InfoNew[-typeId - 1] = new SerializerTypeInfo {
                        Id = typeId, Descriptor = descriptor
                    }
                }
                ;
                typeId = reader.ReadVInt32();
            }

            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i] !.Descriptor.MapNestedTypes(d =>
                {
                    var placeHolderDescriptor = d as PlaceHolderDescriptor;
                    return(placeHolderDescriptor != null
                        ? _id2InfoNew[-placeHolderDescriptor.TypeId - 1] !.Descriptor
                        : d);
                });
            }

            // This additional cycle is needed to fill names of recursive structures
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i] !.Descriptor.MapNestedTypes(d => d);
            }

            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                var infoForType = _id2InfoNew[i] !;
                for (var j = ReservedBuildinTypes; j < _id2Info.Count; j++)
                {
                    if (infoForType.Descriptor.Equals(_id2Info[j].Descriptor))
                    {
                        _remapToOld[infoForType.Descriptor] = _id2Info[j].Descriptor;
                        _id2InfoNew[i] = _id2Info[j];
                        infoForType    = _id2InfoNew[i];
                        break;
                    }
                }

                if (infoForType.Id < 0)
                {
                    infoForType.Id = _id2Info.Count;
                    _id2Info.Add(infoForType);
                    _typeOrDescriptor2Info[infoForType.Descriptor] = infoForType;
                }
            }

            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i] !.Descriptor.MapNestedTypes(d =>
                {
                    ITypeDescriptor res;
                    return(_remapToOld.TryGetValue(d, out res) ? res : d);
                });
            }

            _id2InfoNew.Clear();
            _remapToOld.Clear();
        }
        public void PropertyReset_RaisesPropertyChangedEvent()
        {
            var data = new Dictionary<string, object>();
            data["Property1"] = "value1";
            data["Property2"] = "value2";

            string propertyChanged = null;

            var descriptor = new DictionaryTypeDescriptor(data);
            descriptor.PropertyChanged += (s, e) =>
            {
                propertyChanged = e.PropertyName;
            };

            var properties = descriptor.GetProperties();
            properties[0].ResetValue(descriptor);

            Assert.That(data["Property1"], Is.EqualTo(null));
            Assert.That(propertyChanged, Is.EqualTo("Property1"));
        }
Esempio n. 16
0
 /// <summary>
 /// Formats a dictionary type
 /// </summary>
 /// <param name="dictionaryType"></param>
 /// <returns></returns>
 public abstract string FormatDictionaryType(DictionaryTypeDescriptor dictionaryType);