Skip to content

Commit

Permalink
Refactored Fusion Composition validation
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-84 committed Dec 12, 2024
1 parent 45d8625 commit b8a159a
Show file tree
Hide file tree
Showing 23 changed files with 404 additions and 264 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace HotChocolate.Fusion.Collections;

internal sealed class MultiValueDictionary<TKey, TValue>
: Dictionary<TKey, IList<TValue>> where TKey : notnull
{
public void Add(TKey key, TValue value)
{
ArgumentNullException.ThrowIfNull(key);

if (!TryGetValue(key, out var list))
{
list = [];
Add(key, list);
}

list.Add(value);
}

public void Remove(TKey key, TValue value)
{
ArgumentNullException.ThrowIfNull(key);

if (TryGetValue(key, out var list))
{
list.Remove(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using HotChocolate.Fusion.PreMergeValidation.Contracts;
using static HotChocolate.Fusion.Properties.CompositionResources;

namespace HotChocolate.Fusion.Errors;

internal static class ErrorHelper
{
public static CompositionError PreMergeValidationRuleFailed(IPreMergeValidationRule rule)
=> new(string.Format(ErrorHelper_PreMergeValidationRuleFailed, rule.GetType().Name));
public static CompositionError PreMergeValidationFailed()
=> new(ErrorHelper_PreMergeValidationFailed);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using HotChocolate.Fusion.Collections;

namespace HotChocolate.Fusion.Events;

internal sealed class EventAggregator
{
private readonly MultiValueDictionary<Type, Delegate> _subscribers = new();

public void Subscribe<TEvent>(Action<TEvent> handler)
{
_subscribers.Add(typeof(TEvent), handler);
}

public void Unsubscribe<TEvent>(Action<TEvent> handler)
{
_subscribers.Remove(typeof(TEvent), handler);
}

public void Publish<TEvent>(TEvent @event)
{
if (_subscribers.ContainsKey(typeof(TEvent)))
{
foreach (var handler in _subscribers[typeof(TEvent)])
{
((Action<TEvent>)handler)(@event);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace HotChocolate.Fusion.Logging;

public sealed class CompositionLog : ICompositionLog, IEnumerable<LogEntry>
{
public bool HasErrors { get; private set; }

public bool IsEmpty => _entries.Count == 0;

private readonly List<LogEntry> _entries = [];
Expand All @@ -13,12 +15,12 @@ public void Write(LogEntry entry)
{
ArgumentNullException.ThrowIfNull(entry);

_entries.Add(entry);
}
if (entry.Severity == LogSeverity.Error)
{
HasErrors = true;
}

public ILoggingSession CreateSession()
{
return new LoggingSession(this);
_entries.Add(entry);
}

public IEnumerator<LogEntry> GetEnumerator() => _entries.GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ namespace HotChocolate.Fusion.Logging.Contracts;
/// </summary>
public interface ICompositionLog
{
// FIXME: Docs.
bool HasErrors { get; }

// FIXME: Docs.
bool IsEmpty { get; }

/// <summary>
/// Writes the specified <see cref="LogEntry"/> to the log.
/// </summary>
/// <param name="entry">The <see cref="LogEntry"/> to write.</param>
void Write(LogEntry entry);

/// <summary>
/// Creates a new logging session that keeps track of the number of info, warning, and error
/// entries logged.
/// </summary>
ILoggingSession CreateSession();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace HotChocolate.Fusion.PreMergeValidation.Contracts;

internal interface IEachDirectiveArgumentEventHandler
{
void OnEachDirectiveArgument(EachDirectiveArgumentEvent @event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace HotChocolate.Fusion.PreMergeValidation.Contracts;

internal interface IEachDirectiveEventHandler
{
void OnEachDirective(EachDirectiveEvent @event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace HotChocolate.Fusion.PreMergeValidation.Contracts;

internal interface IEachFieldArgumentEventHandler
{
void OnEachFieldArgument(EachFieldArgumentEvent @event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace HotChocolate.Fusion.PreMergeValidation.Contracts;

internal interface IEachOutputFieldEventHandler
{
void OnEachOutputField(EachOutputFieldEvent @event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace HotChocolate.Fusion.PreMergeValidation.Contracts;

internal interface IEachOutputFieldNameEventHandler
{
void OnEachOutputFieldName(EachOutputFieldNameEvent @event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace HotChocolate.Fusion.PreMergeValidation.Contracts;

internal interface IEachTypeEventHandler
{
void OnEachType(EachTypeEvent @event);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Immutable;
using HotChocolate.Skimmed;

namespace HotChocolate.Fusion.PreMergeValidation;

internal record EachDirectiveArgumentEvent(
CompositionContext Context,
InputFieldDefinition Argument,
DirectiveDefinition Directive,
SchemaDefinition Schema);

internal record EachDirectiveEvent(
CompositionContext Context,
DirectiveDefinition Directive,
SchemaDefinition Schema);

internal record EachFieldArgumentEvent(
CompositionContext Context,
InputFieldDefinition Argument,
OutputFieldDefinition Field,
INamedTypeDefinition Type,
SchemaDefinition Schema);

internal record EachFieldArgumentNameEvent(
CompositionContext Context,
string ArgumentName,
ImmutableArray<FieldArgumentInfo> ArgumentInfo,
string FieldName,
string TypeName);

internal record EachOutputFieldEvent(
CompositionContext Context,
OutputFieldDefinition Field,
INamedTypeDefinition Type,
SchemaDefinition Schema);

internal record EachOutputFieldNameEvent(
CompositionContext Context,
string FieldName,
ImmutableArray<OutputFieldInfo> FieldInfo,
string TypeName);

internal record EachTypeEvent(
CompositionContext Context,
INamedTypeDefinition Type,
SchemaDefinition Schema);

internal record EachTypeNameEvent(
CompositionContext Context,
string TypeName,
ImmutableArray<TypeInfo> TypeInfo);

This file was deleted.

Loading

0 comments on commit b8a159a

Please sign in to comment.