Skip to content

Commit

Permalink
string to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
KSemenenko committed May 3, 2023
1 parent 2c6f160 commit c4d7e4b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="FluentAssertions" Version="6.11.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -21,7 +22,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ManagedCode.FeatureChecker\ManagedCode.FeatureChecker.csproj" />
<ProjectReference Include="..\ManagedCode.FeatureChecker\ManagedCode.FeatureChecker.csproj"/>
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions ManagedCode.FeatureChecker.Tests/MyEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ManagedCode.FeatureChecker.Tests;

internal enum MyEnum
{
feature1,
feature2,
feature3,
feature4,
feature5
}
21 changes: 19 additions & 2 deletions ManagedCode.FeatureChecker.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ManagedCode.FeatureChecker;
using FluentAssertions;
using Xunit;

namespace ManagedCode.FeatureChecker.Tests;
Expand All @@ -8,7 +8,24 @@ public class Tests
[Fact]
public void TestDetectorTest()
{
var holder = new FeatureHolder();
holder.Add(MyEnum.feature1, FeatureStatus.Enabled);
holder.Add(MyEnum.feature2, FeatureStatus.Disabled);
holder.Add(MyEnum.feature3, FeatureStatus.Enabled);
holder.Add(MyEnum.feature4, FeatureStatus.Disabled);
holder.Add(MyEnum.feature5, FeatureStatus.Debug);

var checker = new FeatureChecker(holder);

checker.IsDebug(MyEnum.feature5).Should().BeTrue();
checker.IsDebug(MyEnum.feature1).Should().BeFalse();

checker.IsEnabled(MyEnum.feature1).Should().BeTrue();
checker.IsEnabled(MyEnum.feature5).Should().BeFalse();
checker.IsEnabled(MyEnum.feature2).Should().BeFalse();

checker.IsDisabled(MyEnum.feature2).Should().BeTrue();
checker.IsDisabled(MyEnum.feature3).Should().BeFalse();
checker.IsDisabled(MyEnum.feature5).Should().BeFalse();
}

}
56 changes: 34 additions & 22 deletions ManagedCode.FeatureChecker/FeatureChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,57 @@

namespace ManagedCode.FeatureChecker;

public class FeatureChecker
public class FeatureChecker : IFeatureChecker
{
private readonly ImmutableDictionary<string, FeatureStatus> _features;
private readonly ImmutableDictionary<Enum, FeatureStatus> _features;

public FeatureChecker(IDictionary<Enum, FeatureStatus> featureHolder)
{
_features = featureHolder.ToImmutableDictionary();
}

public int Count => _features.Count;

public FeatureChecker(FeatureHolder featureHolder)
public bool IsFeatureExists(Enum feature)
{
_features = featureHolder.Features;
return _features.ContainsKey(feature);
}

public bool IsFeatureExists(string name)
public bool IsEnabled(Enum feature)
{
return ValidateFeatureName(name)
? _features.ContainsKey(name)
: false;
if (_features.TryGetValue(feature, out var status))
return status == FeatureStatus.Enabled;

return false;
}

public bool TryGetFeatureStatus(string name, out FeatureStatus status)
public bool IsDisabled(Enum feature)
{
status = default;
if (_features.TryGetValue(feature, out var status))
return status == FeatureStatus.Disabled;

return ValidateFeatureName(name)
? _features.TryGetValue(name, out status)
: false;
return false;
}

public List<string> GetFeaturesByStatus(FeatureStatus status)
public bool IsDebug(Enum feature)
{
return _features
.Where(x => x.Value == status)
.Select(x => x.Key)
.ToList();
}
if (_features.TryGetValue(feature, out var status))
return status == FeatureStatus.Debug;

return false;
}

private bool ValidateFeatureName(string featureName)
public bool TryGetFeatureStatus(Enum feature, out FeatureStatus status)
{
return !string.IsNullOrWhiteSpace(featureName);
status = default;
return _features.TryGetValue(feature, out status);
}

}
public List<Enum> GetFeaturesByStatus(FeatureStatus status)
{
return _features
.Where(x => x.Value == status)
.Select(x => x.Key)
.ToList();
}
}
62 changes: 3 additions & 59 deletions ManagedCode.FeatureChecker/FeatureHolder.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,5 @@
using System.Collections.Immutable;
using System.Text.Json.Serialization;
namespace ManagedCode.FeatureChecker;

namespace ManagedCode.FeatureChecker;
public class FeatureHolder
public class FeatureHolder : Dictionary<Enum, FeatureStatus>
{
private Dictionary<string, FeatureStatus> _features;

[JsonInclude]
public ImmutableDictionary<string, FeatureStatus> Features
{
get => _features.ToImmutableDictionary();
private set => _features = new Dictionary<string, FeatureStatus>(value);
}


public FeatureHolder()
{
_features = new Dictionary<string, FeatureStatus>();
}

public bool TryAddFeature(string featureName, FeatureStatus status)
{
return ValidateFeatureName(featureName)
? _features.TryAdd(featureName, status)
: false;
}

public bool TryGetFeatureStatus(string featureName, out FeatureStatus status)
{
status = default;

return ValidateFeatureName(featureName)
? _features.TryGetValue(featureName, out status)
: false;
}

public void RemoveFeature(string featureName)
{
if(ValidateFeatureName(featureName))
{
_features.Remove(featureName);
}
}

public void UpdateFeatureStatus(string featureName, FeatureStatus status)
{
if(!ValidateFeatureName(featureName))
{
return;
}

_features[featureName] = status;
}


private bool ValidateFeatureName(string featureName)
{
return !string.IsNullOrWhiteSpace(featureName);
}
}
}
4 changes: 2 additions & 2 deletions ManagedCode.FeatureChecker/FeatureStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public enum FeatureStatus
{
Disabled,
Enabled,
Debug,
}
Debug
}
12 changes: 12 additions & 0 deletions ManagedCode.FeatureChecker/IFeatureChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ManagedCode.FeatureChecker;

public interface IFeatureChecker
{
int Count { get; }
bool IsFeatureExists(Enum feature);
bool IsEnabled(Enum feature);
bool IsDisabled(Enum feature);
bool IsDebug(Enum feature);
bool TryGetFeatureStatus(Enum feature, out FeatureStatus status);
List<Enum> GetFeaturesByStatus(FeatureStatus status);
}
8 changes: 4 additions & 4 deletions ManagedCode.FeatureChecker/ManagedCode.FeatureChecker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<PackageTags>managedcode, Tests, Detector, TestsDetector, unit tests</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0"/>
<PackageReference Include="System.Collections.Immutable" Version="7.0.0"/>
<PackageReference Include="System.Text.Json" Version="7.0.2"/>
</ItemGroup>

</Project>

0 comments on commit c4d7e4b

Please sign in to comment.