diff --git a/docs/pages/guide/gridifyMapper.md b/docs/pages/guide/gridifyMapper.md index 690ced1d..266ea5cb 100644 --- a/docs/pages/guide/gridifyMapper.md +++ b/docs/pages/guide/gridifyMapper.md @@ -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 diff --git a/src/Gridify/GridifyMapper.cs b/src/Gridify/GridifyMapper.cs index 52dff2a1..6de7032b 100644 --- a/src/Gridify/GridifyMapper.cs +++ b/src/Gridify/GridifyMapper.cs @@ -220,6 +220,48 @@ public IEnumerable> GetCurrentMaps() { return _mappings; } + + public IEnumerable> GetCurrentMapsByType(HashSet 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> GetCurrentMapsByType() + { + return GetCurrentMapsByType([typeof(TTarget)]); + } /// /// Converts current mappings to a comma seperated list of map names. diff --git a/src/Gridify/IGridifyMapper.cs b/src/Gridify/IGridifyMapper.cs index e537b78c..f4409aa4 100644 --- a/src/Gridify/IGridifyMapper.cs +++ b/src/Gridify/IGridifyMapper.cs @@ -47,5 +47,7 @@ IGridifyMapper GenerateMappings(ushort maxNestingDepth) bool HasMap(string key); public GridifyMapperConfiguration Configuration { get; } IEnumerable> GetCurrentMaps(); + IEnumerable> GetCurrentMapsByType(); + IEnumerable> GetCurrentMapsByType(HashSet targetTypes); void ClearMappings(); } diff --git a/test/Gridify.Tests/GridifyMapperShould.cs b/test/Gridify.Tests/GridifyMapperShould.cs index 94cadfa5..bfed83b6 100644 --- a/test/Gridify.Tests/GridifyMapperShould.cs +++ b/test/Gridify.Tests/GridifyMapperShould.cs @@ -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(true); + var maps = gm.GetCurrentMapsByType([filterType]); + Assert.Equal(count, maps.Count()); + } - - - + [Fact] + public void GetMappingsByCustomType() + { + var gm = new GridifyMapper().GenerateMappings(); + var maps = gm.GetCurrentMapsByType([typeof(IEnumerable)]); + Assert.Single(maps); + } } + +public class TypeMappingTest +{ + public List MyListString { get; set; } = []; +} \ No newline at end of file diff --git a/test/Gridify.Tests/TestClass.cs b/test/Gridify.Tests/TestClass.cs index 5d0756d3..95a0ff1e 100644 --- a/test/Gridify.Tests/TestClass.cs +++ b/test/Gridify.Tests/TestClass.cs @@ -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; }