Skip to content

Commit

Permalink
feat: add GetCurrentMapsbyType to GridifyMapper (#214)
Browse files Browse the repository at this point in the history
* get maps by type - #211

* adding other type tests for CurrentMapsByType

* PR fixes, updated test, added docs
  • Loading branch information
moxplod authored Sep 3, 2024
1 parent 22184ee commit 4eccf13
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/pages/guide/gridifyMapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ This method clears the list of mappings

This method returns list of current mappings.

## GetCurrentMapsByType

This method returns list of current mappings for the given type.

## GridifyMapperConfiguration

``` csharp
Expand Down
42 changes: 42 additions & 0 deletions src/Gridify/GridifyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,48 @@ public IEnumerable<IGMap<T>> GetCurrentMaps()
{
return _mappings;
}

public IEnumerable<IGMap<T>> GetCurrentMapsByType(HashSet<Type> targetTypes)
{
foreach (var map in _mappings)
{
switch (map.To.Body)
{
case UnaryExpression unaryExpression:
{
if (targetTypes.Contains(unaryExpression.Operand.Type))
{
yield return map;
}

break;
}
case MethodCallExpression methodCallExpression:
{
if (targetTypes.Contains(methodCallExpression.Type))
{
yield return map;
}

break;
}
case MemberExpression memberExpression:
{
if (targetTypes.Contains(memberExpression.Type))
{
yield return map;
}

break;
}
}
}
}

public IEnumerable<IGMap<T>> GetCurrentMapsByType<TTarget>()
{
return GetCurrentMapsByType([typeof(TTarget)]);
}

/// <summary>
/// Converts current mappings to a comma seperated list of map names.
Expand Down
2 changes: 2 additions & 0 deletions src/Gridify/IGridifyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ IGridifyMapper<T> GenerateMappings(ushort maxNestingDepth)
bool HasMap(string key);
public GridifyMapperConfiguration Configuration { get; }
IEnumerable<IGMap<T>> GetCurrentMaps();
IEnumerable<IGMap<T>> GetCurrentMapsByType<TTarget>();
IEnumerable<IGMap<T>> GetCurrentMapsByType(HashSet<Type> targetTypes);
void ClearMappings();
}
29 changes: 26 additions & 3 deletions test/Gridify.Tests/GridifyMapperShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,31 @@ public void AddMap_DuplicateKey_ShouldThrowErrorIfOverrideIfExistsIsFalse()
//The thrown exception can be used for even more detailed assertions.
Assert.Equal("Duplicate Key. the 'test' key already exists", exception.Message);
}

[Theory]
[InlineData(typeof(DateTime), 1)]
[InlineData(typeof(DateTime?), 1)]
[InlineData(typeof(Guid), 1)]
[InlineData(typeof(string), 2)]
[InlineData(typeof(bool), 1)]
[InlineData(typeof(int), 1)]
public void GetMappingByType(Type filterType, int count)
{
var gm = new GridifyMapper<TestClass>(true);
var maps = gm.GetCurrentMapsByType([filterType]);
Assert.Equal(count, maps.Count());
}




[Fact]
public void GetMappingsByCustomType()
{
var gm = new GridifyMapper<TypeMappingTest>().GenerateMappings();
var maps = gm.GetCurrentMapsByType([typeof(IEnumerable<string>)]);
Assert.Single(maps);
}
}

public class TypeMappingTest
{
public List<string> MyListString { get; set; } = [];
}
1 change: 1 addition & 0 deletions test/Gridify.Tests/TestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public TestClass(int id, string name, TestClass? classProp, Guid myGuid = defaul
public string? Name { get; set; } = string.Empty;
public TestClass? ChildClass { get; set; }
public DateTime? MyDateTime { get; set; }
public DateTime AnotherDateTime { get; set; }
public Guid MyGuid { get; set; }
public string? Tag { get; set; }

Expand Down

0 comments on commit 4eccf13

Please sign in to comment.