| | 1 | | using System.Collections.Concurrent; |
| | 2 | | using System.Text.RegularExpressions; |
| | 3 | |
|
| | 4 | | namespace Kestrun.SharedState; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Thread‑safe, case‑insensitive global key/value store for reference‑type objects. |
| | 8 | | /// </summary> |
| | 9 | | public static partial class SharedStateStore |
| | 10 | | { |
| | 11 | | // ── configuration ─────────────────────────────────────────────── |
| 1 | 12 | | private static readonly Regex _validName = |
| 1 | 13 | | MyRegex(); |
| | 14 | |
|
| | 15 | | // StringComparer.OrdinalIgnoreCase ⇒ 100 % case‑insensitive keys |
| 1 | 16 | | private static readonly ConcurrentDictionary<string, object?> _store = |
| 1 | 17 | | new(StringComparer.OrdinalIgnoreCase); |
| | 18 | |
|
| | 19 | | // ── public API ────────────────────────────────────────────────── |
| | 20 | | /// <summary>Add or overwrite a value (reference‑types only).</summary> |
| | 21 | | public static bool Set(string name, object? value, bool allowsValueType = false) |
| | 22 | | { |
| 73 | 23 | | ValidateName(name); |
| 71 | 24 | | ValidateValue(name, value, allowsValueType); |
| 70 | 25 | | _store[name] = value; |
| 70 | 26 | | return true; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Checks if a variable with the specified name exists in the shared state. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="name">The name of the variable to check.</param> |
| | 33 | | /// <returns> <c>true</c> if the variable exists; otherwise, <c>false</c>.</returns> |
| 0 | 34 | | public static bool Contains(string name) => _store.ContainsKey(name); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Strongly‑typed fetch. Returns <c>false</c> if the key is missing |
| | 38 | | /// or the stored value can’t be cast to <typeparamref name="T"/>. |
| | 39 | | /// </summary> |
| | 40 | | public static bool TryGet<T>(string name, out T? value) |
| | 41 | | { |
| 2 | 42 | | if (_store.TryGetValue(name, out var raw) && raw is T cast) |
| | 43 | | { |
| 2 | 44 | | value = cast; |
| 2 | 45 | | return true; |
| | 46 | | } |
| 0 | 47 | | value = default; |
| 0 | 48 | | return false; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary>Untyped fetch; <c>null</c> if absent.</summary> |
| | 52 | | public static object? Get(string name) => |
| 0 | 53 | | _store.TryGetValue(name, out var val) ? val : null; |
| | 54 | |
|
| | 55 | | /// <summary>Snapshot of *all* current variables (shallow copy).</summary> |
| | 56 | | public static IReadOnlyDictionary<string, object?> Snapshot() => |
| 757 | 57 | | _store.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, |
| 81 | 58 | | StringComparer.OrdinalIgnoreCase); |
| | 59 | |
|
| | 60 | | /// <summary>Snapshot of keys only—handy for quick listings.</summary> |
| | 61 | | public static IReadOnlyCollection<string> KeySnapshot() => |
| 14 | 62 | | [.. _store.Keys]; |
| | 63 | |
|
| | 64 | | // ── helpers ──────────────────────────────────────────────────── |
| | 65 | | private static void ValidateName(string name) |
| | 66 | | { |
| 73 | 67 | | if (string.IsNullOrWhiteSpace(name) || !_validName.IsMatch(name)) |
| | 68 | | { |
| 2 | 69 | | throw new ArgumentException( |
| 2 | 70 | | $"'{name}' is not a valid identifier for C# / PowerShell.", |
| 2 | 71 | | nameof(name)); |
| | 72 | | } |
| 71 | 73 | | } |
| | 74 | |
|
| | 75 | | private static void ValidateValue(string name, object? value, bool allowsValueType = false) |
| | 76 | | { |
| 71 | 77 | | if (value is null) |
| | 78 | | { |
| 65 | 79 | | return; // null is allowed |
| | 80 | | } |
| | 81 | |
|
| 6 | 82 | | var t = value.GetType(); |
| 6 | 83 | | if (t.IsValueType && !allowsValueType) |
| | 84 | | { |
| 1 | 85 | | throw new ArgumentException( |
| 1 | 86 | | $"Cannot define global variable '{name}' of value type '{t.FullName}'. " + |
| 1 | 87 | | "Only reference types are allowed.", |
| 1 | 88 | | nameof(value)); |
| | 89 | | } |
| 5 | 90 | | } |
| | 91 | |
|
| | 92 | | [GeneratedRegex(@"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant)] |
| | 93 | | private static partial Regex MyRegex(); |
| | 94 | | } |