| | | 1 | | using Google.Cloud.PubSub.V1; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Nodes; |
| | | 4 | | |
| | | 5 | | namespace AsyncResponse.Transports.GooglePubSub; |
| | | 6 | | |
| | | 7 | | internal static class GooglePubSubCorrelationIdExtractor |
| | | 8 | | { |
| | | 9 | | /// <summary>Extracts the correlation id from the supplied message.</summary> |
| | | 10 | | public static string? Extract( |
| | | 11 | | PubsubMessage message, |
| | | 12 | | string messageJson, |
| | | 13 | | GooglePubSubAsyncResponseOptions options) |
| | | 14 | | { |
| | 3 | 15 | | if (!string.IsNullOrWhiteSpace(options.CorrelationIdAttribute) |
| | 3 | 16 | | && message.Attributes.TryGetValue(options.CorrelationIdAttribute, out var attributeValue) |
| | 3 | 17 | | && !string.IsNullOrWhiteSpace(attributeValue)) |
| | | 18 | | { |
| | 3 | 19 | | return attributeValue; |
| | | 20 | | } |
| | | 21 | | |
| | 3 | 22 | | var jsonPaths = options.CorrelationIdJsonPaths; |
| | 3 | 23 | | if (jsonPaths is null || jsonPaths.Length == 0 || string.IsNullOrWhiteSpace(messageJson)) |
| | 3 | 24 | | return null; |
| | | 25 | | |
| | | 26 | | JsonNode? root; |
| | | 27 | | try |
| | | 28 | | { |
| | 3 | 29 | | root = JsonNode.Parse(messageJson); |
| | 3 | 30 | | } |
| | 3 | 31 | | catch (JsonException) |
| | | 32 | | { |
| | 3 | 33 | | return null; |
| | | 34 | | } |
| | | 35 | | |
| | 3 | 36 | | if (root is null) |
| | 3 | 37 | | return null; |
| | | 38 | | |
| | 3 | 39 | | foreach (var path in jsonPaths) |
| | | 40 | | { |
| | 3 | 41 | | var value = TryReadPath(root, path); |
| | 3 | 42 | | if (!string.IsNullOrWhiteSpace(value)) |
| | 3 | 43 | | return value; |
| | | 44 | | } |
| | | 45 | | |
| | 2 | 46 | | return null; |
| | 3 | 47 | | } |
| | | 48 | | |
| | | 49 | | private static string? TryReadPath(JsonNode root, string path) |
| | | 50 | | { |
| | 3 | 51 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 52 | | return null; |
| | | 53 | | |
| | 3 | 54 | | var current = root; |
| | 3 | 55 | | foreach (var segment in path.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) |
| | | 56 | | { |
| | 3 | 57 | | current = UnwrapJsonString(current); |
| | 3 | 58 | | if (current is not JsonObject obj) |
| | 3 | 59 | | return null; |
| | | 60 | | |
| | 3 | 61 | | current = TryGetProperty(obj, segment); |
| | 3 | 62 | | if (current is null) |
| | 3 | 63 | | return null; |
| | | 64 | | } |
| | | 65 | | |
| | 3 | 66 | | current = UnwrapJsonString(current); |
| | 3 | 67 | | return current switch |
| | 3 | 68 | | { |
| | 3 | 69 | | JsonValue value when value.TryGetValue<string>(out var s) => s, |
| | 2 | 70 | | JsonValue value => value.ToString(), |
| | 3 | 71 | | _ => null |
| | 3 | 72 | | }; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private static JsonNode? TryGetProperty(JsonObject obj, string name) |
| | | 76 | | { |
| | 3 | 77 | | if (obj.TryGetPropertyValue(name, out var exact)) |
| | 3 | 78 | | return exact; |
| | | 79 | | |
| | 2 | 80 | | foreach (var property in obj) |
| | | 81 | | { |
| | 2 | 82 | | if (string.Equals(property.Key, name, StringComparison.OrdinalIgnoreCase)) |
| | 2 | 83 | | return property.Value; |
| | | 84 | | } |
| | | 85 | | |
| | 2 | 86 | | return null; |
| | 3 | 87 | | } |
| | | 88 | | |
| | | 89 | | private static JsonNode? UnwrapJsonString(JsonNode? node) |
| | | 90 | | { |
| | 3 | 91 | | if (node is not JsonValue value || !value.TryGetValue<string>(out var text)) |
| | 3 | 92 | | return node; |
| | | 93 | | |
| | 3 | 94 | | var trimmed = text.AsSpan().TrimStart(); |
| | 3 | 95 | | if (trimmed.Length == 0 || (trimmed[0] != '{' && trimmed[0] != '[')) |
| | 3 | 96 | | return node; |
| | | 97 | | |
| | | 98 | | try |
| | | 99 | | { |
| | 2 | 100 | | return JsonNode.Parse(text); |
| | | 101 | | } |
| | 2 | 102 | | catch (JsonException) |
| | | 103 | | { |
| | 2 | 104 | | return node; |
| | | 105 | | } |
| | 3 | 106 | | } |
| | | 107 | | } |