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.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement