Skip to content

Commit

Permalink
Changed the StringPrimaryKeyHandler so it takes a string for the pref…
Browse files Browse the repository at this point in the history
…ix, rather than the Func
  • Loading branch information
slewis74 committed Jun 24, 2021
1 parent 0a19e30 commit 9d35dea
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DocumentWithCustomPrefixAndStringIdMap : DocumentMap<DocumentWithCu

public DocumentWithCustomPrefixAndStringIdMap()
{
Id().KeyHandler(new StringPrimaryKeyHandler(_ => CustomPrefix));
Id().KeyHandler(new StringPrimaryKeyHandler(CustomPrefix));
Column(m => m.Name);
}
}
Expand Down
9 changes: 5 additions & 4 deletions source/Nevermore/Mapping/StringPrimaryKeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ namespace Nevermore.Mapping
{
public sealed class StringPrimaryKeyHandler : PrimaryKeyHandler<string>
{
readonly Func<string, string> idPrefix;
readonly Func<(string idPrefix, int key), string> format;

public StringPrimaryKeyHandler(Func<string, string>? idPrefix = null, Func<(string idPrefix, int key), string>? format = null)
public StringPrimaryKeyHandler(string? idPrefix = null, Func<(string idPrefix, int key), string>? format = null)
{
this.idPrefix = idPrefix ?? (tableName => $"{tableName}s");
IdPrefix = idPrefix;
this.format = format ?? (x => $"{x.idPrefix}-{x.key}");
}

public string? IdPrefix { get; private set; }

public override object GetNextKey(IKeyAllocator keyAllocator, string tableName)
{
var nextKey = keyAllocator.NextId(tableName);
return format((idPrefix(tableName), nextKey));
return format((IdPrefix ?? $"{tableName}s", nextKey));
}
}
}

0 comments on commit 9d35dea

Please sign in to comment.