Skip to content

Commit

Permalink
initial PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cynicaljoy committed Nov 7, 2024
1 parent d17f99b commit f1b6436
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Fauna.Test/Integration.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public async Task CanOpenFeedWithQuery()

Assert.NotNull(feed.CurrentPage, "should have loaded a page");
Assert.IsNotEmpty(feed.Cursor, "should have a cursor");
Assert.IsEmpty(feed.CurrentPage!.Events, "should note have events");
Assert.IsEmpty(feed.CurrentPage!.Events, "should not have events");

await _client.QueryAsync(FQL($"StreamingSandbox.create({{ foo: 'bar' }})"));

Expand Down
2 changes: 1 addition & 1 deletion Fauna/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ internal override async IAsyncEnumerator<FeedPage<T>> SubscribeFeedInternal<T>(
{
cancel.ThrowIfCancellationRequested();

var finalOptions = QueryOptions.GetFinalQueryOptions(_config.DefaultQueryOptions, null);
var finalOptions = _config.DefaultQueryOptions;
var headers = GetRequestHeaders(finalOptions);

while (!cancel.IsCancellationRequested)
Expand Down
79 changes: 1 addition & 78 deletions Fauna/Core/FeedEnumberable.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,7 @@
using System.Collections;
using System.Text.Json;
using Fauna.Exceptions;
using Fauna.Mapping;
using Fauna.Serialization;
using Fauna.Types;
using static Fauna.Core.ResponseFields;

namespace Fauna.Core;

/// <summary>
/// Represents the response from Fauna Event Feed requests.
/// </summary>
/// <typeparam name="T"></typeparam>
public class FeedPage<T> where T : notnull
{
/// <summary>
/// List of Events returned by the Feed
/// </summary>
public List<Event<T>> Events { get; private init; } = [];

/// <summary>
/// Cursor returned from the Feed
/// </summary>
public string Cursor { get; private init; } = null!;

/// <summary>
/// Indicates if there are more pages for pagination.
/// </summary>
public bool HasNext { get; private init; }

/// <summary>
/// Stats returned from the Feed.
/// </summary>
public QueryStats Stats { get; private init; }

internal static FeedPage<T> From(string body, MappingContext ctx)
{
var json = JsonSerializer.Deserialize<JsonElement>(body);

var err = GetError(json);
if (err != null)
{
throw new FaunaException(err.Value);
}

return new FeedPage<T>
{
Cursor = GetCursor(json),
Events = GetEvents(json, ctx),
Stats = GetStats(json),
HasNext = json.TryGetProperty(HasNextFieldName, out var elem) && elem.GetBoolean()
};
}

private static List<Event<T>> GetEvents(JsonElement json, MappingContext ctx)
{
if (!json.TryGetProperty(EventsFieldName, out var elem))
{
return new List<Event<T>>();
}

var events = elem.EnumerateArray().Select(e => Event<T>.From(e, ctx)).ToList();
return events;
}

private static QueryStats GetStats(JsonElement json)
{
return json.TryGetProperty(StatsFieldName, out var elem) ? elem.Deserialize<QueryStats>() : default;
}

private static string GetCursor(JsonElement json)
{
return json.TryGetProperty(CursorFieldName, out var elem) ? elem.GetString()! : null!;
}

private static ErrorInfo? GetError(JsonElement json)
{
return json.TryGetProperty(ErrorFieldName, out var elem) ? elem.Deserialize<ErrorInfo>() : null;
}
}

/// <summary>
/// Represents a Fauna Event Feed.
Expand All @@ -97,7 +20,7 @@ public class FeedEnumerable<T> where T : notnull
public string? Cursor => _eventSource.LastCursor;

/// <summary>
/// The last page returned from the Event Feed enumerator.
/// The latest page returned from the Event Feed enumerator.
/// </summary>
public FeedPage<T>? CurrentPage { get; private set; }

Expand Down
79 changes: 79 additions & 0 deletions Fauna/Core/FeedPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Text.Json;
using Fauna.Exceptions;
using Fauna.Mapping;
using Fauna.Types;
using static Fauna.Core.ResponseFields;

namespace Fauna.Core;

/// <summary>
/// Represents the response from Fauna Event Feed requests.
/// </summary>
/// <typeparam name="T"></typeparam>
public class FeedPage<T> where T : notnull
{
/// <summary>
/// List of Events returned by the Feed
/// </summary>
public List<Event<T>> Events { get; private init; } = [];

/// <summary>
/// Cursor returned from the Feed
/// </summary>
public string Cursor { get; private init; } = null!;

/// <summary>
/// Indicates if there are more pages for pagination.
/// </summary>
public bool HasNext { get; private init; }

/// <summary>
/// Stats returned from the Feed.
/// </summary>
public QueryStats Stats { get; private init; }

internal static FeedPage<T> From(string body, MappingContext ctx)
{
var json = JsonSerializer.Deserialize<JsonElement>(body);

var err = GetError(json);
if (err != null)
{
throw new FaunaException(err.Value);
}

return new FeedPage<T>
{
Cursor = GetCursor(json),
Events = GetEvents(json, ctx),
Stats = GetStats(json),
HasNext = json.TryGetProperty(HasNextFieldName, out var elem) && elem.GetBoolean()
};
}

private static List<Event<T>> GetEvents(JsonElement json, MappingContext ctx)
{
if (!json.TryGetProperty(EventsFieldName, out var elem))
{
return new List<Event<T>>();
}

var events = elem.EnumerateArray().Select(e => Event<T>.From(e, ctx)).ToList();
return events;
}

private static QueryStats GetStats(JsonElement json)
{
return json.TryGetProperty(StatsFieldName, out var elem) ? elem.Deserialize<QueryStats>() : default;
}

private static string GetCursor(JsonElement json)
{
return json.TryGetProperty(CursorFieldName, out var elem) ? elem.GetString()! : null!;
}

private static ErrorInfo? GetError(JsonElement json)
{
return json.TryGetProperty(ErrorFieldName, out var elem) ? elem.Deserialize<ErrorInfo>() : null;
}
}
4 changes: 1 addition & 3 deletions Fauna/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,14 +641,12 @@ public async Task<FeedEnumerable<T>> EventFeedAsync<T>(
/// Opens the event feed with Fauna and returns an enumerator for the events.
/// </summary>
/// <param name="query">The query to create the stream from Fauna.</param>
/// <param name="queryOptions">The options for the query.</param>
/// <param name="feedOptions">The options for the feed.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <typeparam name="T">Which Type to map the Events to.</typeparam>
/// <returns></returns>
public async Task<FeedEnumerable<T>> EventFeedAsync<T>(
Query query,
QueryOptions? queryOptions = null,
FeedOptions? feedOptions = null,
CancellationToken cancellationToken = default) where T : notnull
{
Expand All @@ -657,7 +655,7 @@ public async Task<FeedEnumerable<T>> EventFeedAsync<T>(
throw new InvalidOperationException("Cannot use Cursor when opening an EventFeed with a Query.");
}

EventSource eventSource = await GetEventSourceFromQueryAsync(query, queryOptions, cancellationToken);
EventSource eventSource = await GetEventSourceFromQueryAsync(query, null, cancellationToken);

return new FeedEnumerable<T>(this, eventSource, feedOptions, cancellationToken);
}
Expand Down
4 changes: 2 additions & 2 deletions Fauna/Types/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Event<T> where T : notnull
/// <param name="ctx">A mapping context to influence deserialization.</param>
/// <returns>An instance of <see cref="Event{T}"/>.</returns>
/// <exception cref="FaunaException">Thrown when the event includes a Fauna error.</exception>
public static Event<T> From(JsonElement json, MappingContext ctx)
internal static Event<T> From(JsonElement json, MappingContext ctx)
{
var err = GetError(json);
if (err != null)
Expand All @@ -93,7 +93,7 @@ public static Event<T> From(JsonElement json, MappingContext ctx)
/// <param name="ctx">A mapping context to influence deserialization.</param>
/// <returns>An instance of <see cref="Event{T}"/>.</returns>
/// <exception cref="FaunaException">Thrown when the event includes a Fauna error.</exception>
public static Event<T> From(string body, MappingContext ctx)
internal static Event<T> From(string body, MappingContext ctx)
{
var json = JsonSerializer.Deserialize<JsonElement>(body);

Expand Down

0 comments on commit f1b6436

Please sign in to comment.