@@ -67,7 +67,8 @@ defprotocol Inspect do
6767 * `:optional` - (since v1.14.0) a list of fields that should not be
6868 included when they match their default value. This can be used to
6969 simplify the struct representation at the cost of hiding
70- information.
70+ information. Since v1.19.0, the `:all` atom can be passed to
71+ mark all fields as optional.
7172
7273 Whenever `:only` or `:except` are used to restrict fields,
7374 the struct will be printed using the `#User<...>` notation,
@@ -159,11 +160,19 @@ defprotocol Inspect do
159160
160161 only = Keyword . get ( options , :only , fields )
161162 except = Keyword . get ( options , :except , [ ] )
162- optional = Keyword . get ( options , :optional , [ ] )
163163
164164 :ok = validate_option ( :only , only , fields , module )
165165 :ok = validate_option ( :except , except , fields , module )
166- :ok = validate_option ( :optional , optional , fields , module )
166+
167+ optional =
168+ case Keyword . get ( options , :optional , [ ] ) do
169+ :all ->
170+ fields
171+
172+ optional ->
173+ :ok = validate_option ( :optional , optional , fields , module )
174+ optional
175+ end
167176
168177 inspect_module =
169178 if fields == Enum . sort ( only ) and except == [ ] do
@@ -226,6 +235,13 @@ defprotocol Inspect do
226235 end
227236
228237 defp validate_option ( option , option_list , fields , module ) do
238+ if not is_list ( option_list ) do
239+ raise ArgumentError ,
240+ "invalid value #{ Kernel . inspect ( option_list ) } in #{ Kernel . inspect ( option ) } " <>
241+ "when deriving the Inspect protocol for #{ Kernel . inspect ( module ) } " <>
242+ "(expected a list)"
243+ end
244+
229245 case option_list -- fields do
230246 [ ] ->
231247 :ok
0 commit comments