PatrickSachs

PatrickSachs

Inject variable into macro function body

My termininology with the question title is probably all over the place, but I’m really struggling on how to phrase it.

What I essentially want to do, is something like plug does in its “get”/“post”, etc. macros: It injects the “conn” variable without having to declare it anywhere.

I have this macro:

defmacro socket(matcher, do: expr) do
  quote do
    def websocket_handle({:json, unquote(matcher) = json}, state) do
      try do
        unquote(expr)
      rescue
        e -> Logger.error("Websocket Error - #{inspect(e)}")
      end
    end
  end
end

and use it like this:

socket %{"action" => "join_chat", "id" => id} do
  my_fun(id)
  my_fun(json) # :(
  my_fun(state) # :(
  {:reply, %{}, state}
end

Now, the “id” variable declared in the matcher is readily available, however “json” and “state” are not. It’s certainly related to scope, but I have tried all sorts of different things - to no avail.

The “var!” macro didn’t really help because that required me to declare the variable first, which is the exact opposite of what I want. I looked at the plug source code, but I had a hard time following it.

Is what I want to do feasable with a small macro? If yes, how - if no, what are my alternatives for giving access to the “json” and “state” variables?

Thank you for your time!

Marked As Solved

OvermindDL1

OvermindDL1

All variables have to be declared before use, this is what plug does as well. You should be able to just replace the variable names in the function definition inside the quote I’d imagine, to be something like this:

def websocket_handle({:json, unquote(matcher) = unquote(Macro.var(:json, nil))}, unquote(Macro.var(:state, nil))) do

Does that not work?

Also Liked

PatrickSachs

PatrickSachs

This seems to work really well! Thank you very much for this - you been a massive time saver.

Last Post!

PatrickSachs

PatrickSachs

This seems to work really well! Thank you very much for this - you been a massive time saver.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement