cpursley / walex

Postgres change events (CDC) in Elixir
MIT License
276 stars 14 forks source link

invalid quoted expression: %{} #69

Open FabienHenon opened 2 months ago

FabienHenon commented 2 months ago

Hi,

I'm trying to compile my first project with WalEx. So I created a small project to test the lib and I got the following compilation error: invalid quoted expression: %{}

I used this code :

defmodule MyOwnApp.Events.Users do
  use WalEx.Event, name: MyOwnApp

  # any subscribed event
  on_event(:all, fn events ->
    IO.inspect(events: events)
  end)

  # any user event
  on_event(:users, fn users ->
    IO.inspect(on_event: users)
    # do something with users data
  end)

  # any user insert event
  on_insert(:users, fn users ->
    IO.inspect(on_insert: users)
  end)

  on_update(:users, fn users ->
    IO.inspect(on_update: users)
  end)

  on_delete(:users, fn users ->
    IO.inspect(on_delete: users)
  end)
end

And I'm using the version 4 of WalEx.

Any idea? I'm a bit rusted with Elixir macros 😅

cpursley commented 2 months ago

I missed this message @FabienHenon - were you able to figure the issue out? If not, can you describe it in more detail with logs if possible? Merci.

FabienHenon commented 2 months ago

Hi,

Yes I was able to fix the issue.

It was because I was using the default value of the filters parameter (of the on_event macro), which is %{}. This value, when in the macro, is still %{} and cannot be unquoted (This is the exception I got).

I tried to escape it with Macro.escape before unquoting it and it worked, but then it stopped working when I passed a value to the filters parameter of the macro (not using the default value). In this case, the unquoted value of the parameter was {:%{}, [], []}, which was not correct.

I don't know how to make defautl parameter value and explicit value work at the same time with macros.

So I didn't change the code and put %{} explicitly as value for the filters parameter of the on_event macro. It works like that (but not if I use the default value)

I also noticed another issue, with the replication slot this time. When I do not manually set the slot_name config parameter, one is generated. But the generated name uses forbidden characters (like dot '.'). Thus the query fails.

To solve this issue I explicitly set the slot_name config parameter value myself.