-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolate type mapper from service result (#369)
* Add type mapper interface * Don't throw in services if the type isn't mapped
- Loading branch information
1 parent
3969b05
commit e5a8461
Showing
35 changed files
with
358 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (C) Ubiquitous AS.All rights reserved | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
namespace Eventuous; | ||
|
||
public interface ITypeMapper { | ||
public const string UnknownType = "unknown"; | ||
|
||
/// <summary> | ||
/// Try getting a type name for a given type | ||
/// </summary> | ||
/// <param name="type">Type for which the name is requested</param> | ||
/// <param name="typeName">Registered type name or null if the type isn't registered</param> | ||
/// <returns>True if the type is registered, false otherwise</returns> | ||
bool TryGetTypeName(Type type, [NotNullWhen(true)] out string? typeName); | ||
|
||
/// <summary> | ||
/// Try getting a registered type for a given name | ||
/// </summary> | ||
/// <param name="typeName">Known type name</param> | ||
/// <param name="type">Registered type for a given name or null if the type name isn't registered</param> | ||
/// <returns>True if the type is registered, false otherwise</returns> | ||
bool TryGetType(string typeName, [NotNullWhen(true)] out Type? type); | ||
} | ||
|
||
public interface ITypeMapperExt : ITypeMapper { | ||
IEnumerable<(string TypeName, Type Type)> GetRegisteredTypes(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Core/src/Eventuous.Shared/TypeMap/TypeMapperExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (C) Ubiquitous AS.All rights reserved | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
namespace Eventuous; | ||
|
||
using static TypeMapEventSource; | ||
|
||
public static class TypeMapperExtensions { | ||
/// <summary> | ||
/// Get the type name for a given type | ||
/// </summary> | ||
/// <param name="typeMapper">Type mapper instance</param> | ||
/// <param name="type">Object type for which the name needs to be retrieved</param> | ||
/// <param name="fail">Indicates if exception should be thrown if the type is now registered</param> | ||
/// <returns>Type name from the map or "unknown" if the type isn't registered and <code>fail</code> is set to false</returns> | ||
/// <exception cref="UnregisteredTypeException">Thrown if the type isn't registered and fail is set to true</exception> | ||
public static string GetTypeNameByType(this ITypeMapper typeMapper, Type type, bool fail = true) { | ||
var typeKnown = typeMapper.TryGetTypeName(type, out var name); | ||
|
||
if (!typeKnown && fail) { | ||
Log.TypeNotMappedToName(type); | ||
|
||
throw new UnregisteredTypeException(type); | ||
} | ||
|
||
return name ?? ITypeMapper.UnknownType; | ||
} | ||
|
||
public static string GetTypeName(this ITypeMapper typeMapper, object o, bool fail = true) => typeMapper.GetTypeNameByType(o.GetType(), fail); | ||
|
||
public static string GetTypeName<T>(this ITypeMapper typeMapper, bool fail = true) => typeMapper.GetTypeNameByType(typeof(T), fail); | ||
|
||
public static bool TryGetTypeName<T>(this ITypeMapper typeMapper, [NotNullWhen(true)] out string? typeName) => typeMapper.TryGetTypeName(typeof(T), out typeName); | ||
|
||
/// <summary> | ||
/// Get the registered type for a given name | ||
/// </summary> | ||
/// <param name="typeMapper">Type mapper instance</param> | ||
/// <param name="typeName">Type name for which the type needs to be returned</param> | ||
/// <returns>Type that matches the given name</returns> | ||
/// <exception cref="UnregisteredTypeException">Thrown if the type isn't registered and fail is set to true</exception> | ||
public static Type GetType(this ITypeMapper typeMapper, string typeName) { | ||
var typeKnown = typeMapper.TryGetType(typeName, out var type); | ||
|
||
if (!typeKnown) { | ||
Log.TypeNameNotMappedToType(typeName); | ||
|
||
throw new UnregisteredTypeException(typeName); | ||
} | ||
|
||
return type!; | ||
} | ||
|
||
public static void EnsureTypesRegistered(this ITypeMapper typeMapper, IEnumerable<Type> types) { | ||
foreach (var type in types) { | ||
typeMapper.GetTypeNameByType(type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.