Umbra v0.0.4 Umbra.DefinitionExtractor View Source

This module allows to retrieve function name and arguments from a definition.

Definitions looks like this:

:func
{:func}
{:func, a}
{:func, a, b}
{:func, false}
{:func, "_" <> rest = a}
{:func, [head | tail] = a}
{:func, %{id: id, name: name}}

# Where :func is the name of your function.

Link to this section Summary

Functions

Extract the arguments used in calls from a function declaration.

Extract the arguments used for function declaration from a function definition.

Extract the function name from a function definition.

Extract the argument used in call from an argument declaration.

Generate the client call arguments but avoid extra/useless variable.

Generate the client definition arguments.

Generate the GenServer parameter containing the message and the parameters.

Generate the server definition arguments.

This macro help know if the given atom is an operator.

This macro help know if the given atom is a valid name for functions or variables. It should be used in when guards.

Shadow inner argument declaration used for client function declaration.

Shadow left value in argument declaration/assignment used for client function declaration.

Link to this section Functions

Link to this function

extract_arguments_for_call(definition)

View Source

Specs

extract_arguments_for_call(definition :: tuple() | atom()) :: [tuple()]

Extract the arguments used in calls from a function declaration.

Example:

iex> Umbra.DefinitionExtractor.extract_arguments_for_call(quote do: {:my_func})
[]

iex> Umbra.DefinitionExtractor.extract_arguments_for_call(quote do: {:my_func, a})
[{:a, [], UmbraTest}]

iex> Umbra.DefinitionExtractor.extract_arguments_for_call(quote do: {:my_func, %{} = a, b})
[{:a, [], UmbraTest}, {:b, [], UmbraTest}]

iex> Umbra.DefinitionExtractor.extract_arguments_for_call(quote do: {:my_func, a, "toto" = b})
[{:a, [], UmbraTest}, {:b, [], UmbraTest}]

iex> Umbra.DefinitionExtractor.extract_arguments_for_call(quote do: {:my_func, true, []})
[true, []]

iex> Umbra.DefinitionExtractor.extract_arguments_for_call(quote do: {:my_func, [head | tail]})
[[{:|, [], [{:head, [], UmbraTest}, {:tail, [], UmbraTest}]}]]

iex> Umbra.DefinitionExtractor.extract_arguments_for_call(quote do: [:lol])
** (ArgumentError) invalid function definition
Link to this function

extract_arguments_for_declaration(definition)

View Source

Specs

extract_arguments_for_declaration(definition :: tuple() | atom()) :: [tuple()]

Extract the arguments used for function declaration from a function definition.

Example:

iex> Umbra.DefinitionExtractor.extract_arguments_for_declaration(quote do: {:func, a})
[{:a, [], UmbraTest}]

iex> Umbra.DefinitionExtractor.extract_arguments_for_declaration(quote do: {:func, a, b})
[{:a, [], UmbraTest}, {:b, [], UmbraTest}]

iex> Umbra.DefinitionExtractor.extract_arguments_for_declaration(quote do: {:func, %Test{} = a})
[{:=, [], [{:%, [], [{:__aliases__, [alias: false], [:Test]}, {:%{}, [], []}]}, {:a, [], UmbraTest}]}]

iex> Umbra.DefinitionExtractor.extract_arguments_for_declaration(quote do: {:func, 42})
[42]

iex> Umbra.DefinitionExtractor.extract_arguments_for_declaration(quote do: {:func, true, [name: "toto"]})
[true, [name: "toto"]]
Link to this function

extract_function_name(definition)

View Source

Specs

extract_function_name(definition :: tuple() | atom()) :: atom()

Extract the function name from a function definition.

Example:

iex> Umbra.DefinitionExtractor.extract_function_name(quote do: :my_func)
:my_func

iex> Umbra.DefinitionExtractor.extract_function_name(quote do: {:toto, a})
:toto

iex> Umbra.DefinitionExtractor.extract_function_name(quote do: {:my_func})
:my_func
Link to this function

extract_inner_arguments_for_call(definition)

View Source

Specs

extract_inner_arguments_for_call(definition :: tuple() | atom()) :: [tuple()]

Extract the argument used in call from an argument declaration.

Example:

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: a)
{:a, [], UmbraTest}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: [] = my_array)
{:my_array, [], UmbraTest}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: %{} = my_struct)
{:my_struct, [], UmbraTest}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: 32 = my_number)
{:my_number, [], UmbraTest}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: :test = my_atom)
{:my_atom, [], UmbraTest}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: "test" = my_string)
{:my_string, [], UmbraTest}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: nil)
nil

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: 42)
42

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: [])
[]

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: [head | tail])
[{:|, [], [{:head, [], UmbraTest}, {:tail, [], UmbraTest}]}]

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: [name: "toto"])
[name: "toto"]

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: %{})
{:%{}, [], []}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: %{name: "toto"})
{:%{}, [], [name: "toto"]}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: %{"name" => "toto"})
{:%{}, [], [{"name", "toto"}]}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: :test)
:test

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: "test")
"test"

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: %Test{})
{:%, [], [{:__aliases__, [alias: false], [:Test]}, {:%{}, [], []}]}

iex> Umbra.DefinitionExtractor.extract_inner_arguments_for_call(quote do: toto())
** (ArgumentError) invalid argument declaration
Link to this function

generate_client_call_args(definition)

View Source

Specs

generate_client_call_args(tuple()) :: tuple()

Generate the client call arguments but avoid extra/useless variable.

Link to this function

generate_client_definition_args(definition)

View Source

Specs

generate_client_definition_args(tuple()) :: tuple()

Generate the client definition arguments.

They still quite untouched, only unused variable for the server call are automatically shadowed.

Link to this function

generate_handler_tuple(function_name, arguments)

View Source

Specs

generate_handler_tuple(atom(), [tuple()]) :: tuple()

Generate the GenServer parameter containing the message and the parameters.

Link to this function

generate_server_definition_args(type, definition, options)

View Source

Specs

generate_server_definition_args(atom(), tuple(), list()) :: [tuple()]

Generate the server definition arguments.

They still untouched from how the user defines it.

This macro help know if the given atom is an operator.

Example:

iex> Umbra.DefinitionExtractor.is_op?(:%{}) true

iex> Umbra.DefinitionExtractor.is_op?(:%) true

iex> Umbra.DefinitionExtractor.is_op?(:toto) false

Link to this macro

is_var_name?(arg_name)

View Source (macro)

This macro help know if the given atom is a valid name for functions or variables. It should be used in when guards.

Example:

iex> Umbra.DefinitionExtractor.is_var_name?(:toto) true

iex> Umbra.DefinitionExtractor.is_var_name?(:tata) true

iex> Umbra.DefinitionExtractor.is_var_name?(:%{}) false

Link to this function

shadow_arguments(definition)

View Source

Specs

shadow_arguments(definition :: tuple() | atom()) :: [tuple()]

Shadow inner argument declaration used for client function declaration.

Example:

iex> Umbra.DefinitionExtractor.shadow_arguments(quote do: 42 = a)
{:=, [], [42, {:a, [], UmbraTest}]}

iex> Umbra.DefinitionExtractor.shadow_arguments(quote do: %{test: test} = a)
{:=, [], [{:%{}, [], [test: {:_test, [], UmbraTest}]}, {:a, [], UmbraTest}]}

iex> Umbra.DefinitionExtractor.shadow_arguments(quote do: [head | tail] = a)
{:=, [], [[{:|, '', [{:_head, [], UmbraTest}, {:_tail, '', UmbraTest}]}], {:a, '', UmbraTest}]}

iex> Umbra.DefinitionExtractor.shadow_arguments(quote do: [head | tail])
[{:|, [], [{:head, [], UmbraTest}, {:tail, '', UmbraTest}]}]

iex> Umbra.DefinitionExtractor.shadow_arguments(quote do: [head | tail])
[{:|, [], [{:head, [], UmbraTest}, {:tail, [], UmbraTest}]}]
Link to this function

shadow_inner_arguments(definition)

View Source

Specs

shadow_inner_arguments(definition :: tuple() | atom()) :: [tuple()]

Shadow left value in argument declaration/assignment used for client function declaration.

Example:

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: 42)
42

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: %{test: test})
{:%{}, [], [test: {:_test, [], UmbraTest}]}

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: [])
[]

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: [head | tail])
[{:|, '', [{:_head, [], UmbraTest}, {:_tail, '', UmbraTest}]}]

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: %Test{id: 42})
{:%, [], [{:__aliases__, [alias: false], [:Test]}, {:%{}, [], [id: 42]}]}

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: %Test{id: id})
{:%, [], [{:__aliases__, [alias: false], [:Test]}, {:%{}, [], [id: {:_id, [], UmbraTest}]}]}

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: [true = tata, false = toto])
[{:=, [], [true, {:_tata, [], UmbraTest}]}, {:=, [], [false, {:_toto, [], UmbraTest}]}]

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: [[true = tata, false = toto] | others])
[{:|, [], [[{:=, [], [true, {:_tata, [], UmbraTest}]}, {:=, [], [false, {:_toto, [], UmbraTest}]}], {:_others, [], UmbraTest}]}]

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: <<32 :: a>>)
{:<<>>, [], [{:"::", [], [32, {:_a, [], UmbraTest}]}]}

iex> Umbra.DefinitionExtractor.shadow_inner_arguments(quote do: %{id: _id, name: _name})
{:%{}, '', [{:id, {:_id, [], UmbraTest}}, {:name, {:_name, [], UmbraTest}}]}