Skip to content

Commit

Permalink
feat: Add container access as an extension point to more registration…
Browse files Browse the repository at this point in the history
… APIs

Fixes #771.
  • Loading branch information
maw136 committed Apr 9, 2024
1 parent e95ae48 commit 1ea1d36
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,21 @@ public static partial class ServiceCollectionExtensions
action?.Invoke(builder);
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="tbv::BasicClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
public static IServiceCollection AddBasicClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, tbv::BasicClientBuilder> action) =>
services.AddSingleton(provider =>
{
tbv::BasicClientBuilder builder = new tbv::BasicClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ public static partial class ServiceCollectionExtensions
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="td::DeprecatedClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
public static IServiceCollection AddDeprecatedClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, td::DeprecatedClientBuilder> action) =>
services.AddSingleton(provider =>
{
td::DeprecatedClientBuilder builder = new td::DeprecatedClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});

/// <summary>
/// Adds a singleton <see cref="td::DeprecatedServiceClient"/> to <paramref name="services"/>.
/// </summary>
Expand All @@ -60,5 +76,24 @@ public static partial class ServiceCollectionExtensions
action?.Invoke(builder);
return builder.Build(provider);
});

/// <summary>
/// Adds a singleton <see cref="td::DeprecatedServiceClient"/> to <paramref name="services"/>.
/// </summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
[sys::ObsoleteAttribute]
public static IServiceCollection AddDeprecatedServiceClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, td::DeprecatedServiceClientBuilder> action) =>
services.AddSingleton(provider =>
{
td::DeprecatedServiceClientBuilder builder = new td::DeprecatedServiceClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public static partial class ServiceCollectionExtensions
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::ComplianceClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
public static IServiceCollection AddComplianceClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, gsv::ComplianceClientBuilder> action) =>
services.AddSingleton(provider =>
{
gsv::ComplianceClientBuilder builder = new gsv::ComplianceClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::EchoClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
Expand All @@ -62,6 +78,22 @@ public static partial class ServiceCollectionExtensions
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::EchoClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
public static IServiceCollection AddEchoClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, gsv::EchoClientBuilder> action) =>
services.AddSingleton(provider =>
{
gsv::EchoClientBuilder builder = new gsv::EchoClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::IdentityClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
Expand All @@ -78,6 +110,22 @@ public static partial class ServiceCollectionExtensions
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::IdentityClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
public static IServiceCollection AddIdentityClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, gsv::IdentityClientBuilder> action) =>
services.AddSingleton(provider =>
{
gsv::IdentityClientBuilder builder = new gsv::IdentityClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::MessagingClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
Expand All @@ -94,6 +142,22 @@ public static partial class ServiceCollectionExtensions
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::MessagingClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
public static IServiceCollection AddMessagingClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, gsv::MessagingClientBuilder> action) =>
services.AddSingleton(provider =>
{
gsv::MessagingClientBuilder builder = new gsv::MessagingClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::SequenceServiceClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
Expand All @@ -110,6 +174,22 @@ public static partial class ServiceCollectionExtensions
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::SequenceServiceClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
public static IServiceCollection AddSequenceServiceClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, gsv::SequenceServiceClientBuilder> action) =>
services.AddSingleton(provider =>
{
gsv::SequenceServiceClientBuilder builder = new gsv::SequenceServiceClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::TestingClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
Expand All @@ -124,6 +204,22 @@ public static partial class ServiceCollectionExtensions
gsv::TestingClientBuilder builder = new gsv::TestingClientBuilder();
action?.Invoke(builder);
return builder.Build(provider);
});

/// <summary>Adds a singleton <see cref="gsv::TestingClient"/> to <paramref name="services"/>.</summary>
/// <param name="services">
/// The service collection to add the client to. The services are used to configure the client when requested.
/// </param>
/// <param name="action">
/// An optional action to invoke on the client builder. This is invoked before services from
/// <paramref name="services"/> are used.
/// </param>
public static IServiceCollection AddTestingClient(this IServiceCollection services, sys::Action<sys::IServiceProvider, gsv::TestingClientBuilder> action) =>
services.AddSingleton(provider =>
{
gsv::TestingClientBuilder builder = new gsv::TestingClientBuilder();
action?.Invoke(provider, builder);
return builder.Build(provider);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using Google.Api.Generator.Utils;
using Google.Api.Generator.Utils.Roslyn;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using static Google.Api.Generator.Utils.Roslyn.Modifier;
using static Google.Api.Generator.Utils.Roslyn.RoslynBuilder;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
Expand All @@ -43,14 +43,20 @@ public static CompilationUnitSyntax GenerateExtensions(SourceFileContext ctx, Li

var extensionMethods = packageServiceDetails
.OrderBy(p => p.ServiceFullName, StringComparer.Ordinal)
.Select(m => GenerateMethod(ctx, m))
.SelectMany(m => GenerateMethods(ctx, m))
.ToArray();
cls = cls.AddMembers(extensionMethods);
namespaceDeclaration = namespaceDeclaration.AddMembers(cls);
}
return ctx.CreateCompilationUnit(namespaceDeclaration);
}

private static IEnumerable<MethodDeclarationSyntax> GenerateMethods(SourceFileContext ctx, ServiceDetails service)
{
yield return GenerateMethod(ctx, service);
yield return GenerateMethodWithAdditionalDependencyInjection(ctx, service);
}

private static MethodDeclarationSyntax GenerateMethod(SourceFileContext ctx, ServiceDetails service)
{
var name = $"Add{service.ClientAbstractTyp.Name}";
Expand Down Expand Up @@ -79,5 +85,34 @@ private static MethodDeclarationSyntax GenerateMethod(SourceFileContext ctx, Ser
XmlDoc.Param(action, "An optional action to invoke on the client builder. This is invoked before services from ", services, " are used.")
);
}

private static MethodDeclarationSyntax GenerateMethodWithAdditionalDependencyInjection(SourceFileContext ctx, ServiceDetails service)
{
var name = $"Add{service.ClientAbstractTyp.Name}";
var serviceCollection = ctx.Type<IServiceCollection>();
var services = Parameter(serviceCollection, "services")
.WithModifiers(SyntaxTokenList.Create(Token(SyntaxKind.ThisKeyword).WithTrailingSpace()));
var actionType = ctx.Type(Typ.Generic(typeof(Action<>), Typ.Of<IServiceProvider>(), service.BuilderTyp));
var action = Parameter(actionType, "action");

var builderType = ctx.Type(service.BuilderTyp);
var builder = Local(builderType, "builder");

var provider = Parameter(ctx.Type<IServiceProvider>(), "provider");
var lambda = Lambda(provider)(
builder.WithInitializer(New(builderType)()),
action.Call("Invoke", true)(provider, builder),
Return(builder.Call("Build")(provider))
);

return Method(Public | Static, serviceCollection, name)(services, action)
.MaybeWithAttribute(service.IsDeprecated, () => ctx.Type<ObsoleteAttribute>())()
.WithBody(services.Call("AddSingleton")(lambda))
.WithXmlDoc(
XmlDoc.Summary("Adds a singleton ", ctx.Type(service.ClientAbstractTyp), " to ", services, "."),
XmlDoc.Param(services, "The service collection to add the client to. The services are used to configure the client when requested."),
XmlDoc.Param(action, "An optional action to invoke on the client builder. This is invoked before services from ", services, " are used.")
);
}
}
}

0 comments on commit 1ea1d36

Please sign in to comment.