Skip to content

Commit d38af37

Browse files
ankaneroji
andcommitted
Simplified TypeMappingSourcePlugin code - #50
Co-authored-by: Shay Rojansky <[email protected]>
1 parent c8d117d commit d38af37

8 files changed

+22
-67
lines changed

src/Pgvector.EntityFrameworkCore/HalfvecTypeMapping.cs

-19
This file was deleted.

src/Pgvector.EntityFrameworkCore/HalfvecTypeMappingSourcePlugin.cs

-11
This file was deleted.

src/Pgvector.EntityFrameworkCore/SparsevecTypeMapping.cs

-19
This file was deleted.

src/Pgvector.EntityFrameworkCore/SparsevecTypeMappingSourcePlugin.cs

-11
This file was deleted.

src/Pgvector.EntityFrameworkCore/VectorDbContextOptionsExtension.cs

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public void ApplyServices(IServiceCollection services)
1717
.TryAdd<IMethodCallTranslatorPlugin, VectorDbFunctionsTranslatorPlugin>();
1818

1919
services.AddSingleton<IRelationalTypeMappingSourcePlugin, VectorTypeMappingSourcePlugin>();
20-
services.AddSingleton<IRelationalTypeMappingSourcePlugin, HalfvecTypeMappingSourcePlugin>();
21-
services.AddSingleton<IRelationalTypeMappingSourcePlugin, SparsevecTypeMappingSourcePlugin>();
2220
}
2321

2422
public void Validate(IDbContextOptions options) { }

src/Pgvector.EntityFrameworkCore/VectorDesignTimeServices.cs

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ public class VectorDesignTimeServices : IDesignTimeServices
1010
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
1111
=> serviceCollection
1212
.AddSingleton<IRelationalTypeMappingSourcePlugin, VectorTypeMappingSourcePlugin>()
13-
.AddSingleton<IRelationalTypeMappingSourcePlugin, HalfvecTypeMappingSourcePlugin>()
14-
.AddSingleton<IRelationalTypeMappingSourcePlugin, SparsevecTypeMappingSourcePlugin>()
1513
.AddSingleton<IProviderCodeGeneratorPlugin, VectorCodeGeneratorPlugin>();
1614
}

src/Pgvector.EntityFrameworkCore/VectorTypeMapping.cs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public VectorTypeMapping() : base("vector", typeof(Vector)) { }
1212

1313
public VectorTypeMapping(string storeType) : base(storeType, typeof(Vector)) { }
1414

15+
public VectorTypeMapping(string storeType, Type clrType) : base(storeType, clrType) { }
16+
1517
protected VectorTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }
1618

1719
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)

src/Pgvector.EntityFrameworkCore/VectorTypeMappingSourcePlugin.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,24 @@ namespace Pgvector.EntityFrameworkCore;
55
public class VectorTypeMappingSourcePlugin : IRelationalTypeMappingSourcePlugin
66
{
77
public RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
8-
=> mappingInfo.ClrType == typeof(Vector) || (mappingInfo.ClrType == null && (mappingInfo.StoreTypeNameBase ?? mappingInfo.StoreTypeName) == "vector")
9-
? new VectorTypeMapping(mappingInfo.StoreTypeName ?? "vector")
10-
: null;
8+
{
9+
if (mappingInfo.ClrType == null)
10+
{
11+
return (mappingInfo.StoreTypeNameBase ?? mappingInfo.StoreTypeName) switch
12+
{
13+
"vector" => new VectorTypeMapping(mappingInfo.StoreTypeName ?? "vector", typeof(Vector)),
14+
"halfvec" => new VectorTypeMapping(mappingInfo.StoreTypeName ?? "halfvec", typeof(HalfVector)),
15+
"sparsevec" => new VectorTypeMapping(mappingInfo.StoreTypeName ?? "sparsevec", typeof(SparseVector)),
16+
_ => null,
17+
};
18+
}
19+
20+
return mappingInfo.ClrType switch
21+
{
22+
var t when t == typeof(Vector) => new VectorTypeMapping(mappingInfo.StoreTypeName ?? "vector", typeof(Vector)),
23+
var t when t == typeof(HalfVector) => new VectorTypeMapping(mappingInfo.StoreTypeName ?? "halfvec", typeof(HalfVector)),
24+
var t when t == typeof(SparseVector) => new VectorTypeMapping(mappingInfo.StoreTypeName ?? "sparsevec", typeof(SparseVector)),
25+
_ => null,
26+
};
27+
}
1128
}

0 commit comments

Comments
 (0)