Skip to content

Commit a3edcd1

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Add nullable attributes (#6526)
* Add not null when attributes to methods * Add nullable attributes polyfill * Add nullable attribute to product registration
1 parent c27120f commit a3edcd1

File tree

3 files changed

+148
-4
lines changed

3 files changed

+148
-4
lines changed

src/Elastic.Clients.Elasticsearch/Common/ElasticsearchClientProductRegistration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Diagnostics.CodeAnalysis;
67
using Elastic.Transport;
78
using Elastic.Transport.Products.Elasticsearch;
89

@@ -28,7 +29,7 @@ public override bool HttpStatusCodeClassifier(HttpMethod method, int statusCode)
2829
/// Makes the low level transport aware of Elastic.Clients.Elasticsearch's <see cref="ElasticsearchResponseBase" />
2930
/// so that it can peek in to its exposed error when reporting failures.
3031
/// </summary>
31-
public override bool TryGetServerErrorReason<TResponse>(TResponse response, out string? reason)
32+
public override bool TryGetServerErrorReason<TResponse>(TResponse response, [NotNullWhen(returnValue: true)] out string? reason)
3233
{
3334
if (response is not ElasticsearchResponseBase r)
3435
return base.TryGetServerErrorReason(response, out reason);
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
// Copyright (c) Microsoft Corporation. All rights reserved.
6+
// Licensed under the MIT License.
7+
8+
#define INTERNAL_NULLABLE_ATTRIBUTES
9+
10+
#pragma warning disable SA1402 // File may only contain a single type
11+
#pragma warning disable SA1649 // File name should match first type name
12+
13+
#if !NETCOREAPP3_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER || NET461
14+
namespace System.Diagnostics.CodeAnalysis
15+
{
16+
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
17+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
18+
#if INTERNAL_NULLABLE_ATTRIBUTES
19+
internal
20+
#else
21+
public
22+
#endif
23+
sealed class AllowNullAttribute : Attribute
24+
{ }
25+
26+
/// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
27+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
28+
#if INTERNAL_NULLABLE_ATTRIBUTES
29+
internal
30+
#else
31+
public
32+
#endif
33+
sealed class DisallowNullAttribute : Attribute
34+
{ }
35+
36+
/// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
37+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
38+
#if INTERNAL_NULLABLE_ATTRIBUTES
39+
internal
40+
#else
41+
public
42+
#endif
43+
sealed class MaybeNullAttribute : Attribute
44+
{ }
45+
46+
/// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary>
47+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
48+
#if INTERNAL_NULLABLE_ATTRIBUTES
49+
internal
50+
#else
51+
public
52+
#endif
53+
sealed class NotNullAttribute : Attribute
54+
{ }
55+
56+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
57+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
58+
#if INTERNAL_NULLABLE_ATTRIBUTES
59+
internal
60+
#else
61+
public
62+
#endif
63+
sealed class MaybeNullWhenAttribute : Attribute
64+
{
65+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
66+
/// <param name="returnValue">
67+
/// The return value condition. If the method returns this value, the associated parameter may be null.
68+
/// </param>
69+
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
70+
71+
/// <summary>Gets the return value condition.</summary>
72+
public bool ReturnValue { get; }
73+
}
74+
75+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
76+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
77+
#if INTERNAL_NULLABLE_ATTRIBUTES
78+
internal
79+
#else
80+
public
81+
#endif
82+
sealed class NotNullWhenAttribute : Attribute
83+
{
84+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
85+
/// <param name="returnValue">
86+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
87+
/// </param>
88+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
89+
90+
/// <summary>Gets the return value condition.</summary>
91+
public bool ReturnValue { get; }
92+
}
93+
94+
/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
95+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
96+
#if INTERNAL_NULLABLE_ATTRIBUTES
97+
internal
98+
#else
99+
public
100+
#endif
101+
sealed class NotNullIfNotNullAttribute : Attribute
102+
{
103+
/// <summary>Initializes the attribute with the associated parameter name.</summary>
104+
/// <param name="parameterName">
105+
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
106+
/// </param>
107+
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
108+
109+
/// <summary>Gets the associated parameter name.</summary>
110+
public string ParameterName { get; }
111+
}
112+
113+
/// <summary>Applied to a method that will never return under any circumstance.</summary>
114+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
115+
#if INTERNAL_NULLABLE_ATTRIBUTES
116+
internal
117+
#else
118+
public
119+
#endif
120+
sealed class DoesNotReturnAttribute : Attribute
121+
{ }
122+
123+
/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
124+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
125+
#if INTERNAL_NULLABLE_ATTRIBUTES
126+
internal
127+
#else
128+
public
129+
#endif
130+
sealed class DoesNotReturnIfAttribute : Attribute
131+
{
132+
/// <summary>Initializes the attribute with the specified parameter value.</summary>
133+
/// <param name="parameterValue">
134+
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
135+
/// the associated parameter matches this value.
136+
/// </param>
137+
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
138+
139+
/// <summary>Gets the condition parameter value.</summary>
140+
public bool ParameterValue { get; }
141+
}
142+
}
143+
#endif

src/Elastic.Clients.Elasticsearch/Types/SourceConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Text.Json;
78
using System.Text.Json.Serialization;
89

@@ -15,8 +16,7 @@ public partial class SourceConfig
1516

1617
public bool HasSourceFilterValue => Tag == 1;
1718

18-
// TODO - Not null when
19-
public bool TryGetBool(out bool? value)
19+
public bool TryGetBool([NotNullWhen(returnValue: true)] out bool? value)
2020
{
2121
if (Tag == 0)
2222
{
@@ -28,7 +28,7 @@ public bool TryGetBool(out bool? value)
2828
return false;
2929
}
3030

31-
public bool TryGetSourceFilter(out SourceFilter? value)
31+
public bool TryGetSourceFilter([NotNullWhen(returnValue: true)] out SourceFilter? value)
3232
{
3333
if (Tag == 1)
3434
{

0 commit comments

Comments
 (0)