Skip to content

[WIP] Add Configuration references#129734

Draft
rosebyte wants to merge 4 commits into
dotnet:mainfrom
rosebyte:configuration-references
Draft

[WIP] Add Configuration references#129734
rosebyte wants to merge 4 commits into
dotnet:mainfrom
rosebyte:configuration-references

Conversation

@rosebyte

Copy link
Copy Markdown
Member

No description provided.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new “configuration references” feature for Microsoft.Extensions.Configuration, enabling configuration values to be expanded via reference syntax (e.g., ref(Target:Key)) and composed values (e.g., format(template, key1, key2, ...)). It does so by adding a contextual configuration source/provider that materializes expanded values on top of upstream providers, plus a fairly comprehensive new test suite.

Changes:

  • Adds a new reference rule model + pattern matcher (ReferenceRule, KeyPattern) and a contextual configuration provider (ReferenceConfigurationProvider) to materialize expanded values and mirror subtrees.
  • Extends ConfigurationBuilder and ConfigurationManager to support building “contextual” configuration sources that require an upstream provider snapshot.
  • Introduces new public API surface (AllowReferences, ConfigurationReferenceBuilder, ConfigurationExpansion) and adds end-to-end tests.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationReferenceTests.cs New test coverage for reference resolution, templates, composition, reload behavior, and parsing.
src/libraries/Microsoft.Extensions.Configuration/src/Resources/Strings.resx Adds new user-facing error strings for reference expansion and validation.
src/libraries/Microsoft.Extensions.Configuration/src/ReferenceRule.cs Defines rule semantics (subject + allowed/denied targets) and validates parser output.
src/libraries/Microsoft.Extensions.Configuration/src/ReferenceConfigurationSource.cs Contextual source that requires upstream provider snapshot to build its provider.
src/libraries/Microsoft.Extensions.Configuration/src/ReferenceConfigurationProvider.cs Core implementation: resolves references, mirrors subtrees, composes format expansions, handles reload.
src/libraries/Microsoft.Extensions.Configuration/src/ReferenceConfigurationBuilderExtensions.cs Public builder extension method AllowReferences(...).
src/libraries/Microsoft.Extensions.Configuration/src/KeyPattern.cs Wildcard/doublestar key-pattern matching for subjects/targets.
src/libraries/Microsoft.Extensions.Configuration/src/IContextualConfigurationSource.cs Internal contract to allow sources to build with upstream provider list.
src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationReferenceBuilder.cs Public builder object for declaring rules and customizing the parser.
src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationManager.cs Adds contextual-source dispatch when adding/reloading sources.
src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationExpansion.cs Public struct representing expansion kinds (reference/literal/format) returned by parsers.
src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationBuilder.cs Adds contextual-source dispatch during build.
src/libraries/Microsoft.Extensions.Configuration/ref/Microsoft.Extensions.Configuration.cs Updates public API contract to include the new APIs/types.

Comment on lines +52 to +62
public static ConfigurationExpansion Format(string template, params string[] referencedKeys)
{
ArgumentNullException.ThrowIfNull(template);
ArgumentNullException.ThrowIfNull(referencedKeys);
return new ConfigurationExpansion(template, referencedKeys.Length switch
{
0 => StringValues.Empty,
1 => new StringValues(referencedKeys[0]),
_ => new StringValues(referencedKeys),
});
}
Comment on lines +180 to +184
[Fact]
public void Configurable_ValidSelection_ResolvesThrough()
{
IConfigurationRoot root = BuilderWith(Dict(
("Shared:Prod:Credential", "prod"),
Comment on lines +9 to +13
public sealed partial class ConfigurationReferenceBuilder
{
internal ConfigurationReferenceBuilder() { }
public Microsoft.Extensions.Configuration.ConfigurationReferenceBuilder Allow(string subject, string target, params string[] additionalTargets) { throw null; }
public Microsoft.Extensions.Configuration.ConfigurationReferenceBuilder Deny(string subject, string target, params string[] additionalTargets) { throw null; }
Comment on lines +43 to +49
ReferenceRule rule = FindOrCreateRule(NormaliseSubject(subject));
rule.AddTarget(NormaliseTarget(target));
foreach (string additionalTarget in additionalTargets)
{
rule.AddTarget(NormaliseTarget(additionalTarget));
}
return this;
Comment on lines +67 to +73
ReferenceRule rule = FindOrCreateRule(NormaliseSubject(subject));
rule.AddDisallowedTarget(NormaliseTarget(target));
foreach (string additionalTarget in additionalTargets)
{
rule.AddDisallowedTarget(NormaliseTarget(additionalTarget));
}
return this;
Comment on lines +389 to +405
// Reads the value already materialised for <paramref name="key"/> if any; otherwise
// falls back to the first non-empty upstream literal at the same key.
private string? OverlayAwareRead(string key, Dictionary<string, string?> values)
{
if (values.TryGetValue(key, out string? ov))
{
return ov;
}
for (int i = _upstream.Count - 1; i >= 0; i--)
{
if (_upstream[i].TryGet(key, out string? pv) && !string.IsNullOrEmpty(pv))
{
return pv;
}
}
return null;
}
Comment on lines +325 to +335
// A non-empty upstream literal at the overlay key should win on read;
// skip the overlay entry so it does.
string? sourceAtOverlay = ReadUpstream(overlayKey);
if (string.IsNullOrEmpty(sourceAtOverlay))
{
string? targetValue = OverlayAwareRead(childKey, values);
if (targetValue is not null)
{
values[overlayKey] = targetValue;
}
}
Comment on lines +141 to +146
<data name="Error_ReferenceTargetsRequired" xml:space="preserve">
<value>At least one non-empty reference target must be provided.</value>
</data>
<data name="Error_ReferenceTargetInvalid" xml:space="preserve">
<value>'{0}' is not a valid reference target pattern. Each ':'-separated segment must be non-empty; '*' is permitted only as a whole segment.</value>
</data>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants