Skip to content

Properly persist computed colums for same type #253

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

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/TableStorage/EntityPropertiesMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Devlooped
{
class EntityPropertiesMapper
{
static readonly ConcurrentDictionary<Type, PropertyInfo[]> propertiesMap = new();
static readonly ConcurrentDictionary<(Type, string), PropertyInfo[]> propertiesMap = new();
static readonly ConcurrentDictionary<Type, IDictionary<string, string>> edmAnnotations = new();
static readonly KeyValuePair<string, string> edmNone = new("", "");

Expand All @@ -22,7 +22,7 @@ private EntityPropertiesMapper() { }

public IDictionary<string, object> ToProperties<T>(T entity, string? partitionKeyProperty = default, string? rowKeyProperty = default) where T: notnull
{
var properties = propertiesMap.GetOrAdd(entity.GetType(), type => type
var properties = propertiesMap.GetOrAdd((entity.GetType(), $"{partitionKeyProperty}|{rowKeyProperty}"), key => key.Item1
.GetProperties()
.Where(prop =>
prop.GetCustomAttribute<BrowsableAttribute>()?.Browsable != false &&
Expand Down
40 changes: 40 additions & 0 deletions src/Tests/RepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,46 @@ public async Task CanGetFromEdmEntityTypes()
Assert.IsType<long>(generic["Count"]);
}

[Fact]
public async Task CanPersistPropertiesFromComputedRowKeys()
{
var storage = CloudStorageAccount.DevelopmentStorageAccount;
var repo1 = TableRepository.Create<Dependency>(
storage,
tableName: "Dependency",
partitionKey: d => d.Repository,
rowKey: d => $"{d.Name}|{d.Version}");

var repo2 = TableRepository.Create<Dependency>(
storage,
tableName: "Dependency",
partitionKey: d => d.Name,
rowKey: d => $"{d.Version}|{d.Repository}");

var dep = new Dependency("org", "repo", "npm", "foo", "1.0");

await repo1.PutAsync(dep);
await repo2.PutAsync(dep);

var entities = TableRepository.Create(storage, "Dependency");

var entity = await entities.GetAsync("repo", "foo|1.0");
Assert.NotNull(entity);
// Since the PK is the Repository property, it's not persisted as a property.
Assert.Null(entity[nameof(Dependency.Repository)]);
// But the name is, since it's a computed row key.
Assert.Equal("foo", entity[nameof(Dependency.Name)]);

entity = await entities.GetAsync("foo", "1.0|repo");
Assert.NotNull(entity);
// Conversely, since the PK here is the Name property, the Repository should be persisted.
Assert.Equal("repo", entity[nameof(Dependency.Repository)]);
// And the Name shouldn't, since it's the PK
Assert.Null(entity[nameof(Dependency.Name)]);
}

record Dependency(string Organization, string Repository, string Type, string Name, string Version);

class MyEntity
{
public MyEntity(string id) => Id = id;
Expand Down