You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/elixir/lib/inspect.ex
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -455,6 +455,9 @@ defimpl Inspect, for: Any do
455
455
only=Keyword.get(options,:only,fields)
456
456
except=Keyword.get(options,:except,[])
457
457
458
+
:ok=validate_option(only,fields,module)
459
+
:ok=validate_option(except,fields,module)
460
+
458
461
filtered_fields=
459
462
fields
460
463
|>Enum.reject(&(&1inexcept))
@@ -478,6 +481,17 @@ defimpl Inspect, for: Any do
478
481
end
479
482
end
480
483
484
+
defpvalidate_option(option_list,fields,module)do
485
+
caseoption_list--fieldsdo
486
+
[]->
487
+
:ok
488
+
489
+
unknown_fields->
490
+
raiseArgumentError,
491
+
"unknown fields #{Kernel.inspect(unknown_fields)} given when deriving the Inspect protocol for #{Kernel.inspect(module)}. :only and :except values must match struct fields"
"unknown fields [:c] given when deriving the Inspect protocol for Inspect.MapTest.StructMissingFieldsInOnlyOption. :only and :except values must match struct fields",
700
+
fn->
701
+
defmoduleStructMissingFieldsInOnlyOptiondo
702
+
@derive{Inspect,only: [:c]}
703
+
defstruct[:a,:b]
704
+
end
705
+
end
706
+
end
707
+
708
+
test"struct missing fields in the :except option"do
709
+
assert_raiseArgumentError,
710
+
"unknown fields [:c, :d] given when deriving the Inspect protocol for Inspect.MapTest.StructMissingFieldsInExceptOption. :only and :except values must match struct fields",
0 commit comments