-
Notifications
You must be signed in to change notification settings - Fork 765
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
[SDK] Remove the dependency on M.E.Options 5.0 #4093
Merged
cijothomas
merged 5 commits into
open-telemetry:main
from
CodeBlanch:options-dependency
Jan 25, 2023
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
103 changes: 103 additions & 0 deletions
103
src/OpenTelemetry/Internal/Options/DelegatingOptionsFactory.cs
This file contains 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,103 @@ | ||||||||||||
// <auto-generated /> | ||||||||||||
// Licensed to the .NET Foundation under one or more agreements. | ||||||||||||
// The .NET Foundation licenses this file to you under the MIT license. | ||||||||||||
|
||||||||||||
#nullable enable | ||||||||||||
|
||||||||||||
using System; | ||||||||||||
using System.Collections.Generic; | ||||||||||||
using System.Diagnostics; | ||||||||||||
using System.Diagnostics.CodeAnalysis; | ||||||||||||
using Microsoft.Extensions.Configuration; | ||||||||||||
|
||||||||||||
namespace Microsoft.Extensions.Options | ||||||||||||
{ | ||||||||||||
/// <summary> | ||||||||||||
/// Implementation of <see cref="IOptionsFactory{TOptions}"/>. | ||||||||||||
/// </summary> | ||||||||||||
/// <typeparam name="TOptions">The type of options being requested.</typeparam> | ||||||||||||
internal sealed class DelegatingOptionsFactory<TOptions> : | ||||||||||||
IOptionsFactory<TOptions> | ||||||||||||
where TOptions : class, new() | ||||||||||||
{ | ||||||||||||
private readonly Func<IConfiguration, string, TOptions> optionsFactoryFunc; | ||||||||||||
private readonly IConfiguration configuration; | ||||||||||||
private readonly IConfigureOptions<TOptions>[] _setups; | ||||||||||||
private readonly IPostConfigureOptions<TOptions>[] _postConfigures; | ||||||||||||
private readonly IValidateOptions<TOptions>[] _validations; | ||||||||||||
|
||||||||||||
/// <summary> | ||||||||||||
/// Initializes a new instance with the specified options configurations. | ||||||||||||
/// </summary> | ||||||||||||
/// <param name="setups">The configuration actions to run.</param> | ||||||||||||
/// <param name="postConfigures">The initialization actions to run.</param> | ||||||||||||
/// <param name="validations">The validations to run.</param> | ||||||||||||
public DelegatingOptionsFactory( | ||||||||||||
Func<IConfiguration, string, TOptions> optionsFactoryFunc, | ||||||||||||
IConfiguration configuration, | ||||||||||||
IEnumerable<IConfigureOptions<TOptions>> setups, | ||||||||||||
IEnumerable<IPostConfigureOptions<TOptions>> postConfigures, | ||||||||||||
IEnumerable<IValidateOptions<TOptions>> validations) | ||||||||||||
{ | ||||||||||||
// The default DI container uses arrays under the covers. Take advantage of this knowledge | ||||||||||||
// by checking for an array and enumerate over that, so we don't need to allocate an enumerator. | ||||||||||||
// When it isn't already an array, convert it to one, but don't use System.Linq to avoid pulling Linq in to | ||||||||||||
// small trimmed applications. | ||||||||||||
|
||||||||||||
Debug.Assert(optionsFactoryFunc != null, "optionsFactoryFunc was null"); | ||||||||||||
Debug.Assert(configuration != null, "configuration was null"); | ||||||||||||
|
||||||||||||
this.optionsFactoryFunc = optionsFactoryFunc!; | ||||||||||||
this.configuration = configuration!; | ||||||||||||
_setups = setups as IConfigureOptions<TOptions>[] ?? new List<IConfigureOptions<TOptions>>(setups).ToArray(); | ||||||||||||
_postConfigures = postConfigures as IPostConfigureOptions<TOptions>[] ?? new List<IPostConfigureOptions<TOptions>>(postConfigures).ToArray(); | ||||||||||||
_validations = validations as IValidateOptions<TOptions>[] ?? new List<IValidateOptions<TOptions>>(validations).ToArray(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// <summary> | ||||||||||||
/// Returns a configured <typeparamref name="TOptions"/> instance with the given <paramref name="name"/>. | ||||||||||||
/// </summary> | ||||||||||||
/// <param name="name">The name of the <typeparamref name="TOptions"/> instance to create.</param> | ||||||||||||
/// <returns>The created <typeparamref name="TOptions"/> instance with the given <paramref name="name"/>.</returns> | ||||||||||||
/// <exception cref="OptionsValidationException">One or more <see cref="IValidateOptions{TOptions}"/> return failed <see cref="ValidateOptionsResult"/> when validating the <typeparamref name="TOptions"/> instance been created.</exception> | ||||||||||||
/// <exception cref="MissingMethodException">The <typeparamref name="TOptions"/> does not have a public parameterless constructor or <typeparamref name="TOptions"/> is <see langword="abstract"/>.</exception> | ||||||||||||
public TOptions Create(string name) | ||||||||||||
{ | ||||||||||||
TOptions options = this.optionsFactoryFunc(this.configuration, name); | ||||||||||||
foreach (IConfigureOptions<TOptions> setup in _setups) | ||||||||||||
{ | ||||||||||||
if (setup is IConfigureNamedOptions<TOptions> namedSetup) | ||||||||||||
{ | ||||||||||||
namedSetup.Configure(name, options); | ||||||||||||
} | ||||||||||||
else if (name == Options.DefaultName) | ||||||||||||
{ | ||||||||||||
setup.Configure(options); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
foreach (IPostConfigureOptions<TOptions> post in _postConfigures) | ||||||||||||
Comment on lines
+88
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||||||||
{ | ||||||||||||
post.PostConfigure(name, options); | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (_validations.Length > 0) | ||||||||||||
{ | ||||||||||||
var failures = new List<string>(); | ||||||||||||
foreach (IValidateOptions<TOptions> validate in _validations) | ||||||||||||
{ | ||||||||||||
ValidateOptionsResult result = validate.Validate(name, options); | ||||||||||||
if (result is not null && result.Failed) | ||||||||||||
{ | ||||||||||||
failures.AddRange(result.Failures); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
if (failures.Count > 0) | ||||||||||||
{ | ||||||||||||
throw new OptionsValidationException(name, typeof(TOptions), failures); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return options; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: The class is still
internal
this just satisfies thenew()
constraint which Options 3.1 requires of TOptions.