Skip to content

Commit

Permalink
Treat TimeSpan as a simple type
Browse files Browse the repository at this point in the history
  • Loading branch information
VyacheslavPritykin committed Oct 22, 2020
1 parent ce67ae4 commit 70f4967
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 45 deletions.
47 changes: 23 additions & 24 deletions NFormlySchema.UnitTests/UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,40 @@ namespace NFormlySchema.UnitTests
public class UtilsTests
{
[Theory]
[InlineData(typeof(int), false)]
[InlineData(typeof(DateTime), false)]
[InlineData(typeof(DateTimeOffset), false)]
[InlineData(typeof(TestEnum), false)]
[InlineData(typeof(string), false)]
[InlineData(typeof(Guid), false)]
[InlineData(typeof(TestClass), true)]
[InlineData(typeof(TestStruct), true)]
[InlineData(typeof(object), true)]
[InlineData(typeof(Collection<string>), false)]
[InlineData(typeof(List<string>), false)]
[InlineData(typeof(string[]), false)]
public void IsFormGroup(Type type, bool expectedResult)
[InlineData(typeof(int), true)]
[InlineData(typeof(DateTime), true)]
[InlineData(typeof(DateTimeOffset), true)]
[InlineData(typeof(TestEnum), true)]
[InlineData(typeof(string), true)]
[InlineData(typeof(Guid), true)]
[InlineData(typeof(TimeSpan), true)]
[InlineData(typeof(TestClass), false)]
[InlineData(typeof(TestStruct), false)]
[InlineData(typeof(object), false)]
public void IsSimple(Type type, bool expectedResult)
{
// act
var isFormGroup = TypeUtils.IsFormGroup(type);
var isSimple = type.IsSimple();

// assert
isFormGroup.Should().Be(expectedResult);
isSimple.Should().Be(expectedResult);
}

[Theory]
[InlineData(typeof(int?), false)]
[InlineData(typeof(DateTime?), false)]
[InlineData(typeof(DateTimeOffset?), false)]
[InlineData(typeof(TestEnum?), false)]
[InlineData(typeof(Guid?), false)]
[InlineData(typeof(TestStruct?), true)]
public void IsFormGroupWithNullableTypes(Type type, bool expectedResult)
[InlineData(typeof(int?), true)]
[InlineData(typeof(DateTime?), true)]
[InlineData(typeof(DateTimeOffset?), true)]
[InlineData(typeof(TimeSpan?), true)]
[InlineData(typeof(TestEnum?), true)]
[InlineData(typeof(Guid?), true)]
[InlineData(typeof(TestStruct?), false)]
public void IsSimpleWithNullableTypes(Type type, bool expectedResult)
{
// act
var isFormGroup = TypeUtils.IsFormGroup(type);
var isSimple = type.IsSimple();

// assert
isFormGroup.Should().Be(expectedResult);
isSimple.Should().Be(expectedResult);
}
}

Expand Down
13 changes: 13 additions & 0 deletions NFormlySchema/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,18 @@ internal static class TypeExtensions
{
public static bool IsCollection(this Type type) =>
type.GetInterface(nameof(ICollection)) != null;

public static bool IsSimple(this Type type)
{
type = Nullable.GetUnderlyingType(type) ?? type;
return type.IsPrimitive
|| type.IsEnum
|| type == typeof(string)
|| type == typeof(decimal)
|| type == typeof(DateTime)
|| type == typeof(DateTimeOffset)
|| type == typeof(TimeSpan)
|| type == typeof(Guid);
}
}
}
8 changes: 4 additions & 4 deletions NFormlySchema/FormlySchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static FormlyFieldConfig BuildFormlyFieldConfigForSimpleArrayElement(Typ
return null;

var elementType = propertyInfo.PropertyType.GetElementType()!;
if (!TypeUtils.IsFormGroup(elementType))
if (elementType.IsSimple())
return BuildFormlyFieldConfigForSimpleArrayElement(elementType);

return null; // TODO
Expand All @@ -140,15 +140,15 @@ private static FormlyFieldConfig BuildFormlyFieldConfigForSimpleArrayElement(Typ
Attribute[] attributes,
FormlyGenerationSettings setting)
{
return TypeUtils.IsFormGroup(propertyInfo.PropertyType)
return !propertyInfo.PropertyType.IsSimple()
&& !propertyInfo.PropertyType.IsCollection()
&& attributes.OfType<FieldGroupAttribute>().Any()
? FromType(propertyInfo.PropertyType, setting)
: null;
}

private static bool IsInlineNestedFieldGroup(PropertyInfo propertyInfo, Attribute[] attributes) =>
TypeUtils.IsFormGroup(propertyInfo.PropertyType)
!propertyInfo.PropertyType.IsSimple()
&& !propertyInfo.PropertyType.IsCollection()
&& !attributes.OfType<FieldGroupAttribute>().Any();

Expand Down Expand Up @@ -427,7 +427,7 @@ private static bool IsInlineNestedFieldGroup(PropertyInfo propertyInfo, Attribut

private static string? ResolveFieldType(Type propertyType, Attribute[] attributes)
{
if (TypeUtils.IsFormGroup(propertyType))
if (!propertyType.IsSimple() && !propertyType.IsCollection())
return null;

var fieldType = attributes.OfType<FieldTypeAttribute>().FirstOrDefault()?.Type;
Expand Down
17 changes: 0 additions & 17 deletions NFormlySchema/Internal/TypeUtils.cs

This file was deleted.

0 comments on commit 70f4967

Please sign in to comment.