| | | 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 partial class SharedState |
| | | 10 | | { |
| | | 11 | | // ── configuration ─────────────────────────────────────────────── |
| | 1 | 12 | | private static readonly Regex _validName = |
| | 1 | 13 | | MyRegex(); |
| | | 14 | | |
| | | 15 | | // StringComparer.OrdinalIgnoreCase ⇒ 100 % case‑insensitive keys |
| | | 16 | | private readonly ConcurrentDictionary<string, object?> _store; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="SharedState"/> class. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="ordinalIgnoreCase">If <c>true</c>, keys are compared in a case-insensitive manner; otherwise, case- |
| | 518 | 22 | | public SharedState(bool ordinalIgnoreCase = true) |
| | | 23 | | { |
| | 518 | 24 | | if (ordinalIgnoreCase) |
| | | 25 | | { |
| | 517 | 26 | | _store = new(StringComparer.OrdinalIgnoreCase); |
| | 517 | 27 | | return; |
| | | 28 | | } |
| | | 29 | | else |
| | | 30 | | { |
| | 1 | 31 | | _store = new(StringComparer.Ordinal); |
| | | 32 | | } |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | // ── public API ────────────────────────────────────────────────── |
| | | 36 | | /// <summary> |
| | | 37 | | /// Add or overwrite a value (reference‑types only). |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="name">The name of the variable to set.</param> |
| | | 40 | | /// <param name="value">The value to set. Must be a reference type unless <paramref name="allowsValueType"/> is <c>t |
| | | 41 | | /// <param name="allowsValueType">If <c>true</c>, allows setting value types; otherwise, only reference types are al |
| | | 42 | | /// <returns><c>true</c> if the value was set successfully.</returns> |
| | | 43 | | public bool Set(string name, object? value, bool allowsValueType = false) |
| | | 44 | | { |
| | 562 | 45 | | ValidateName(name); |
| | 547 | 46 | | ValidateValue(name, value, allowsValueType); |
| | 539 | 47 | | _store[name] = value; |
| | 539 | 48 | | return true; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Checks if a variable with the specified name exists in the shared state. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="name">The name of the variable to check.</param> |
| | | 55 | | /// <returns> <c>true</c> if the variable exists; otherwise, <c>false</c>.</returns> |
| | 33 | 56 | | public bool Contains(string name) => _store.ContainsKey(name); |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Strongly‑typed fetch. Returns <c>false</c> if the key is missing |
| | | 60 | | /// or the stored value can’t be cast to <typeparamref name="T"/>. |
| | | 61 | | /// </summary> |
| | | 62 | | public bool TryGet<T>(string name, out T? value) |
| | | 63 | | { |
| | 16 | 64 | | if (_store.TryGetValue(name, out var raw) && raw is T cast) |
| | | 65 | | { |
| | 10 | 66 | | value = cast; |
| | 10 | 67 | | return true; |
| | | 68 | | } |
| | 6 | 69 | | value = default; |
| | 6 | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary>Untyped fetch; <c>null</c> if absent.</summary> |
| | | 74 | | public object? Get(string name) => |
| | 61 | 75 | | _store.TryGetValue(name, out var val) ? val : null; |
| | | 76 | | |
| | | 77 | | /// <summary>Snapshot of *all* current variables (shallow copy).</summary> |
| | | 78 | | public IReadOnlyDictionary<string, object?> Snapshot() => |
| | 276 | 79 | | _store.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, |
| | 248 | 80 | | StringComparer.OrdinalIgnoreCase); |
| | | 81 | | |
| | | 82 | | /// <summary>Snapshot of keys only—handy for quick listings.</summary> |
| | | 83 | | public IReadOnlyCollection<string> KeySnapshot() => |
| | 5 | 84 | | [.. _store.Keys]; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Clears all entries in the shared state. |
| | | 88 | | /// </summary> |
| | | 89 | | public void Clear() => |
| | 109 | 90 | | _store.Clear(); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the number of key/value pairs in the shared state. |
| | | 94 | | /// </summary> |
| | | 95 | | public int Count => |
| | 27 | 96 | | _store.Count; |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets an enumerable collection of all keys in the shared state. |
| | | 99 | | /// </summary> |
| | | 100 | | public IEnumerable<string> Keys => |
| | 3 | 101 | | _store.Keys; |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets an enumerable collection of all values in the shared state. |
| | | 104 | | /// </summary> |
| | | 105 | | public IEnumerable<object?> Values => |
| | 3 | 106 | | _store.Values; |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Attempts to remove the value with the specified name from the shared state. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="name">The name of the variable to remove.</param> |
| | | 112 | | /// <returns><c>true</c> if the variable was successfully removed; otherwise, <c>false</c>.</returns> |
| | | 113 | | public bool Remove(string name) => |
| | 209 | 114 | | _store.Remove(name, out _); |
| | | 115 | | |
| | | 116 | | // ── helpers ──────────────────────────────────────────────────── |
| | | 117 | | private static void ValidateName(string name) |
| | | 118 | | { |
| | 562 | 119 | | if (string.IsNullOrWhiteSpace(name) || !_validName.IsMatch(name)) |
| | | 120 | | { |
| | 15 | 121 | | throw new ArgumentException( |
| | 15 | 122 | | $"'{name}' is not a valid identifier for C# / PowerShell.", |
| | 15 | 123 | | nameof(name)); |
| | | 124 | | } |
| | 547 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Validates the value to be stored in the shared state. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="name">The name of the variable.</param> |
| | | 131 | | /// <param name="value">The value to validate.</param> |
| | | 132 | | /// <param name="allowsValueType">Indicates whether value types are allowed.</param> |
| | | 133 | | /// <exception cref="ArgumentException">Thrown if the value is a value type and value types are not allowed.</except |
| | | 134 | | private static void ValidateValue(string name, object? value, bool allowsValueType = false) |
| | | 135 | | { |
| | 547 | 136 | | if (value is null) |
| | | 137 | | { |
| | 5 | 138 | | return; // null is allowed |
| | | 139 | | } |
| | | 140 | | |
| | 542 | 141 | | var t = value.GetType(); |
| | 542 | 142 | | if (t.IsValueType && !allowsValueType) |
| | | 143 | | { |
| | 8 | 144 | | throw new ArgumentException( |
| | 8 | 145 | | $"Cannot define global variable '{name}' of value type '{t.FullName}'. " + |
| | 8 | 146 | | "Only reference types are allowed.", |
| | 8 | 147 | | nameof(value)); |
| | | 148 | | } |
| | 534 | 149 | | } |
| | | 150 | | |
| | | 151 | | [GeneratedRegex(@"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant)] |
| | | 152 | | private static partial Regex MyRegex(); |
| | | 153 | | } |