Skip to content

Commit 817ffb4

Browse files
committed
✨ Add allow_duplicate options
1 parent c34a522 commit 817ffb4

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ If you defined custom guards like:
3232
- defguard is_color_value(value) when value in color(:__values__)
3333
```
3434

35-
You can remove them entirely, they are now generated automatically:
35+
You can remove them entirely, they are now generated automatically.
36+
37+
The following guards are now generated automatically:
3638

3739
- `is_color/1` - checks if a value is a valid key or value
3840
- `is_color_key/1` - checks if a value is a valid key
@@ -44,6 +46,8 @@ You can remove them entirely, they are now generated automatically:
4446
for compile-time access to enum data.
4547
- **Guards**: `is_name/1`, `is_name_key/1`, `is_name_value/1` guard macros
4648
generated automatically for each enum.
49+
- **Options**: `allow_duplicate_keys` and `allow_duplicate_values` for
50+
fine-grained control over duplicate validation.
4751

4852
### Improvements
4953

lib/simple_enum.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ defmodule SimpleEnum do
2323
[integer-based Enumeration](guides/integer_based_enum.md), you can check the
2424
corresponding guide.
2525
26+
## Options
27+
28+
* `:allow_duplicate_keys` - when `true`, allows duplicate keys
29+
in the enumerators list. Defaults to `false`.
30+
* `:allow_duplicate_values` - when `true`, allows duplicate values
31+
in the enumerators list. Defaults to `false`.
32+
2633
The following macros are generated:
2734
2835
* `name/1` for bidirectional key/value lookup
@@ -102,11 +109,14 @@ defmodule SimpleEnum do
102109
fields = kv_to_fields(expanded_kv, enum_name, __CALLER__)
103110
keys = Keyword.keys(fields)
104111
values = Keyword.values(fields)
105-
allow_duplicate = Keyword.get(opts, :allow_duplicate, false)
112+
allow_duplicate_keys = Keyword.get(opts, :allow_duplicate_keys, false)
113+
allow_duplicate_values = Keyword.get(opts, :allow_duplicate_values, false)
106114

107-
raise_if_duplicate!("key", keys, enum_name, __CALLER__)
115+
if !allow_duplicate_keys do
116+
raise_if_duplicate!("key", keys, enum_name, __CALLER__)
117+
end
108118

109-
if not allow_duplicate do
119+
if !allow_duplicate_values do
110120
raise_if_duplicate!("value", values, enum_name, __CALLER__)
111121
end
112122

test/simple_enum_test.exs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,24 @@ defmodule SimpleEnumTest do
207207
defmodule DuplicateAllowedValuesEnum do
208208
import SimpleEnum, only: [defenum: 3]
209209
210-
defenum :test, [allow_duplicate: true], [{:a, 1}, :b, :c, {:d, 3}]
210+
defenum :test, [allow_duplicate_values: true], [{:a, 1}, :b, :c, {:d, 3}]
211211
end
212212
"""
213213

214214
assert [{DuplicateAllowedValuesEnum, _}] = Code.compile_string(code)
215215
end
216+
217+
test "duplicate keys are allowed" do
218+
code = """
219+
defmodule DuplicateAllowedKeysEnum do
220+
import SimpleEnum, only: [defenum: 3]
221+
222+
defenum :test, [allow_duplicate_keys: true], ~w(a b c a)a
223+
end
224+
"""
225+
226+
assert [{DuplicateAllowedKeysEnum, _}] = Code.compile_string(code)
227+
end
216228
end
217229

218230
describe "helpers" do

0 commit comments

Comments
 (0)