Skip to content

Commit

Permalink
Minor code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsensesoftware committed Nov 17, 2022
1 parent f768005 commit 341b18f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static void Finialize( EndpointBuilder endpointBuilder, ApiVersionSet? v
{
if ( versionSet is null )
{
// this should be impossible because WithApiVersionSet had to be called to get here
// this could only happen if the ApiVersionSet was removed elsewhere from the metadata
endpointBuilder.Metadata.Add( ApiVersionMetadata.Empty );
return;
}
Expand All @@ -56,7 +56,7 @@ private static void Finialize( EndpointBuilder endpointBuilder, ApiVersionSet? v
endpointBuilder.RequestDelegate = requestDelegate;
}

var parameterSource = endpointBuilder.ApplicationServices.GetRequiredService<IApiVersionParameterSource>();
var parameterSource = services.GetRequiredService<IApiVersionParameterSource>();

if ( parameterSource.VersionsByMediaType() )
{
Expand Down Expand Up @@ -122,10 +122,10 @@ private static bool ReportApiVersions( IList<object> metadata )
{
for ( var i = metadata.Count - 1; i >= 0; i-- )
{
if ( metadata[i] is ApiVersionSet set )
if ( metadata[i] is ApiVersionSet versionSet )
{
metadata.RemoveAt( i );
return set;
return versionSet;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static TBuilder WithApiVersionSet<TBuilder>(
throw new ArgumentNullException( nameof( apiVersionSet ) );
}

builder.Add( endpoint => AddWithValidation( endpoint, apiVersionSet ) );
builder.Add( endpoint => AddMetadata( endpoint, apiVersionSet ) );
builder.Finally( EndpointBuilderFinalizer.FinalizeEndpoints );

return builder;
Expand Down Expand Up @@ -393,7 +393,7 @@ public static TBuilder ReportApiVersions<TBuilder>( this TBuilder builder )
return builder;
}

private static void AddWithValidation( EndpointBuilder builder, ApiVersionSet versionSet )
private static void AddMetadata( EndpointBuilder builder, ApiVersionSet versionSet )
{
var metadata = builder.Metadata;
var grouped = builder.ApplicationServices.GetService( typeof( ApiVersionSetBuilder ) ) is not null;
Expand All @@ -414,30 +414,6 @@ private static void AddWithValidation( EndpointBuilder builder, ApiVersionSet ve
metadata.Add( versionSet );
}

private static void AdvertiseInApiVersionSet( IList<object> metadata, ApiVersion apiVersion )
{
for ( var i = metadata.Count - 1; i >= 0; i-- )
{
if ( metadata[i] is ApiVersionSet versionSet )
{
versionSet.AdvertisesApiVersion( apiVersion );
break;
}
}
}

private static void AdvertiseDeprecatedInApiVersionSet( IList<object> metadata, ApiVersion apiVersion )
{
for ( var i = metadata.Count - 1; i >= 0; i-- )
{
if ( metadata[i] is ApiVersionSet versionSet )
{
versionSet.AdvertisesDeprecatedApiVersion( apiVersion );
break;
}
}
}

private static void AddMetadata( EndpointBuilder builder, object item )
{
var metadata = builder.Metadata;
Expand Down Expand Up @@ -467,6 +443,30 @@ private static void AddMetadata( EndpointBuilder builder, object item )
nameof( IEndpointRouteBuilderExtensions.WithApiVersionSet ) ) );
}

private static void AdvertiseInApiVersionSet( IList<object> metadata, ApiVersion apiVersion )
{
for ( var i = metadata.Count - 1; i >= 0; i-- )
{
if ( metadata[i] is ApiVersionSet versionSet )
{
versionSet.AdvertisesApiVersion( apiVersion );
break;
}
}
}

private static void AdvertiseDeprecatedInApiVersionSet( IList<object> metadata, ApiVersion apiVersion )
{
for ( var i = metadata.Count - 1; i >= 0; i-- )
{
if ( metadata[i] is ApiVersionSet versionSet )
{
versionSet.AdvertisesDeprecatedApiVersion( apiVersion );
break;
}
}
}

private sealed class SingleItemReadOnlyList : IReadOnlyList<ApiVersion>
{
private readonly ApiVersion item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public VersionedEndpointRouteBuilder(
protected ApiVersionSetBuilder VersionSetBuilder { get; }

/// <inheritdoc />
public virtual IApplicationBuilder CreateApplicationBuilder() => routeBuilder.CreateApplicationBuilder();
public virtual IApplicationBuilder CreateApplicationBuilder() =>
routeBuilder.CreateApplicationBuilder();

/// <inheritdoc />
public virtual IServiceProvider ServiceProvider => serviceProvider;
Expand All @@ -55,30 +56,33 @@ public VersionedEndpointRouteBuilder(
public virtual ICollection<EndpointDataSource> DataSources => dataSources;

/// <inheritdoc />
public virtual void Add( Action<EndpointBuilder> convention ) => conventionBuilder.Add( convention );
public virtual void Add( Action<EndpointBuilder> convention ) =>
conventionBuilder.Add( convention );

private sealed class ServiceProviderDecorator : IServiceProvider
{
private readonly IServiceProvider decorated;
private readonly ApiVersionSetBuilder versionSetBuilder;
private ApiVersionSet? versionSet;

internal ServiceProviderDecorator( IServiceProvider decorated, ApiVersionSetBuilder versionSetBuilder )
internal ServiceProviderDecorator(
IServiceProvider decorated,
ApiVersionSetBuilder versionSetBuilder )
{
this.decorated = decorated;
this.versionSetBuilder = versionSetBuilder;
}

public object? GetService( Type serviceType )
{
if ( typeof( ApiVersionSet ).Equals( serviceType ) )
if ( typeof( ApiVersionSetBuilder ).Equals( serviceType ) )
{
return versionSet ??= versionSetBuilder.Build();
return versionSetBuilder;
}

if ( typeof( ApiVersionSetBuilder ).Equals( serviceType ) )
if ( typeof( ApiVersionSet ).Equals( serviceType ) )
{
return versionSetBuilder;
return versionSet ??= versionSetBuilder.Build();
}

return decorated.GetService( serviceType );
Expand All @@ -90,7 +94,9 @@ private sealed class EndpointDataSourceDecorator : EndpointDataSource
private readonly EndpointDataSource decorated;
private readonly ApiVersionSetBuilder versionSetBuilder;

internal EndpointDataSourceDecorator( EndpointDataSource decorated, ApiVersionSetBuilder versionSetBuilder )
internal EndpointDataSourceDecorator(
EndpointDataSource decorated,
ApiVersionSetBuilder versionSetBuilder )
{
this.decorated = decorated;
this.versionSetBuilder = versionSetBuilder;
Expand All @@ -104,14 +110,16 @@ public override IReadOnlyList<Endpoint> GetGroupedEndpoints( RouteGroupContext c
{
CollateGroupApiVersions();

// HACK: we don't have a way to pass the version set for the group down to each convention so
// decorate the service provider to allow it to be resolved. this requires rebuilding the
// current context as well.
// HACK: we don't have a way to pass the version set for the group down
// to each convention so decorate the service provider to allow it to
// be resolved. this requires rebuilding the current context as well.
if ( context.ApplicationServices is not ServiceProviderDecorator )
{
context = new()
{
ApplicationServices = new ServiceProviderDecorator( context.ApplicationServices, versionSetBuilder ),
ApplicationServices = new ServiceProviderDecorator(
context.ApplicationServices,
versionSetBuilder ),
Conventions = context.Conventions,
FinallyConventions = context.FinallyConventions,
Prefix = context.Prefix,
Expand All @@ -121,7 +129,8 @@ public override IReadOnlyList<Endpoint> GetGroupedEndpoints( RouteGroupContext c
return decorated.GetGroupedEndpoints( context );
}

public override bool Equals( object? obj ) => ReferenceEquals( this, obj ) || ReferenceEquals( decorated, obj );
public override bool Equals( object? obj ) =>
ReferenceEquals( this, obj ) || ReferenceEquals( decorated, obj );

public override int GetHashCode() => decorated.GetHashCode();

Expand Down Expand Up @@ -190,7 +199,8 @@ public void Add( EndpointDataSource item ) =>

public bool Contains( EndpointDataSource item ) => adapted.Contains( item );

public void CopyTo( EndpointDataSource[] array, int arrayIndex ) => adapted.CopyTo( array, arrayIndex );
public void CopyTo( EndpointDataSource[] array, int arrayIndex ) =>
adapted.CopyTo( array, arrayIndex );

public IEnumerator<EndpointDataSource> GetEnumerator() => adapted.GetEnumerator();

Expand Down

0 comments on commit 341b18f

Please sign in to comment.