Skip to content

Commit

Permalink
fix: handling struct nested values properly
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed Nov 27, 2024
1 parent d5c95e7 commit 2f74b3a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
4 changes: 4 additions & 0 deletions apps/one_piece_commanded/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## v0.21.1 - 2024-11-26

- Fix casting structs in `OnePiece.Commanded.ValueObject`. Related to https://github.com/elixir-ecto/ecto/issues/4168

## v0.21.0 - 2024-11-02

- Added `OnePiece.Commanded.Enum` module.
Expand Down
28 changes: 11 additions & 17 deletions apps/one_piece_commanded/lib/one_piece/commanded/value_object.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,33 +140,27 @@ defmodule OnePiece.Commanded.ValueObject do

changeset =
message
|> Changeset.cast(attrs, fields -- embeds)
|> Changeset.cast(from_struct(attrs), fields -- embeds)
|> Changeset.validate_required(struct_module.__enforced_keys__() -- embeds)

Enum.reduce(
embeds,
changeset,
&cast_embed(&1, &2, struct_module, attrs)
&cast_embed(&1, &2, struct_module)
)
end

defp cast_embed(field, changeset, struct_module, attrs) do
case is_struct(attrs[field]) do
false ->
Changeset.cast_embed(changeset, field, required: struct_module.__enforced_keys__?(field))

true ->
# credo:disable-for-next-line Credo.Check.Design.TagTODO
# TODO: Validate that the struct is of the correct type.
# It may be the case that you passed a completely different struct as the value. We could `cast_embed`
# always and fix the `Changeset.cast(attrs, fields -- embeds)` by converting the `attrs` into a map. But it
# would be a bit more expensive since it will run the casting for a field that was already casted.
# Checking the struct types MAY be enough but taking into consideration `embeds_many` could complicated
# things. For now, we'll just assume that the user knows what they're doing.
Changeset.put_change(changeset, field, attrs[field])
end
defp cast_embed(field, changeset, struct_module) do
Changeset.cast_embed(changeset, field, required: struct_module.__enforced_keys__?(field))
end

defp from_struct(value) when is_struct(value) do
# https://github.com/elixir-ecto/ecto/issues/4168
Map.from_struct(value)
end

defp from_struct(value), do: value

defp apply_changeset(struct_module, attrs) do
struct(struct_module)
|> struct_module.changeset(attrs)
Expand Down
2 changes: 1 addition & 1 deletion apps/one_piece_commanded/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule OnePiece.Commanded.MixProject do
use Mix.Project

@app :one_piece_commanded
@version "0.21.0"
@version "0.21.1"
@elixir_version "~> 1.13"
@source_url "https://github.com/straw-hat-team/beam-monorepo"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ defmodule OnePiece.Commanded.ValueObjectTest do
TestSupport.MessageThree.new(%{target: %{title: "Hello, World!"}})
end

test "bypass casting structs" do
test "casting structs" do
assert {:ok, %TestSupport.MessageThree{target: %TestSupport.MessageOne{title: "Hello, World!"}}} =
TestSupport.MessageThree.new(%{target: %TestSupport.MessageOne{title: "Hello, World!"}})

assert {:ok,
%TestSupport.MessageFour{
targets: [%TestSupport.MessageThree{target: %TestSupport.MessageOne{title: "Hello, World!"}}]
}} =
TestSupport.MessageFour.new(%{
targets: [%TestSupport.MessageThree{target: %TestSupport.MessageOne{title: "Hello, World!"}}]
})
end

test "validates casting embed fields with a wrong value" do
Expand Down
11 changes: 11 additions & 0 deletions apps/one_piece_commanded/test/support/test_support.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ defmodule TestSupport do
end
end

defmodule MessageFour do
@moduledoc false

use OnePiece.Commanded.ValueObject

@enforce_keys [:targets]
embedded_schema do
embeds_many(:targets, MessageThree)
end
end

defmodule MyEntityOne do
@moduledoc false

Expand Down

0 comments on commit 2f74b3a

Please sign in to comment.