|
11 | 11 | // limitations under the License.
|
12 | 12 | // ------------------------------------------------------------------------
|
13 | 13 |
|
14 |
| -namespace Dapr.Actors.Runtime |
| 14 | +#nullable enable |
| 15 | +namespace Dapr.Actors.Runtime; |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.IO; |
| 19 | +using System.Text; |
| 20 | +using System.Text.Json; |
| 21 | +using System.Threading.Tasks; |
| 22 | + |
| 23 | +// represents the wire format used by Dapr to store reminder info with the runtime |
| 24 | +internal class ReminderInfo |
15 | 25 | {
|
16 |
| - using System; |
17 |
| - using System.IO; |
18 |
| - using System.Text; |
19 |
| - using System.Text.Json; |
20 |
| - using System.Threading.Tasks; |
21 |
| - |
22 |
| - // represents the wire format used by Dapr to store reminder info with the runtime |
23 |
| - internal class ReminderInfo |
| 26 | + public ReminderInfo( |
| 27 | + byte[] data, |
| 28 | + TimeSpan dueTime, |
| 29 | + TimeSpan period, |
| 30 | + int? repetitions = null, |
| 31 | + TimeSpan? ttl = null) |
24 | 32 | {
|
25 |
| - public ReminderInfo( |
26 |
| - byte[] data, |
27 |
| - TimeSpan dueTime, |
28 |
| - TimeSpan period, |
29 |
| - int? repetitions = null, |
30 |
| - TimeSpan? ttl = null) |
| 33 | + this.Data = data; |
| 34 | + this.DueTime = dueTime; |
| 35 | + this.Period = period; |
| 36 | + this.Ttl = ttl; |
| 37 | + this.Repetitions = repetitions; |
| 38 | + } |
| 39 | + |
| 40 | + public TimeSpan DueTime { get; private set; } |
| 41 | + |
| 42 | + public TimeSpan Period { get; private set; } |
| 43 | + |
| 44 | + public byte[] Data { get; private set; } |
| 45 | + |
| 46 | + public TimeSpan? Ttl { get; private set; } |
| 47 | + |
| 48 | + public int? Repetitions { get; private set; } |
| 49 | + |
| 50 | + internal static async Task<ReminderInfo?> DeserializeAsync(Stream stream) |
| 51 | + { |
| 52 | + var json = await JsonSerializer.DeserializeAsync<JsonElement>(stream); |
| 53 | + if(json.ValueKind == JsonValueKind.Null) |
31 | 54 | {
|
32 |
| - this.Data = data; |
33 |
| - this.DueTime = dueTime; |
34 |
| - this.Period = period; |
35 |
| - this.Ttl = ttl; |
36 |
| - this.Repetitions = repetitions; |
| 55 | + return null; |
37 | 56 | }
|
38 | 57 |
|
39 |
| - public TimeSpan DueTime { get; private set; } |
| 58 | + var setAnyProperties = false; //Used to determine if anything was actually deserialized |
| 59 | + var dueTime = TimeSpan.Zero; |
| 60 | + var period = TimeSpan.Zero; |
| 61 | + var data = Array.Empty<byte>(); |
| 62 | + int? repetition = null; |
| 63 | + TimeSpan? ttl = null; |
| 64 | + if (json.TryGetProperty("dueTime", out var dueTimeProperty)) |
| 65 | + { |
| 66 | + setAnyProperties = true; |
| 67 | + var dueTimeString = dueTimeProperty.GetString(); |
| 68 | + dueTime = ConverterUtils.ConvertTimeSpanFromDaprFormat(dueTimeString); |
| 69 | + } |
40 | 70 |
|
41 |
| - public TimeSpan Period { get; private set; } |
| 71 | + if (json.TryGetProperty("period", out var periodProperty)) |
| 72 | + { |
| 73 | + setAnyProperties = true; |
| 74 | + var periodString = periodProperty.GetString(); |
| 75 | + (period, repetition) = ConverterUtils.ConvertTimeSpanValueFromISO8601Format(periodString); |
| 76 | + } |
42 | 77 |
|
43 |
| - public byte[] Data { get; private set; } |
| 78 | + if (json.TryGetProperty("data", out var dataProperty) && dataProperty.ValueKind != JsonValueKind.Null) |
| 79 | + { |
| 80 | + setAnyProperties = true; |
| 81 | + data = dataProperty.GetBytesFromBase64(); |
| 82 | + } |
44 | 83 |
|
45 |
| - public TimeSpan? Ttl { get; private set; } |
46 |
| - |
47 |
| - public int? Repetitions { get; private set; } |
| 84 | + if (json.TryGetProperty("ttl", out var ttlProperty)) |
| 85 | + { |
| 86 | + setAnyProperties = true; |
| 87 | + var ttlString = ttlProperty.GetString(); |
| 88 | + ttl = ConverterUtils.ConvertTimeSpanFromDaprFormat(ttlString); |
| 89 | + } |
48 | 90 |
|
49 |
| - internal static async Task<ReminderInfo> DeserializeAsync(Stream stream) |
| 91 | + if (!setAnyProperties) |
50 | 92 | {
|
51 |
| - var json = await JsonSerializer.DeserializeAsync<JsonElement>(stream); |
52 |
| - if(json.ValueKind == JsonValueKind.Null) |
53 |
| - { |
54 |
| - return null; |
55 |
| - } |
56 |
| - |
57 |
| - var dueTime = default(TimeSpan); |
58 |
| - var period = default(TimeSpan); |
59 |
| - var data = default(byte[]); |
60 |
| - int? repetition = null; |
61 |
| - TimeSpan? ttl = null; |
62 |
| - if (json.TryGetProperty("dueTime", out var dueTimeProperty)) |
63 |
| - { |
64 |
| - var dueTimeString = dueTimeProperty.GetString(); |
65 |
| - dueTime = ConverterUtils.ConvertTimeSpanFromDaprFormat(dueTimeString); |
66 |
| - } |
67 |
| - |
68 |
| - if (json.TryGetProperty("period", out var periodProperty)) |
69 |
| - { |
70 |
| - var periodString = periodProperty.GetString(); |
71 |
| - (period, repetition) = ConverterUtils.ConvertTimeSpanValueFromISO8601Format(periodString); |
72 |
| - } |
73 |
| - |
74 |
| - if (json.TryGetProperty("data", out var dataProperty) && dataProperty.ValueKind != JsonValueKind.Null) |
75 |
| - { |
76 |
| - data = dataProperty.GetBytesFromBase64(); |
77 |
| - } |
78 |
| - |
79 |
| - if (json.TryGetProperty("ttl", out var ttlProperty)) |
80 |
| - { |
81 |
| - var ttlString = ttlProperty.GetString(); |
82 |
| - ttl = ConverterUtils.ConvertTimeSpanFromDaprFormat(ttlString); |
83 |
| - } |
84 |
| - |
85 |
| - return new ReminderInfo(data, dueTime, period, repetition, ttl); |
| 93 | + return null; //No properties were ever deserialized, so return null instead of default values |
86 | 94 | }
|
87 | 95 |
|
88 |
| - internal async ValueTask<string> SerializeAsync() |
| 96 | + return new ReminderInfo(data, dueTime, period, repetition, ttl); |
| 97 | + } |
| 98 | + |
| 99 | + internal async ValueTask<string> SerializeAsync() |
| 100 | + { |
| 101 | + using var stream = new MemoryStream(); |
| 102 | + await using Utf8JsonWriter writer = new Utf8JsonWriter(stream); |
| 103 | + |
| 104 | + writer.WriteStartObject(); |
| 105 | + writer.WriteString("dueTime", ConverterUtils.ConvertTimeSpanValueInDaprFormat(this.DueTime)); |
| 106 | + writer.WriteString("period", ConverterUtils.ConvertTimeSpanValueInISO8601Format( |
| 107 | + this.Period, this.Repetitions)); |
| 108 | + writer.WriteBase64String("data", this.Data); |
| 109 | + |
| 110 | + if (Ttl != null) |
89 | 111 | {
|
90 |
| - using var stream = new MemoryStream(); |
91 |
| - using Utf8JsonWriter writer = new Utf8JsonWriter(stream); |
92 |
| - |
93 |
| - writer.WriteStartObject(); |
94 |
| - writer.WriteString("dueTime", ConverterUtils.ConvertTimeSpanValueInDaprFormat(this.DueTime)); |
95 |
| - writer.WriteString("period", ConverterUtils.ConvertTimeSpanValueInISO8601Format( |
96 |
| - this.Period, this.Repetitions)); |
97 |
| - writer.WriteBase64String("data", this.Data); |
98 |
| - |
99 |
| - if (Ttl != null) |
100 |
| - { |
101 |
| - writer.WriteString("ttl", ConverterUtils.ConvertTimeSpanValueInDaprFormat(Ttl)); |
102 |
| - } |
103 |
| - |
104 |
| - writer.WriteEndObject(); |
105 |
| - await writer.FlushAsync(); |
106 |
| - return Encoding.UTF8.GetString(stream.ToArray()); |
| 112 | + writer.WriteString("ttl", ConverterUtils.ConvertTimeSpanValueInDaprFormat(Ttl)); |
107 | 113 | }
|
| 114 | + |
| 115 | + writer.WriteEndObject(); |
| 116 | + await writer.FlushAsync(); |
| 117 | + return Encoding.UTF8.GetString(stream.ToArray()); |
108 | 118 | }
|
109 | 119 | }
|
0 commit comments