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

context-data-dependent providing - to enable passing in different configiration sources #225

Open
RonnyPfannschmidt opened this issue Aug 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@RonnyPfannschmidt
Copy link

based on whats provided in the context i'd like to choose different implementation paths

FORCE_USERNAME = NewType("FORCE_USERNAME", str)
UserDetails = NewType("UserDetails")



Class UserProvider(Provider):
     forced_username = fromContext(FORCE_USERNAME)

     @provide
     def user_lookup(self) -> UserLookup:
         return UserLookup()


    @provide
    def user_forced(self, username: FORCE_USERNAME, lookup: UserProvider) -> UserDetails:
      return lookup[username]

   user_fromcontext = from_context(UserDetails)
   
   @provide
   def default_user(self, lookup: UserLookup) -> UserDetails:
      return lookup.default_user

c = makeContainer(UserProvider()):

with c(context={FORCE_USERNAME: "bad" ) as sub:
   sub.get(UserDetails) # look up the specific user

with c(context={UserDetails: UserDetails(...)}) as sub:
   sub.get(UserDetails) # use the provided one
 
    
with c() as sub:
  sub.get(UserDetails) # get the default one
   
  
@Tishka17
Copy link
Member

Relates to #9 but suggests selection based on context contents, not only values

@Tishka17 Tishka17 added the enhancement New feature or request label Aug 27, 2024
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

2 participants