Skip to content

Commit

Permalink
Namespaced Feature Flags Support (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
derekmckinnon committed Jan 10, 2024
1 parent b274580 commit 48f5ac7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ To get around some of the limitations of how AppConfig lets you construct attrib
- A filter with parameters should be named `featureFilter__parameterName` and have a value for the parameter (e.g. `"percentage__value": 50`)
- The provider uses the double underscore (`__`) to separate the filter name from the parameter name
- You can supply multiple parameters using this scheme (e.g. `"percentage__value": 50, "percentage__foobar": "baz"`)
- Namespaced filters are support with the usage of a single underscore (`_`) (e.g. `"namespace_featureFilter": null` -> `Namespace.FeatureFilter`)
- Extremely complex filters with deeply nested attributes are not supported (ex. `Microsoft.Targeting` and its parameters)

There are likely to be some limitations with this approach, so please open an issue if you find any that don't match your use case.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.AppConfigData" Version="3.7.300.16" />
<PackageReference Include="AWSSDK.AppConfigData" Version="3.7.300.32" />
<PackageReference Include="CatConsult.ConfigurationParsers" Version="2.1.0" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ private void ParseInternal(FeatureFlagsProfile profile)

foreach ((string name, FeatureFlag flag) in profile)
{
PushContext(name.Pascalize());
// To support flag names that contain dots, we replace them with underscores and convert to PascalCase (e.g. "foo_bar" becomes "Foo.Bar")
var flagName = name.Replace("_", ".").Pascalize();

PushContext(flagName);
VisitFeatureFlag(flag);
PopContext();
}
Expand Down Expand Up @@ -81,13 +84,13 @@ private void VisitFeatureFlag(FeatureFlag flag)
PushContext(i);

PushContext("Name");
SetValue(featureFilter.Pascalize());
SetValue(featureFilter);
PopContext();

PushContext("Parameters");
foreach ((string parameterName, string? parameterValue) in parameters)
{
PushContext(parameterName.Pascalize());
PushContext(parameterName);
SetValue(parameterValue);
PopContext(); // parameterName
}
Expand Down Expand Up @@ -116,8 +119,8 @@ private void VisitFeatureFlag(FeatureFlag flag)
continue; // We ignore invalid keys
}

// We convert the feature filter name to PascalCase (e.g. "percentage" becomes "Percentage")
var featureFilterName = keyParts[0].Pascalize();
// To support namespaced feature filters, we split on single underscores, convert the parts to PascalCase, and join them with "." (e.g. "microsoft_percentage" becomes "Microsoft.Percentage")
var featureFilterName = string.Join(".", keyParts[0].Split("_").Select(p => p.Pascalize()));

// If there's a second element, we treat it as the parameter name and convert to PascalCase as well (e.g. "fooValue" becomes "FooValue")
var parameterName = keyParts.Length == 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,16 @@ public void Load_Should_Parse_FeatureFlags()

VerifyValue(sut, $"{prefix}:EdgeCase1:RequirementType", "Any");
VerifyValue(sut, $"{prefix}:EdgeCase1:EnabledFor:0:Name", "AlwaysOn");

VerifyValue(sut, $"{prefix}:EdgeCase2:RequirementType", "Any");
VerifyValue(sut, $"{prefix}:EdgeCase2:EnabledFor:0:Name", "Foobar");
VerifyValue(sut, $"{prefix}:EdgeCase2:EnabledFor:0:Parameters:Value", null);

VerifyValue(sut, $"{prefix}:Namespaced.Case1", "true");

VerifyValue(sut, $"{prefix}:Namespaced.Case2:RequirementType", "Any");
VerifyValue(sut, $"{prefix}:Namespaced.Case2:EnabledFor:0:Name", "Microsoft.Percentage");
VerifyValue(sut, $"{prefix}:Namespaced.Case2:EnabledFor:0:Parameters:Value", "100");
}

// Helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Bogus" Version="35.3.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@
"enabled": true,
"requirementType": "Any",
"foobar__value": null
},
"namespaced_Case1": {
"enabled": true
},
"namespaced_Case2": {
"enabled": true,
"requirementType": "Any",
"microsoft_percentage__Value": 100
}
}

0 comments on commit 48f5ac7

Please sign in to comment.