Skip to content

Commit 7c56a02

Browse files
author
José Valim
committed
Allow :macros and :includes to be given to Record.extract, closes #7195
1 parent e442bc6 commit 7c56a02

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

lib/elixir/lib/record.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,20 @@ defmodule Record do
5454
that contains the record definition to extract; with this option, this
5555
function uses the same path lookup used by the `-include` attribute used in
5656
Erlang modules.
57+
5758
* `:from_lib` - (binary representing a path to a file) path to the Erlang
5859
file that contains the record definition to extract; with this option,
5960
this function uses the same path lookup used by the `-include_lib`
6061
attribute used in Erlang modules.
6162
63+
* `:includes` - (a list of directories as binaries) if the record being
64+
extracted depends on relative includes, this option allows developers
65+
to specify the directory those relative includes exist
66+
67+
* `:macros` - (keyword list of macro names and values) if the record
68+
being extract depends on the values of macros, this option allows
69+
the value of those macros to be set
70+
6271
These options are expected to be literals (including the binary values) at
6372
compile time.
6473

lib/elixir/lib/record/extractor.ex

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
defmodule Record.Extractor do
22
@moduledoc false
33

4-
# Retrieve a record definition from an Erlang file using
5-
# the same lookup as the *include* attribute from Erlang modules.
6-
def extract(name, from: file) when is_binary(file) do
7-
extract_record(name, from_file(file))
4+
def extract(name, opts) do
5+
extract_record(name, from_or_from_lib_file(opts))
86
end
97

10-
# Retrieve a record definition from an Erlang file using
11-
# the same lookup as the *include_lib* attribute from Erlang modules.
12-
def extract(name, from_lib: file) when is_binary(file) do
13-
extract_record(name, from_lib_file(file))
8+
def extract_all(opts) do
9+
extract_all_records(from_or_from_lib_file(opts))
1410
end
1511

16-
# Retrieve all records definitions from an Erlang file using
17-
# the same lookup as the *include* attribute from Erlang modules.
18-
def extract_all(from: file) when is_binary(file) do
19-
extract_all_records(from_file(file))
20-
end
12+
defp from_or_from_lib_file(opts) do
13+
cond do
14+
file = opts[:from] ->
15+
{from_file(file), Keyword.delete(opts, :from)}
16+
17+
file = opts[:from_lib] ->
18+
{from_lib_file(file), Keyword.delete(opts, :from_lib)}
2119

22-
# Retrieve all records definitions from an Erlang file using
23-
# the same lookup as the *include_lib* attribute from Erlang modules.
24-
def extract_all(from_lib: file) when is_binary(file) do
25-
extract_all_records(from_lib_file(file))
20+
true ->
21+
raise ArgumentError, "expected :from or :from_lib to be given as option"
22+
end
2623
end
2724

2825
# Find file using the same lookup as the *include* attribute from Erlang modules.
@@ -49,20 +46,23 @@ defmodule Record.Extractor do
4946
end
5047

5148
# Retrieve the record with the given name from the given file
52-
defp extract_record(name, file) do
53-
form = read_file(file)
49+
defp extract_record(name, {file, opts}) do
50+
form = read_file(file, opts)
51+
IO.inspect form
5452
records = extract_records(form)
5553

5654
if record = List.keyfind(records, name, 0) do
5755
parse_record(record, form)
5856
else
59-
raise ArgumentError, "no record #{name} found at #{file}"
57+
raise ArgumentError,
58+
"no record #{name} found at #{file}. Or the record does not exist or " <>
59+
"its entry is malformed or depends on other include files"
6060
end
6161
end
6262

6363
# Retrieve all records from the given file
64-
defp extract_all_records(file) do
65-
form = read_file(file)
64+
defp extract_all_records({file, opts}) do
65+
form = read_file(file, opts)
6666
records = extract_records(form)
6767
for rec = {name, _fields} <- records, do: {name, parse_record(rec, form)}
6868
end
@@ -76,8 +76,8 @@ defmodule Record.Extractor do
7676
# includes record but with macros and other attributes expanded,
7777
# such as "-include(...)" and "-include_lib(...)". This is done
7878
# by using Erlang's epp.
79-
defp read_file(file) do
80-
case :epp.parse_file(file, []) do
79+
defp read_file(file, opts) do
80+
case :epp.parse_file(file, opts) do
8181
{:ok, form} ->
8282
form
8383

0 commit comments

Comments
 (0)