nhpip

nhpip

Hey,

I know there are other ways to do this, but I went down a deep rabbit hole last night trying to figure this one out :slight_smile: … this is purely a though experiment.

Say you have 2 almost identical dependencies, but you only want to use one of them at a time. Maybe the choice of dependency is geographic based?

Both dependencies have an example module hierarchy like:

 M0
  |
  M0.M1 - M0.M2 - M0.M3
            |
         M0.M2.M4

Now of course a simple solution to ensure the code in the rest of the application selects the correct dependency would be something like this in all the modules that use it:

  config = Application.compile_env(:my_app, SpecialDependency, [])
  @dependency Keyword.get(config, :special_dep)

  # And code would do:
  @dependency.foo(a, b, c)

Seems a bit hokey…what would be nice is to create a new (set) of stub modules that, for want of a better word “alias” the correct dependency. All the application code would call the stub which would get mapped onto the correct target dependency.

For example (yes, I can also use defdelegate)

defmodule SpecialDependencyStub do

  config = Application.compile_env(:my_app, SpecialDependency, [])
  @dependency Keyword.get(config, :special_dep)

 # Generate the same functions as the real dependency
  Enum.each(@dependency.__info__(:functions), fn({fun, arity}) ->
     args = Macro.generate_arguments(arity, __MODULE__)
     def unquote(fun)(unquote_splicing(args)), do: unquote(@dependency).unquote(fun)(unquote_splicing(args))
  end)
end

Now that works fine…where I am stumped is how to handle all the modules under the SpecialDependency. I want those modules to retain the same name as the real dependency, and I’d rather not have to repeat the above for every module. Something like

Enum.each(modules, fn(mod) -> WizzardsBeHere end)

Hopefully that all makes sense

Showing Posts 1 to 4

hst337

hst337

Why would you want to write Stub.foo(bar) instead of @stub.foo(bar)?

felix-starman

felix-starman

I am slowly working on a core rewrite of the Double library for this purpose, so it can be used as a drop-in for Mox and allow folks the tools to slowly introduce behaviours and mocks

The code is dense and in the middle of a refactor, but if you’re interested, I’m taking a somewhat similar approach, at least in the defmock side.

https://github.com/petermueller/double/blob/add-defmock/lib/double.ex

nhpip

nhpip OP

I’ll take a look…thanks

Hermanverschooten

Hermanverschooten

If these modules have the same interface, couldn’t you use a macro that would alias the correct module and you’d use that alias in your code?

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
perzanko
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews