Commit a564fcbd authored by Michael Herndon's avatar Michael Herndon

PROTO flex

- extract configurations classes into new file for testing.
- add test for reader.VisitValue
parent dd6c2d3e
......@@ -15,7 +15,12 @@ namespace NerdyMishka.Extensions.Flex
{
private ITextTransform propertyTransform = new PascalCaseTransform();
private FlexSerializationSettings settings;
private IFlexSerializationSettings settings;
public YamlObjectReader(IFlexSerializationSettings settings = null)
{
this.settings = settings ?? new FlexSerializationSettings();
}
public object Visit(YamlNode node, IProperty propertyInfo, IType typeInfo)
......@@ -64,6 +69,9 @@ namespace NerdyMishka.Extensions.Flex
public object VisitValue(YamlScalarNode node, IProperty property, IType type = null)
{
if(node.Value == "null")
return null;
IReadOnlyCollection<ValueConverter> converters = new List<ValueConverter>();
string propertyName = "unknown property";
Type clrType = type?.ClrType;
......@@ -81,13 +89,18 @@ namespace NerdyMishka.Extensions.Flex
}
}
foreach(var converter in this.settings.ValueConverters)
if(this.settings.ValueConverters != null && this.settings.ValueConverters.Count > 0)
{
// Property => Store
if(converter.CanConvertFrom(clrType) && converter.CanConvertTo(typeof(string)))
return converter.ConvertFrom(node.Value);
foreach(var converter in this.settings.ValueConverters)
{
// Property => Store
if(converter.CanConvertFrom(clrType) && converter.CanConvertTo(typeof(string)))
return converter.ConvertFrom(node.Value);
}
}
if(!type.IsDataType)
throw new MappingException("Scalar Nodes must be a data type");
......
using System;
using NerdyMishka.ComponentModel.DataAnnotations;
namespace Tests
{
public class SimpleConfigurationAttributes
{
[Bit("enabled", "disabled")]
public bool IsEnabled { get; set; } = true;
[Encrypt]
public string ConnectionString { get; set; } = "Server=localhost;";
[Hash]
public string Password { get; set; } = "my-great-and-terrible-pw";
}
public class SimpleConfigurationValues
{
public string String { get; set; } = "test-value";
public int Int32 { get; set; } = 443;
public int? NullableInt32 { get; set; } = null;
public long Int64 { get; set; } = long.MaxValue;
public long? NullableInt64 { get; set; } = long.MaxValue;
public DateTime DateTime { get; set; } = DateTime.UtcNow;
public Double Double { get; set; } = 80923.2342;
public Decimal Decimal { get; set; } = 3421231234.23423M;
public byte[] Bytes { get; set; } = System.Text.Encoding.UTF8.GetBytes("bytes");
public char[] Chars { get; set; } = "chars".ToCharArray();
public bool Boolean { get; set; } = true;
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using NerdyMishka.ComponentModel.DataAnnotations;
using NerdyMishka.Extensions.Flex;
using NerdyMishka.Reflection.Extensions;
using Xunit;
using YamlDotNet.RepresentationModel;
namespace Tests
{
public class YamlObjectReaderTests
{
[Fact]
public void Visit()
{
var reader = new YamlObjectReader();
var configuration = new SimpleConfigurationValues();
var properties = configuration.GetType().AsTypeInfo().Properties;
foreach(var propInfo in properties)
{
var value = propInfo.GetValue(configuration);
string strValue = null;
if(value == null)
strValue = "null";
if(value != null)
{
switch(value)
{
case byte[] bytes:
strValue = Convert.ToBase64String(bytes);
break;
case char[] chars:
strValue = new string(chars);
break;
default:
strValue = value.ToString();
break;
}
}
var scalar = new YamlScalarNode(strValue);
var convertedValue = reader.VisitValue(scalar, propInfo, propInfo.ClrType.AsTypeInfo());
Assert.Equal(value, convertedValue);
}
}
}
}
\ No newline at end of file
......@@ -194,41 +194,5 @@ namespace Tests
}
public class SimpleConfigurationAttributes
{
[Bit("enabled", "disabled")]
public bool IsEnabled { get; set; } = true;
[Encrypt]
public string ConnectionString { get; set; } = "Server=localhost;";
[Hash]
public string Password { get; set; } = "my-great-and-terrible-pw";
}
public class SimpleConfigurationValues
{
public string String { get; set; } = "test-value";
public int Int32 { get; set; } = 443;
public int? NullableInt32 { get; set; } = null;
public long Int64 { get; set; } = long.MaxValue;
public long? NullableInt64 { get; set; } = long.MaxValue;
public DateTime DateTime { get; set; } = DateTime.UtcNow;
public Double Double { get; set; } = 80923.2342;
public Decimal Decimal { get; set; } = 3421231234.23423M;
public byte[] Bytes { get; set; } = System.Text.Encoding.UTF8.GetBytes("bytes");
public char[] Chars { get; set; } = "chars".ToCharArray();
public bool Boolean { get; set; } = true;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment