|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using System.Text.Json; |
| 3 | + |
| 4 | +namespace OpenClaw.Shared; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Canonical home for the <em>non-nullable, fallback-returning</em> family of |
| 8 | +/// <see cref="JsonElement"/> coercion helpers: the gateway-client-style getters |
| 9 | +/// (<c>GetString</c> returning <c>null</c> when absent, numeric getters returning |
| 10 | +/// a caller-supplied fallback, <c>GetArrayLength</c>, and a verbatim |
| 11 | +/// <c>FirstNonEmpty</c>) plus the defensive non-object guard that the |
| 12 | +/// <c>GetStringSafe</c> variant added. See docs/ARCHITECTURE.md → |
| 13 | +/// <c>json-read-helpers</c>. |
| 14 | +/// |
| 15 | +/// All accessors are <em>total</em> (they never throw): a missing property, a |
| 16 | +/// JSON null, a value of the wrong <see cref="JsonValueKind"/>, or a non-object |
| 17 | +/// <paramref name="parent"/> yields the documented fallback (<c>null</c> / 0 / |
| 18 | +/// the caller-supplied fallback) rather than an exception — including a non-object |
| 19 | +/// element, which <see cref="JsonElement.TryGetProperty(string, out JsonElement)"/> |
| 20 | +/// would otherwise throw on. |
| 21 | +/// |
| 22 | +/// <para><b>Scope — do not blindly migrate.</b> This is deliberately NOT a |
| 23 | +/// drop-in for every private JSON helper in the repo. Several existing helpers |
| 24 | +/// have different contracts; routing them through these methods changes behavior |
| 25 | +/// and needs a call-site guard or a dedicated variant:</para> |
| 26 | +/// <list type="bullet"> |
| 27 | +/// <item><c>Models.GetInt</c> / <c>Models.GetLong</c> return <c>int?</c> / |
| 28 | +/// <c>long?</c> (a null sentinel means "missing"), and <c>Models.GetLong</c> has |
| 29 | +/// no double-to-long fallback — the canonical <see cref="GetLong"/> instead |
| 30 | +/// truncates a fractional number.</item> |
| 31 | +/// <item><c>WorkspaceFilesModel.GetLong</c> rejects negatives (requires |
| 32 | +/// <c>>= 0</c>); the canonical getters accept negative numbers.</item> |
| 33 | +/// <item><c>WindowsNodeClient.TryGetString</c> treats empty/whitespace as |
| 34 | +/// <em>absent</em>; the canonical <see cref="TryGetString"/> returns <c>true</c> |
| 35 | +/// with an empty string ("" is present).</item> |
| 36 | +/// <item><c>Mxc.MxcAvailability.FirstNonEmpty</c> trims its result, and |
| 37 | +/// <c>SetupSteps.FirstNonEmpty</c> trims and falls back to <c>"no output"</c>; |
| 38 | +/// the canonical <see cref="FirstNonEmpty"/> returns the first value verbatim, or |
| 39 | +/// <c>null</c>.</item> |
| 40 | +/// <item>Some older getters throw on a non-object <paramref name="parent"/> |
| 41 | +/// (failing the parse); the canonical getters are total and return the fallback.</item> |
| 42 | +/// </list> |
| 43 | +/// </summary> |
| 44 | +internal static class JsonReadHelpers |
| 45 | +{ |
| 46 | + /// <summary> |
| 47 | + /// Returns the string value of <paramref name="property"/>, or <c>null</c> |
| 48 | + /// when <paramref name="parent"/> is not an object, the property is absent, or |
| 49 | + /// the value is not a JSON string. The returned string may be empty; only |
| 50 | + /// missing / non-string values collapse to <c>null</c>. |
| 51 | + /// </summary> |
| 52 | + internal static string? GetString(JsonElement parent, string property) |
| 53 | + { |
| 54 | + if (parent.ValueKind != JsonValueKind.Object |
| 55 | + || !parent.TryGetProperty(property, out var value) |
| 56 | + || value.ValueKind != JsonValueKind.String) |
| 57 | + { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + return value.GetString(); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Try-pattern companion to <see cref="GetString"/>. Returns <c>true</c> and |
| 66 | + /// sets <paramref name="value"/> when the property exists and is a JSON string |
| 67 | + /// (the string may be empty); otherwise returns <c>false</c> with |
| 68 | + /// <paramref name="value"/> set to <c>null</c>. |
| 69 | + /// </summary> |
| 70 | + internal static bool TryGetString(JsonElement parent, string property, [NotNullWhen(true)] out string? value) |
| 71 | + { |
| 72 | + value = GetString(parent, property); |
| 73 | + return value is not null; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Returns the <see cref="int"/> value of <paramref name="property"/>, or |
| 78 | + /// <paramref name="fallback"/> when it is missing or not a JSON number. A |
| 79 | + /// number within <see cref="long"/> range that exceeds the <see cref="int"/> |
| 80 | + /// range is clamped to the <see cref="int"/> range; a number beyond |
| 81 | + /// <see cref="long"/> range returns <paramref name="fallback"/> (matching the |
| 82 | + /// prior private helper). |
| 83 | + /// </summary> |
| 84 | + internal static int GetInt(JsonElement parent, string property, int fallback = 0) |
| 85 | + { |
| 86 | + if (parent.ValueKind != JsonValueKind.Object |
| 87 | + || !parent.TryGetProperty(property, out var value) |
| 88 | + || value.ValueKind != JsonValueKind.Number) |
| 89 | + { |
| 90 | + return fallback; |
| 91 | + } |
| 92 | + |
| 93 | + if (value.TryGetInt32(out var intValue)) |
| 94 | + { |
| 95 | + return intValue; |
| 96 | + } |
| 97 | + |
| 98 | + if (value.TryGetInt64(out var longValue)) |
| 99 | + { |
| 100 | + return (int)Math.Clamp(longValue, int.MinValue, int.MaxValue); |
| 101 | + } |
| 102 | + |
| 103 | + return fallback; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Returns the <see cref="long"/> value of <paramref name="property"/>, or |
| 108 | + /// <paramref name="fallback"/> when it is missing or not a JSON number. A |
| 109 | + /// fractional number is truncated toward zero. |
| 110 | + /// </summary> |
| 111 | + internal static long GetLong(JsonElement parent, string property, long fallback = 0) |
| 112 | + { |
| 113 | + if (parent.ValueKind != JsonValueKind.Object |
| 114 | + || !parent.TryGetProperty(property, out var value) |
| 115 | + || value.ValueKind != JsonValueKind.Number) |
| 116 | + { |
| 117 | + return fallback; |
| 118 | + } |
| 119 | + |
| 120 | + if (value.TryGetInt64(out var longValue)) |
| 121 | + { |
| 122 | + return longValue; |
| 123 | + } |
| 124 | + |
| 125 | + if (value.TryGetDouble(out var doubleValue)) |
| 126 | + { |
| 127 | + return (long)doubleValue; |
| 128 | + } |
| 129 | + |
| 130 | + return fallback; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Returns the <see cref="double"/> value of <paramref name="property"/>, or |
| 135 | + /// <paramref name="fallback"/> when it is missing or not a JSON number. |
| 136 | + /// </summary> |
| 137 | + internal static double GetDouble(JsonElement parent, string property, double fallback = 0) |
| 138 | + { |
| 139 | + if (parent.ValueKind != JsonValueKind.Object |
| 140 | + || !parent.TryGetProperty(property, out var value) |
| 141 | + || value.ValueKind != JsonValueKind.Number) |
| 142 | + { |
| 143 | + return fallback; |
| 144 | + } |
| 145 | + |
| 146 | + return value.TryGetDouble(out var doubleValue) ? doubleValue : fallback; |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Returns the number of elements in the array at <paramref name="property"/>, |
| 151 | + /// or 0 when it is missing or not a JSON array. |
| 152 | + /// </summary> |
| 153 | + internal static int GetArrayLength(JsonElement parent, string property) |
| 154 | + { |
| 155 | + if (parent.ValueKind != JsonValueKind.Object |
| 156 | + || !parent.TryGetProperty(property, out var value) |
| 157 | + || value.ValueKind != JsonValueKind.Array) |
| 158 | + { |
| 159 | + return 0; |
| 160 | + } |
| 161 | + |
| 162 | + return value.GetArrayLength(); |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Returns the first value that is not <c>null</c>, empty, or whitespace, or |
| 167 | + /// <c>null</c> when none qualify. Values are returned verbatim (not trimmed). |
| 168 | + /// </summary> |
| 169 | + internal static string? FirstNonEmpty(params string?[] values) |
| 170 | + { |
| 171 | + // A bare `null` argument binds to the params array itself rather than a |
| 172 | + // one-element array; guard so the canonical helper never throws. |
| 173 | + if (values is null) |
| 174 | + { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + foreach (var value in values) |
| 179 | + { |
| 180 | + if (!string.IsNullOrWhiteSpace(value)) |
| 181 | + { |
| 182 | + return value; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return null; |
| 187 | + } |
| 188 | +} |
0 commit comments