Skip to content

Allow optional: :all when deriving Inspect #14400

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

Merged
merged 2 commits into from
Apr 5, 2025
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
22 changes: 19 additions & 3 deletions lib/elixir/lib/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ defprotocol Inspect do
* `:optional` - (since v1.14.0) a list of fields that should not be
included when they match their default value. This can be used to
simplify the struct representation at the cost of hiding
information.
information. Since v1.19.0, the `:all` atom can be passed to
mark all fields as optional.

Whenever `:only` or `:except` are used to restrict fields,
the struct will be printed using the `#User<...>` notation,
Expand Down Expand Up @@ -159,11 +160,19 @@ defprotocol Inspect do

only = Keyword.get(options, :only, fields)
except = Keyword.get(options, :except, [])
optional = Keyword.get(options, :optional, [])

:ok = validate_option(:only, only, fields, module)
:ok = validate_option(:except, except, fields, module)
:ok = validate_option(:optional, optional, fields, module)

optional =
case Keyword.get(options, :optional, []) do
:all ->
fields

optional ->
:ok = validate_option(:optional, optional, fields, module)
optional
end

inspect_module =
if fields == Enum.sort(only) and except == [] do
Expand Down Expand Up @@ -226,6 +235,13 @@ defprotocol Inspect do
end

defp validate_option(option, option_list, fields, module) do
if not is_list(option_list) do
raise ArgumentError,
"invalid value #{Kernel.inspect(option_list)} in #{Kernel.inspect(option)} " <>
"when deriving the Inspect protocol for #{Kernel.inspect(module)} " <>
"(expected a list)"
end

case option_list -- fields do
[] ->
:ok
Expand Down
25 changes: 25 additions & 0 deletions lib/elixir/test/elixir/inspect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,17 @@ defmodule Inspect.MapTest do
end
end

test "passing a non-list to the :only option" do
assert_raise ArgumentError,
"invalid value :not_a_list in :only when deriving the Inspect protocol for Inspect.MapTest.StructInvalidListInOnlyOption (expected a list)",
fn ->
defmodule StructInvalidListInOnlyOption do
@derive {Inspect, only: :not_a_list}
defstruct [:a, :b]
end
end
end

defmodule StructWithExceptOption do
@derive {Inspect, except: [:b, :c]}
defstruct [:a, :b, :c, :d]
Expand Down Expand Up @@ -772,6 +783,20 @@ defmodule Inspect.MapTest do
assert inspect(struct) ==
"#Inspect.MapTest.StructWithExceptOptionalAndOrder<d: nil, a: nil, ...>"
end

defmodule StructWithOptionalAll do
@derive {Inspect, optional: :all}
defstruct [:a, :b, :c, :d]
end

test "struct with :optional set to :all" do
struct = %StructWithOptionalAll{a: 1, b: 2}

assert inspect(struct) == "%Inspect.MapTest.StructWithOptionalAll{a: 1, b: 2}"

struct = %StructWithOptionalAll{}
assert inspect(struct) == "%Inspect.MapTest.StructWithOptionalAll{}"
end
end

defmodule Inspect.OthersTest do
Expand Down
Loading