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 ...@@ -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"); throw new MappingException("Scalar Nodes must be a data type");
switch(type.FullName) switch(type.FullName)
{ {
case "System.Byte[]": case "System.Byte[]":
...@@ -118,6 +116,12 @@ namespace NerdyMishka.Extensions.Flex ...@@ -118,6 +116,12 @@ namespace NerdyMishka.Extensions.Flex
case "System.String": case "System.String":
return node.Value; 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.Boolean":
case "System.Nullable[System.Boolean]": case "System.Nullable[System.Boolean]":
switch (node.Value) switch (node.Value)
...@@ -146,14 +150,19 @@ namespace NerdyMishka.Extensions.Flex ...@@ -146,14 +150,19 @@ namespace NerdyMishka.Extensions.Flex
} }
default: default:
bool isNull = string.IsNullOrWhiteSpace(node.Value) || node.Value.Match("null"); bool isNull = string.IsNullOrWhiteSpace(node.Value) || node.Value.Match("null");
if(type.IsNullableOfT() && isNull) clrType = type.ClrType;
return null; if(type.IsNullableOfT())
{
if(isNull)
return null;
clrType = type.UnderlyingType.ClrType;
}
if(isNull) if(isNull)
throw new MappingException($"{propertyName} is type {type.FullName} which must not be null."); 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 ...@@ -196,10 +205,10 @@ namespace NerdyMishka.Extensions.Flex
public bool MoveNext() public bool MoveNext()
{ {
if(index >= this.keys.Count) if(this.index >= (this.keys.Count - 1))
return false; return false;
this.index++; this.index = this.index + 1;
return true; return true;
} }
...@@ -233,15 +242,17 @@ namespace NerdyMishka.Extensions.Flex ...@@ -233,15 +242,17 @@ namespace NerdyMishka.Extensions.Flex
var isDictionaryLike = typeInfo.IsIDictionaryOfKv() || typeInfo.IsIDictionary(); var isDictionaryLike = typeInfo.IsIDictionaryOfKv() || typeInfo.IsIDictionary();
var childType = typeInfo.AsItemType()?.ItemType; var childType = typeInfo.AsItemType()?.ItemType;
var clrType = childType?.ClrType; var clrType = childType?.ClrType;
if(childType == null) if(childType == null || clrType == typeof(Object))
{ {
clrType = typeof(Object); clrType = typeof(string);
childType = ReflectionCache.GetOrAdd(clrType); childType = ReflectionCache.GetOrAdd(clrType);
} }
if(isDictionaryLike) if(isDictionaryLike)
{ {
var dictionary = (IDictionary)Activator.CreateInstance(typeInfo.ClrType); var dictionary = (IDictionary)Activator.CreateInstance(typeInfo.ClrType);
var enumerator = new YamlPropertyEnumerator(node); var enumerator = new YamlPropertyEnumerator(node);
...@@ -250,7 +261,7 @@ namespace NerdyMishka.Extensions.Flex ...@@ -250,7 +261,7 @@ namespace NerdyMishka.Extensions.Flex
var child = enumerator.Current; var child = enumerator.Current;
dictionary.Add( dictionary.Add(
this.ConvertToPropertyName(child.Key), child.Key,
this.Visit(child.Value, null, childType)); this.Visit(child.Value, null, childType));
} }
......
...@@ -254,6 +254,9 @@ namespace NerdyMishka.Extensions.Flex ...@@ -254,6 +254,9 @@ namespace NerdyMishka.Extensions.Flex
case char[] chars: case char[] chars:
return new YamlScalarNode(new string(chars)); return new YamlScalarNode(new string(chars));
case DateTime dt:
return dt.ToUniversalTime().ToString("o");
default: default:
return new YamlScalarNode(value.ToString()); return new YamlScalarNode(value.ToString());
......
...@@ -10,6 +10,53 @@ namespace Tests ...@@ -10,6 +10,53 @@ namespace Tests
{ {
public class YamlObjectReaderTests 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] [Fact]
public void Visit() public void Visit()
...@@ -36,6 +83,11 @@ namespace Tests ...@@ -36,6 +83,11 @@ namespace Tests
case char[] chars: case char[] chars:
strValue = new string(chars); strValue = new string(chars);
break; break;
case DateTime dt:
if(dt.Kind != DateTimeKind.Utc)
throw new Exception("bad date");
strValue = dt.ToString("o");
break;
default: default:
strValue = value.ToString(); strValue = value.ToString();
break; 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