python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Selector should be able to select between different Configurations

Open andresi opened this issue 2 years ago • 1 comments
trafficstars

Let's say I have this yaml configuration:

selected: option1

option1:
    param1:...
    param2:....

option2:
   param1:...
   param2:...

I want to be able to do something like this:

class Container(containers.DeclarativeContainer):
    config = providers.Configuration()

    parameters = providers.Selector(
        config.selected,
        option1=config.option1,
        option2=config.option2
    )

    foo = providers.Factory(
          SomeClass,
          param1=parameters.param1,
          param1=parameters.param2
    )

andresi avatar Mar 31 '23 21:03 andresi

I've been trying to make a custom Provider for this, but I don't know if it will be possible. To make this work, this "ConfigurationSelector" should behave like ConfigurationOption, so that you can getattr additional ConfigurationOptions. But the problem here is that ConfigurationOption works by referencing the Configuration root + full selector. And in this case the full selector is not known. I would need to make a new ConfigurationOption that works completly different... recursively looking backwards instead of referencing the root directly. That seems like a lot of work that I don't even know if it will work.

andresi avatar Apr 01 '23 13:04 andresi