From cc3a36141e0d3ae8b066d0cc287057d7eef01009 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Sat, 15 Oct 2022 05:27:41 +0700 Subject: [PATCH] [BACKPORT #6173] Add HashCodeMessageExtractor factory (#6182) * add HashCodeMEssageExtractor Factory method (#6173) * feat: add add HashCodeMEssageExtractor Factory method * test: verified change of previous commit * test: verified missing api changes Co-authored-by: Aaron Stannard Co-authored-by: Gregorius Soedharmo * Update API Verify list Co-authored-by: JonnyII <102029509+JonnyII@users.noreply.github.com> Co-authored-by: Aaron Stannard --- .../Akka.Cluster.Sharding/ClusterSharding.cs | 27 +++++++++++++++++++ ...PISpec.ApproveClusterSharding.verified.txt | 1 + 2 files changed, 28 insertions(+) diff --git a/src/contrib/cluster/Akka.Cluster.Sharding/ClusterSharding.cs b/src/contrib/cluster/Akka.Cluster.Sharding/ClusterSharding.cs index 44860d928e4..326e2f3cc8e 100644 --- a/src/contrib/cluster/Akka.Cluster.Sharding/ClusterSharding.cs +++ b/src/contrib/cluster/Akka.Cluster.Sharding/ClusterSharding.cs @@ -55,6 +55,33 @@ public override ClusterSharding CreateExtension(ExtendedActorSystem system) /// public abstract class HashCodeMessageExtractor : IMessageExtractor { + private class Implementation : HashCodeMessageExtractor + { + private readonly Func _entityIdExtractor; + private readonly Func _messageExtractor; + public Implementation(int maxNumberOfShards, Func entityIdExtractor, Func messageExtractor = null) : base(maxNumberOfShards) + { + _entityIdExtractor = entityIdExtractor ?? throw new NullReferenceException(nameof(entityIdExtractor)); + _messageExtractor = messageExtractor; + } + + public override string EntityId(object message) + => _entityIdExtractor.Invoke(message); + + public override object EntityMessage(object message) + => _messageExtractor?.Invoke(message) ?? base.EntityMessage(message); + } + + /// + /// creates a instance of the with the given handlers + /// + /// + /// + /// + /// + public static HashCodeMessageExtractor Create(int maxNumberOfShards, Func entityIdExtractor, Func messageExtractor = null) + => new Implementation(maxNumberOfShards, entityIdExtractor, messageExtractor); + /// /// TBD /// diff --git a/src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterSharding.verified.txt b/src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterSharding.verified.txt index 6366aa9828f..c59d8ba3db9 100644 --- a/src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterSharding.verified.txt +++ b/src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterSharding.verified.txt @@ -135,6 +135,7 @@ namespace Akka.Cluster.Sharding { public readonly int MaxNumberOfShards; protected HashCodeMessageExtractor(int maxNumberOfShards) { } + public static Akka.Cluster.Sharding.HashCodeMessageExtractor Create(int maxNumberOfShards, System.Func entityIdExtractor, System.Func messageExtractor = null) { } public abstract string EntityId(object message); public virtual object EntityMessage(object message) { } public virtual string ShardId(object message) { }