meh / amnesia

Mnesia wrapper for Elixir.
693 stars 68 forks source link

Pass arguments to deftable #66

Open sheharyarn opened 7 years ago

sheharyarn commented 7 years ago

I'm trying to set the attributes of a table from a variable / module_attribute in the deftable macro, but it raises an exception instead.

use Amnesia

defdatabase MyApp.DB do
  @attrs [:id, :a, :b, :c]

  deftable SomeTable, @attrs, type: :bag do
  end 
end

I also tried using unquote(@attrs) since it's a macro but that doesn't work either. Is there some way I can get this to work? (This is just an example, the attributes need to be loaded from another module during compile-time).

This is the error I get:

Compiling 1 file (.ex)

== Compilation error on file lib/mnesia.ex ==
** (ArgumentError) argument error
    :erlang.length({:@, [line: 6], [{:attrs, [line: 6], nil}]})
    lib/amnesia/table/definition.ex:63: Amnesia.Table.Definition.define/4
    expanding macro: Amnesia.Database.deftable/4
    lib/db.ex:6: MyApp.DB (module)
meh commented 7 years ago

What's the error in the unquote(@attrs) case? I honestly don't know how to make that work right now but my brain tells me "unquote fragments" and "using bind_quoted somewhere in defdatabase/deftable" are part of the answer.

sheharyarn commented 7 years ago

This is the error when I try with unquote(@attrs):

Compiling 1 file (.ex)

== Compilation error on file lib/mnesia.ex ==
** (ArgumentError) argument error
    :erlang.length({:unquote, [line: 6], [{:@, [line: 6], [{:attrs, [line: 6], nil}]}]})
    lib/amnesia/table/definition.ex:63: Amnesia.Table.Definition.define/4
    expanding macro: Amnesia.Database.deftable/4
    lib/db.ex:6: MyApp.DB (module)
ourway commented 7 years ago

Any updates?

meh commented 7 years ago

@ourway I played around with it but didn't figure out a way to make it work :(