Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Maps by Type #214

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading