Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renders ETS as tuples #90

Merged
merged 3 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions lib/kino/ets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ defmodule Kino.ETS do

@behaviour Kino.Table

alias Kino.Utils

@type t :: Kino.JS.Live.t()

@doc """
Expand Down Expand Up @@ -51,9 +49,9 @@ defmodule Kino.ETS do
@impl true
def get_data(rows_spec, state) do
records = get_records(state.tid, rows_spec)
rows = Enum.map(records, &record_to_row/1)
rows = Enum.map(records, fn record -> %{fields: %{0 => inspect(record)}} end)
total_rows = :ets.info(state.tid, :size)
columns = records |> Utils.Table.keys_for_records() |> Utils.Table.keys_to_columns()
columns = [%{key: 0, label: "row", type: "tuple"}]
{:ok, %{columns: columns, rows: rows, total_rows: total_rows}, state}
end

Expand All @@ -69,14 +67,4 @@ defmodule Kino.ETS do
:qlc.delete_cursor(cursor)
records
end

defp record_to_row(record) do
fields =
record
|> Tuple.to_list()
|> Enum.with_index()
|> Map.new(fn {val, idx} -> {idx, inspect(val)} end)

%{fields: fields}
end
end
20 changes: 8 additions & 12 deletions test/kino/ets_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Kino.ETSTest do

assert %{
content: %{
columns: [],
columns: [%{key: "0", label: "row", type: "tuple"}],
rows: []
}
} = data
Expand All @@ -62,13 +62,12 @@ defmodule Kino.ETSTest do
assert %{
content: %{
columns: [
%{key: "0", label: "0"},
%{key: "1", label: "1"}
%{key: "0", label: "row", type: "tuple"}
],
rows: [
%{fields: %{"0" => "1", "1" => ~s/"Jake Peralta"/}},
%{fields: %{"0" => "2", "1" => ~s/"Terry Jeffords"/}},
%{fields: %{"0" => "3", "1" => ~s/"Amy Santiago"/}}
%{fields: %{"0" => "{1, \"Jake Peralta\"}"}},
%{fields: %{"0" => "{2, \"Terry Jeffords\"}"}},
%{fields: %{"0" => "{3, \"Amy Santiago\"}"}}
],
page: 1,
max_page: 1,
Expand All @@ -92,10 +91,7 @@ defmodule Kino.ETSTest do
assert %{
content: %{
columns: [
%{key: "0", label: "0"},
%{key: "1", label: "1"},
%{key: "2", label: "2"},
%{key: "3", label: "3"}
%{key: "0", label: "row", type: "tuple"}
]
}
} = data
Expand All @@ -113,7 +109,7 @@ defmodule Kino.ETSTest do
content: %{
page: 1,
max_page: 3,
rows: [%{fields: %{"0" => "1"}} | _]
rows: [%{fields: %{"0" => "{1}"}} | _]
}
} = data

Expand All @@ -122,7 +118,7 @@ defmodule Kino.ETSTest do
assert_broadcast_event(widget, "update_content", %{
page: 2,
max_page: 3,
rows: [%{fields: %{"0" => "11"}} | _]
rows: [%{fields: %{"0" => "{11}"}} | _]
})
end
end