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 1 commit
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
20 changes: 20 additions & 0 deletions src/Gridify/GridifyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,26 @@ public IEnumerable<IGMap<T>> GetCurrentMaps()
{
return _mappings;
}

public IEnumerable<IGMap<T>> GetCurrentMapsByType(HashSet<Type> targetTypes)
{
foreach (var map in _mappings)
{
if (map.To.Body is UnaryExpression unaryExpression)
alirezanet marked this conversation as resolved.
Show resolved Hide resolved
{
if (targetTypes.Contains(unaryExpression.Operand.Type))
{
yield return map;
}
}
// TODO: need to implement other types that aren't unary expression
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be completed to support and test all types; currently, it only works with unary expressions. Adding a couple of additional unit tests for known dotnet types and a few custom types is also required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ppdated the method a bit further, I am not sure what other types of expressions are out there. Where can I find the list?

Probably my lack of depth in understanding expressions. This approach isn't looking so hot anymore, where we have to figure out each expression type to find the property type.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling all types of expressions isn't necessary; I believe these three cover at least 80% of the types, and so far, it looks very promising. Perhaps we should consider adding another test case to determine if we can support custom types. Lastly, the PR seems fine, but the documentation needs to be updated.

}
}

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();
}
7 changes: 7 additions & 0 deletions test/Gridify.Tests/GridifyMapperShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ public void AddMap_DuplicateKey_ShouldThrowErrorIfOverrideIfExistsIsFalse()
}


[Fact]
public void GetMappingByType()
{
var gm = new GridifyMapper<TestClass>(true);

var dates = gm.GetCurrentMapsByType([typeof(DateTime), typeof(DateTime?)]);

Assert.Single(dates);
}
}
Loading