Skip to content

Commit

Permalink
Adding more nullable fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
  • Loading branch information
askpt committed Mar 28, 2024
1 parent 45355d1 commit 3d21bd9
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 57 deletions.
6 changes: 3 additions & 3 deletions src/OpenFeature/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void SetProvider(FeatureProvider featureProvider)
/// </summary>
/// <remarks>The provider cannot be set to null. Attempting to set the provider to null has no effect.</remarks>
/// <param name="featureProvider">Implementation of <see cref="FeatureProvider"/></param>
public async Task SetProviderAsync(FeatureProvider featureProvider)
public async Task SetProviderAsync(FeatureProvider? featureProvider)
{
this._eventExecutor.RegisterDefaultFeatureProvider(featureProvider);
await this._repository.SetProvider(featureProvider, this.GetContext()).ConfigureAwait(false);
Expand All @@ -78,7 +78,7 @@ public void SetProvider(string clientName, FeatureProvider featureProvider)
/// </summary>
/// <param name="clientName">Name of client</param>
/// <param name="featureProvider">Implementation of <see cref="FeatureProvider"/></param>
public async Task SetProviderAsync(string clientName, FeatureProvider featureProvider)
public async Task SetProviderAsync(string? clientName, FeatureProvider featureProvider)
{
this._eventExecutor.RegisterClientFeatureProvider(clientName, featureProvider);
await this._repository.SetProvider(clientName, featureProvider, this.GetContext()).ConfigureAwait(false);
Expand Down Expand Up @@ -138,7 +138,7 @@ public FeatureProvider GetProvider(string clientName)
/// <param name="logger">Logger instance used by client</param>
/// <param name="context">Context given to this client</param>
/// <returns><see cref="FeatureClient"/></returns>
public FeatureClient GetClient(string name, string version, ILogger? logger = null,
public FeatureClient GetClient(string? name = null, string? version = null, ILogger? logger = null,
EvaluationContext? context = null) =>
new FeatureClient(name, version, logger, context);

Expand Down
6 changes: 3 additions & 3 deletions src/OpenFeature/EventExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ internal void RegisterDefaultFeatureProvider(FeatureProvider? provider)
}
}

internal void RegisterClientFeatureProvider(string client, FeatureProvider? provider)
internal void RegisterClientFeatureProvider(string? client, FeatureProvider? provider)
{
if (provider == null)
if (provider == null || client == null)
{
return;
}
Expand Down Expand Up @@ -260,7 +260,7 @@ private async void ProcessEventAsync()
// look for client handlers and call invoke method there
foreach (var keyAndValue in this._namedProviderReferences)
{
if (keyAndValue.Value == e.Provider)
if (keyAndValue.Value == e.Provider && keyAndValue.Key != null)
{
if (this._clientHandlers.TryGetValue(keyAndValue.Key, out var clientRegistry))
{
Expand Down
4 changes: 2 additions & 2 deletions src/OpenFeature/Model/ClientMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public sealed class ClientMetadata : Metadata
/// <summary>
/// Version of the client
/// </summary>
public string Version { get; }
public string? Version { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ClientMetadata"/> class
/// </summary>
/// <param name="name">Name of client</param>
/// <param name="version">Version of client</param>
public ClientMetadata(string name, string version) : base(name)
public ClientMetadata(string? name, string? version) : base(name)
{
this.Version = version;
}
Expand Down
8 changes: 4 additions & 4 deletions src/OpenFeature/Model/HookContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public sealed class HookContext<T>
/// <param name="providerMetadata">Provider metadata</param>
/// <param name="evaluationContext">Evaluation context</param>
/// <exception cref="ArgumentNullException">When any of arguments are null</exception>
public HookContext(string flagKey,
public HookContext(string? flagKey,
T defaultValue,
FlagValueType flagValueType,
ClientMetadata clientMetadata,
Metadata providerMetadata,
EvaluationContext evaluationContext)
ClientMetadata? clientMetadata,
Metadata? providerMetadata,
EvaluationContext? evaluationContext)
{
this.FlagKey = flagKey ?? throw new ArgumentNullException(nameof(flagKey));
this.DefaultValue = defaultValue;
Expand Down
4 changes: 2 additions & 2 deletions src/OpenFeature/Model/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public class Metadata
/// <summary>
/// Gets name of <see cref="Api"/> instance
/// </summary>
public string Name { get; }
public string? Name { get; }

/// <summary>
/// Initializes a new instance of the <see cref="Metadata"/> class.
/// </summary>
/// <param name="name">Name of <see cref="Api"/> instance</param>
public Metadata(string name)
public Metadata(string? name)
{
this.Name = name;
}
Expand Down
8 changes: 4 additions & 4 deletions src/OpenFeature/OpenFeatureClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public sealed class FeatureClient : IFeatureClient
{
// Alias the provider reference so getting the method and returning the provider are
// guaranteed to be the same object.
var provider = Api.Instance.GetProvider(this._metadata.Name);
var provider = Api.Instance.GetProvider(this._metadata.Name!);

return (method(provider), provider);
}
Expand Down Expand Up @@ -73,7 +73,7 @@ public void SetContext(EvaluationContext? context)
/// <param name="logger">Logger used by client</param>
/// <param name="context">Context given to this client</param>
/// <exception cref="ArgumentNullException">Throws if any of the required parameters are null</exception>
public FeatureClient(string name, string version, ILogger? logger = null, EvaluationContext? context = null)
public FeatureClient(string? name, string? version, ILogger? logger = null, EvaluationContext? context = null)
{
this._metadata = new ClientMetadata(name, version);
this._logger = logger ?? new Logger<Api>(new NullLoggerFactory());
Expand All @@ -96,13 +96,13 @@ public FeatureClient(string name, string version, ILogger? logger = null, Evalua
/// <inheritdoc />
public void AddHandler(ProviderEventTypes eventType, EventHandlerDelegate handler)
{
Api.Instance.AddClientHandler(this._metadata.Name, eventType, handler);
Api.Instance.AddClientHandler(this._metadata.Name!, eventType, handler);
}

/// <inheritdoc />
public void RemoveHandler(ProviderEventTypes type, EventHandlerDelegate handler)
{
Api.Instance.RemoveClientHandler(this._metadata.Name, type, handler);
Api.Instance.RemoveClientHandler(this._metadata.Name!, type, handler);
}

/// <inheritdoc />
Expand Down
4 changes: 2 additions & 2 deletions src/OpenFeature/ProviderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ public FeatureProvider GetProvider()
}
}

public FeatureProvider GetProvider(string clientName)
public FeatureProvider GetProvider(string? clientName)
{
if (string.IsNullOrEmpty(clientName))
if (clientName == null || string.IsNullOrEmpty(clientName))
{
return this.GetProvider();
}
Expand Down
8 changes: 6 additions & 2 deletions test/OpenFeature.Tests/OpenFeatureClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public void OpenFeatureClient_Should_Allow_Hooks()
{
var fixture = new Fixture();
var clientName = fixture.Create<string>();
var clientVersion = fixture.Create<string>();
var hook1 = Substitute.For<Hook>();
var hook2 = Substitute.For<Hook>();
var hook3 = Substitute.For<Hook>();

var client = Api.Instance.GetClient(clientName);
var client = Api.Instance.GetClient(clientName, clientVersion);

client.AddHooks(new[] { hook1, hook2 });

Expand Down Expand Up @@ -359,9 +360,12 @@ public async Task Should_Use_No_Op_When_Provider_Is_Null()
[Fact]
public void Should_Get_And_Set_Context()
{
var fixture = new Fixture();
var clientName = fixture.Create<string>();
var clientVersion = fixture.Create<string>();
var KEY = "key";
var VAL = 1;
FeatureClient client = Api.Instance.GetClient();
FeatureClient client = Api.Instance.GetClient(clientName, clientVersion);
client.SetContext(new EvaluationContextBuilder().Set(KEY, VAL).Build());
Assert.Equal(VAL, client.GetContext().GetValue(KEY).AsInteger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void EvaluationContext_Should_All_Types()
var context = contextBuilder.Build();

context.TargetingKey.Should().Be("targeting_key");
var targetingKeyValue = context.GetValue(context.TargetingKey);
var targetingKeyValue = context.GetValue(context.TargetingKey!);
targetingKeyValue.IsString.Should().BeTrue();
targetingKeyValue.AsString.Should().Be("userId");

Expand Down
18 changes: 11 additions & 7 deletions test/OpenFeature.Tests/OpenFeatureEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ public async Task Client_Level_Event_Handlers_Should_Be_Registered()
var fixture = new Fixture();
var eventHandler = Substitute.For<EventHandlerDelegate>();

var myClient = Api.Instance.GetClient(fixture.Create<string>());
var clientName = fixture.Create<string>();
var clientVersion = fixture.Create<string>();
var myClient = Api.Instance.GetClient(clientName, clientVersion);

var testProvider = new TestProvider();
await Api.Instance.SetProviderAsync(myClient.GetMetadata().Name, testProvider);
Expand All @@ -326,7 +328,9 @@ public async Task Client_Level_Event_Handlers_Should_Be_Executed_When_Other_Hand
failingEventHandler.When(x => x.Invoke(Arg.Any<ProviderEventPayload>()))
.Do(x => throw new Exception());

var myClient = Api.Instance.GetClient(fixture.Create<string>());
var clientName = fixture.Create<string>();
var clientVersion = fixture.Create<string>();
var myClient = Api.Instance.GetClient(clientName, clientVersion);

myClient.AddHandler(ProviderEventTypes.ProviderReady, failingEventHandler);
myClient.AddHandler(ProviderEventTypes.ProviderReady, eventHandler);
Expand Down Expand Up @@ -354,8 +358,8 @@ public async Task Client_Level_Event_Handlers_Should_Be_Registered_To_Default_Pr
var eventHandler = Substitute.For<EventHandlerDelegate>();
var clientEventHandler = Substitute.For<EventHandlerDelegate>();

var myClientWithNoBoundProvider = Api.Instance.GetClient(fixture.Create<string>());
var myClientWithBoundProvider = Api.Instance.GetClient(fixture.Create<string>());
var myClientWithNoBoundProvider = Api.Instance.GetClient(fixture.Create<string>(), fixture.Create<string>());
var myClientWithBoundProvider = Api.Instance.GetClient(fixture.Create<string>(), fixture.Create<string>());

var apiProvider = new TestProvider(fixture.Create<string>());
var clientProvider = new TestProvider(fixture.Create<string>());
Expand Down Expand Up @@ -387,7 +391,7 @@ public async Task Client_Level_Event_Handlers_Should_Be_Receive_Events_From_Name
var fixture = new Fixture();
var clientEventHandler = Substitute.For<EventHandlerDelegate>();

var client = Api.Instance.GetClient(fixture.Create<string>());
var client = Api.Instance.GetClient(fixture.Create<string>(), fixture.Create<string>());

var defaultProvider = new TestProvider(fixture.Create<string>());
var clientProvider = new TestProvider(fixture.Create<string>());
Expand Down Expand Up @@ -433,7 +437,7 @@ public async Task Client_Level_Event_Handlers_Should_Be_Informed_About_Ready_Sta
var fixture = new Fixture();
var eventHandler = Substitute.For<EventHandlerDelegate>();

var myClient = Api.Instance.GetClient(fixture.Create<string>());
var myClient = Api.Instance.GetClient(fixture.Create<string>(), fixture.Create<string>());

var testProvider = new TestProvider();
await Api.Instance.SetProviderAsync(myClient.GetMetadata().Name, testProvider);
Expand All @@ -456,7 +460,7 @@ public async Task Client_Level_Event_Handlers_Should_Be_Removable()

var eventHandler = Substitute.For<EventHandlerDelegate>();

var myClient = Api.Instance.GetClient(fixture.Create<string>());
var myClient = Api.Instance.GetClient(fixture.Create<string>(), fixture.Create<string>());

myClient.AddHandler(ProviderEventTypes.ProviderReady, eventHandler);

Expand Down
2 changes: 1 addition & 1 deletion test/OpenFeature.Tests/OpenFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public async Task OpenFeature_Should_Get_Metadata()
[InlineData("client2", null)]
[InlineData(null, null)]
[Specification("1.1.6", "The `API` MUST provide a function for creating a `client` which accepts the following options: - name (optional): A logical string identifier for the client.")]
public void OpenFeature_Should_Create_Client(string name = null, string version = null)
public void OpenFeature_Should_Create_Client(string? name = null, string? version = null)
{
var openFeature = Api.Instance;
var client = openFeature.GetClient(name, version);
Expand Down
16 changes: 8 additions & 8 deletions test/OpenFeature.Tests/ProviderRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public async Task AfterError_Is_Invoked_If_Initialization_Errors_Default_Provide
var context = new EvaluationContextBuilder().Build();
providerMock.When(x => x.Initialize(context)).Throw(new Exception("BAD THINGS"));
var callCount = 0;
Exception receivedError = null;
Exception? receivedError = null;
await repository.SetProvider(providerMock, context, afterError: (theProvider, error) =>
{
Assert.Equal(providerMock, theProvider);
callCount++;
receivedError = error;
});
Assert.Equal("BAD THINGS", receivedError.Message);
Assert.Equal("BAD THINGS", receivedError?.Message);
Assert.Equal(1, callCount);
}

Expand Down Expand Up @@ -170,15 +170,15 @@ public async Task AfterError_Is_Called_For_Shutdown_That_Throws()
var context = new EvaluationContextBuilder().Build();
await repository.SetProvider(provider1, context);
var callCount = 0;
Exception errorThrown = null;
Exception? errorThrown = null;
await repository.SetProvider(provider2, context, afterError: (provider, ex) =>
{
Assert.Equal(provider, provider1);
errorThrown = ex;
callCount++;
});
Assert.Equal(1, callCount);
Assert.Equal("SHUTDOWN ERROR", errorThrown.Message);
Assert.Equal("SHUTDOWN ERROR", errorThrown?.Message);
}

[Fact]
Expand Down Expand Up @@ -244,14 +244,14 @@ public async Task AfterError_Is_Invoked_If_Initialization_Errors_Named_Provider(
var context = new EvaluationContextBuilder().Build();
providerMock.When(x => x.Initialize(context)).Throw(new Exception("BAD THINGS"));
var callCount = 0;
Exception receivedError = null;
Exception? receivedError = null;
await repository.SetProvider("the-provider", providerMock, context, afterError: (theProvider, error) =>
{
Assert.Equal(providerMock, theProvider);
callCount++;
receivedError = error;
});
Assert.Equal("BAD THINGS", receivedError.Message);
Assert.Equal("BAD THINGS", receivedError?.Message);
Assert.Equal(1, callCount);
}

Expand Down Expand Up @@ -337,15 +337,15 @@ public async Task AfterError_Is_Called_For_Shutdown_Named_Provider_That_Throws()
var context = new EvaluationContextBuilder().Build();
await repository.SetProvider("the-name", provider1, context);
var callCount = 0;
Exception errorThrown = null;
Exception? errorThrown = null;
await repository.SetProvider("the-name", provider2, context, afterError: (provider, ex) =>
{
Assert.Equal(provider, provider1);
errorThrown = ex;
callCount++;
});
Assert.Equal(1, callCount);
Assert.Equal("SHUTDOWN ERROR", errorThrown.Message);
Assert.Equal("SHUTDOWN ERROR", errorThrown?.Message);
}

[Fact]
Expand Down
10 changes: 5 additions & 5 deletions test/OpenFeature.Tests/Providers/Memory/InMemoryProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public InMemoryProviderTests()
},
defaultVariant: "external",
(context) => {
if (context.GetValue("email").AsString.Contains("@faas.com"))
if (context.GetValue("email").AsString?.Contains("@faas.com") == true)
{
return "internal";
}
Expand Down Expand Up @@ -148,9 +148,9 @@ public async void GetDouble_ShouldEvaluateWithReasonAndVariant()
public async void GetStruct_ShouldEvaluateWithReasonAndVariant()
{
ResolutionDetails<Value> details = await this.commonProvider.ResolveStructureValue("object-flag", new Value(), EvaluationContext.Empty);
Assert.Equal(true, details.Value.AsStructure["showImages"].AsBoolean);
Assert.Equal("Check out these pics!", details.Value.AsStructure["title"].AsString);
Assert.Equal(100, details.Value.AsStructure["imagesPerPage"].AsInteger);
Assert.Equal(true, details.Value.AsStructure?["showImages"].AsBoolean);
Assert.Equal("Check out these pics!", details.Value.AsStructure?["title"].AsString);
Assert.Equal(100, details.Value.AsStructure?["imagesPerPage"].AsInteger);
Assert.Equal(Reason.Static, details.Reason);
Assert.Equal("template", details.Variant);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ await provider.UpdateFlags(new Dictionary<string, Flag>(){
}});

var res = await provider.GetEventChannel().Reader.ReadAsync() as ProviderEventPayload;
Assert.Equal(ProviderEventTypes.ProviderConfigurationChanged, res.Type);
Assert.Equal(ProviderEventTypes.ProviderConfigurationChanged, res?.Type);

await Assert.ThrowsAsync<FlagNotFoundException>(() => provider.ResolveBooleanValue("old-flag", false, EvaluationContext.Empty));

Expand Down
4 changes: 2 additions & 2 deletions test/OpenFeature.Tests/StructureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public void TryGetValue_Should_Return_Value()

var structure = Structure.Builder()
.Set(KEY, VAL).Build();
Value value;
Value? value;
Assert.True(structure.TryGetValue(KEY, out value));
Assert.Equal(VAL, value.AsString);
Assert.Equal(VAL, value?.AsString);
}

[Fact]
Expand Down
Loading

0 comments on commit 3d21bd9

Please sign in to comment.