Browse Source

Better text colors.

pull/853/head
Sebastian 4 years ago
parent
commit
b43a909243
  1. 3
      backend/src/Squidex.Infrastructure/Reflection/Internal/PropertyAccessor.cs
  2. 66
      backend/src/Squidex.Infrastructure/Reflection/SimpleMapper.cs
  3. 12
      backend/tests/Squidex.Infrastructure.Tests/Reflection/PropertiesTypeAccessorTests.cs
  4. 28
      backend/tests/Squidex.Infrastructure.Tests/Reflection/SimpleMapperTests.cs
  5. 2
      frontend/src/app/theme/_vars.scss

3
backend/src/Squidex.Infrastructure/Reflection/Internal/PropertyAccessor.cs

@ -57,9 +57,8 @@ namespace Squidex.Infrastructure.Reflection.Internal
private readonly IPropertyAccessor internalAccessor; private readonly IPropertyAccessor internalAccessor;
public PropertyAccessor(Type targetType, PropertyInfo propertyInfo) public PropertyAccessor(PropertyInfo propertyInfo)
{ {
Guard.NotNull(targetType);
Guard.NotNull(propertyInfo); Guard.NotNull(propertyInfo);
var type = typeof(PropertyWrapper<,>).MakeGenericType(propertyInfo.DeclaringType!, propertyInfo.PropertyType); var type = typeof(PropertyWrapper<,>).MakeGenericType(propertyInfo.DeclaringType!, propertyInfo.PropertyType);

66
backend/src/Squidex.Infrastructure/Reflection/SimpleMapper.cs

@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using System.ComponentModel;
using System.Globalization; using System.Globalization;
using Squidex.Infrastructure.Reflection.Internal; using Squidex.Infrastructure.Reflection.Internal;
@ -66,6 +67,41 @@ namespace Squidex.Infrastructure.Reflection
} }
} }
private sealed class TypeConverterPropertyMapper : PropertyMapper
{
private readonly TypeConverter converter;
public TypeConverterPropertyMapper(
PropertyAccessor sourceAccessor,
PropertyAccessor targetAccessor,
TypeConverter converter)
: base(sourceAccessor, targetAccessor)
{
this.converter = converter;
}
public override void MapProperty(object source, object target, CultureInfo culture)
{
var value = GetValue(source);
if (value == null)
{
return;
}
try
{
var converted = converter.ConvertFrom(null, culture, value);
SetValue(target, converted);
}
catch
{
return;
}
}
}
private class PropertyMapper private class PropertyMapper
{ {
private readonly PropertyAccessor sourceAccessor; private readonly PropertyAccessor sourceAccessor;
@ -126,21 +162,33 @@ namespace Squidex.Infrastructure.Reflection
if (sourceType == targetType) if (sourceType == targetType)
{ {
Mappers.Add(new PropertyMapper( Mappers.Add(new PropertyMapper(
new PropertyAccessor(sourceClassType, sourceProperty), new PropertyAccessor(sourceProperty),
new PropertyAccessor(targetClassType, targetProperty))); new PropertyAccessor(targetProperty)));
} }
else if (targetType == typeof(string)) else if (targetType == typeof(string))
{ {
Mappers.Add(new StringConversionPropertyMapper( Mappers.Add(new StringConversionPropertyMapper(
new PropertyAccessor(sourceClassType, sourceProperty), new PropertyAccessor(sourceProperty),
new PropertyAccessor(targetClassType, targetProperty))); new PropertyAccessor(targetProperty)));
} }
else if (sourceType.Implements<IConvertible>() || targetType.Implements<IConvertible>()) else
{ {
Mappers.Add(new ConversionPropertyMapper( var converter = TypeDescriptor.GetConverter(targetType);
new PropertyAccessor(sourceClassType, sourceProperty),
new PropertyAccessor(targetClassType, targetProperty), if (converter.CanConvertFrom(sourceType))
targetType)); {
Mappers.Add(new TypeConverterPropertyMapper(
new PropertyAccessor(sourceProperty),
new PropertyAccessor(targetProperty),
converter));
}
else if (sourceType.Implements<IConvertible>() || targetType.Implements<IConvertible>())
{
Mappers.Add(new ConversionPropertyMapper(
new PropertyAccessor(sourceProperty),
new PropertyAccessor(targetProperty),
targetType));
}
} }
} }
} }

12
backend/tests/Squidex.Infrastructure.Tests/Reflection/PropertiesTypeAccessorTests.cs

@ -44,7 +44,7 @@ namespace Squidex.Infrastructure.Reflection
[Fact] [Fact]
public void Should_set_read_write_property() public void Should_set_read_write_property()
{ {
var sut = new PropertyAccessor(typeof(TestClass), typeof(TestClass).GetProperty("ReadWrite")!); var sut = new PropertyAccessor(typeof(TestClass).GetProperty("ReadWrite")!);
sut.Set(target, 123); sut.Set(target, 123);
@ -54,7 +54,7 @@ namespace Squidex.Infrastructure.Reflection
[Fact] [Fact]
public void Should_set_write_property() public void Should_set_write_property()
{ {
var accessor = new PropertyAccessor(typeof(TestClass), typeof(TestClass).GetProperty("Write")!); var accessor = new PropertyAccessor(typeof(TestClass).GetProperty("Write")!);
accessor.Set(target, 123); accessor.Set(target, 123);
@ -64,7 +64,7 @@ namespace Squidex.Infrastructure.Reflection
[Fact] [Fact]
public void Should_throw_exception_if_setting_readonly() public void Should_throw_exception_if_setting_readonly()
{ {
var sut = new PropertyAccessor(typeof(TestClass), typeof(TestClass).GetProperty("Read")!); var sut = new PropertyAccessor(typeof(TestClass).GetProperty("Read")!);
Assert.Throws<NotSupportedException>(() => sut.Set(target, 123)); Assert.Throws<NotSupportedException>(() => sut.Set(target, 123));
} }
@ -72,7 +72,7 @@ namespace Squidex.Infrastructure.Reflection
[Fact] [Fact]
public void Should_get_read_write_property() public void Should_get_read_write_property()
{ {
var sut = new PropertyAccessor(typeof(TestClass), typeof(TestClass).GetProperty("ReadWrite")!); var sut = new PropertyAccessor(typeof(TestClass).GetProperty("ReadWrite")!);
target.Write = 123; target.Write = 123;
@ -82,7 +82,7 @@ namespace Squidex.Infrastructure.Reflection
[Fact] [Fact]
public void Should_get_read_property() public void Should_get_read_property()
{ {
var sut = new PropertyAccessor(typeof(TestClass), typeof(TestClass).GetProperty("Read")!); var sut = new PropertyAccessor(typeof(TestClass).GetProperty("Read")!);
target.Write = 123; target.Write = 123;
@ -92,7 +92,7 @@ namespace Squidex.Infrastructure.Reflection
[Fact] [Fact]
public void Should_throw_exception_if_getting_writeonly_property() public void Should_throw_exception_if_getting_writeonly_property()
{ {
var sut = new PropertyAccessor(typeof(TestClass), typeof(TestClass).GetProperty("Write")!); var sut = new PropertyAccessor(typeof(TestClass).GetProperty("Write")!);
Assert.Throws<NotSupportedException>(() => sut.Get(target)); Assert.Throws<NotSupportedException>(() => sut.Get(target));
} }

28
backend/tests/Squidex.Infrastructure.Tests/Reflection/SimpleMapperTests.cs

@ -102,17 +102,31 @@ namespace Squidex.Infrastructure.Reflection
} }
[Fact] [Fact]
public void Should_map_nullables() public void Should_map_from_nullable()
{ {
var obj1 = new Class1<bool?, bool?> var obj1 = new Class1<long?, long?>
{ {
P1 = true, P1 = 6,
P2 = true P2 = 8
};
var obj2 = SimpleMapper.Map(obj1, new Class2<long, long>());
Assert.Equal(8, obj2.P2);
Assert.Equal(0, obj2.P3);
}
[Fact]
public void Should_map_to_nullable()
{
var obj1 = new Class1<long, long>
{
P1 = 6,
P2 = 8
}; };
var obj2 = SimpleMapper.Map(obj1, new Class2<bool, bool>()); var obj2 = SimpleMapper.Map(obj1, new Class2<long?, long?>());
Assert.True(obj2.P2); Assert.Equal(8, obj2.P2);
Assert.False(obj2.P3); Assert.Null(obj2.P3);
} }
[Fact] [Fact]

2
frontend/src/app/theme/_vars.scss

@ -10,7 +10,7 @@ $color-border-darker: darken($color-border, 15%);
$color-title: #000; $color-title: #000;
$color-text: #373a3c; $color-text: #373a3c;
$color-text-decent: #9aa1b6; $color-text-decent: #6c707f;
$color-tooltip: #1a2129; $color-tooltip: #1a2129;
$color-code-background: #f5f7f9; $color-code-background: #f5f7f9;

Loading…
Cancel
Save