未验证 提交 1f555b25 编写于 作者: M Maksym Koshovyi 提交者: GitHub

[Group 2] Enable nullable annotations for...

[Group 2] Enable nullable annotations for `Microsoft.Extensions.Configuration.Abstractions` (#57401)

* Annotate src

* Annotate ref

* Fix FakeConfigurationProvider

* Don't use using in ref

* DisableImplicitAssemblyReferences

* Update ConfigurationExtensions.cs

* TryGet can return null

* Remove not used namespace
上级 1c30027a
...@@ -8,11 +8,11 @@ namespace Microsoft.Extensions.Configuration ...@@ -8,11 +8,11 @@ namespace Microsoft.Extensions.Configuration
{ {
public static partial class ConfigurationExtensions public static partial class ConfigurationExtensions
{ {
public static Microsoft.Extensions.Configuration.IConfigurationBuilder Add<TSource>(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<TSource> configureSource) where TSource : Microsoft.Extensions.Configuration.IConfigurationSource, new() { throw null; } public static Microsoft.Extensions.Configuration.IConfigurationBuilder Add<TSource>(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<TSource>? configureSource) where TSource : Microsoft.Extensions.Configuration.IConfigurationSource, new() { throw null; }
public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> AsEnumerable(this Microsoft.Extensions.Configuration.IConfiguration configuration) { throw null; } public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>> AsEnumerable(this Microsoft.Extensions.Configuration.IConfiguration configuration) { throw null; }
public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> AsEnumerable(this Microsoft.Extensions.Configuration.IConfiguration configuration, bool makePathsRelative) { throw null; } public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>> AsEnumerable(this Microsoft.Extensions.Configuration.IConfiguration configuration, bool makePathsRelative) { throw null; }
public static bool Exists(this Microsoft.Extensions.Configuration.IConfigurationSection section) { throw null; } public static bool Exists([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] this Microsoft.Extensions.Configuration.IConfigurationSection? section) { throw null; }
public static string GetConnectionString(this Microsoft.Extensions.Configuration.IConfiguration configuration, string name) { throw null; } public static string? GetConnectionString(this Microsoft.Extensions.Configuration.IConfiguration configuration, string name) { throw null; }
public static Microsoft.Extensions.Configuration.IConfigurationSection GetRequiredSection(this Microsoft.Extensions.Configuration.IConfiguration configuration, string key) { throw null; } public static Microsoft.Extensions.Configuration.IConfigurationSection GetRequiredSection(this Microsoft.Extensions.Configuration.IConfiguration configuration, string key) { throw null; }
} }
[System.AttributeUsageAttribute(System.AttributeTargets.Property)] [System.AttributeUsageAttribute(System.AttributeTargets.Property)]
...@@ -26,8 +26,9 @@ public static partial class ConfigurationPath ...@@ -26,8 +26,9 @@ public static partial class ConfigurationPath
public static readonly string KeyDelimiter; public static readonly string KeyDelimiter;
public static string Combine(System.Collections.Generic.IEnumerable<string> pathSegments) { throw null; } public static string Combine(System.Collections.Generic.IEnumerable<string> pathSegments) { throw null; }
public static string Combine(params string[] pathSegments) { throw null; } public static string Combine(params string[] pathSegments) { throw null; }
public static string GetParentPath(string path) { throw null; } public static string? GetParentPath(string? path) { throw null; }
public static string GetSectionKey(string path) { throw null; } [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("path")]
public static string? GetSectionKey(string? path) { throw null; }
} }
public static partial class ConfigurationRootExtensions public static partial class ConfigurationRootExtensions
{ {
...@@ -35,7 +36,7 @@ public static partial class ConfigurationRootExtensions ...@@ -35,7 +36,7 @@ public static partial class ConfigurationRootExtensions
} }
public partial interface IConfiguration public partial interface IConfiguration
{ {
string this[string key] { get; set; } string? this[string key] { get; set; }
System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationSection> GetChildren(); System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationSection> GetChildren();
Microsoft.Extensions.Primitives.IChangeToken GetReloadToken(); Microsoft.Extensions.Primitives.IChangeToken GetReloadToken();
Microsoft.Extensions.Configuration.IConfigurationSection GetSection(string key); Microsoft.Extensions.Configuration.IConfigurationSection GetSection(string key);
...@@ -49,11 +50,11 @@ public partial interface IConfigurationBuilder ...@@ -49,11 +50,11 @@ public partial interface IConfigurationBuilder
} }
public partial interface IConfigurationProvider public partial interface IConfigurationProvider
{ {
System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string parentPath); System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string? parentPath);
Microsoft.Extensions.Primitives.IChangeToken GetReloadToken(); Microsoft.Extensions.Primitives.IChangeToken GetReloadToken();
void Load(); void Load();
void Set(string key, string value); void Set(string key, string? value);
bool TryGet(string key, out string value); bool TryGet(string key, out string? value);
} }
public partial interface IConfigurationRoot : Microsoft.Extensions.Configuration.IConfiguration public partial interface IConfigurationRoot : Microsoft.Extensions.Configuration.IConfiguration
{ {
...@@ -64,7 +65,7 @@ public partial interface IConfigurationSection : Microsoft.Extensions.Configurat ...@@ -64,7 +65,7 @@ public partial interface IConfigurationSection : Microsoft.Extensions.Configurat
{ {
string Key { get; } string Key { get; }
string Path { get; } string Path { get; }
string Value { get; set; } string? Value { get; set; }
} }
public partial interface IConfigurationSource public partial interface IConfigurationSource
{ {
......
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks> <TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Microsoft.Extensions.Configuration.Abstractions.cs" /> <Compile Include="Microsoft.Extensions.Configuration.Abstractions.cs" />
...@@ -8,4 +9,8 @@ ...@@ -8,4 +9,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Primitives\ref\Microsoft.Extensions.Primitives.csproj" /> <ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Primitives\ref\Microsoft.Extensions.Primitives.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections\ref\System.Collections.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
</ItemGroup>
</Project> </Project>
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
namespace Microsoft.Extensions.Configuration namespace Microsoft.Extensions.Configuration
...@@ -18,7 +19,7 @@ public static class ConfigurationExtensions ...@@ -18,7 +19,7 @@ public static class ConfigurationExtensions
/// <param name="builder">The builder to add to.</param> /// <param name="builder">The builder to add to.</param>
/// <param name="configureSource">Configures the source secrets.</param> /// <param name="configureSource">Configures the source secrets.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns> /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder Add<TSource>(this IConfigurationBuilder builder, Action<TSource> configureSource) where TSource : IConfigurationSource, new() public static IConfigurationBuilder Add<TSource>(this IConfigurationBuilder builder, Action<TSource>? configureSource) where TSource : IConfigurationSource, new()
{ {
var source = new TSource(); var source = new TSource();
configureSource?.Invoke(source); configureSource?.Invoke(source);
...@@ -31,9 +32,9 @@ public static IConfigurationBuilder Add<TSource>(this IConfigurationBuilder buil ...@@ -31,9 +32,9 @@ public static IConfigurationBuilder Add<TSource>(this IConfigurationBuilder buil
/// <param name="configuration">The configuration to enumerate.</param> /// <param name="configuration">The configuration to enumerate.</param>
/// <param name="name">The connection string key.</param> /// <param name="name">The connection string key.</param>
/// <returns>The connection string.</returns> /// <returns>The connection string.</returns>
public static string GetConnectionString(this IConfiguration configuration, string name) public static string? GetConnectionString(this IConfiguration configuration, string name)
{ {
return configuration?.GetSection("ConnectionStrings")?[name]; return configuration?.GetSection("ConnectionStrings")[name];
} }
/// <summary> /// <summary>
...@@ -41,7 +42,7 @@ public static string GetConnectionString(this IConfiguration configuration, stri ...@@ -41,7 +42,7 @@ public static string GetConnectionString(this IConfiguration configuration, stri
/// </summary> /// </summary>
/// <param name="configuration">The configuration to enumerate.</param> /// <param name="configuration">The configuration to enumerate.</param>
/// <returns>An enumeration of key value pairs.</returns> /// <returns>An enumeration of key value pairs.</returns>
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration) => configuration.AsEnumerable(makePathsRelative: false); public static IEnumerable<KeyValuePair<string, string?>> AsEnumerable(this IConfiguration configuration) => configuration.AsEnumerable(makePathsRelative: false);
/// <summary> /// <summary>
/// Get the enumeration of key value pairs within the <see cref="IConfiguration" /> /// Get the enumeration of key value pairs within the <see cref="IConfiguration" />
...@@ -49,19 +50,18 @@ public static string GetConnectionString(this IConfiguration configuration, stri ...@@ -49,19 +50,18 @@ public static string GetConnectionString(this IConfiguration configuration, stri
/// <param name="configuration">The configuration to enumerate.</param> /// <param name="configuration">The configuration to enumerate.</param>
/// <param name="makePathsRelative">If true, the child keys returned will have the current configuration's Path trimmed from the front.</param> /// <param name="makePathsRelative">If true, the child keys returned will have the current configuration's Path trimmed from the front.</param>
/// <returns>An enumeration of key value pairs.</returns> /// <returns>An enumeration of key value pairs.</returns>
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration, bool makePathsRelative) public static IEnumerable<KeyValuePair<string, string?>> AsEnumerable(this IConfiguration configuration, bool makePathsRelative)
{ {
var stack = new Stack<IConfiguration>(); var stack = new Stack<IConfiguration>();
stack.Push(configuration); stack.Push(configuration);
var rootSection = configuration as IConfigurationSection; int prefixLength = (makePathsRelative && configuration is IConfigurationSection rootSection) ? rootSection.Path.Length + 1 : 0;
int prefixLength = (makePathsRelative && rootSection != null) ? rootSection.Path.Length + 1 : 0;
while (stack.Count > 0) while (stack.Count > 0)
{ {
IConfiguration config = stack.Pop(); IConfiguration config = stack.Pop();
// Don't include the sections value if we are removing paths, since it will be an empty key // Don't include the sections value if we are removing paths, since it will be an empty key
if (config is IConfigurationSection section && (!makePathsRelative || config != configuration)) if (config is IConfigurationSection section && (!makePathsRelative || config != configuration))
{ {
yield return new KeyValuePair<string, string>(section.Path.Substring(prefixLength), section.Value); yield return new KeyValuePair<string, string?>(section.Path.Substring(prefixLength), section.Value);
} }
foreach (IConfigurationSection child in config.GetChildren()) foreach (IConfigurationSection child in config.GetChildren())
{ {
...@@ -75,7 +75,7 @@ public static string GetConnectionString(this IConfiguration configuration, stri ...@@ -75,7 +75,7 @@ public static string GetConnectionString(this IConfiguration configuration, stri
/// </summary> /// </summary>
/// <param name="section">The section to enumerate.</param> /// <param name="section">The section to enumerate.</param>
/// <returns><see langword="true" /> if the section has values or children; otherwise, <see langword="false" />.</returns> /// <returns><see langword="true" /> if the section has values or children; otherwise, <see langword="false" />.</returns>
public static bool Exists(this IConfigurationSection section) public static bool Exists([NotNullWhen(true)] this IConfigurationSection? section)
{ {
if (section == null) if (section == null)
{ {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.Extensions.Configuration namespace Microsoft.Extensions.Configuration
{ {
...@@ -49,7 +50,8 @@ public static string Combine(IEnumerable<string> pathSegments) ...@@ -49,7 +50,8 @@ public static string Combine(IEnumerable<string> pathSegments)
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
/// <returns>The last path segment of the path.</returns> /// <returns>The last path segment of the path.</returns>
public static string GetSectionKey(string path) [return: NotNullIfNotNull("path")]
public static string? GetSectionKey(string? path)
{ {
if (string.IsNullOrEmpty(path)) if (string.IsNullOrEmpty(path))
{ {
...@@ -65,7 +67,7 @@ public static string GetSectionKey(string path) ...@@ -65,7 +67,7 @@ public static string GetSectionKey(string path)
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
/// <returns>The original path minus the last individual segment found in it. Null if the original path corresponds to a top level node.</returns> /// <returns>The original path minus the last individual segment found in it. Null if the original path corresponds to a top level node.</returns>
public static string GetParentPath(string path) public static string? GetParentPath(string? path)
{ {
if (string.IsNullOrEmpty(path)) if (string.IsNullOrEmpty(path))
{ {
......
...@@ -25,7 +25,7 @@ public static string GetDebugView(this IConfigurationRoot root) ...@@ -25,7 +25,7 @@ public static string GetDebugView(this IConfigurationRoot root)
{ {
foreach (IConfigurationSection child in children) foreach (IConfigurationSection child in children)
{ {
(string Value, IConfigurationProvider Provider) valueAndProvider = GetValueAndProvider(root, child.Path); (string? Value, IConfigurationProvider? Provider) valueAndProvider = GetValueAndProvider(root, child.Path);
if (valueAndProvider.Provider != null) if (valueAndProvider.Provider != null)
{ {
...@@ -57,13 +57,13 @@ public static string GetDebugView(this IConfigurationRoot root) ...@@ -57,13 +57,13 @@ public static string GetDebugView(this IConfigurationRoot root)
return builder.ToString(); return builder.ToString();
} }
private static (string Value, IConfigurationProvider Provider) GetValueAndProvider( private static (string? Value, IConfigurationProvider? Provider) GetValueAndProvider(
IConfigurationRoot root, IConfigurationRoot root,
string key) string key)
{ {
foreach (IConfigurationProvider provider in root.Providers.Reverse()) foreach (IConfigurationProvider provider in root.Providers.Reverse())
{ {
if (provider.TryGet(key, out string value)) if (provider.TryGet(key, out string? value))
{ {
return (value, provider); return (value, provider);
} }
......
...@@ -16,7 +16,7 @@ public interface IConfiguration ...@@ -16,7 +16,7 @@ public interface IConfiguration
/// </summary> /// </summary>
/// <param name="key">The configuration key.</param> /// <param name="key">The configuration key.</param>
/// <returns>The configuration value.</returns> /// <returns>The configuration value.</returns>
string this[string key] { get; set; } string? this[string key] { get; set; }
/// <summary> /// <summary>
/// Gets a configuration sub-section with the specified key. /// Gets a configuration sub-section with the specified key.
......
...@@ -17,14 +17,14 @@ public interface IConfigurationProvider ...@@ -17,14 +17,14 @@ public interface IConfigurationProvider
/// <param name="key">The key.</param> /// <param name="key">The key.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
/// <returns><c>True</c> if a value for the specified key was found, otherwise <c>false</c>.</returns> /// <returns><c>True</c> if a value for the specified key was found, otherwise <c>false</c>.</returns>
bool TryGet(string key, out string value); bool TryGet(string key, out string? value);
/// <summary> /// <summary>
/// Sets a configuration value for the specified key. /// Sets a configuration value for the specified key.
/// </summary> /// </summary>
/// <param name="key">The key.</param> /// <param name="key">The key.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
void Set(string key, string value); void Set(string key, string? value);
/// <summary> /// <summary>
/// Returns a change token if this provider supports change tracking, null otherwise. /// Returns a change token if this provider supports change tracking, null otherwise.
...@@ -45,6 +45,6 @@ public interface IConfigurationProvider ...@@ -45,6 +45,6 @@ public interface IConfigurationProvider
/// <param name="earlierKeys">The child keys returned by the preceding providers for the same parent path.</param> /// <param name="earlierKeys">The child keys returned by the preceding providers for the same parent path.</param>
/// <param name="parentPath">The parent path.</param> /// <param name="parentPath">The parent path.</param>
/// <returns>The child keys.</returns> /// <returns>The child keys.</returns>
IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string parentPath); IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string? parentPath);
} }
} }
...@@ -21,6 +21,6 @@ public interface IConfigurationSection : IConfiguration ...@@ -21,6 +21,6 @@ public interface IConfigurationSection : IConfiguration
/// <summary> /// <summary>
/// Gets or sets the section value. /// Gets or sets the section value.
/// </summary> /// </summary>
string Value { get; set; } string? Value { get; set; }
} }
} }
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks> <TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
<EnableDefaultItems>true</EnableDefaultItems> <EnableDefaultItems>true</EnableDefaultItems>
<!-- Use targeting pack references instead of granular ones in the project file. -->
<DisableImplicitAssemblyReferences>false</DisableImplicitAssemblyReferences>
<PackageDescription>Abstractions of key-value pair based configuration. <PackageDescription>Abstractions of key-value pair based configuration.
Commonly Used Types: Commonly Used Types:
......
...@@ -11,7 +11,7 @@ internal class FakeConfigurationProvider : MemoryConfigurationProvider, IConfigu ...@@ -11,7 +11,7 @@ internal class FakeConfigurationProvider : MemoryConfigurationProvider, IConfigu
public FakeConfigurationProvider(MemoryConfigurationSource source) public FakeConfigurationProvider(MemoryConfigurationSource source)
: base(source) { } : base(source) { }
public new void Set(string key, string value) public new void Set(string key, string? value)
{ {
base.Set(key, value); base.Set(key, value);
OnReload(); OnReload();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册