Skip to content

Commit 2233ffd

Browse files
authored
Enabling nullability in NuGet.Packaging - Annotate ContentModel, LicenseMetadata & some extraction types (#7195)
1 parent b761098 commit 2233ffd

24 files changed

Lines changed: 310 additions & 342 deletions

src/NuGet.Clients/NuGet.VisualStudio.Internal.Contracts/Formatters/LicenseMetadataFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ private LicenseMetadataFormatter()
6969
}
7070

7171
return new LicenseMetadata(type,
72-
license,
72+
license!,
7373
licenseExpression,
7474
warningsAndErrors,
75-
version);
75+
version!);
7676
}
7777

7878
protected override void SerializeCore(ref MessagePackWriter writer, LicenseMetadata value, MessagePackSerializerOptions options)

src/NuGet.Core/NuGet.Packaging/ContentModel/Asset.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
4+
using System;
55

66
namespace NuGet.ContentModel
77
{
88
public class Asset
99
{
10-
public string Path { get; set; }
11-
public string Link { get; set; }
10+
public required string Path { get; set; }
1211

13-
public override string ToString()
12+
[Obsolete("This is unused and will be removed in a future release.")]
13+
public string? Link { get; set; }
14+
15+
public override string? ToString()
1416
{
1517
return Path;
1618
}

src/NuGet.Core/NuGet.Packaging/ContentModel/ContentPropertyDefinition.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Linq;
@@ -15,45 +13,45 @@ namespace NuGet.ContentModel
1513
/// </summary>
1614
public class ContentPropertyDefinition
1715
{
18-
private static readonly Func<object, object, bool> EqualsTest = (left, right) => Equals(left, right);
16+
private static readonly Func<object?, object?, bool> EqualsTest = (left, right) => Equals(left, right);
1917

2018
internal ContentPropertyDefinition(
2119
string name,
22-
Func<ReadOnlyMemory<char>, PatternTable, bool, object> parser)
20+
Func<ReadOnlyMemory<char>, PatternTable?, bool, object?> parser)
2321
: this(name, parser, null, null, null, false)
2422
{
2523
}
2624

2725
internal ContentPropertyDefinition(
2826
string name,
29-
Func<ReadOnlyMemory<char>, PatternTable, bool, object> parser,
30-
Func<object, object, bool> compatibilityTest)
27+
Func<ReadOnlyMemory<char>, PatternTable?, bool, object?> parser,
28+
Func<object?, object?, bool>? compatibilityTest)
3129
: this(name, parser, compatibilityTest, null, null, false)
3230
{
3331
}
3432

3533
internal ContentPropertyDefinition(string name,
36-
Func<ReadOnlyMemory<char>, PatternTable, bool, object> parser,
37-
Func<object, object, bool> compatibilityTest,
38-
Func<object, object, object, int> compareTest)
34+
Func<ReadOnlyMemory<char>, PatternTable?, bool, object?> parser,
35+
Func<object?, object?, bool>? compatibilityTest,
36+
Func<object?, object?, object?, int>? compareTest)
3937
: this(name, parser, compatibilityTest, compareTest, null, false)
4038
{
4139
}
4240

4341
internal ContentPropertyDefinition(
4442
string name,
45-
Func<ReadOnlyMemory<char>, PatternTable, bool, object> parser,
43+
Func<ReadOnlyMemory<char>, PatternTable?, bool, object?> parser,
4644
IEnumerable<string> fileExtensions)
4745
: this(name, parser, null, null, fileExtensions, false)
4846
{
4947
}
5048

5149
internal ContentPropertyDefinition(
5250
string name,
53-
Func<ReadOnlyMemory<char>, PatternTable, bool, object> parser,
54-
Func<object, object, bool> compatibilityTest,
55-
Func<object, object, object, int> compareTest,
56-
IEnumerable<string> fileExtensions,
51+
Func<ReadOnlyMemory<char>, PatternTable?, bool, object?> parser,
52+
Func<object?, object?, bool>? compatibilityTest,
53+
Func<object?, object?, object?, int>? compareTest,
54+
IEnumerable<string>? fileExtensions,
5755
bool allowSubfolders)
5856
{
5957
Name = name;
@@ -66,7 +64,7 @@ internal ContentPropertyDefinition(
6664

6765
public string Name { get; }
6866

69-
public List<string> FileExtensions { get; }
67+
public List<string>? FileExtensions { get; }
7068

7169
public bool FileExtensionAllowSubFolders { get; }
7270

@@ -76,7 +74,7 @@ internal ContentPropertyDefinition(
7674
/// If the bool is true, the return object will be non-null, and match what the ReadOnlyMemory char represents.
7775
/// If the bool is false, the return object will be non-null if the ReadOnlyMemory char represents a valid value for this definition. This is a performance optimization.
7876
/// </summary>
79-
internal Func<ReadOnlyMemory<char>, PatternTable, bool, object> Parser { get; }
77+
internal Func<ReadOnlyMemory<char>, PatternTable?, bool, object?> Parser { get; }
8078

8179
/// <summary>
8280
/// Looks up a definition for the given substring.
@@ -90,7 +88,7 @@ internal ContentPropertyDefinition(
9088
/// <param name="matchOnly">Whether this is a grouping match, or we actually want to actualize the value of name as a string.</param>
9189
/// <param name="value">The out param. If matchonly, it will always be null. Otherwise, set to actualized value of name if the return is true, set to null if false.</param>
9290
/// <returns>True if the name matches the definition. False otherwise.</returns>
93-
internal virtual bool TryLookup(ReadOnlyMemory<char> name, PatternTable table, bool matchOnly, out object value)
91+
internal virtual bool TryLookup(ReadOnlyMemory<char> name, PatternTable? table, bool matchOnly, out object? value)
9492
{
9593
if (name.IsEmpty)
9694
{
@@ -148,19 +146,19 @@ private static bool ContainsSlash(ReadOnlyMemory<char> name)
148146
return containsSlash;
149147
}
150148

151-
public Func<object, object, bool> CompatibilityTest { get; }
149+
public Func<object?, object?, bool> CompatibilityTest { get; }
152150

153151
/// <summary>
154152
/// Find the nearest compatible candidate.
155153
/// </summary>
156-
public Func<object, object, object, int> CompareTest { get; }
154+
public Func<object?, object?, object?, int>? CompareTest { get; }
157155

158-
public virtual bool IsCriteriaSatisfied(object critieriaValue, object candidateValue)
156+
public virtual bool IsCriteriaSatisfied(object? critieriaValue, object? candidateValue)
159157
{
160158
return CompatibilityTest.Invoke(critieriaValue, candidateValue);
161159
}
162160

163-
public virtual int Compare(object criteriaValue, object candidateValue1, object candidateValue2)
161+
public virtual int Compare(object? criteriaValue, object? candidateValue1, object? candidateValue2)
164162
{
165163
var betterCoverageFromValue1 = IsCriteriaSatisfied(candidateValue1, candidateValue2);
166164
var betterCoverageFromValue2 = IsCriteriaSatisfied(candidateValue2, candidateValue1);

src/NuGet.Core/NuGet.Packaging/ContentModel/Infrastructure/Parser.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Diagnostics;
@@ -15,7 +13,7 @@ public class PatternExpression
1513
{
1614
private readonly List<Segment> _segments = new List<Segment>();
1715
private readonly Dictionary<string, object> _defaults;
18-
private readonly PatternTable _table;
16+
private readonly PatternTable? _table;
1917

2018
public PatternExpression(PatternDefinition pattern)
2119
{
@@ -66,9 +64,9 @@ private void Initialize(string pattern, bool preserveRawValues)
6664
}
6765
}
6866

69-
internal ContentItem Match(string path, IReadOnlyDictionary<string, ContentPropertyDefinition> propertyDefinitions)
67+
internal ContentItem? Match(string path, IReadOnlyDictionary<string, ContentPropertyDefinition> propertyDefinitions)
7068
{
71-
ContentItem item = null;
69+
ContentItem? item = null;
7270
var startIndex = 0;
7371
foreach (var segment in _segments)
7472
{
@@ -109,7 +107,7 @@ internal ContentItem Match(string path, IReadOnlyDictionary<string, ContentPrope
109107

110108
private abstract class Segment
111109
{
112-
internal abstract bool TryMatch(ref ContentItem item, string path, IReadOnlyDictionary<string, ContentPropertyDefinition> propertyDefinitions, int startIndex, out int endIndex);
110+
internal abstract bool TryMatch(ref ContentItem? item, string path, IReadOnlyDictionary<string, ContentPropertyDefinition> propertyDefinitions, int startIndex, out int endIndex);
113111
}
114112

115113
[DebuggerDisplay("{_pattern.Substring(_start, _length)}")]
@@ -127,7 +125,7 @@ public LiteralSegment(string pattern, int start, int length)
127125
}
128126

129127
internal override bool TryMatch(
130-
ref ContentItem item,
128+
ref ContentItem? item,
131129
string path,
132130
IReadOnlyDictionary<string, ContentPropertyDefinition> propertyDefinitions,
133131
int startIndex,
@@ -152,11 +150,11 @@ private class TokenSegment : Segment
152150
private readonly string _token;
153151
private readonly char _delimiter;
154152
private readonly bool _matchOnly;
155-
private readonly PatternTable _table;
153+
private readonly PatternTable? _table;
156154
private readonly bool _preserveRawValue = false;
157-
private readonly string _rawToken;
155+
private readonly string? _rawToken;
158156

159-
public TokenSegment(string token, char delimiter, bool matchOnly, PatternTable table, bool preserveRawValues)
157+
public TokenSegment(string token, char delimiter, bool matchOnly, PatternTable? table, bool preserveRawValues)
160158
{
161159
_token = token;
162160
_delimiter = delimiter;
@@ -170,13 +168,13 @@ public TokenSegment(string token, char delimiter, bool matchOnly, PatternTable t
170168
}
171169

172170
internal override bool TryMatch(
173-
ref ContentItem item,
171+
ref ContentItem? item,
174172
string path,
175173
IReadOnlyDictionary<string, ContentPropertyDefinition> propertyDefinitions,
176174
int startIndex,
177175
out int endIndex)
178176
{
179-
ContentPropertyDefinition propertyDefinition;
177+
ContentPropertyDefinition? propertyDefinition;
180178
if (!propertyDefinitions.TryGetValue(_token, out propertyDefinition))
181179
{
182180
throw new Exception(string.Format(CultureInfo.CurrentCulture, "Unable to find property definition for {{{0}}}", _token));
@@ -200,7 +198,7 @@ internal override bool TryMatch(
200198
break;
201199
}
202200
ReadOnlyMemory<char> substring = path.AsMemory(startIndex, delimiterIndex - startIndex);
203-
object value;
201+
object? value;
204202
if (propertyDefinition.TryLookup(substring, _table, _matchOnly, out value))
205203
{
206204
if (!_matchOnly)
@@ -215,9 +213,9 @@ internal override bool TryMatch(
215213
}
216214
if (_preserveRawValue)
217215
{
218-
item.Add(_rawToken, substring.ToString());
216+
item.Add(_rawToken!, substring.ToString());
219217
}
220-
item.Add(_token, value);
218+
item.Add(_token, value!);
221219
}
222220
endIndex = delimiterIndex;
223221
return true;

src/NuGet.Core/NuGet.Packaging/ContentModel/ManagedCodeConventions.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Collections.ObjectModel;
@@ -68,15 +66,15 @@ public class ManagedCodeConventions
6866

6967
private static readonly FrameworkReducer FrameworkReducer = new();
7068

71-
private RuntimeGraph _runtimeGraph;
69+
private RuntimeGraph? _runtimeGraph;
7270

7371
private Dictionary<ReadOnlyMemory<char>, NuGetFramework> _frameworkCache = new(ReadOnlyMemoryCharComparerOrdinal.Instance);
7472

7573
public ManagedCodeCriteria Criteria { get; }
7674
public IReadOnlyDictionary<string, ContentPropertyDefinition> Properties { get; }
7775
public ManagedCodePatterns Patterns { get; }
7876

79-
public ManagedCodeConventions(RuntimeGraph runtimeGraph)
77+
public ManagedCodeConventions(RuntimeGraph? runtimeGraph)
8078
{
8179
_runtimeGraph = runtimeGraph;
8280

@@ -105,7 +103,7 @@ public ManagedCodeConventions(RuntimeGraph runtimeGraph)
105103
Patterns = new ManagedCodePatterns(this);
106104
}
107105

108-
private bool RuntimeIdentifier_CompatibilityTest(object criteria, object available)
106+
private bool RuntimeIdentifier_CompatibilityTest(object? criteria, object? available)
109107
{
110108
if (_runtimeGraph == null)
111109
{
@@ -129,11 +127,11 @@ private bool RuntimeIdentifier_CompatibilityTest(object criteria, object availab
129127
/// If matchOnly is true, then an empty string may be returned as a performance optimization.
130128
/// If matchOnly is false, the parsed result will be returned.
131129
/// </summary>
132-
private static object CodeLanguage_Parser(ReadOnlyMemory<char> name, PatternTable table, bool matchOnly)
130+
private static object? CodeLanguage_Parser(ReadOnlyMemory<char> name, PatternTable? table, bool matchOnly)
133131
{
134132
if (table != null)
135133
{
136-
object val;
134+
object? val;
137135
if (table.TryLookup(PropertyNames.CodeLanguage, name, out val))
138136
{
139137
return val;
@@ -162,11 +160,11 @@ private static object CodeLanguage_Parser(ReadOnlyMemory<char> name, PatternTabl
162160
/// If matchOnly is true, then an empty string may be returned as a performance optimization.
163161
/// If matchOnly is false, the parsed result will be returned.
164162
/// </summary>
165-
internal static object Locale_Parser(ReadOnlyMemory<char> name, PatternTable table, bool matchOnly)
163+
internal static object? Locale_Parser(ReadOnlyMemory<char> name, PatternTable? table, bool matchOnly)
166164
{
167165
if (table != null)
168166
{
169-
object val;
167+
object? val;
170168
if (table.TryLookup(PropertyNames.Locale, name, out val))
171169
{
172170
return val;
@@ -213,15 +211,13 @@ internal static object Locale_Parser(ReadOnlyMemory<char> name, PatternTable tab
213211

214212
private object TargetFrameworkName_Parser(
215213
ReadOnlyMemory<char> name,
216-
PatternTable table,
214+
PatternTable? table,
217215
bool matchOnly)
218216
{
219-
object obj = null;
220-
221217
// Check for replacements
222218
if (table != null)
223219
{
224-
if (table.TryLookup(PropertyNames.TargetFrameworkMoniker, name, out obj))
220+
if (table.TryLookup(PropertyNames.TargetFrameworkMoniker, name, out var obj))
225221
{
226222
return obj;
227223
}
@@ -230,8 +226,7 @@ private object TargetFrameworkName_Parser(
230226
// Check the cache for an exact match
231227
if (!name.IsEmpty)
232228
{
233-
NuGetFramework cachedResult;
234-
if (!_frameworkCache.TryGetValue(name, out cachedResult))
229+
if (!_frameworkCache.TryGetValue(name, out NuGetFramework? cachedResult))
235230
{
236231
// Parse and add the framework to the cache
237232
cachedResult = TargetFrameworkName_ParserCore(name.ToString());
@@ -272,7 +267,7 @@ private static NuGetFramework TargetFrameworkName_ParserCore(string name)
272267
/// If matchOnly is true, then an empty string is returned as a performance optimization.
273268
/// If matchOnly is false, the string will be actualized.
274269
/// </summary>
275-
private static object IdentityParser(ReadOnlyMemory<char> s, PatternTable _, bool matchOnly)
270+
private static object IdentityParser(ReadOnlyMemory<char> s, PatternTable? _, bool matchOnly)
276271
{
277272
if (matchOnly)
278273
{
@@ -286,7 +281,7 @@ private static object IdentityParser(ReadOnlyMemory<char> s, PatternTable _, boo
286281
/// If matchOnly is true, then an empty string is returned as a performance optimization.
287282
/// If matchOnly is false, the parsed result will be returned.
288283
/// </summary>
289-
private static object AllowEmptyFolderParser(ReadOnlyMemory<char> s, PatternTable table, bool matchOnly)
284+
private static object? AllowEmptyFolderParser(ReadOnlyMemory<char> s, PatternTable? _, bool matchOnly)
290285
{
291286
// Accept "_._" as a pseudo-assembly
292287
if (MemoryExtensions.Equals(PackagingCoreConstants.EmptyFolder.AsSpan(), s.Span, StringComparison.Ordinal))
@@ -301,7 +296,7 @@ private static object AllowEmptyFolderParser(ReadOnlyMemory<char> s, PatternTabl
301296
return null;
302297
}
303298

304-
private static bool TargetFrameworkName_CompatibilityTest(object criteria, object available)
299+
private static bool TargetFrameworkName_CompatibilityTest(object? criteria, object? available)
305300
{
306301
var criteriaFrameworkName = criteria as NuGetFramework;
307302
var availableFrameworkName = available as NuGetFramework;
@@ -334,7 +329,7 @@ private static bool TargetFrameworkName_CompatibilityTest(object criteria, objec
334329
return false;
335330
}
336331

337-
private static int TargetFrameworkName_NearestCompareTest(object projectFramework, object criteria, object available)
332+
private static int TargetFrameworkName_NearestCompareTest(object? projectFramework, object? criteria, object? available)
338333
{
339334
var projectFrameworkName = projectFramework as NuGetFramework;
340335
var criteriaFrameworkName = criteria as NuGetFramework;
@@ -376,7 +371,7 @@ internal ManagedCodeCriteria(ManagedCodeConventions conventions)
376371
_conventions = conventions;
377372
}
378373

379-
public SelectionCriteria ForFrameworkAndRuntime(NuGetFramework framework, string runtimeIdentifier)
374+
public SelectionCriteria ForFrameworkAndRuntime(NuGetFramework framework, string? runtimeIdentifier)
380375
{
381376
if (framework is FallbackFramework)
382377
{

0 commit comments

Comments
 (0)