Skip to content

Commit 4f213e3

Browse files
committed
Remove nullability for older targets. Fixes #22
1 parent 8506f9d commit 4f213e3

File tree

8 files changed

+36
-71
lines changed

8 files changed

+36
-71
lines changed

src/MimeTypes/MimeTypeFunctions.ttinclude

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@
8383
.OrderBy(x => x.Extension, StringComparer.InvariantCulture)
8484
.ToList();
8585

86-
public void GenerateClass(bool includeAttributes)
86+
public void GenerateClass(bool nullability)
8787
{
8888
#>
8989
// <auto-generated />
9090

91+
<#+ if (nullability) { #>
9192
#nullable enable
93+
<#+ } #>
9294
#pragma warning disable
9395

9496
namespace $rootnamespace$
@@ -97,7 +99,9 @@ namespace $rootnamespace$
9799
using global::System.Linq;
98100
using global::System.Collections.Generic;
99101
using global::System.Diagnostics;
102+
<#+ if (nullability) { #>
100103
using global::System.Diagnostics.CodeAnalysis;
104+
<#+ } #>
101105
using global::System.Runtime.CompilerServices;
102106

103107
/// <summary>
@@ -113,7 +117,9 @@ namespace $rootnamespace$
113117
/// <summary>
114118
/// The fallback MIME-type. Defaults to <c>application/octet-stream</c>.
115119
/// </summary>
120+
<#+ if (nullability) { #>
116121
[AllowNull]
122+
<#+ } #>
117123
public static string FallbackMimeType
118124
{
119125
get => s_fallbackMimeType;
@@ -161,7 +167,11 @@ namespace $rootnamespace$
161167
/// <param name="fileName">The name of the file.</param>
162168
/// <param name="mimeType">The MIME-type for the given file name.</param>
163169
/// <returns><c>true</c> if a MIME-type was found, <c>false</c> otherwise.</returns>
170+
<#+ if (nullability) { #>
164171
public static bool TryGetMimeType(string? fileName, [NotNullWhen(true)] out string? mimeType)
172+
<#+ } else { #>
173+
public static bool TryGetMimeType(string fileName, out string mimeType)
174+
<#+ } #>
165175
{
166176
if (fileName is null)
167177
{
@@ -197,29 +207,6 @@ namespace $rootnamespace$
197207
}
198208
}
199209
}
200-
<#+ if (includeAttributes) { #>
201-
202-
namespace System.Diagnostics.CodeAnalysis
203-
{
204-
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
205-
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property)]
206-
internal sealed class AllowNullAttribute : Attribute { }
207-
208-
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
209-
[AttributeUsage(AttributeTargets.Parameter)]
210-
internal sealed class NotNullWhenAttribute : Attribute
211-
{
212-
/// <summary>Initializes the attribute with the specified return value condition.</summary>
213-
/// <param name="returnValue">
214-
/// The return value condition. If the method returns this value, the associated parameter will not be null.
215-
/// </param>
216-
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
217-
218-
/// <summary>Gets the return value condition.</summary>
219-
public bool ReturnValue { get; }
220-
}
221-
}
222-
<#+ } #>
223210

224211
#pragma warning enable
225212
<#+

src/MimeTypes/MimeTypes.NullableAttributes.cs.pp renamed to src/MimeTypes/MimeTypes.Nullable.cs.pp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,25 +1283,4 @@
12831283
}
12841284
}
12851285

1286-
namespace System.Diagnostics.CodeAnalysis
1287-
{
1288-
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
1289-
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property)]
1290-
internal sealed class AllowNullAttribute : Attribute { }
1291-
1292-
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
1293-
[AttributeUsage(AttributeTargets.Parameter)]
1294-
internal sealed class NotNullWhenAttribute : Attribute
1295-
{
1296-
/// <summary>Initializes the attribute with the specified return value condition.</summary>
1297-
/// <param name="returnValue">
1298-
/// The return value condition. If the method returns this value, the associated parameter will not be null.
1299-
/// </param>
1300-
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
1301-
1302-
/// <summary>Gets the return value condition.</summary>
1303-
public bool ReturnValue { get; }
1304-
}
1305-
}
1306-
13071286
#pragma warning enable
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<#@ template debug="false" hostspecific="false" language="C#" #>
2+
<#@ include file="MimeTypeFunctions.ttinclude" #>
3+
<#@ output extension=".cs.pp" encoding="utf-8" #>
4+
<# GenerateClass(nullability: true); #>

src/MimeTypes/MimeTypes.NullableAttributes.tt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/MimeTypes/MimeTypes.cs.pp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// <auto-generated />
22

3-
#nullable enable
43
#pragma warning disable
54

65
namespace $rootnamespace$
@@ -9,7 +8,6 @@
98
using global::System.Linq;
109
using global::System.Collections.Generic;
1110
using global::System.Diagnostics;
12-
using global::System.Diagnostics.CodeAnalysis;
1311
using global::System.Runtime.CompilerServices;
1412

1513
/// <summary>
@@ -25,7 +23,6 @@
2523
/// <summary>
2624
/// The fallback MIME-type. Defaults to <c>application/octet-stream</c>.
2725
/// </summary>
28-
[AllowNull]
2926
public static string FallbackMimeType
3027
{
3128
get => s_fallbackMimeType;
@@ -1246,7 +1243,7 @@
12461243
/// <param name="fileName">The name of the file.</param>
12471244
/// <param name="mimeType">The MIME-type for the given file name.</param>
12481245
/// <returns><c>true</c> if a MIME-type was found, <c>false</c> otherwise.</returns>
1249-
public static bool TryGetMimeType(string? fileName, [NotNullWhen(true)] out string? mimeType)
1246+
public static bool TryGetMimeType(string fileName, out string mimeType)
12501247
{
12511248
if (fileName is null)
12521249
{

src/MimeTypes/MimeTypes.csproj

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netstandard2.0</TargetFrameworks>
4-
<LangVersion>8.0</LangVersion>
54
</PropertyGroup>
65

76
<ItemGroup>
87
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
98
</ItemGroup>
109

1110
<ItemGroup>
12-
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" PrivateAssets="All" GeneratePathProperty="True" />
11+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" PrivateAssets="All" GeneratePathProperty="True" />
1312
</ItemGroup>
1413

1514
<ItemGroup>
15+
<None Update="MimeTypeFunctions.ttinclude" />
16+
1617
<None Update="MimeTypes.tt">
17-
<Generator>TextTemplatingFileGenerator</Generator>
1818
<LastGenOutput>MimeTypes.cs.pp</LastGenOutput>
19-
</None>
20-
<None Update="MimeTypeFunctions.ttinclude" />
21-
<None Update="MimeTypes.NullableAttributes.tt">
2219
<Generator>TextTemplatingFileGenerator</Generator>
23-
<LastGenOutput>MimeTypes.NullableAttributes.cs.pp</LastGenOutput>
24-
</None>
25-
<None Update="MimeTypes.NullableAttributes.cs.pp">
26-
<AutoGen>True</AutoGen>
27-
<DesignTime>True</DesignTime>
28-
<DependentUpon>MimeTypes.NullableAttributes.tt</DependentUpon>
2920
</None>
3021
<None Update="MimeTypes.cs.pp">
31-
<AutoGen>True</AutoGen>
3222
<DesignTime>True</DesignTime>
23+
<AutoGen>True</AutoGen>
3324
<DependentUpon>MimeTypes.tt</DependentUpon>
3425
</None>
26+
27+
<None Update="MimeTypes.Nullable.tt">
28+
<Generator>TextTemplatingFileGenerator</Generator>
29+
<LastGenOutput>MimeTypes.Nullable.cs.pp</LastGenOutput>
30+
</None>
31+
<None Update="MimeTypes.Nullable.cs.pp">
32+
<DesignTime>True</DesignTime>
33+
<AutoGen>True</AutoGen>
34+
<DependentUpon>MimeTypes.Nullable.tt</DependentUpon>
35+
</None>
3536
</ItemGroup>
3637
</Project>

src/MimeTypes/MimeTypes.nuspec

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
</contentFiles>
2121
</metadata>
2222
<files>
23-
<file src="MimeTypes.NullableAttributes.cs.pp" target="contentFiles\cs\netstandard2.0" />
24-
<file src="MimeTypes.NullableAttributes.cs.pp" target="contentFiles\cs\net472" />
25-
<file src="MimeTypes.cs.pp" target="contentFiles\cs\netstandard2.1" />
26-
<file src="MimeTypes.cs.pp" target="contentFiles\cs\netcoreapp3.0" />
23+
<file src="MimeTypes.cs.pp" target="contentFiles\cs\netstandard2.0" />
24+
<file src="MimeTypes.cs.pp" target="contentFiles\cs\net472" />
25+
<file src="MimeTypes.Nullable.cs.pp" target="contentFiles\cs\netstandard2.1" />
26+
<file src="MimeTypes.Nullable.cs.pp" target="contentFiles\cs\netcoreapp3.0" />
27+
<file src="MimeTypes.Nullable.cs.pp" target="contentFiles\cs\net5.0" />
2728
<file src="..\..\etc\nuget-icon.png" target="images" />
2829
<file src="..\..\README.md" target="docs\" />
2930
</files>

src/MimeTypes/MimeTypes.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<#@ template debug="false" hostspecific="false" language="C#" #>
22
<#@ include file="MimeTypeFunctions.ttinclude" #>
33
<#@ output extension=".cs.pp" encoding="utf-8" #>
4-
<# GenerateClass(includeAttributes: false); #>
4+
<# GenerateClass(nullability: false); #>

0 commit comments

Comments
 (0)