scott2e

scott2e

How do I pass variables to Protox's __using__ macro?

Apologies, this question has been asked in numerous variations before, but it seems I haven’t found the answer I’m looking for.

I’ve found myself in a situation where I need to support multiple versions of protobuf messages. This was due to some poor decisions by the vendor in changing protobuf files for different versions of software they’re running. i.e. they’ve kept the same protobuf files between different software versions but changed some data types on existing fields in messages. I need to be able to interface with both versions of their software..

So I need to generate both sets of protobuf files but with different namespaces

I did start doing this (which works) but it’s very tedious:

defmodule MyModule do
  use Protox, files: ["./proto/v1/foo.proto"], namespace: V1
  use Protox, files: ["./proto/v2/foo.proto"], namespace: V2
  ...
  use Protox, files: ["./proto/vN/foo.proto"], namespace: VN
end

I’ve attempted this (does not work):

defmodule MyModule do
  MyOtherModule.get_files_and_namespaces()
  |> Enum.each(fn {files, namespace} ->
    use Protox, files: files, namespace: namespace
  end)
end

This responds with the usual:

variable "files" does not exist and is being expanded to "files()", please use parentheses to remove the ambiguity or change the variable name

I’ve attempted to use unquote/1

defmodule MyModule do
  MyOtherModule.get_files_and_namespaces()
  |> Enum.each(fn {files, namespace} ->
    use Protox, unquote(files: files, namespace: namespace)
  end)
end

This responds with:

== Compilation error in file lib/mymodule.ex ==
** (CompileError) nofile:4: unquote called outside quote
    (elixir 1.12.3) lib/code.ex:1036: Code.eval_quoted/3
    (protox 1.7.0) expanding macro: Protox.__using__/1
    lib/mymodule.ex:4: MyModule (module)
    (elixir 1.12.3) expanding macro: Kernel.use/2
    lib/mymodule.ex:4: MyModule (module)
    (elixir 1.12.3) expanding macro: Kernel.|>/2

The __using__/1 macro from the Protox module looks like this:

 defmacro __using__(opts) do
    {opts, _} = Code.eval_quoted(opts)

    {paths, opts} = get_paths(opts)
    {files, opts} = get_files(opts)

    {:ok, file_descriptor_set} = Protox.Protoc.run(files, paths)

    %{enums: enums, messages: messages} = Protox.Parse.parse(file_descriptor_set, opts)

    quote do
      unquote(make_external_resources(files))
      unquote(Protox.Define.define(enums, messages, opts))
    end
  end

Can anyone relieve me from my macro ignorance?

Thank-you in advance :slight_smile:

Marked As Solved

zachallaun

zachallaun

Hi Scott!

On my phone so I can’t give in depth code examples, but a few notes to hopefully nudge you in the right direction:

The important thing you remember with macros is that they run at compile time (a step called macro expansion). So in your attempt with Enum.each, the call to use is being expanded before the loop runs. As seen in the pasted using code, they then evaluate the passes options — at that point, files isn’t bound to anything because the function isn’t being run simultaneously.

So, the question now is how do we get those values at macro expansion time? The answer is macros! What you need is to define a macro that generates quoted expressions containing the use calls that you need, with the right arguments injected into the expressions. You might have something like this:

defmacro define_protox_versions do
  for {files, namespaces} <- get_files_and_namespaces() do
    quote do
      use Protox, files: unquote(files), namespace: unquote(namespace)
    end
  end
end

and then you can do:

defmodule MyModule do
  require MyOtherModule
  MyOtherModule.define_protox_versions()
end

So, now your macro is going to return a list of quoted expressions, injecting them into MyModule. Those expressions contain macros, so they will be expanded as well, but now they also contain the literal lists and namespaces that use Protox needs.

Hopefully this helps!

Also Liked

dimitarvp

dimitarvp

Not to be mistaken with the at least 20 answers I posted when on phone and they were all wrong, lol. :003:

But yeah I was about to suggest the same as @zachallaun. Try it and let us know.

zachallaun

zachallaun

Yep, this is always the fear :joy: it’s amazing how reliant I sometimes am on just being able to quickly try something out in a scratch file.

zachallaun

zachallaun

One other thing I’ll mention is this: after you get this working, you may want to consider still co-locating the files/namespaces, or maybe a shortened version (I’m not sure what your theoretical get_files_and_namespaces() is doing).

define_protox_versions(
  v1: V1,
  v2: V2,
  …
  vN: VN
)

As cool as macros are, someone (even yourself sometime later!) will have to go through multiple layers of indirection to figure out which versions are being defined. I don’t begrudge someone wanting to save some keystrokes, but explicit is often better than implicit. Also might make some things easier down the road, like when you decide that V4 was a total buggy piece of crap that absolutely shouldn’t be used — now you can just delete that version from the list instead of having to add some special exclusion somewhere. :slight_smile:

Last Post!

scott2e

scott2e

The get_files_and_namespaces() I made just parses the directory hierarchy where the protobufs are stored and determines the version and namespaces based off the names of the directories. But yes, perhaps I should make it more explicit.

Thank-you for the input

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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

We're in Beta

About us Mission Statement