Using macros to interface with Faker lib for generating data

I’m trying to use a macro to receive the module and function as atoms corresponding to equivalent modules and functions present in the faker lib. (The code below should make my intentions clear)
I’m new to Elixir and the question is not how to make it work, because it does work, but how could I make it better? I’m particularly concerned with the macro itself because the Code.eval_quoted() is giving me a bit of a code smell, but since I don’t have enough experience with the language I’m not sure how to evaluate this(the code quality and best practices).

(The maybe excessive documentation is kind of a note taking system to help with my learning)

  @typedoc """
  An uppercased atom equivalent to one of the available faker modules
  @type faker_module :: atom

  @typedoc """
  A lower cased atom equivalent to a faker function whose inside a `faker_module` type
  @type faker_function :: atom

  @doc """
  Given a faker module and a function (as atoms) of that module, inserts the
  module and function in a quoted expression — AST representation — and return a
  function call that could be made using the faker lib. The result should be the same
  as the call to Faker in the given module with the given function.

  ## Examples
       iex> resolve(:Address, :city)
       iex> resolve(:Person, :name)
       "Darrion Emmerich MD"

  @spec resolve(faker_module, faker_function) :: any
  defmacro resolve(module, function) do
    quote do
      {{:., [], [{:__aliases__, [alias: false], [:Faker, unquote(module)]}, unquote(function)]},
       [], []}
      |> Code.eval_quoted()
      |> elem(0)

And I’m using a mapper that looks like this

  def str_to_faker_map do
      "@id.uuid" => [:UUID, :v4],
      "@person.first_name" => [:Person, :name],
      "@geo.state" => [:Address, :state],

And here’s how I’m using this system to generate data based on a given JSON with this generator syntax (using “@”).

 @doc """
  Used to generate mock data for a model.
  ## Examples
      iex> model = %{
        "name" => "",
        "city" => "",
      iex> generate_data_for_model(model)
      {:ok, %{"city" => "Lake Gussie", "name" => "Hattie Schimmel"}}
  @spec generate_data_for_model(model) :: {atom, map}
  def generate_data_for_model(model) when is_map(model) do
    case check_model_validity(model) do #in the code this is a private function to check if the model values are
      true ->                                                 # are in the mapper keys
        generated_model =
          for {k, v} <- model, into: %{} do
            [module, function] = map_string_to_faker_atoms(v) #Map.get on the mapper
            {k, resolve(module, function)}

        {:ok, generated_model}

      _ ->
          "The model has unrepresentable values to a faker generator" <>
            "the values should have the pattern \"@module.function\"." <>
            "Notice that some modules might be remapped (ex: Address is mapped as @geo)"

Feel free to point out any improvements I could make in any part of these snippets. This is a pet project of mine for an app that helps generate mock data.

  1. I would say don’t use macros.
def resolve(model, fun) do
  |> Module.concat(model)
  |> apply(fun, [])
  1. instead of check_model_validity(model)

why not just do Map.fetch! and let it crash if the model isn’t in there. Presumably you’re fully in control of the supplied models.

1 Like

Thank you!

Just discovered the Module behavior and was trying to hammer it into a macro for some reason, but point 1 is definitely the way to go! Made the code a lot easier to read.

For point two, it was just an idea for handling bad requests, since this is supposed to be an API that will be consumed by a frontend that I’m building as well. But I guess you may be right and I might not need this.