| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace NGql.Core; |
| | | 5 | | |
| | | 6 | | internal static class ValueFormatter |
| | | 7 | | { |
| | | 8 | | internal const string DateFormat = "yyyy-MM-dd'T'HH:mm:ss.fffK"; |
| | | 9 | | |
| | | 10 | | // Pre-allocated strings for common boolean values to avoid repeated allocations |
| | | 11 | | private const string TrueString = "true"; |
| | | 12 | | private const string FalseString = "false"; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Returns true when <paramref name="value"/> is a primitive type this formatter handles. |
| | | 16 | | /// Single is-test per category keeps cyclomatic complexity flat. |
| | | 17 | | /// </summary> |
| | | 18 | | internal static bool IsPrimitiveType(object value) |
| | 103923 | 19 | | => IsString(value) || IsBoolean(value) || IsInteger(value) || IsFloating(value) |
| | 103923 | 20 | | || IsDate(value) || IsEnumLike(value) || value is Variable; |
| | | 21 | | |
| | 103923 | 22 | | private static bool IsString(object v) => v is string; |
| | 100923 | 23 | | private static bool IsBoolean(object v) => v is bool; |
| | 99573 | 24 | | private static bool IsInteger(object v) => IsSignedInteger(v) || IsUnsignedInteger(v); |
| | 99573 | 25 | | private static bool IsSignedInteger(object v) => v is sbyte or short or int or long; |
| | 8493 | 26 | | private static bool IsUnsignedInteger(object v) => v is byte or ushort or uint or ulong; |
| | 8469 | 27 | | private static bool IsFloating(object v) => v is float or double or decimal; |
| | 8340 | 28 | | private static bool IsDate(object v) => v is DateTime or DateTimeOffset; |
| | 8310 | 29 | | private static bool IsEnumLike(object v) => v is EnumValue or Enum; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Writes the primitive representation of <paramref name="value"/> directly to |
| | | 33 | | /// <paramref name="builder"/> without allocating an intermediate string. |
| | | 34 | | /// Each value category dispatches to a small dedicated helper to keep this top-level |
| | | 35 | | /// switch's cyclomatic complexity low. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <returns>True if the value was handled; false if it is not a known primitive.</returns> |
| | | 38 | | internal static bool TryAppendPrimitive(object value, StringBuilder builder) |
| | 26243 | 39 | | => TryAppendScalar(value, builder) |
| | 26243 | 40 | | || TryAppendNamed(value, builder) |
| | 26243 | 41 | | || TryAppendInteger(value, builder) |
| | 26243 | 42 | | || TryAppendFloating(value, builder); |
| | | 43 | | |
| | | 44 | | private static bool TryAppendScalar(object value, StringBuilder builder) |
| | | 45 | | { |
| | | 46 | | switch (value) |
| | | 47 | | { |
| | 6750 | 48 | | case string s: AppendString(builder, s); return true; |
| | 1374 | 49 | | case bool b: AppendBoolean(builder, b); return true; |
| | 42 | 50 | | case DateTime dt: AppendQuotedFormattable(builder, dt); return true; |
| | 24 | 51 | | case DateTimeOffset dto: AppendQuotedFormattable(builder, dto); return true; |
| | 22148 | 52 | | default: return false; |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | private static bool TryAppendNamed(object value, StringBuilder builder) |
| | | 57 | | { |
| | | 58 | | switch (value) |
| | | 59 | | { |
| | 42 | 60 | | case Enum e: builder.Append(FormatEnumName(e)); return true; |
| | 210 | 61 | | case EnumValue ev: builder.Append(ev.Value); return true; |
| | 666 | 62 | | case Variable variable: builder.Append(variable.Name); return true; |
| | 21686 | 63 | | default: return false; |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Renders an enum as its single GraphQL <c>Name</c>. Per spec § 3.9 an EnumValue must be a |
| | | 69 | | /// Name, so undefined numeric values and unnamed <c>[Flags]</c> combinations (whose |
| | | 70 | | /// <c>ToString()</c> yields <c>"999"</c> or <c>"Read, Write"</c>) are rejected rather than |
| | | 71 | | /// emitted as invalid output. |
| | | 72 | | /// </summary> |
| | | 73 | | internal static string FormatEnumName(Enum value) |
| | | 74 | | { |
| | 54 | 75 | | if (!Enum.IsDefined(value.GetType(), value)) |
| | | 76 | | { |
| | 12 | 77 | | throw new ArgumentException( |
| | 12 | 78 | | $"GraphQL enum arguments must be a single defined enum member. " + |
| | 12 | 79 | | $"The value '{value}' of type '{value.GetType().Name}' is not a defined member " + |
| | 12 | 80 | | $"(undefined numeric values and unnamed [Flags] combinations are not valid GraphQL enum names).", |
| | 12 | 81 | | nameof(value)); |
| | | 82 | | } |
| | 42 | 83 | | return value.ToString(); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static void AppendString(StringBuilder builder, string s) |
| | | 87 | | { |
| | 3375 | 88 | | builder.Append('"'); |
| | 3375 | 89 | | if (NeedsEscape(s)) |
| | | 90 | | { |
| | 33 | 91 | | AppendEscapedBody(builder, s); |
| | | 92 | | } |
| | | 93 | | else |
| | | 94 | | { |
| | 3342 | 95 | | builder.Append(s); |
| | | 96 | | } |
| | 3375 | 97 | | builder.Append('"'); |
| | 3375 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Per GraphQL spec § 2.9.4 a regular string literal must escape <c>\</c>, <c>"</c>, and |
| | | 102 | | /// the C0 control characters. Non-control Unicode characters pass through verbatim — the |
| | | 103 | | /// transport carries UTF-8. |
| | | 104 | | /// </summary> |
| | | 105 | | private static bool NeedsEscape(string s) |
| | | 106 | | { |
| | 176514 | 107 | | for (var i = 0; i < s.Length; i++) |
| | | 108 | | { |
| | 84915 | 109 | | var c = s[i]; |
| | 84915 | 110 | | if (c == '"' || c == '\\' || c < 0x20) |
| | 33 | 111 | | return true; |
| | | 112 | | } |
| | 3342 | 113 | | return false; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private static void AppendEscapedBody(StringBuilder builder, string s) |
| | | 117 | | { |
| | 900 | 118 | | for (var i = 0; i < s.Length; i++) |
| | | 119 | | { |
| | 417 | 120 | | var c = s[i]; |
| | | 121 | | switch (c) |
| | | 122 | | { |
| | 48 | 123 | | case '"': builder.Append("\\\""); break; |
| | 12 | 124 | | case '\\': builder.Append("\\\\"); break; |
| | 6 | 125 | | case '\b': builder.Append("\\b"); break; |
| | 6 | 126 | | case '\f': builder.Append("\\f"); break; |
| | 6 | 127 | | case '\n': builder.Append("\\n"); break; |
| | 6 | 128 | | case '\r': builder.Append("\\r"); break; |
| | 6 | 129 | | case '\t': builder.Append("\\t"); break; |
| | | 130 | | default: |
| | 372 | 131 | | if (c < 0x20) |
| | | 132 | | { |
| | | 133 | | // Other C0 controls — emit as \u00XX (GraphQL accepts EscapedUnicode). |
| | 3 | 134 | | builder.Append("\\u"); |
| | 3 | 135 | | builder.Append(((int)c).ToString("x4", CultureInfo.InvariantCulture)); |
| | | 136 | | } |
| | | 137 | | else |
| | | 138 | | { |
| | 369 | 139 | | builder.Append(c); |
| | | 140 | | } |
| | | 141 | | break; |
| | | 142 | | } |
| | | 143 | | } |
| | 33 | 144 | | } |
| | | 145 | | |
| | | 146 | | private static void AppendBoolean(StringBuilder builder, bool b) |
| | 687 | 147 | | => builder.Append(b ? TrueString : FalseString); |
| | | 148 | | |
| | | 149 | | private static bool TryAppendInteger(object value, StringBuilder builder) |
| | 21686 | 150 | | => TryAppendSignedInteger(value, builder) || TryAppendUnsignedInteger(value, builder); |
| | | 151 | | |
| | | 152 | | private static bool TryAppendSignedInteger(object value, StringBuilder builder) |
| | | 153 | | { |
| | | 154 | | switch (value) |
| | | 155 | | { |
| | 35770 | 156 | | case int v: AppendFormattable(builder, v); return true; |
| | 4854 | 157 | | case long v: AppendFormattable(builder, v); return true; |
| | 36 | 158 | | case short v: AppendFormattable(builder, (int)v); return true; |
| | 36 | 159 | | case sbyte v: AppendFormattable(builder, (int)v); return true; |
| | 1338 | 160 | | default: return false; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | private static bool TryAppendUnsignedInteger(object value, StringBuilder builder) |
| | | 165 | | { |
| | | 166 | | switch (value) |
| | | 167 | | { |
| | 24 | 168 | | case uint v: builder.Append(v); return true; |
| | 24 | 169 | | case ulong v: builder.Append(v); return true; |
| | 24 | 170 | | case ushort v: builder.Append(v); return true; |
| | 24 | 171 | | case byte v: builder.Append(v); return true; |
| | 1290 | 172 | | default: return false; |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | private static bool TryAppendFloating(object value, StringBuilder builder) |
| | | 177 | | { |
| | | 178 | | switch (value) |
| | | 179 | | { |
| | 90 | 180 | | case float v: GuardFiniteFloat(v); AppendFormattable(builder, v); return true; |
| | 162 | 181 | | case double v: GuardFiniteDouble(v); AppendFormattable(builder, v); return true; |
| | 66 | 182 | | case decimal v: AppendFormattable(builder, v); return true; |
| | 1161 | 183 | | default: return false; |
| | | 184 | | } |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// GraphQL FloatValue (spec § 3.5.2) has no representation for NaN or Infinity. The BCL would |
| | | 189 | | /// otherwise emit the bare tokens <c>NaN</c> / <c>Infinity</c> / <c>-Infinity</c>, which a |
| | | 190 | | /// conforming server rejects, so such values are refused at build time instead. |
| | | 191 | | /// </summary> |
| | | 192 | | private static void GuardFiniteDouble(double v) |
| | | 193 | | { |
| | 60 | 194 | | if (double.IsNaN(v) || double.IsInfinity(v)) |
| | 9 | 195 | | throw new FormatException($"GraphQL does not support NaN/Infinity float values (got '{v.ToString(CultureInfo |
| | 51 | 196 | | } |
| | | 197 | | |
| | | 198 | | private static void GuardFiniteFloat(float v) |
| | | 199 | | { |
| | 36 | 200 | | if (float.IsNaN(v) || float.IsInfinity(v)) |
| | 9 | 201 | | throw new FormatException($"GraphQL does not support NaN/Infinity float values (got '{v.ToString(CultureInfo |
| | 27 | 202 | | } |
| | | 203 | | |
| | | 204 | | /// <summary>Formats <paramref name="value"/> with invariant culture. Used for |
| | | 205 | | /// int/long/float/double/decimal — none of those produce strings longer than the BCL guarantees.</summary> |
| | | 206 | | private static void AppendFormattable<T>(StringBuilder builder, T value, ReadOnlySpan<char> format = default) |
| | | 207 | | where T : struct, ISpanFormattable |
| | | 208 | | { |
| | | 209 | | // Enough for the supported numeric types and DateFormat. The fallback inside |
| | | 210 | | // AppendSpanFormatted keeps this helper safe if another format or value type is |
| | | 211 | | // introduced later. |
| | 20492 | 212 | | Span<char> buffer = stackalloc char[StackBufferLength]; |
| | 20492 | 213 | | AppendSpanFormatted(builder, value, format, buffer); |
| | 20492 | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary>Stack buffer size used by <see cref="AppendFormattable{T}"/>; comfortably above the |
| | | 217 | | /// longest output of any currently-supported value type and format.</summary> |
| | | 218 | | internal const int StackBufferLength = 64; |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Formats <paramref name="value"/> into <paramref name="buffer"/> and appends the result, |
| | | 222 | | /// falling back to an allocating <c>ToString</c> when the buffer is too small. Split out from |
| | | 223 | | /// <see cref="AppendFormattable{T}"/> so the undersized-buffer fallback is exercisable without |
| | | 224 | | /// changing the production buffer size. |
| | | 225 | | /// </summary> |
| | | 226 | | internal static void AppendSpanFormatted<T>(StringBuilder builder, T value, ReadOnlySpan<char> format, Span<char> bu |
| | | 227 | | where T : struct, ISpanFormattable |
| | | 228 | | { |
| | 20540 | 229 | | if (value.TryFormat(buffer, out var written, format, CultureInfo.InvariantCulture)) |
| | | 230 | | { |
| | 20516 | 231 | | builder.Append(buffer[..written]); |
| | 20516 | 232 | | return; |
| | | 233 | | } |
| | | 234 | | |
| | 24 | 235 | | AppendFormattedFallback(builder, value, format); |
| | 24 | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary>Allocating path taken only when the destination span cannot hold the formatted |
| | | 239 | | /// value. Non-generic so it is compiled once rather than per value type.</summary> |
| | | 240 | | private static void AppendFormattedFallback(StringBuilder builder, IFormattable value, ReadOnlySpan<char> format) |
| | 24 | 241 | | => builder.Append(value.ToString(format.IsEmpty ? null : format.ToString(), CultureInfo.InvariantCulture)); |
| | | 242 | | |
| | | 243 | | /// <summary>Formats <paramref name="value"/> with the NGql DateFormat and quotes it. |
| | | 244 | | /// Used for DateTime/DateTimeOffset.</summary> |
| | | 245 | | private static void AppendQuotedFormattable<T>(StringBuilder builder, T value) |
| | | 246 | | where T : struct, ISpanFormattable |
| | | 247 | | { |
| | 33 | 248 | | builder.Append('"'); |
| | 33 | 249 | | AppendFormattable(builder, value, DateFormat); |
| | 33 | 250 | | builder.Append('"'); |
| | 33 | 251 | | } |
| | | 252 | | } |