| | | 1 | | namespace TeleFlow.Framework.States; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Builds deterministic string keys from <see cref="StateKey"/> for durable state providers while |
| | | 5 | | /// preserving the structured state ownership model used by the runtime pipeline. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed class DefaultStateStorageKeyBuilder : IStateStorageKeyBuilder |
| | | 8 | | { |
| | | 9 | | public string Build(StateKey key, StateStorageKeyPart part) |
| | | 10 | | { |
| | 5 | 11 | | return string.Join( |
| | 5 | 12 | | ":", |
| | 5 | 13 | | Escape(key.Namespace), |
| | 5 | 14 | | FormatPart(part), |
| | 5 | 15 | | $"scope={Escape(key.Scope)}", |
| | 5 | 16 | | $"subject={Escape(key.Subject)}", |
| | 5 | 17 | | $"partition={Escape(key.Partition)}", |
| | 5 | 18 | | $"destiny={Escape(key.Destiny)}"); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | private static string FormatPart(StateStorageKeyPart part) |
| | | 22 | | { |
| | 5 | 23 | | return part switch |
| | 5 | 24 | | { |
| | 2 | 25 | | StateStorageKeyPart.State => "state", |
| | 1 | 26 | | StateStorageKeyPart.Data => "data", |
| | 1 | 27 | | StateStorageKeyPart.History => "history", |
| | 1 | 28 | | StateStorageKeyPart.Lock => "lock", |
| | 0 | 29 | | _ => throw new ArgumentOutOfRangeException(nameof(part), part, "Unknown state storage key part.") |
| | 5 | 30 | | }; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | private static string Escape(string? value) |
| | | 34 | | { |
| | 25 | 35 | | return value is null |
| | 25 | 36 | | ? string.Empty |
| | 25 | 37 | | : Uri.EscapeDataString(value); |
| | | 38 | | } |
| | | 39 | | } |