| | | 1 | | using StackExchange.Redis; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Nodes; |
| | | 4 | | |
| | | 5 | | namespace AsyncResponse.Transports.Redis; |
| | | 6 | | |
| | | 7 | | internal static class RedisCorrelationIdExtractor |
| | | 8 | | { |
| | | 9 | | /// <summary>Extracts the correlation id from the supplied message.</summary> |
| | | 10 | | public static string? Extract( |
| | | 11 | | StreamEntry entry, |
| | | 12 | | string messageJson, |
| | | 13 | | RedisAsyncResponseTransportOptions options) |
| | | 14 | | { |
| | 3 | 15 | | var field = RedisTransportOptionsValidator.Required( |
| | 3 | 16 | | options.CorrelationIdField, |
| | 3 | 17 | | nameof(options.CorrelationIdField)); |
| | | 18 | | |
| | 3 | 19 | | var fieldValue = TryReadField(entry, field); |
| | 3 | 20 | | if (!string.IsNullOrWhiteSpace(fieldValue)) |
| | 3 | 21 | | return fieldValue; |
| | | 22 | | |
| | 3 | 23 | | var jsonPaths = options.CorrelationIdJsonPaths; |
| | 3 | 24 | | if (jsonPaths is null || jsonPaths.Length == 0 || string.IsNullOrWhiteSpace(messageJson)) |
| | 3 | 25 | | return null; |
| | | 26 | | |
| | | 27 | | JsonNode? root; |
| | | 28 | | try |
| | | 29 | | { |
| | 3 | 30 | | root = JsonNode.Parse(messageJson); |
| | 3 | 31 | | } |
| | 3 | 32 | | catch (JsonException) |
| | | 33 | | { |
| | 3 | 34 | | return null; |
| | | 35 | | } |
| | | 36 | | |
| | 3 | 37 | | if (root is null) |
| | 3 | 38 | | return null; |
| | | 39 | | |
| | 3 | 40 | | foreach (var path in jsonPaths) |
| | | 41 | | { |
| | 3 | 42 | | var value = TryReadPath(root, path); |
| | 3 | 43 | | if (!string.IsNullOrWhiteSpace(value)) |
| | 3 | 44 | | return value; |
| | | 45 | | } |
| | | 46 | | |
| | 2 | 47 | | return null; |
| | 3 | 48 | | } |
| | | 49 | | |
| | | 50 | | internal static string? TryReadField(StreamEntry entry, string fieldName) |
| | | 51 | | { |
| | | 52 | | // A tombstone left after trimming (an entry still referenced by a PEL but deleted from the |
| | | 53 | | // stream) has null Values; treat it as a missing field rather than throwing. |
| | 3 | 54 | | if (entry.Values is null) |
| | 3 | 55 | | return null; |
| | | 56 | | |
| | 3 | 57 | | foreach (var value in entry.Values) |
| | | 58 | | { |
| | 3 | 59 | | if (StringComparer.Ordinal.Equals(value.Name.ToString(), fieldName)) |
| | 3 | 60 | | return value.Value.ToString(); |
| | | 61 | | } |
| | | 62 | | |
| | 3 | 63 | | return null; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | private static string? TryReadPath(JsonNode root, string path) |
| | | 67 | | { |
| | 3 | 68 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 69 | | return null; |
| | | 70 | | |
| | 3 | 71 | | var current = root; |
| | 3 | 72 | | foreach (var segment in path.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) |
| | | 73 | | { |
| | 3 | 74 | | current = UnwrapJsonString(current); |
| | 3 | 75 | | if (current is not JsonObject obj) |
| | 3 | 76 | | return null; |
| | | 77 | | |
| | 3 | 78 | | current = TryGetProperty(obj, segment); |
| | 3 | 79 | | if (current is null) |
| | 3 | 80 | | return null; |
| | | 81 | | } |
| | | 82 | | |
| | 3 | 83 | | current = UnwrapJsonString(current); |
| | 3 | 84 | | return current switch |
| | 3 | 85 | | { |
| | 3 | 86 | | JsonValue value when value.TryGetValue<string>(out var s) => s, |
| | 2 | 87 | | JsonValue value => value.ToString(), |
| | 3 | 88 | | _ => null |
| | 3 | 89 | | }; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private static JsonNode? TryGetProperty(JsonObject obj, string name) |
| | | 93 | | { |
| | 3 | 94 | | if (obj.TryGetPropertyValue(name, out var exact)) |
| | 3 | 95 | | return exact; |
| | | 96 | | |
| | 2 | 97 | | foreach (var property in obj) |
| | | 98 | | { |
| | 2 | 99 | | if (string.Equals(property.Key, name, StringComparison.OrdinalIgnoreCase)) |
| | 2 | 100 | | return property.Value; |
| | | 101 | | } |
| | | 102 | | |
| | 2 | 103 | | return null; |
| | 3 | 104 | | } |
| | | 105 | | |
| | | 106 | | private static JsonNode? UnwrapJsonString(JsonNode? node) |
| | | 107 | | { |
| | 3 | 108 | | if (node is not JsonValue value || !value.TryGetValue<string>(out var text)) |
| | 3 | 109 | | return node; |
| | | 110 | | |
| | 3 | 111 | | var trimmed = text.AsSpan().TrimStart(); |
| | 3 | 112 | | if (trimmed.Length == 0 || (trimmed[0] != '{' && trimmed[0] != '[')) |
| | 3 | 113 | | return node; |
| | | 114 | | |
| | | 115 | | try |
| | | 116 | | { |
| | 2 | 117 | | return JsonNode.Parse(text); |
| | | 118 | | } |
| | 2 | 119 | | catch (JsonException) |
| | | 120 | | { |
| | 2 | 121 | | return node; |
| | | 122 | | } |
| | 3 | 123 | | } |
| | | 124 | | } |