@@ -67,7 +67,8 @@ defprotocol Inspect do
67
67
* `:optional` - (since v1.14.0) a list of fields that should not be
68
68
included when they match their default value. This can be used to
69
69
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.
71
72
72
73
Whenever `:only` or `:except` are used to restrict fields,
73
74
the struct will be printed using the `#User<...>` notation,
@@ -159,11 +160,19 @@ defprotocol Inspect do
159
160
160
161
only = Keyword . get ( options , :only , fields )
161
162
except = Keyword . get ( options , :except , [ ] )
162
- optional = Keyword . get ( options , :optional , [ ] )
163
163
164
164
:ok = validate_option ( :only , only , fields , module )
165
165
: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
167
176
168
177
inspect_module =
169
178
if fields == Enum . sort ( only ) and except == [ ] do
@@ -226,6 +235,13 @@ defprotocol Inspect do
226
235
end
227
236
228
237
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
+
229
245
case option_list -- fields do
230
246
[ ] ->
231
247
:ok
0 commit comments