| | | 1 | | using StackExchange.Redis; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.Redis; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extracts the AsyncResponse correlation id from an inbound response message: first from the |
| | | 7 | | /// configured stream field, then from the JSON body via the configured paths (walked by the shared |
| | | 8 | | /// <see cref="CorrelationIdJsonPaths"/>). |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class RedisCorrelationIdExtractor |
| | | 11 | | { |
| | | 12 | | /// <summary>Extracts the correlation id from the supplied message.</summary> |
| | | 13 | | public static string? Extract( |
| | | 14 | | StreamEntry entry, |
| | | 15 | | string messageJson, |
| | | 16 | | RedisAsyncResponseTransportOptions options) |
| | | 17 | | { |
| | 32 | 18 | | var field = RedisTransportOptionsValidator.Required( |
| | 32 | 19 | | options.CorrelationIdField, |
| | 32 | 20 | | nameof(options.CorrelationIdField)); |
| | | 21 | | |
| | 32 | 22 | | var fieldValue = TryReadField(entry, field); |
| | 32 | 23 | | if (!string.IsNullOrWhiteSpace(fieldValue)) |
| | 2 | 24 | | return fieldValue; |
| | | 25 | | |
| | 30 | 26 | | return CorrelationIdJsonPaths.Extract(messageJson, options.CorrelationIdJsonPaths); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | internal static string? TryReadField(StreamEntry entry, string fieldName) |
| | | 30 | | { |
| | | 31 | | // A tombstone left after trimming (an entry still referenced by a PEL but deleted from the |
| | | 32 | | // stream) has null Values; treat it as a missing field rather than throwing. |
| | 938 | 33 | | if (entry.Values is null) |
| | 2 | 34 | | return null; |
| | | 35 | | |
| | 3427 | 36 | | foreach (var value in entry.Values) |
| | | 37 | | { |
| | 1073 | 38 | | if (StringComparer.Ordinal.Equals(value.Name.ToString(), fieldName)) |
| | 591 | 39 | | return value.Value.ToString(); |
| | | 40 | | } |
| | | 41 | | |
| | 345 | 42 | | return null; |
| | | 43 | | } |
| | | 44 | | } |