Skip to content

fix: avoid enforcing the field when default is unset and enforce is false #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.1.2 (2024-06-29)

### Bug Fixes
- avoid enforcing the field when default is unset and enforce is false

## 0.1.1 (2024-06-28)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add `:typed_structor` to the list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:typed_structor, "~> 0.1.1"}
{:typed_structor, "~> 0.1.2"}
]
end
```
Expand Down
10 changes: 3 additions & 7 deletions lib/typed_structor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ defmodule TypedStructor do
name = Keyword.fetch!(field, :name)
default = Keyword.get(field, :default)

if TypedStructor.__is_enforced__?(field) do
if Keyword.get(field, :enforce, false) do
{{name, default}, [name | acc]}
else
{{name, default}, acc}
Expand Down Expand Up @@ -251,7 +251,7 @@ defmodule TypedStructor do
name = Keyword.fetch!(field, :name)
type = Keyword.fetch!(field, :type)

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

enforced_fields =
@__ts_definition__.fields
|> Stream.filter(&TypedStructor.__is_enforced__?/1)
|> Stream.filter(&Keyword.get(&1, :enforce, false))
|> Stream.map(&Keyword.fetch!(&1, :name))
|> Enum.to_list()

Expand Down Expand Up @@ -347,8 +347,4 @@ defmodule TypedStructor do
)
end
end

def __is_enforced__?(field) do
Keyword.get(field, :enforce, false) or Keyword.has_key?(field, :default)
end
end
2 changes: 1 addition & 1 deletion test/reflection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule ReflectionTest do
assert [:age] === Struct.__typed_structor__(:parameters)
assert [] === MyModule.Struct.__typed_structor__(:parameters)

assert [:name, :age] === Struct.__typed_structor__(:enforced_fields)
assert [:name] === Struct.__typed_structor__(:enforced_fields)
assert [:name, :age] === MyModule.Struct.__typed_structor__(:enforced_fields)

assert "String.t()" === Macro.to_string(Struct.__typed_structor__(:type, :name))
Expand Down
8 changes: 8 additions & 0 deletions test/typed_structor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ defmodule TypedStructorTest do
field :name, String.t()
field :age, integer(), enforce: false
end

def enforce_keys, do: @enforce_keys
end

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

assert [:name] === TestModule.enforce_keys()

assert expected_types === types(bytecode)
end
end
Expand Down Expand Up @@ -276,6 +280,8 @@ defmodule TypedStructorTest do
field :name, String.t(), default: "Phil"
field :age, integer()
end

def enforce_keys, do: @enforce_keys
end

assert match?(
Expand All @@ -287,6 +293,8 @@ defmodule TypedStructorTest do
struct(TestModule)
)

assert [] === TestModule.enforce_keys()

assert expected_types === types(bytecode)
end
end
Expand Down