Skip to content

Commit 7c495d3

Browse files
Fix merge conflicts, code clean up, and integrate deprecation policies
1 parent 1969960 commit 7c495d3

36 files changed

Lines changed: 404 additions & 760 deletions

src/Abstractions/src/Asp.Versioning.Abstractions/DeprecationPolicy.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,9 @@ public DeprecationPolicy() { }
5757
public DeprecationPolicy( LinkHeaderValue link ) => Links.Add( link );
5858

5959
/// <summary>
60-
/// Returns a boolean to indicate if this policy is effective at the given <paramref name="dateTimeOffset"/>.
60+
/// Returns a value indicating if this policy is effective for the specified date and time.
6161
/// </summary>
62-
/// <param name="dateTimeOffset">The point in time to serve as a reference.</param>
63-
/// <returns>A boolean which indicates if this policy is effective.</returns>
64-
public bool IsEffective( DateTimeOffset? dateTimeOffset )
65-
{
66-
if ( dateTimeOffset is not { } when )
67-
{
68-
return true;
69-
}
70-
71-
if ( Date is not { } date )
72-
{
73-
return true;
74-
}
75-
76-
return date <= when;
77-
}
62+
/// <param name="dateTime">The <see cref="DateTimeOffset">date and time</see> to evaluate.</param>
63+
/// <returns>True if the policy is effective; otherwise, false.</returns>
64+
public bool IsEffective( DateTimeOffset dateTime ) => Date is { } date ? date <= dateTime : true;
7865
}

src/Abstractions/src/Asp.Versioning.Abstractions/IDeprecationPolicyBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ namespace Asp.Versioning;
55
/// <summary>
66
/// Defines the behavior of a deprecation policy builder.
77
/// </summary>
8-
public interface IDeprecationPolicyBuilder : IPolicyBuilder<DeprecationPolicy>, IPolicyWithLink, IPolicyWithEffectiveDate { }
8+
public interface IDeprecationPolicyBuilder : IPolicyBuilder<DeprecationPolicy>, IPolicyWithLink, IPolicyWithEffectiveDate
9+
{
10+
}

src/Abstractions/src/Asp.Versioning.Abstractions/IPolicyBuilderExtensions.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,48 @@ namespace Asp.Versioning;
77
/// </summary>
88
public static class IPolicyBuilderExtensions
99
{
10-
/// <summary>
11-
/// Creates and returns a new link builder.
12-
/// </summary>
1310
/// <param name="builder">The extended <see cref="IPolicyBuilder{T}">policy builder</see>.</param>
14-
/// <param name="linkTarget">The link target URL.</param>
15-
/// <returns>A new <see cref="ILinkBuilder">link builder</see>.</returns>
16-
public static ILinkBuilder Link( this IPolicyWithLink builder, string linkTarget )
11+
extension( IPolicyWithLink builder )
1712
{
18-
ArgumentNullException.ThrowIfNull( builder );
19-
return builder.Link( new Uri( linkTarget, UriKind.RelativeOrAbsolute ) );
13+
/// <summary>
14+
/// Creates and returns a new link builder.
15+
/// </summary>
16+
/// <param name="linkTarget">The link target URL.</param>
17+
/// <returns>A new <see cref="ILinkBuilder">link builder</see>.</returns>
18+
public ILinkBuilder Link( string linkTarget )
19+
{
20+
ArgumentNullException.ThrowIfNull( builder );
21+
return builder.Link( new Uri( linkTarget, UriKind.RelativeOrAbsolute ) );
22+
}
2023
}
2124

22-
/// <summary>
23-
/// Indicates when a policy is applied.
24-
/// </summary>
2525
/// <typeparam name="TBuilder">The type of <see cref="IPolicyBuilder{T}">policy builder</see>.</typeparam>
2626
/// <param name="builder">The extended <see cref="IPolicyBuilder{T}">policy builder</see>.</param>
27-
/// <param name="effectiveDate">The time when the policy is applied.</param>
28-
/// <returns>The current <see cref="IPolicyBuilder{T}">policy builder</see>.</returns>
29-
public static TBuilder Effective<TBuilder>( this TBuilder builder, DateTimeOffset effectiveDate )
30-
where TBuilder : notnull, IPolicyWithEffectiveDate
27+
extension<TBuilder>( TBuilder builder ) where TBuilder : notnull, IPolicyWithEffectiveDate
3128
{
32-
ArgumentNullException.ThrowIfNull( builder );
33-
builder.SetEffectiveDate( effectiveDate );
34-
return builder;
35-
}
29+
/// <summary>
30+
/// Indicates when a policy is applied.
31+
/// </summary>
32+
/// <param name="effectiveDate">The time when the policy is applied.</param>
33+
/// <returns>The current policy builder.</returns>
34+
public TBuilder Effective( DateTimeOffset effectiveDate )
35+
{
36+
ArgumentNullException.ThrowIfNull( builder );
37+
builder.SetEffectiveDate( effectiveDate );
38+
return builder;
39+
}
3640

37-
/// <summary>
38-
/// Indicates when a policy is applied.
39-
/// </summary>
40-
/// <typeparam name="TBuilder">The type of <see cref="IPolicyBuilder{T}">policy builder</see>.</typeparam>
41-
/// <param name="builder">The extended <see cref="IPolicyBuilder{T}">policy builder</see>.</param>
42-
/// <param name="year">The year when the policy is applied.</param>
43-
/// <param name="month">The month when the policy is applied.</param>
44-
/// <param name="day">The day when the policy is applied.</param>
45-
/// <returns>The current <see cref="IPolicyBuilder{T}">policy builder</see>.</returns>
46-
public static TBuilder Effective<TBuilder>( this TBuilder builder, int year, int month, int day )
47-
where TBuilder : notnull, IPolicyWithEffectiveDate
48-
{
49-
ArgumentNullException.ThrowIfNull( builder );
50-
return builder.Effective( new DateTimeOffset( new DateTime( year, month, day ) ) );
41+
/// <summary>
42+
/// Indicates when a policy is applied.
43+
/// </summary>
44+
/// <param name="year">The year when the policy is applied.</param>
45+
/// <param name="month">The month when the policy is applied.</param>
46+
/// <param name="day">The day when the policy is applied.</param>
47+
/// <returns>The current policy builder.</returns>
48+
public TBuilder Effective( int year, int month, int day )
49+
{
50+
ArgumentNullException.ThrowIfNull( builder );
51+
return builder.Effective( new DateTimeOffset( new DateTime( year, month, day ) ) );
52+
}
5153
}
5254
}

src/Abstractions/src/Asp.Versioning.Abstractions/IPolicyManagerExtensions.cs

Lines changed: 75 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -7,112 +7,96 @@ namespace Asp.Versioning;
77
/// </summary>
88
public static class IPolicyManagerExtensions
99
{
10-
/// <summary>
11-
/// Returns the policy for the specified API and version.
12-
/// </summary>
10+
/// <typeparam name="T">The type of policy.</typeparam>
1311
/// <param name="policyManager">The extended <see cref="IPolicyManager{T}">policy manager</see>.</param>
14-
/// <param name="apiVersion">The API version to get the policy for.</param>
15-
/// <param name="policy">The applicable <typeparamref name="TPolicy">policy</typeparamref>, if any.</param>
16-
/// <typeparam name="TPolicy">The type of policy.</typeparam>
17-
/// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
18-
public static bool TryGetPolicy<TPolicy>(
19-
this IPolicyManager<TPolicy> policyManager,
20-
ApiVersion apiVersion,
21-
[MaybeNullWhen( false )] out TPolicy policy )
12+
extension<T>( IPolicyManager<T> policyManager )
2213
{
23-
ArgumentNullException.ThrowIfNull( policyManager );
24-
return policyManager.TryGetPolicy( default, apiVersion, out policy );
25-
}
26-
27-
/// <summary>
28-
/// Returns the policy for the specified API and version.
29-
/// </summary>
30-
/// <param name="policyManager">The extended <see cref="IPolicyManager{T}">policy manager</see>.</param>
31-
/// <param name="name">The name of the API.</param>
32-
/// <param name="policy">The applicable <typeparamref name="TPolicy">policy</typeparamref>, if any.</param>
33-
/// <typeparam name="TPolicy">The type of policy.</typeparam>
34-
/// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
35-
public static bool TryGetPolicy<TPolicy>(
36-
this IPolicyManager<TPolicy> policyManager,
37-
string name,
38-
[MaybeNullWhen( false )] out TPolicy policy )
39-
{
40-
ArgumentNullException.ThrowIfNull( policyManager );
41-
return policyManager.TryGetPolicy( name, default, out policy );
42-
}
43-
44-
/// <summary>
45-
/// Attempts to resolve a policy for the specified name and API version combination.
46-
/// </summary>
47-
/// <param name="policyManager">The extended <see cref="IPolicyManager{T}">policy manager</see>.</param>
48-
/// <param name="name">The name of the API.</param>
49-
/// <param name="apiVersion">The API version to get the policy for.</param>
50-
/// <typeparam name="TPolicy">The type of policy.</typeparam>
51-
/// <returns>The applicable <typeparamref name="TPolicy">policy</typeparamref>, if any.</returns>
52-
/// <remarks>The resolution order is as follows:
53-
/// <list type="bullet">
54-
/// <item><paramref name="name"/> and <paramref name="apiVersion"/></item>
55-
/// <item><paramref name="name"/> only</item>
56-
/// <item><paramref name="apiVersion"/> only</item>
57-
/// </list>
58-
/// </remarks>
59-
public static TPolicy? ResolvePolicyOrDefault<TPolicy>(
60-
this IPolicyManager<TPolicy> policyManager,
61-
string? name,
62-
ApiVersion? apiVersion )
63-
{
64-
ArgumentNullException.ThrowIfNull( policyManager );
14+
/// <summary>
15+
/// Returns the policy for the specified API and version.
16+
/// </summary>
17+
/// <param name="apiVersion">The API version to get the policy for.</param>
18+
/// <param name="policy">The applicable <typeparamref name="T">policy</typeparamref>, if any.</param>
19+
/// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
20+
public bool TryGetPolicy( ApiVersion apiVersion, [MaybeNullWhen( false )] out T policy )
21+
{
22+
ArgumentNullException.ThrowIfNull( policyManager );
23+
return policyManager.TryGetPolicy( default, apiVersion, out policy );
24+
}
6525

66-
if ( policyManager.TryResolvePolicy( name, apiVersion, out var policy ) )
26+
/// <summary>
27+
/// Returns the policy for the specified API and version.
28+
/// </summary>
29+
/// <param name="name">The name of the API.</param>
30+
/// <param name="policy">The applicable <typeparamref name="T">policy</typeparamref>, if any.</param>
31+
/// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
32+
public bool TryGetPolicy( string name, [MaybeNullWhen( false )] out T policy )
6733
{
68-
return policy;
34+
ArgumentNullException.ThrowIfNull( policyManager );
35+
return policyManager.TryGetPolicy( name, default, out policy );
6936
}
7037

71-
return default;
72-
}
38+
/// <summary>
39+
/// Attempts to resolve a policy for the specified name and API version combination.
40+
/// </summary>
41+
/// <param name="name">The name of the API.</param>
42+
/// <param name="apiVersion">The API version to get the policy for.</param>
43+
/// <returns>The applicable <typeparamref name="T">policy</typeparamref>, if any.</returns>
44+
/// <remarks>The resolution order is as follows:
45+
/// <list type="bullet">
46+
/// <item><paramref name="name"/> and <paramref name="apiVersion"/></item>
47+
/// <item><paramref name="name"/> only</item>
48+
/// <item><paramref name="apiVersion"/> only</item>
49+
/// </list>
50+
/// </remarks>
51+
public T? ResolvePolicyOrDefault( string? name, ApiVersion? apiVersion )
52+
{
53+
ArgumentNullException.ThrowIfNull( policyManager );
7354

74-
/// <summary>
75-
/// Attempts to resolve a policy for the specified name and API version combination.
76-
/// </summary>
77-
/// <param name="policyManager">The extended <see cref="IPolicyManager{T}">policy manager</see>.</param>
78-
/// <param name="name">The name of the API.</param>
79-
/// <param name="apiVersion">The API version to get the policy for.</param>
80-
/// <param name="policy">The applicable <typeparamref name="TPolicy">policy</typeparamref>, if any.</param>
81-
/// <typeparam name="TPolicy">The type of policy.</typeparam>
82-
/// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
83-
/// <remarks>The resolution order is as follows:
84-
/// <list type="bullet">
85-
/// <item><paramref name="name"/> and <paramref name="apiVersion"/></item>
86-
/// <item><paramref name="name"/> only</item>
87-
/// <item><paramref name="apiVersion"/> only</item>
88-
/// </list>
89-
/// </remarks>
90-
public static bool TryResolvePolicy<TPolicy>(
91-
this IPolicyManager<TPolicy> policyManager,
92-
string? name,
93-
ApiVersion? apiVersion,
94-
[MaybeNullWhen( false )] out TPolicy policy )
95-
{
96-
ArgumentNullException.ThrowIfNull( policyManager );
55+
if ( policyManager.TryResolvePolicy( name, apiVersion, out var policy ) )
56+
{
57+
return policy;
58+
}
59+
60+
return default;
61+
}
9762

98-
if ( !string.IsNullOrEmpty( name ) )
63+
/// <summary>
64+
/// Attempts to resolve a policy for the specified name and API version combination.
65+
/// </summary>
66+
/// <param name="name">The name of the API.</param>
67+
/// <param name="apiVersion">The API version to get the policy for.</param>
68+
/// <param name="policy">The applicable <typeparamref name="T">policy</typeparamref>, if any.</param>
69+
/// <returns>True if the <paramref name="policy">policy</paramref> was retrieved; otherwise, false.</returns>
70+
/// <remarks>The resolution order is as follows:
71+
/// <list type="bullet">
72+
/// <item><paramref name="name"/> and <paramref name="apiVersion"/></item>
73+
/// <item><paramref name="name"/> only</item>
74+
/// <item><paramref name="apiVersion"/> only</item>
75+
/// </list>
76+
/// </remarks>
77+
public bool TryResolvePolicy( string? name, ApiVersion? apiVersion, [MaybeNullWhen( false )] out T policy )
9978
{
100-
if ( apiVersion != null && policyManager.TryGetPolicy( name, apiVersion, out policy ) )
79+
ArgumentNullException.ThrowIfNull( policyManager );
80+
81+
if ( !string.IsNullOrEmpty( name ) )
10182
{
102-
return true;
83+
if ( apiVersion != null && policyManager.TryGetPolicy( name, apiVersion, out policy ) )
84+
{
85+
return true;
86+
}
87+
else if ( policyManager.TryGetPolicy( name!, out policy ) )
88+
{
89+
return true;
90+
}
10391
}
104-
else if ( policyManager.TryGetPolicy( name!, out policy ) )
92+
93+
if ( apiVersion != null && policyManager.TryGetPolicy( apiVersion, out policy ) )
10594
{
10695
return true;
10796
}
108-
}
10997

110-
if ( apiVersion != null && policyManager.TryGetPolicy( apiVersion, out policy ) )
111-
{
112-
return true;
98+
policy = default!;
99+
return false;
113100
}
114-
115-
policy = default!;
116-
return false;
117101
}
118102
}

src/Abstractions/src/Asp.Versioning.Abstractions/IPolicyWithEffectiveDate.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
namespace Asp.Versioning;
44

55
/// <summary>
6-
/// A policy which can be configured to only be effective after a particular date.
6+
/// Defines the behavior of a policy which can be configured to only be effective after a particular date.
77
/// </summary>
88
public interface IPolicyWithEffectiveDate
99
{
1010
/// <summary>
11-
/// Indicates when a policy is applied.
11+
/// Sets the effective date when a policy is applied.
1212
/// </summary>
13-
/// <param name="effectiveDate">
14-
/// The <see cref="DateTimeOffset">date and time</see> when a policy is applied.
15-
/// </param>
13+
/// <param name="effectiveDate">The <see cref="DateTimeOffset">date and time</see> when a policy is applied.</param>
1614
void SetEffectiveDate( DateTimeOffset effectiveDate );
1715
}

src/Abstractions/src/Asp.Versioning.Abstractions/ISunsetPolicyBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ namespace Asp.Versioning;
55
/// <summary>
66
/// Defines the behavior of a sunset policy builder.
77
/// </summary>
8-
public interface ISunsetPolicyBuilder : IPolicyBuilder<SunsetPolicy>, IPolicyWithLink, IPolicyWithEffectiveDate { }
8+
public interface ISunsetPolicyBuilder : IPolicyBuilder<SunsetPolicy>, IPolicyWithLink, IPolicyWithEffectiveDate
9+
{
10+
}

src/Abstractions/src/Asp.Versioning.Abstractions/ISunsetPolicyBuilderExtensions.cs

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

0 commit comments

Comments
 (0)