From ee25c69b838209d7d82cfc6ada3cc9c5911898c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Andr=C3=A9n?= Date: Mon, 9 Sep 2024 17:30:49 +0200 Subject: [PATCH] feat: Load client config from arbitrary config block (#1974) --- .../scala/akka/grpc/GrpcClientSettings.scala | 25 +++++++++++-------- .../akka/grpc/GrpcClientSettingsSpec.scala | 16 ++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/runtime/src/main/scala/akka/grpc/GrpcClientSettings.scala b/runtime/src/main/scala/akka/grpc/GrpcClientSettings.scala index 40b605a85..d72a91ce2 100644 --- a/runtime/src/main/scala/akka/grpc/GrpcClientSettings.scala +++ b/runtime/src/main/scala/akka/grpc/GrpcClientSettings.scala @@ -46,17 +46,22 @@ object GrpcClientSettings { * @param clientName of the client configuration to lookup config from the ActorSystem's config */ def fromConfig(clientName: String)(implicit actorSystem: ClassicActorSystemProvider): GrpcClientSettings = { - val system = actorSystem.classicSystem - val akkaGrpcClientConfig = system.settings.config.getConfig("akka.grpc.client") - val clientConfig = { - // Use config named "*" by default - val defaultServiceConfig = akkaGrpcClientConfig.getConfig("\"*\"") - require( - akkaGrpcClientConfig.hasPath(s""""$clientName""""), - s"Config path `akka.grpc.client.$clientName` does not exist") - akkaGrpcClientConfig.getConfig(s""""$clientName"""").withFallback(defaultServiceConfig) - } + val akkaGrpcClientConfig = actorSystem.classicSystem.settings.config.getConfig("akka.grpc.client") + fromConfig(clientName, akkaGrpcClientConfig) + } + /** + * Look up client settings from the given configuration. Will look for an entry with the given name client name + * directly in the config block. Each client configuration falls back to the defaults defined in reference.conf + * + * @param clientName of the client name to lookup config from the given config + */ + def fromConfig(clientName: String, config: Config)( + implicit actorSystem: ClassicActorSystemProvider): GrpcClientSettings = { + // Use config named "*" by default + val defaultServiceConfig = actorSystem.classicSystem.settings.config.getConfig("akka.grpc.client.\"*\"") + require(config.hasPath(s""""$clientName""""), s"Config path `akka.grpc.client.$clientName` does not exist") + val clientConfig = config.getConfig(s""""$clientName"""").withFallback(defaultServiceConfig) GrpcClientSettings.fromConfig(clientConfig) } diff --git a/runtime/src/test/scala/akka/grpc/GrpcClientSettingsSpec.scala b/runtime/src/test/scala/akka/grpc/GrpcClientSettingsSpec.scala index 0c3d472d1..2c5c56016 100644 --- a/runtime/src/test/scala/akka/grpc/GrpcClientSettingsSpec.scala +++ b/runtime/src/test/scala/akka/grpc/GrpcClientSettingsSpec.scala @@ -194,6 +194,22 @@ class GrpcClientSettingsSpec extends AnyWordSpec with Matchers with ScalaFutures settings.serviceDiscovery should not be null } finally actorSystem.terminate() } + + "allow a user provided config object" in { + val config = ConfigFactory.parseString(""" + "service-with-user-config" { + host = "my-host" + port = 43 + service-discovery { + mechanism = "static" + } + } + """) + val settings = GrpcClientSettings.fromConfig("service-with-user-config", config) + settings.defaultPort should ===(43) + settings.useTls should ===(true) // from system default config + + } } override def afterAll(): Unit = {