Repository navigation
feat: add type-first service access #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| { | ||
| ".": "0.1.0" | ||
| ".": "0.0.1" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| plugins { id("gg.grounds.base-conventions") version "0.7.0" } | ||
| plugins { id("gg.grounds.base-conventions") version "0.8.0" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Service Registry | ||
|
|
||
| `library-jvm-modules` uses typed service keys so modules can publish and consume services without string lookups at call sites. | ||
|
|
||
| ## Default: Access Services By Type | ||
|
|
||
| Use type-first access when there is one implementation for a service contract: | ||
|
|
||
| ```kotlin | ||
| val registry = DefaultServiceRegistry() | ||
|
|
||
| registry.register<MyService>(MyServiceImpl()) | ||
|
|
||
| val service = registry.require<MyService>() | ||
| ``` | ||
|
|
||
| For non-reified call sites, pass the Kotlin class: | ||
|
|
||
| ```kotlin | ||
| registry.register(MyService::class, MyServiceImpl()) | ||
|
|
||
| val service = registry.require(MyService::class) | ||
| ``` | ||
|
|
||
| ## Multiple Implementations: Use Qualified Keys | ||
|
|
||
| Use qualified keys only when the same contract has multiple implementations: | ||
|
|
||
| ```kotlin | ||
| object NatsServiceKeys { | ||
| val Public = qualifiedServiceKey<NatsClient>("grounds.nats.public") | ||
| val Internal = qualifiedServiceKey<NatsClient>("grounds.nats.internal") | ||
| } | ||
| ``` | ||
|
|
||
| Register and read those keys explicitly: | ||
|
|
||
| ```kotlin | ||
| registry.register(NatsServiceKeys.Public, publicNats) | ||
| registry.register(NatsServiceKeys.Internal, internalNats) | ||
|
|
||
| val publicNats = registry.require(NatsServiceKeys.Public) | ||
| ``` | ||
|
|
||
| Do not create qualified keys inline at call sites. Define them once in the module API that owns the service contract. | ||
|
|
||
| ## Module Descriptors | ||
|
|
||
| Use the same keys in module descriptors: | ||
|
|
||
| ```kotlin | ||
| class MatchmakingModuleProvider : ModuleProvider<MatchmakingModule> { | ||
| override val descriptor = | ||
| ModuleDescriptor( | ||
| id = "grounds.matchmaking", | ||
| version = "1.0.0", | ||
| requires = setOf(NatsServiceKeys.Internal), | ||
| provides = setOf(serviceKey<MatchmakingService>()), | ||
| ) | ||
|
|
||
| override fun create(): MatchmakingModule = MatchmakingModule() | ||
| } | ||
| ``` | ||
|
|
||
| `dependsOn` is for module startup order. `requires` and `provides` are for service contracts. |
11 changes: 11 additions & 0 deletions
11
module-api/src/main/kotlin/gg/grounds/modules/ModuleDescriptor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
module-api/src/main/kotlin/gg/grounds/modules/ModuleProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| package gg.grounds.modules | ||
|
|
||
| /** | ||
| * Factory discovered from the application classpath. | ||
| * | ||
| * Implementations are commonly loaded through Java [java.util.ServiceLoader]. The created module | ||
| * type is application-specific; this library only models discovery and dependency metadata. | ||
| */ | ||
| interface ModuleProvider<M : Any> { | ||
| /** Descriptor used for validation and startup ordering before [create] is called. */ | ||
| val descriptor: ModuleDescriptor | ||
|
|
||
| /** Creates a new module instance. */ | ||
| fun create(): M | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
module-api/src/main/kotlin/gg/grounds/modules/ServiceRegistry.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,88 @@ | ||
| package gg.grounds.modules | ||
|
|
||
| import kotlin.reflect.KClass | ||
|
|
||
| /** | ||
| * Registry for typed module services. | ||
| * | ||
| * Implementations store services by [ServiceKey]. Consumers should normally use the type-first | ||
| * extension functions in this file, such as `registry.require<MyService>()`. | ||
| */ | ||
| interface ServiceRegistry { | ||
| /** | ||
| * Registers [service] under [key]. | ||
| * | ||
| * Throws [DuplicateServiceException] if the key is already registered. | ||
| */ | ||
| fun <T : Any> register(key: ServiceKey<T>, service: T) | ||
|
|
||
| /** Returns the service registered under [key], or `null` when no service exists. */ | ||
| fun <T : Any> get(key: ServiceKey<T>): T? | ||
|
|
||
| /** | ||
| * Returns the service registered under [key]. | ||
| * | ||
| * Throws [MissingServiceException] when no service exists. | ||
| */ | ||
| fun <T : Any> require(key: ServiceKey<T>): T { | ||
| return get(key) ?: throw MissingServiceException(key) | ||
| } | ||
|
|
||
| /** Returns true when [key] has a registered service. */ | ||
| fun contains(key: ServiceKey<*>): Boolean = getUntyped(key) != null | ||
|
|
||
| /** Returns a snapshot of currently registered service keys. */ | ||
| fun keys(): Set<ServiceKey<*>> | ||
|
|
||
| /** | ||
| * Returns the raw service registered under [key]. | ||
| * | ||
| * Prefer typed accessors for application code. This method exists for diagnostics and registry | ||
| * internals that need to inspect unknown service types. | ||
| */ | ||
| fun getUntyped(key: ServiceKey<*>): Any? | ||
| } | ||
|
|
||
| /** Registers [service] under the default key for [T]. */ | ||
| inline fun <reified T : Any> ServiceRegistry.register(service: T) { | ||
| register(serviceKey<T>(), service) | ||
| } | ||
|
|
||
| /** Registers [service] under the default key for [type]. */ | ||
| fun <T : Any> ServiceRegistry.register(type: KClass<T>, service: T) { | ||
| register(serviceKey(type), service) | ||
| } | ||
|
|
||
| /** Returns the service registered for [T], or `null`. */ | ||
| inline fun <reified T : Any> ServiceRegistry.get(): T? = get(serviceKey<T>()) | ||
|
|
||
| /** Returns the service registered for [type], or `null`. */ | ||
| fun <T : Any> ServiceRegistry.get(type: KClass<T>): T? = get(serviceKey(type)) | ||
|
|
||
| /** | ||
| * Returns the service registered for [T]. | ||
| * | ||
| * Throws [MissingServiceException] when no service exists. | ||
| */ | ||
| inline fun <reified T : Any> ServiceRegistry.require(): T = require(serviceKey<T>()) | ||
|
|
||
| /** | ||
| * Returns the service registered for [type]. | ||
| * | ||
| * Throws [MissingServiceException] when no service exists. | ||
| */ | ||
| fun <T : Any> ServiceRegistry.require(type: KClass<T>): T = require(serviceKey(type)) | ||
|
|
||
| /** Returns true when [T] has a registered service. */ | ||
| inline fun <reified T : Any> ServiceRegistry.contains(): Boolean = contains(serviceKey<T>()) | ||
|
|
||
| /** Returns true when [type] has a registered service. */ | ||
| fun <T : Any> ServiceRegistry.contains(type: KClass<T>): Boolean = contains(serviceKey(type)) | ||
|
|
||
| /** Thrown when a required service is not registered. */ | ||
| class MissingServiceException(key: ServiceKey<*>) : | ||
| IllegalStateException("required service is not registered: $key") | ||
|
|
||
| /** Thrown when a service key is registered more than once. */ | ||
| class DuplicateServiceException(key: ServiceKey<*>) : | ||
| IllegalStateException("service is already registered: $key") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.