@@ -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
0 commit comments