Skip to content

Commit

Permalink
Inspect non-utf8 binaries in Kino.DataTable (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Oct 31, 2023
1 parent 00c75ea commit 18ef92c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/kino/data_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ defmodule Kino.DataTable do
end
end

defp value_to_string(value) when is_binary(value) do
inspect_opts = Inspect.Opts.new([])
if String.printable?(value, inspect_opts.limit), do: value, else: inspect(value)
end

defp value_to_string(value) do
if mod = String.Chars.impl_for(value), do: mod.to_string(value), else: inspect(value)
end
Expand Down
15 changes: 15 additions & 0 deletions test/kino/data_table_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ defmodule Kino.DataTableTest do
} = data
end

test "supports non-utf8 binary values" do
entries = [
binaries: [<<110, 120>>, <<200, 210>>]
]

kino = Kino.DataTable.new(entries)
data = connect(kino)

assert %{
content: %{
data: [["nx"], ["<<200, 210>>"]]
}
} = data
end

test "sends only relevant fields if user-specified keys are given" do
kino = Kino.DataTable.new(@people_entries, keys: [:id])
data = connect(kino)
Expand Down

0 comments on commit 18ef92c

Please sign in to comment.