Skip to content

Commit bfdbbff

Browse files
authored
feat: add a method to check if a scheme can be handled (#749)
1 parent 67b582a commit bfdbbff

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

DubUrl.Core/Mapping/SchemeMapperBuilder.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,52 @@ namespace DubUrl.Mapping;
1515

1616
public class SchemeMapperBuilder
1717
{
18+
private char[] Separators = ['+', ':'];
19+
1820
private readonly record struct ProviderInfo(string ProviderName, List<string> Aliases, Type DialectType, DriverLocatorFactory? DriverLocatorFactory);
1921
private bool IsBuilt { get; set; } = false;
2022

2123
private List<MapperInfo> MapperData { get; } = new();
2224

23-
protected Dictionary<string, IMapper> Mappers { get; set; } = new();
24-
private BaseMapperIntrospector[] MapperIntrospectors { get; } = new BaseMapperIntrospector[] { new NativeMapperIntrospector(), new WrapperMapperIntrospector() };
25+
protected Dictionary<string, IMapper> Mappers { get; set; } = [];
26+
private BaseMapperIntrospector[] MapperIntrospectors { get; } = [new NativeMapperIntrospector(), new WrapperMapperIntrospector()];
2527
private DialectBuilder DialectBuilder { get; } = new();
2628

2729
public SchemeMapperBuilder()
28-
: this(new[] { typeof(SchemeMapperBuilder).Assembly }) { }
30+
: this([typeof(SchemeMapperBuilder).Assembly]) { }
2931

3032
public SchemeMapperBuilder(Assembly[] assemblies)
3133
{
3234
var asmTypesProbe = new AssemblyTypesProbe(assemblies);
33-
MapperIntrospectors = new BaseMapperIntrospector[]
34-
{
35+
MapperIntrospectors =
36+
[
3537
new NativeMapperIntrospector(asmTypesProbe)
36-
, new WrapperMapperIntrospector(asmTypesProbe)
37-
};
38+
,
39+
new WrapperMapperIntrospector(asmTypesProbe)
40+
];
3841
Initialize();
3942
}
4043

4144
protected virtual void Initialize()
4245
{
43-
foreach (var mapperData
46+
foreach (var mapperData
4447
in MapperIntrospectors.Aggregate(
4548
Array.Empty<MapperInfo>(), (data, introspector)
4649
=> data.Concat(introspector.Locate()).ToArray())
4750
)
4851
AddMapping(mapperData);
4952
}
5053

54+
public bool CanHandle(string scheme)
55+
{
56+
var alias = GetAlias(scheme.Split(Separators));
57+
return Mappers.ContainsKey(alias);
58+
}
59+
5160
public virtual void Build()
5261
{
5362
foreach (var mapperData in MapperData)
54-
DialectBuilder.AddAliases(mapperData.DialectType, mapperData.Aliases.ToArray());
63+
DialectBuilder.AddAliases(mapperData.DialectType, [.. mapperData.Aliases]);
5564
DialectBuilder.Build();
5665

5766
Mappers.Clear();
@@ -107,7 +116,7 @@ private static string GetAlias(string[] aliases)
107116
}
108117

109118
public IMapper GetMapper(string alias)
110-
=> GetMapper(new[] { alias });
119+
=> GetMapper(alias.Split(Separators));
111120

112121
public virtual IMapper GetMapper(string[] aliases)
113122
{

DubUrl.Testing/Mapping/SchemeMapperBuilderTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ public StubRewriter(DbConnectionStringBuilder csb)
6565
[TestCase("ts", typeof(TimescaleMapper))]
6666
[TestCase("quest", typeof(QuestDbMapper))]
6767
[TestCase("tr", typeof(TrinoMapper))]
68-
//[TestCase("odbc", typeof(OdbcMapper))]
6968
[TestCase("odbc+mssql", typeof(OdbcMapper))]
7069
[TestCase("mssql+odbc", typeof(OdbcMapper))]
71-
public void Instantiate_Scheme_CorrectType(string schemeList, Type expected)
70+
public void Instantiate_Scheme_CorrectType(string scheme, Type expected)
7271
{
7372
var builder = new SchemeMapperBuilder();
7473
builder.Build();
75-
var result = builder.GetMapper(schemeList.Split(new[] { '+', ':' }));
74+
var result = builder.GetMapper(scheme.Split(['+', ':']));
7675

7776
Assert.That(result, Is.Not.Null);
7877
Assert.That(result, Is.TypeOf(expected));
78+
Assert.That(builder.CanHandle(scheme), Is.True);
7979
}
8080

8181
[Test]

0 commit comments

Comments
 (0)