Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
Event.cs
Go to the documentation of this file.
1using System.Text.Json;
2using Fauna.Core;
4using Fauna.Mapping;
6using static Fauna.Core.ResponseFields;
7
8namespace Fauna.Types;
9
10
14public enum EventType
15{
19 Add,
23 Update,
27 Remove,
31 Status
32}
33
38public class Event<T> where T : notnull
39{
43 public EventType Type { get; private init; }
47 public long TxnTime { get; private init; }
51 public string Cursor { get; private init; } = null!;
55 public T? Data { get; private init; }
59 public QueryStats Stats { get; private init; }
60
61
69 internal static Event<T> From(JsonElement json, MappingContext ctx)
70 {
71 var err = GetError(json);
72 if (err != null)
73 {
74 throw new EventException(err.Value);
75 }
76
77 var evt = new Event<T>
78 {
79 TxnTime = GetTxnTime(json),
80 Cursor = GetCursor(json),
81 Type = GetType(json),
82 Stats = GetStats(json),
83 Data = GetData(json, ctx),
84 };
85
86 return evt;
87 }
88
96 internal static Event<T> From(string body, MappingContext ctx)
97 {
98 var json = JsonSerializer.Deserialize<JsonElement>(body);
99
100 return From(json, ctx);
101 }
102
103 private static long GetTxnTime(JsonElement json)
104 {
105 if (!json.TryGetProperty(LastSeenTxnFieldName, out var elem))
106 {
107 return default;
108 }
109
110 return elem.TryGetInt64(out long i) ? i : default;
111 }
112
113 private static string GetCursor(JsonElement json)
114 {
115 if (!json.TryGetProperty(CursorFieldName, out var elem))
116 {
117 throw new InvalidDataException($"Missing required field: cursor - {json.ToString()}");
118 }
119
120 return elem.Deserialize<string>()!;
121 }
122
123
124 private static EventType GetType(JsonElement json)
125 {
126 if (!json.TryGetProperty("type", out var elem))
127 {
128 throw new InvalidDataException($"Missing required field: type - {json.ToString()}");
129 }
130
131 string? evtType = elem.Deserialize<string?>();
132 EventType type = evtType switch
133 {
134 "add" => EventType.Add,
135 "update" => EventType.Update,
136 "remove" => EventType.Remove,
137 "status" => EventType.Status,
138 _ => throw new InvalidOperationException($"Unknown event type: {evtType}")
139 };
140
141 return type;
142 }
143
144 private static QueryStats GetStats(JsonElement json)
145 {
146 return json.TryGetProperty(StatsFieldName, out var elem) ? elem.Deserialize<QueryStats>() : default;
147 }
148
149 private static T? GetData(JsonElement json, MappingContext ctx)
150 {
151 if (!json.TryGetProperty(DataFieldName, out var elem))
152 {
153 return default;
154 }
155
156 var reader = new Utf8FaunaReader(elem.GetRawText());
157 reader.Read();
158
159 return Serializer.Generate<T>(ctx).Deserialize(ctx, ref reader);
160 }
161
162 private static ErrorInfo? GetError(JsonElement json)
163 {
164 return json.TryGetProperty(ErrorFieldName, out var elem) ? elem.Deserialize<ErrorInfo>() : null;
165 }
166}
Represents an exception related to Fauna Event Stream and Event Feed errors.
A class representing the mapping context to be used during serialization and deserialization.
A class representing an event from an Event Feed or Event Stream.
Definition Event.cs:39
EventType Type
The type of the event.
Definition Event.cs:43
string Cursor
A cursor for the event. Used to resume an Event Feed or Event Stream after the event.
Definition Event.cs:51
T? Data
Document data for the event.
Definition Event.cs:55
long TxnTime
The transaction time of the event.
Definition Event.cs:47
object? ISerializer. Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
Consumes or partially consumes the provided reader and deserializes into a result.
EventType
An enum representing Fauna event types.
Definition Event.cs:15
@ Update
An update event. Emitted when a document is updated in the tracked Set.
@ Remove
A remove event. Emitted when a document is removed from the tracked Set.
@ Add
An add event. Emitted when a document is added to the tracked Set.
@ Status
A status event. Typically used as an implementation detail for a driver. Indicates a status change on...
Contains detailed information about an error in a query response.
Definition ErrorInfo.cs:11
A struct representing stats aggregated across queries.
Represents a reader that provides fast, non-cached, forward-only access to serialized data.