Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace WB.Configuration;

/// <inheritdoc cref="IConfiguration"/>
public sealed class Configuration : IConfiguration, IConfigurationNode

Check warning on line 9 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

The type name Configuration conflicts in whole or in part with the namespace name 'System.Configuration' defined in the .NET Framework. Rename the type to eliminate the conflict. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1724)

Check warning on line 9 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

The type name Configuration conflicts in whole or in part with the namespace name 'System.Configuration' defined in the .NET Framework. Rename the type to eliminate the conflict. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1724)

Check warning on line 9 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationNodeTests

The type name Configuration conflicts in whole or in part with the namespace name 'System.Configuration' defined in the .NET Framework. Rename the type to eliminate the conflict. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1724)

Check warning on line 9 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationTests

The type name Configuration conflicts in whole or in part with the namespace name 'System.Configuration' defined in the .NET Framework. Rename the type to eliminate the conflict. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1724)

Check warning on line 9 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / IConfigurationTests

The type name Configuration conflicts in whole or in part with the namespace name 'System.Configuration' defined in the .NET Framework. Rename the type to eliminate the conflict. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1724)
{
// ┌─────────────────────────────────────────────────────────────────────────────┐
// │ Private Fields │
Expand All @@ -17,7 +17,7 @@

private ConfigurationNode? configurationNode;

public IConfigurationNode this[int index]

Check warning on line 20 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

Missing XML comment for publicly visible type or member 'Configuration.this[int]'

Check warning on line 20 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

Missing XML comment for publicly visible type or member 'Configuration.this[int]'

Check warning on line 20 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationNodeTests

Missing XML comment for publicly visible type or member 'Configuration.this[int]'

Check warning on line 20 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationTests

Missing XML comment for publicly visible type or member 'Configuration.this[int]'

Check warning on line 20 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / IConfigurationTests

Missing XML comment for publicly visible type or member 'Configuration.this[int]'
{
get
{
Expand All @@ -30,7 +30,7 @@
}
}

public IConfigurationNode this[string key]

Check warning on line 33 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

Missing XML comment for publicly visible type or member 'Configuration.this[string]'

Check warning on line 33 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

Missing XML comment for publicly visible type or member 'Configuration.this[string]'

Check warning on line 33 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationNodeTests

Missing XML comment for publicly visible type or member 'Configuration.this[string]'

Check warning on line 33 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationTests

Missing XML comment for publicly visible type or member 'Configuration.this[string]'

Check warning on line 33 in src/Configuration.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / IConfigurationTests

Missing XML comment for publicly visible type or member 'Configuration.this[string]'
{
get
{
Expand All @@ -47,6 +47,17 @@
// │ Public Methods │
// └─────────────────────────────────────────────────────────────────────────────┘

/// <inheritdoc />
public T Get<T>()
{
if (configurationNode is null)
{
return default!;
}

return configurationNode.Get<T>();
}

/// <inheritdoc />
public bool TryGet<T>(string key, out T? value)
{
Expand Down
14 changes: 14 additions & 0 deletions src/ConfigurationNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace WB.Configuration;

using System;
using System.Text.Json;
using System.Text.Json.Nodes;

Expand All @@ -15,6 +16,19 @@ internal sealed class ConfigurationNode(JsonNode? jsonNode) : IConfigurationNode
// │ Public Indexers │
// └─────────────────────────────────────────────────────────────────────────────┘

/// <inheritdoc />
public T Get<T>()
{
if (jsonNode is null)
{
return default!;
}

T? result = jsonNode.Deserialize<T>() ?? throw new InvalidOperationException($"Failed to deserialize JSON node to type {typeof(T).FullName}.");

return result;
}

/// <inheritdoc />
public IConfigurationNode this[string key]
{
Expand Down
2 changes: 2 additions & 0 deletions src/IConfigurationNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
// │ Public Methods │
// └─────────────────────────────────────────────────────────────────────────────┘

public T Get<T>();

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

Rename virtual/interface member IConfigurationNode.Get<T>() so that it no longer conflicts with the reserved language keyword 'Get'. Using a reserved keyword as the name of a virtual/interface member makes it harder for consumers in other languages to override/implement the member. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1716)

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

Missing XML comment for publicly visible type or member 'IConfigurationNode.Get<T>()'

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

Rename virtual/interface member IConfigurationNode.Get<T>() so that it no longer conflicts with the reserved language keyword 'Get'. Using a reserved keyword as the name of a virtual/interface member makes it harder for consumers in other languages to override/implement the member. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1716)

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / 🏗️ Build Nuget Package / build

Missing XML comment for publicly visible type or member 'IConfigurationNode.Get<T>()'

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationNodeTests

Rename virtual/interface member IConfigurationNode.Get<T>() so that it no longer conflicts with the reserved language keyword 'Get'. Using a reserved keyword as the name of a virtual/interface member makes it harder for consumers in other languages to override/implement the member. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1716)

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationNodeTests

Missing XML comment for publicly visible type or member 'IConfigurationNode.Get<T>()'

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationTests

Rename virtual/interface member IConfigurationNode.Get<T>() so that it no longer conflicts with the reserved language keyword 'Get'. Using a reserved keyword as the name of a virtual/interface member makes it harder for consumers in other languages to override/implement the member. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1716)

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / ConfigurationTests

Missing XML comment for publicly visible type or member 'IConfigurationNode.Get<T>()'

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / IConfigurationTests

Rename virtual/interface member IConfigurationNode.Get<T>() so that it no longer conflicts with the reserved language keyword 'Get'. Using a reserved keyword as the name of a virtual/interface member makes it harder for consumers in other languages to override/implement the member. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1716)

Check warning on line 33 in src/IConfigurationNode.cs

View workflow job for this annotation

GitHub Actions / ✅ Run Tests / IConfigurationTests

Missing XML comment for publicly visible type or member 'IConfigurationNode.Get<T>()'

/// <summary>
/// Tries to get a value of type <typeparamref name="T"/> associated with the specified key. Returns true if the key exists and the value can be converted to type <typeparamref name="T"/>; otherwise, returns false and sets the output parameter to default.
/// </summary>
Expand Down
33 changes: 33 additions & 0 deletions tests/ConfigurationNodeTests/src/MethodTests/GetMethodTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Text.Json.Nodes;
using AwesomeAssertions;

namespace WB.Configuration;

public sealed class GetMethodTests
{
[Test]
public void ShouldReturnDefaultValue_WhenCurrentNodeIsNull()
{
// Arrange
ConfigurationNode configurationNode = new(null);

// Act
string? value = configurationNode.Get<string>();

// Assert
value.Should().BeNull(because: "the current node is null and should return the default value for the requested type");
}

[Test]
public void ShouldReturnDeserializedValue_WhenCurrentNodeIsValidJson()
{
// Arrange
ConfigurationNode configurationNode = new(JsonNode.Parse("\"value\""));

// Act
string? value = configurationNode.Get<string>();

// Assert
value.Should().Be("value", because: "the current node contains valid JSON that can be deserialized to the requested type");
}
}
Loading