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

Auto-inject parameter of primitive data type into Singleton #2

Open
davidscheutz opened this issue Apr 17, 2024 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@davidscheutz
Copy link
Owner

Feature Request

Currently it's not possible to directly inject parameters such as String, Int etc. using the AutoSetup protocol.

The following code will result in a crash:

/// @Singleton
class ApiClient {
   private let baseUrl: String

   init(baseUrl: String) {
      self.baseUrl = baseUrl
   }
}

Error: DependencyContainer.ResolveError<Swift.String>.notRegistered

Proposed Solution

DependencyContainer does already support injecting and resolving objects using a Key: AnyHashable.

The question is how to integrate that feature using the AutoSetup code generation?

@davidscheutz davidscheutz added the enhancement New feature or request label Apr 17, 2024
@davidscheutz
Copy link
Owner Author

For now, there is a simple workaround available:

1. Create a wrapper type for your parameter(s)

Using the example above:

/// @Singleton
class ApiClient {
   struct Config {
      let baseUrl: String
   }

   init(config: Config) {
      self.baseUrl = config.baseUrl
   }
}

2. Manually register the wrapper type

Using the override function of your dependencies entry point:

struct YourDependencies: AutoSetup {
   func override(_ container: DependencyContainer) throws {
      container.register { ApiClient.Config(baseUrl: "{YOUR_API_URL}") }
   }
}

Hope that unblocks everyone for now! Will update this issue once support for this feature is available 🙌🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant