Skip to content
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

feat: @ComponentScan glob support #131

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

OffRange
Copy link

@OffRange OffRange commented Jun 7, 2024

Summary

This PR enhances the @ComponentScan annotation in Koin Annotations to support glob patterns. Currently, @ComponentScan only allows specifying a single, exact package name per Koin module. With this change, developers can use wildcards (* and **) to scan multiple packages or subpackages in a more flexible and concise way.

Motivation

In modern, modular JVM applications, components (like services, repositories, or utilities) are often spread across multiple subpackages. The current @ComponentScan requires developers to either:

  1. Create a Koin module for each subpackage, leading to module proliferation.
  2. Place all components in a single package, which can become unwieldy.

By supporting glob patterns, we allow developers to organize their code more naturally without compromising on Koin's autowiring capabilities.

Implementation

  1. Added GlobToRegex.kt utility:

    • Converts glob patterns to regular expressions.
    • Supports * (single-level) and ** (multi-level) wildcards.
    • Case-sensitive by default, as package names in JVM are case-sensitive.
  2. Modified KoinMetaDataScanner and KoinMetaData:

    • Now uses custom extension function to interact with the GlobToRegex utility
  3. Tests

    • Still passes

Examples

Before:

@ComponentScan("com.app.service")
class ServiceModule
@ComponentScan("com.app.service.user")
class UserServiceModule
@ComponentScan("com.app.service.order")
class OrderServiceModule

After:

@ComponentScan("com.app.service.**")
class ServiceModule

However we can still use:

@ComponentScan("com.app.core")
class CoreModule

This commit introduces a new Kotlin file, GlobToRegex.kt, which provides
utilities for converting glob patterns to regular expressions, primarily for
matching package names in JVM projects.
Suffix and prefix wildcard pattern like are now supported
Now `com.example.*.service` does ot match `com.example.service`.
@arnaudgiuliani arnaudgiuliani added this to the 1.4.0 milestone Jun 25, 2024
@arnaudgiuliani arnaudgiuliani self-requested a review June 25, 2024 12:47
@arnaudgiuliani
Copy link
Member

Hello,

great addition. Could you add some local unit tests to help be sure we cover those regex?

Thanks 🙏

@arnaudgiuliani arnaudgiuliani added the status:checking Ticket is currently being checked label Jun 25, 2024
@OffRange
Copy link
Author

OffRange commented Jun 26, 2024

Hello,

great addition. Could you add some local unit tests to help be sure we cover those regex?

Thanks 🙏

Of course, since all tests are located in modules within the examples module, I assume my tests should be placed there as well. Do you have any preferences regarding a specific module? Alternatively, would you prefer that I create a new module within examples for the regex testing, or should I create a new test source set for the koin-ksp-compiler module within the projects module?

@arnaudgiuliani arnaudgiuliani modified the milestones: 1.4.0, 2.0 Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal status:checking Ticket is currently being checked
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants