Skip to content

Commit 03085dc

Browse files
authored
fix: avoid enforcing the field when default is unset and enforce is false (#3)
1 parent 6ec153c commit 03085dc

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.1.2 (2024-06-29)
4+
5+
### Bug Fixes
6+
- avoid enforcing the field when default is unset and enforce is false
7+
38
## 0.1.1 (2024-06-28)
49

510
### Bug Fixes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Add `:typed_structor` to the list of dependencies in `mix.exs`:
1616
```elixir
1717
def deps do
1818
[
19-
{:typed_structor, "~> 0.1.1"}
19+
{:typed_structor, "~> 0.1.2"}
2020
]
2121
end
2222
```

lib/typed_structor.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ defmodule TypedStructor do
221221
name = Keyword.fetch!(field, :name)
222222
default = Keyword.get(field, :default)
223223

224-
if TypedStructor.__is_enforced__?(field) do
224+
if Keyword.get(field, :enforce, false) do
225225
{{name, default}, [name | acc]}
226226
else
227227
{{name, default}, acc}
@@ -251,7 +251,7 @@ defmodule TypedStructor do
251251
name = Keyword.fetch!(field, :name)
252252
type = Keyword.fetch!(field, :type)
253253

254-
if TypedStructor.__is_enforced__?(field) do
254+
if Keyword.get(field, :enforce, false) or Keyword.has_key?(field, :default) do
255255
[{name, type} | acc]
256256
else
257257
[{name, quote(do: unquote(type) | nil)} | acc]
@@ -288,7 +288,7 @@ defmodule TypedStructor do
288288

289289
enforced_fields =
290290
@__ts_definition__.fields
291-
|> Stream.filter(&TypedStructor.__is_enforced__?/1)
291+
|> Stream.filter(&Keyword.get(&1, :enforce, false))
292292
|> Stream.map(&Keyword.fetch!(&1, :name))
293293
|> Enum.to_list()
294294

@@ -347,8 +347,4 @@ defmodule TypedStructor do
347347
)
348348
end
349349
end
350-
351-
def __is_enforced__?(field) do
352-
Keyword.get(field, :enforce, false) or Keyword.has_key?(field, :default)
353-
end
354350
end

test/reflection_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule ReflectionTest do
2828
assert [:age] === Struct.__typed_structor__(:parameters)
2929
assert [] === MyModule.Struct.__typed_structor__(:parameters)
3030

31-
assert [:name, :age] === Struct.__typed_structor__(:enforced_fields)
31+
assert [:name] === Struct.__typed_structor__(:enforced_fields)
3232
assert [:name, :age] === MyModule.Struct.__typed_structor__(:enforced_fields)
3333

3434
assert "String.t()" === Macro.to_string(Struct.__typed_structor__(:type, :name))

test/typed_structor_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,16 @@ defmodule TypedStructorTest do
156156
field :name, String.t()
157157
field :age, integer(), enforce: false
158158
end
159+
160+
def enforce_keys, do: @enforce_keys
159161
end
160162

161163
assert_raise_on_enforce_error(TestModule, [:name], fn ->
162164
Code.eval_quoted(quote do: %TestModule{})
163165
end)
164166

167+
assert [:name] === TestModule.enforce_keys()
168+
165169
assert expected_types === types(bytecode)
166170
end
167171
end
@@ -276,6 +280,8 @@ defmodule TypedStructorTest do
276280
field :name, String.t(), default: "Phil"
277281
field :age, integer()
278282
end
283+
284+
def enforce_keys, do: @enforce_keys
279285
end
280286

281287
assert match?(
@@ -287,6 +293,8 @@ defmodule TypedStructorTest do
287293
struct(TestModule)
288294
)
289295

296+
assert [] === TestModule.enforce_keys()
297+
290298
assert expected_types === types(bytecode)
291299
end
292300
end

0 commit comments

Comments
 (0)