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
Specs
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
Specs
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"]]
Specs
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
Specs
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
Specs
Generate the client call arguments but avoid extra/useless variable.
Specs
Generate the client definition arguments.
They still quite untouched, only unused variable for the server call are automatically shadowed.
Specs
Generate the GenServer
parameter containing the message and the parameters.
Specs
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
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
Specs
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}]}]
Specs
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}}]}