-
Notifications
You must be signed in to change notification settings - Fork 79
Add support for YAML-based config #1491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/add-yaml-config-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c1b6598
Initial plan
Copilot 766dc40
Add YAML configuration support with anchors and merge keys
Copilot 77ccd75
Fix YAML merge key precedence logic
Copilot 31f1cc9
Fix unsafe casts in YAML processing
Copilot be07f52
Improve YAML boolean and null value detection per YAML spec
Copilot 0188110
Merge branch 'main' into copilot/add-yaml-config-support
waldekmastykarz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
259 changes: 259 additions & 0 deletions
259
DevProxy.Abstractions/Extensions/YamlConfigurationExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using DevProxy.Abstractions.Utils; | ||
| using YamlDotNet.RepresentationModel; | ||
|
|
||
| #pragma warning disable IDE0130 | ||
| namespace Microsoft.Extensions.Configuration; | ||
| #pragma warning restore IDE0130 | ||
|
|
||
| /// <summary> | ||
| /// A YAML file configuration source. | ||
| /// </summary> | ||
| public sealed class YamlConfigurationSource : FileConfigurationSource | ||
| { | ||
| /// <inheritdoc/> | ||
| public override IConfigurationProvider Build(IConfigurationBuilder builder) | ||
| { | ||
| EnsureDefaults(builder); | ||
| return new YamlConfigurationProvider(this); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A YAML file configuration provider that supports anchors and merge keys. | ||
| /// </summary> | ||
| public sealed class YamlConfigurationProvider : FileConfigurationProvider | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="YamlConfigurationProvider"/> class. | ||
| /// </summary> | ||
| /// <param name="source">The configuration source.</param> | ||
| public YamlConfigurationProvider(YamlConfigurationSource source) : base(source) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Load(Stream stream) | ||
| { | ||
| using var reader = new StreamReader(stream); | ||
| var yamlContent = reader.ReadToEnd(); | ||
|
|
||
| // Parse the YAML using RepresentationModel which handles anchors/aliases natively | ||
| var yaml = new YamlStream(); | ||
| using var stringReader = new StringReader(yamlContent); | ||
| yaml.Load(stringReader); | ||
|
|
||
| Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| if (yaml.Documents.Count == 0 || yaml.Documents[0].RootNode is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (yaml.Documents[0].RootNode is YamlMappingNode mappingNode) | ||
| { | ||
| FlattenYamlNode(mappingNode, string.Empty); | ||
| } | ||
| } | ||
|
|
||
| private void FlattenYamlNode(YamlNode node, string prefix) | ||
| { | ||
| switch (node) | ||
| { | ||
| case YamlMappingNode mappingNode: | ||
| FlattenMappingNode(mappingNode, prefix); | ||
| break; | ||
| case YamlSequenceNode sequenceNode: | ||
| FlattenSequenceNode(sequenceNode, prefix); | ||
| break; | ||
| case YamlScalarNode scalarNode: | ||
| Data[prefix] = scalarNode.Value; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| private void FlattenMappingNode(YamlMappingNode mappingNode, string prefix) | ||
| { | ||
| // First, collect all merge key values | ||
| var mergedValues = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| foreach (var entry in mappingNode.Children) | ||
| { | ||
| var key = GetScalarValue(entry.Key); | ||
| if (key is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Handle YAML merge key (<<) | ||
| if (key == "<<") | ||
| { | ||
| if (entry.Value is YamlMappingNode mergeMapping) | ||
| { | ||
| CollectMergedValues(mergeMapping, string.Empty, mergedValues); | ||
| } | ||
| else if (entry.Value is YamlSequenceNode mergeSequence) | ||
| { | ||
| foreach (var item in mergeSequence.Children) | ||
| { | ||
| if (item is YamlMappingNode itemMapping) | ||
| { | ||
| CollectMergedValues(itemMapping, string.Empty, mergedValues); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Add merged values first (they can be overridden by explicit values) | ||
| foreach (var kvp in mergedValues) | ||
| { | ||
| var fullKey = string.IsNullOrEmpty(prefix) | ||
| ? kvp.Key | ||
| : $"{prefix}{ConfigurationPath.KeyDelimiter}{kvp.Key}"; | ||
| Data[fullKey] = kvp.Value; | ||
| } | ||
|
|
||
| // Then process regular keys (they override merged values) | ||
| foreach (var entry in mappingNode.Children) | ||
| { | ||
| var key = GetScalarValue(entry.Key); | ||
| if (key is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Skip merge key | ||
| if (key == "<<") | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var newPrefix = string.IsNullOrEmpty(prefix) | ||
| ? key | ||
| : $"{prefix}{ConfigurationPath.KeyDelimiter}{key}"; | ||
|
|
||
| FlattenYamlNode(entry.Value, newPrefix); | ||
| } | ||
| } | ||
|
|
||
| private static string? GetScalarValue(YamlNode node) | ||
| { | ||
| return node is YamlScalarNode scalarNode ? scalarNode.Value : null; | ||
| } | ||
|
|
||
| private void CollectMergedValues(YamlMappingNode mappingNode, string prefix, Dictionary<string, string?> values) | ||
| { | ||
| foreach (var entry in mappingNode.Children) | ||
| { | ||
| var key = GetScalarValue(entry.Key); | ||
| if (key is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Skip nested merge keys in merged content | ||
| if (key == "<<") | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var newPrefix = string.IsNullOrEmpty(prefix) | ||
| ? key | ||
| : $"{prefix}{ConfigurationPath.KeyDelimiter}{key}"; | ||
|
|
||
| CollectMergedValuesFromNode(entry.Value, newPrefix, values); | ||
| } | ||
| } | ||
|
|
||
| private void CollectMergedValuesFromNode(YamlNode node, string prefix, Dictionary<string, string?> values) | ||
| { | ||
| switch (node) | ||
| { | ||
| case YamlMappingNode mappingNode: | ||
| CollectMergedValues(mappingNode, prefix, values); | ||
| break; | ||
| case YamlSequenceNode sequenceNode: | ||
| for (int i = 0; i < sequenceNode.Children.Count; i++) | ||
| { | ||
| var newPrefix = $"{prefix}{ConfigurationPath.KeyDelimiter}{i}"; | ||
| CollectMergedValuesFromNode(sequenceNode.Children[i], newPrefix, values); | ||
| } | ||
| break; | ||
| case YamlScalarNode scalarNode: | ||
| // Later values override earlier values within merged content | ||
| values[prefix] = scalarNode.Value; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| private void FlattenSequenceNode(YamlSequenceNode sequenceNode, string prefix) | ||
| { | ||
| for (int i = 0; i < sequenceNode.Children.Count; i++) | ||
| { | ||
| var newPrefix = $"{prefix}{ConfigurationPath.KeyDelimiter}{i}"; | ||
| FlattenYamlNode(sequenceNode.Children[i], newPrefix); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for adding YAML configuration. | ||
| /// </summary> | ||
| public static class YamlConfigurationExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds a YAML configuration source to the configuration builder. | ||
| /// </summary> | ||
| /// <param name="builder">The configuration builder.</param> | ||
| /// <param name="path">The path to the YAML file.</param> | ||
| /// <param name="optional">Whether the file is optional.</param> | ||
| /// <param name="reloadOnChange">Whether to reload on change.</param> | ||
| /// <returns>The configuration builder.</returns> | ||
| public static IConfigurationBuilder AddYamlFile( | ||
| this IConfigurationBuilder builder, | ||
| string path, | ||
| bool optional = false, | ||
| bool reloadOnChange = false) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
| ArgumentException.ThrowIfNullOrEmpty(path); | ||
|
|
||
| return builder.Add<YamlConfigurationSource>(s => | ||
| { | ||
| s.FileProvider = null; | ||
| s.Path = path; | ||
| s.Optional = optional; | ||
| s.ReloadOnChange = reloadOnChange; | ||
| s.ResolveFileProvider(); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a configuration file (JSON or YAML based on extension). | ||
| /// </summary> | ||
| /// <param name="builder">The configuration builder.</param> | ||
| /// <param name="path">The path to the configuration file.</param> | ||
| /// <param name="optional">Whether the file is optional.</param> | ||
| /// <param name="reloadOnChange">Whether to reload on change.</param> | ||
| /// <returns>The configuration builder.</returns> | ||
| public static IConfigurationBuilder AddConfigFile( | ||
| this IConfigurationBuilder builder, | ||
| string path, | ||
| bool optional = false, | ||
| bool reloadOnChange = false) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
| ArgumentException.ThrowIfNullOrEmpty(path); | ||
|
|
||
| if (ProxyYaml.IsYamlFile(path)) | ||
| { | ||
| return builder.AddYamlFile(path, optional, reloadOnChange); | ||
| } | ||
|
|
||
| return builder.AddJsonFile(path, optional, reloadOnChange); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.