Skip to content

Commit 4bd7948

Browse files
Support a reader tuple as an argument in to_rows/to_columns (#12)
1 parent 2133de4 commit 4bd7948

2 files changed

Lines changed: 38 additions & 40 deletions

File tree

lib/table.ex

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ defmodule Table do
5151

5252
@type column :: term()
5353

54-
@type table_info :: %{columns: list(column())}
54+
@type tabular :: Reader.t() | Reader.row_reader() | Reader.column_reader()
5555

5656
@doc """
5757
Accesses tabular data as a sequence of rows.
@@ -73,30 +73,25 @@ defmodule Table do
7373
[%{id: 1, name: "Sherlock"}, %{id: 2, name: "John"}, %{id: 3, name: "Mycroft"}]
7474
7575
"""
76-
@spec to_rows(Reader.t(), keyword()) :: Enumerable.t()
76+
@spec to_rows(tabular(), keyword()) :: Enumerable.t()
7777
def to_rows(tabular, opts \\ []) do
78-
tabular |> to_rows_with_info(opts) |> elem(0)
79-
end
80-
81-
@doc """
82-
Same as `to_rows/2`, extended with information about the table.
83-
84-
## Examples
78+
only = opts[:only] && MapSet.new(opts[:only])
8579

86-
iex> data = %{id: [1, 2, 3], name: ["Sherlock", "John", "Mycroft"]}
87-
iex> {_rows, info} = Table.to_rows_with_info(data)
88-
iex> info
89-
%{columns: [:id, :name]}
80+
tabular
81+
|> init_reader!()
82+
|> read_rows(only)
83+
end
9084

91-
"""
92-
@spec to_rows_with_info(Reader.t(), keyword()) :: {Enumerable.t(), table_info()}
85+
# TODO: remove in v0.2
86+
@deprecated "Use Table.Reader.init/1 to get reader with metadata, then pass the reader to Table.to_rows/2"
9387
def to_rows_with_info(tabular, opts \\ []) do
94-
only = opts[:only] && MapSet.new(opts[:only])
95-
96-
reader = init_reader!(tabular)
97-
{read_rows(reader, only), get_info(reader)}
88+
reader = {_, meta, _} = Table.Reader.init(tabular)
89+
{to_rows(reader, opts), meta}
9890
end
9991

92+
defp init_reader!({:rows, %{}, _} = reader), do: reader
93+
defp init_reader!({:columns, %{}, _} = reader), do: reader
94+
10095
defp init_reader!(tabular) do
10196
with :none <- Reader.init(tabular) do
10297
raise ArgumentError, "expected valid tabular data, but got: #{inspect(tabular)}"
@@ -141,29 +136,20 @@ defmodule Table do
141136
["Sherlock", "John", "Mycroft"]
142137
143138
"""
144-
@spec to_columns(Reader.t(), keyword()) :: %{column() => Enumerable.t()}
139+
@spec to_columns(tabular(), keyword()) :: %{column() => Enumerable.t()}
145140
def to_columns(tabular, opts \\ []) do
146-
tabular |> to_columns_with_info(opts) |> elem(0)
147-
end
148-
149-
@doc """
150-
Same as `to_columns/2`, extended with information about the table.
151-
152-
## Examples
141+
only = opts[:only] && MapSet.new(opts[:only])
153142

154-
iex> data = [%{id: 1, name: "Sherlock"}, %{id: 2, name: "John"}, %{id: 3, name: "Mycroft"}]
155-
iex> {_columns, info} = Table.to_columns_with_info(data)
156-
iex> info
157-
%{columns: [:id, :name]}
143+
tabular
144+
|> init_reader!()
145+
|> read_columns(only)
146+
end
158147

159-
"""
160-
@spec to_columns_with_info(Reader.t(), keyword()) ::
161-
{%{column() => Enumerable.t()}, table_info()}
148+
# TODO: remove in v0.2
149+
@deprecated "Use Table.Reader.init/1 to get reader with metadata, then pass the reader to Table.to_columns/2"
162150
def to_columns_with_info(tabular, opts \\ []) do
163-
only = opts[:only] && MapSet.new(opts[:only])
164-
165-
reader = init_reader!(tabular)
166-
{read_columns(reader, only), get_info(reader)}
151+
reader = {_, meta, _} = Table.Reader.init(tabular)
152+
{to_columns(reader, opts), meta}
167153
end
168154

169155
defp read_columns({:columns, meta, enum}, only) do
@@ -204,8 +190,6 @@ defmodule Table do
204190
defp include_column?(nil, _column), do: true
205191
defp include_column?(only, column), do: MapSet.member?(only, column)
206192

207-
defp get_info({_, %{columns: columns}, _}), do: %{columns: columns}
208-
209193
# --- Backports ---
210194

211195
# TODO: remove once we require Elixir v1.12

test/table_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ defmodule TableTest do
4747
%{"name" => "John"}
4848
]
4949
end
50+
51+
test "accepts initialized reader" do
52+
assert @row_data |> Table.Reader.init() |> Table.to_rows() |> Enum.to_list() == [
53+
%{"id" => 1, "name" => "Sherlock"},
54+
%{"id" => 2, "name" => "John"}
55+
]
56+
end
5057
end
5158

5259
describe "to_columns/1" do
@@ -81,6 +88,13 @@ defmodule TableTest do
8188
"name" => ["Sherlock", "John"]
8289
}
8390
end
91+
92+
test "accepts initialized reader" do
93+
assert @row_data |> Table.Reader.init() |> Table.to_columns() |> enumerate_columns() == %{
94+
"id" => [1, 2],
95+
"name" => ["Sherlock", "John"]
96+
}
97+
end
8498
end
8599

86100
defp enumerate_columns(%{} = columns) do

0 commit comments

Comments
 (0)