mikunn

mikunn

I’m trying to replace a little functionality with a fake one in dev, while every other environment must use the default one. For security reasons, I don’t want the call to the fake functionality even exist in production, so I’m using a macro. Here’s the barebones idea:

defmodule Myapp.MacroModule do

  defmacro build_impl(default) do
    if Mix.env() == :dev do
      quote do: Myapp.MacroModule.fake_impl()
    else
      quote do: unquote(default)
    end
  end

  def fake_impl() do
    :fake_value
  end

end

defmodule Myapp do
  require Myapp.MacroModule

  def hello(params) do
    result = Myapp.MacroModule.build_impl(Map.get(params, :key))
    IO.inspect(result)
    # do_something_else_with_result(result)
  end

  def hello2(param) do
    Myapp.MacroModule.build_impl(param)
    IO.inspect(param)
  end

end

In dev it uses fake_impl/0 and in other envs the default one passed to the macro and it works fine. The macro can be called with different implementations from different places.

The issue is that I of course get variable "params" is unused warning in dev since to my understanding the first line of hello/1 would turn out to be result = Myapp.MacroModule.fake_impl() and params is not used.

How could I go about suppressing the warning? Or should I approach the original issue some other way?

Thanks for any help!

EDIT: added hello2/1 function to demonstrate another default implementation

Showing Posts 1 to 8

LostKobrakai

LostKobrakai

Add the following to your fake implementation part of your macro.

_ = params
mikunn

mikunn OP

Thanks for the response!

I tried both

_ = params
quote do: Myapp.MacroModule.fake_impl()

and

quote do
  _ = params
  Myapp.MacroModule.fake_impl()
end

The first one complains that params doesn’t exist inside the macro and second complains that params doesn’t exist on the first line of hello/1.

Also, I have something like this as well:

def hello2(param) do
  result = Myapp.MacroModule.build_impl(param)
  IO.inspect(result)
  IO.inspect(param)
end

I would assume adding both _ = params and _ = param would not work?

LostKobrakai

LostKobrakai

It needs to go inside the quote, but I forgot about macro hygiene. This should work _ = var!(params) when the variable is not explicitly passed.

mikunn

mikunn OP

Thanks a lot! That works for the original case and I understand what that is doing.

My example was lacking a bit, since I have other default implementations, so I added hello2/1 to the original post. params doesn’t exist in that case, so I tried to find a way to find out in the macro if params exists before using it, but I had no luck.

A solution that works for both cases is to add _ = params to hello/1, but there must a better way to do this without cluttering the original function because of the macro.

LostKobrakai

LostKobrakai

You can maybe use binding/1, but you’ll not know which in scope variable is used by the AST being the input to your macro without recursing through said AST. So blindly using it might silence ligitimate warnings.

mikunn

mikunn OP

I thought I could run some filtering on that inside quote, but I guess just calling binding() binds everything, so like you said, it might be best avoided.

I came up with this

defmacro build_impl(default) do
  if Mix.env() == :dev do
    if Macro.Env.has_var?(__CALLER__, {:params, nil}) do
      quote do
        _ = var!(params)
        Myapp.MacroModule.fake_impl()
      end
    else
      quote do: Myapp.MacroModule.fake_impl()
    end
  else
    quote do: unquote(default)
  end
end

It seems to work and will suffice for now since other callers to the macro have all the variables used inside the function. If I needed to check for multiple variables that would get clumsy.

al2o3cr

al2o3cr

Another approach: you could add a never-taken branch with the unused code in it:

  defmacro build_impl(default) do
    if Mix.env() == :dev do
      quote do
        if false do
          unquote(default)
        end
        Myapp.MacroModule.fake_impl()
      end
    else
      quote do: unquote(default)
    end
  end
mikunn

mikunn OP

That’s pretty neat and simple! Thanks! Will mark this as solution.

— 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
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews