未验证 提交 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
{
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 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 bool Exists(this Microsoft.Extensions.Configuration.IConfigurationSection section) { throw null; }
public static string GetConnectionString(this Microsoft.Extensions.Configuration.IConfiguration configuration, string name) { 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, bool makePathsRelative) { 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 Microsoft.Extensions.Configuration.IConfigurationSection GetRequiredSection(this Microsoft.Extensions.Configuration.IConfiguration configuration, string key) { throw null; }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Property)]
......@@ -26,8 +26,9 @@ public static partial class ConfigurationPath
public static readonly string KeyDelimiter;
public static string Combine(System.Collections.Generic.IEnumerable<string> pathSegments) { throw null; }
public static string Combine(params string[] pathSegments) { throw null; }
public static string GetParentPath(string path) { throw null; }
public static string GetSectionKey(string path) { throw null; }
public static string? GetParentPath(string? path) { throw null; }
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("path")]
public static string? GetSectionKey(string? path) { throw null; }
}
public static partial class ConfigurationRootExtensions
{
......@@ -35,7 +36,7 @@ public static partial class ConfigurationRootExtensions
}
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();
Microsoft.Extensions.Primitives.IChangeToken GetReloadToken();
Microsoft.Extensions.Configuration.IConfigurationSection GetSection(string key);
......@@ -49,11 +50,11 @@ public partial interface IConfigurationBuilder
}
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();
void Load();
void Set(string key, string value);
bool TryGet(string key, out string value);
void Set(string key, string? value);
bool TryGet(string key, out string? value);
}
public partial interface IConfigurationRoot : Microsoft.Extensions.Configuration.IConfiguration
{
......@@ -64,7 +65,7 @@ public partial interface IConfigurationSection : Microsoft.Extensions.Configurat
{
string Key { get; }
string Path { get; }
string Value { get; set; }
string? Value { get; set; }
}
public partial interface IConfigurationSource
{
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="Microsoft.Extensions.Configuration.Abstractions.cs" />
......@@ -8,4 +9,8 @@
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Primitives\ref\Microsoft.Extensions.Primitives.csproj" />
</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>
......@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Microsoft.Extensions.Configuration
......@@ -18,7 +19,7 @@ public static class ConfigurationExtensions
/// <param name="builder">The builder to add to.</param>
/// <param name="configureSource">Configures the source secrets.</param>
/// <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();
configureSource?.Invoke(source);
......@@ -31,9 +32,9 @@ public static IConfigurationBuilder Add<TSource>(this IConfigurationBuilder buil
/// <param name="configuration">The configuration to enumerate.</param>
/// <param name="name">The connection string key.</param>
/// <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>
......@@ -41,7 +42,7 @@ public static string GetConnectionString(this IConfiguration configuration, stri
/// </summary>
/// <param name="configuration">The configuration to enumerate.</param>
/// <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>
/// Get the enumeration of key value pairs within the <see cref="IConfiguration" />
......@@ -49,19 +50,18 @@ public static string GetConnectionString(this IConfiguration configuration, stri
/// <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>
/// <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>();
stack.Push(configuration);
var rootSection = configuration as IConfigurationSection;
int prefixLength = (makePathsRelative && rootSection != null) ? rootSection.Path.Length + 1 : 0;
int prefixLength = (makePathsRelative && configuration is IConfigurationSection rootSection) ? rootSection.Path.Length + 1 : 0;
while (stack.Count > 0)
{
IConfiguration config = stack.Pop();
// 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))
{
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())
{
......@@ -75,7 +75,7 @@ public static string GetConnectionString(this IConfiguration configuration, stri
/// </summary>
/// <param name="section">The section to enumerate.</param>
/// <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)
{
......
......@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.Extensions.Configuration
{
......@@ -49,7 +50,8 @@ public static string Combine(IEnumerable<string> pathSegments)
/// </summary>
/// <param name="path">The path.</param>
/// <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))
{
......@@ -65,7 +67,7 @@ public static string GetSectionKey(string path)
/// </summary>
/// <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>
public static string GetParentPath(string path)
public static string? GetParentPath(string? path)
{
if (string.IsNullOrEmpty(path))
{
......
......@@ -25,7 +25,7 @@ public static string GetDebugView(this IConfigurationRoot root)
{
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)
{
......@@ -57,13 +57,13 @@ public static string GetDebugView(this IConfigurationRoot root)
return builder.ToString();
}
private static (string Value, IConfigurationProvider Provider) GetValueAndProvider(
private static (string? Value, IConfigurationProvider? Provider) GetValueAndProvider(
IConfigurationRoot root,
string key)
{
foreach (IConfigurationProvider provider in root.Providers.Reverse())
{
if (provider.TryGet(key, out string value))
if (provider.TryGet(key, out string? value))
{
return (value, provider);
}
......
......@@ -16,7 +16,7 @@ public interface IConfiguration
/// </summary>
/// <param name="key">The configuration key.</param>
/// <returns>The configuration value.</returns>
string this[string key] { get; set; }
string? this[string key] { get; set; }
/// <summary>
/// Gets a configuration sub-section with the specified key.
......
......@@ -17,14 +17,14 @@ public interface IConfigurationProvider
/// <param name="key">The key.</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>
bool TryGet(string key, out string value);
bool TryGet(string key, out string? value);
/// <summary>
/// Sets a configuration value for the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
void Set(string key, string value);
void Set(string key, string? value);
/// <summary>
/// Returns a change token if this provider supports change tracking, null otherwise.
......@@ -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="parentPath">The parent path.</param>
/// <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
/// <summary>
/// Gets or sets the section value.
/// </summary>
string Value { get; set; }
string? Value { get; set; }
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
<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.
Commonly Used Types:
......
......@@ -11,7 +11,7 @@ internal class FakeConfigurationProvider : MemoryConfigurationProvider, IConfigu
public FakeConfigurationProvider(MemoryConfigurationSource source)
: base(source) { }
public new void Set(string key, string value)
public new void Set(string key, string? value)
{
base.Set(key, value);
OnReload();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册