Commit d7225816 authored by Michael Herndon's avatar Michael Herndon

PROTO: flex

- VisitList test
- VisitValue, fix date to default to ISO 8601 Date format and use Utc dates
  in dotnet.
- VisitDictiontary test, fix dictionary to default to string value
parent a564fcbd
......@@ -101,12 +101,10 @@ namespace NerdyMishka.Extensions.Flex
if(!type.IsDataType)
if(!(type.IsDataType || type.IsNullableOfT() && type.UnderlyingType.IsDataType))
throw new MappingException("Scalar Nodes must be a data type");
switch(type.FullName)
{
case "System.Byte[]":
......@@ -118,6 +116,12 @@ namespace NerdyMishka.Extensions.Flex
case "System.String":
return node.Value;
case "System.DateTime":
var dt = DateTime.Parse(node.Value, null, System.Globalization.DateTimeStyles.AssumeUniversal);
if(dt.Kind != DateTimeKind.Utc)
return dt.ToUniversalTime();
return dt;
case "System.Boolean":
case "System.Nullable[System.Boolean]":
switch (node.Value)
......@@ -146,14 +150,19 @@ namespace NerdyMishka.Extensions.Flex
}
default:
bool isNull = string.IsNullOrWhiteSpace(node.Value) || node.Value.Match("null");
if(type.IsNullableOfT() && isNull)
return null;
clrType = type.ClrType;
if(type.IsNullableOfT())
{
if(isNull)
return null;
clrType = type.UnderlyingType.ClrType;
}
if(isNull)
throw new MappingException($"{propertyName} is type {type.FullName} which must not be null.");
return Convert.ChangeType(node.Value, type.ClrType);
return Convert.ChangeType(node.Value, clrType);
}
......@@ -196,10 +205,10 @@ namespace NerdyMishka.Extensions.Flex
public bool MoveNext()
{
if(index >= this.keys.Count)
if(this.index >= (this.keys.Count - 1))
return false;
this.index++;
this.index = this.index + 1;
return true;
}
......@@ -233,15 +242,17 @@ namespace NerdyMishka.Extensions.Flex
var isDictionaryLike = typeInfo.IsIDictionaryOfKv() || typeInfo.IsIDictionary();
var childType = typeInfo.AsItemType()?.ItemType;
var clrType = childType?.ClrType;
if(childType == null)
if(childType == null || clrType == typeof(Object))
{
clrType = typeof(Object);
clrType = typeof(string);
childType = ReflectionCache.GetOrAdd(clrType);
}
if(isDictionaryLike)
{
var dictionary = (IDictionary)Activator.CreateInstance(typeInfo.ClrType);
var enumerator = new YamlPropertyEnumerator(node);
......@@ -250,7 +261,7 @@ namespace NerdyMishka.Extensions.Flex
var child = enumerator.Current;
dictionary.Add(
this.ConvertToPropertyName(child.Key),
child.Key,
this.Visit(child.Value, null, childType));
}
......
......@@ -254,6 +254,9 @@ namespace NerdyMishka.Extensions.Flex
case char[] chars:
return new YamlScalarNode(new string(chars));
case DateTime dt:
return dt.ToUniversalTime().ToString("o");
default:
return new YamlScalarNode(value.ToString());
......
......@@ -10,6 +10,53 @@ namespace Tests
{
public class YamlObjectReaderTests
{
[Fact]
public static void VisitDictionary()
{
var reader = new YamlObjectReader();
var map = new YamlMappingNode();
map.Add("one", "1");
map.Add("two", "2");
map.Add("three", "3");
var @object = reader.VisitElement(map, null, typeof(Dictionary<string, object>).AsTypeInfo());
Assert.NotNull(@object);
Assert.IsType<Dictionary<string, object>>(@object);
var dictionary = (Dictionary<string, object>)@object;
Assert.Equal("1", dictionary["one"]);
Assert.Equal("2", dictionary["two"]);
Assert.Equal("3", dictionary["three"]);
}
[Fact]
public static void VisitList()
{
var reader = new YamlObjectReader();
var sequence = new YamlSequenceNode() {
"1",
"2",
"3",
"4",
"5",
"6"
};
var list = reader.VisitArray(sequence, null, typeof(List<string>).AsTypeInfo());
Assert.NotNull(list);
var set = (IList<string>)list;
Assert.Collection(set,
(v) => Assert.Equal("1", ((YamlScalarNode)v).Value),
(v) => Assert.Equal("2", ((YamlScalarNode)v).Value),
(v) => Assert.Equal("3", ((YamlScalarNode)v).Value),
(v) => Assert.Equal("4", ((YamlScalarNode)v).Value),
(v) => Assert.Equal("5", ((YamlScalarNode)v).Value),
(v) => Assert.Equal("6", ((YamlScalarNode)v).Value)
);
}
[Fact]
public void Visit()
......@@ -36,6 +83,11 @@ namespace Tests
case char[] chars:
strValue = new string(chars);
break;
case DateTime dt:
if(dt.Kind != DateTimeKind.Utc)
throw new Exception("bad date");
strValue = dt.ToString("o");
break;
default:
strValue = value.ToString();
break;
......
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