Skip to content

Commit

Permalink
feat: allow aldeady casted structs (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed Mar 14, 2023
1 parent bbe6189 commit 06ec947
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.14.2
erlang 25.2
elixir 1.14.3
erlang 25.3
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.15.2 - 2023-03-14

- Fix casting already cast structs in `OnePiece.Commanded.ValueObject`.

## v0.15.1 - 2022-12-24

- Fix `OnePiece.Commanded.Event` and ``OnePiece.Commanded.Entity` to extend from `OnePiece.Commanded.ValueObject`.
Expand Down
23 changes: 20 additions & 3 deletions apps/one_piece_commanded/lib/one_piece/commanded/value_object.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,37 @@ defmodule OnePiece.Commanded.ValueObject do

def __changeset__(%struct_module{} = message, attrs) do
embeds = struct_module.__schema__(:embeds)
allowed = struct_module.__schema__(:fields) -- embeds
fields = struct_module.__schema__(:fields)

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

Enum.reduce(
embeds,
changeset,
&Changeset.cast_embed(&2, &1, required: struct_module.__enforced_keys__?(&1))
&cast_embed(&1, &2, struct_module, attrs)
)
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
end

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.15.1"
@version "0.15.2"
@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,6 +21,11 @@ defmodule OnePiece.Commanded.ValueObjectTest do
TestSupport.MessageThree.new(%{target: %{title: "Hello, World!"}})
end

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

test "validates casting embed fields with a wrong value" do
{:error, changeset} = TestSupport.MessageThree.new(%{target: "a wrong value"})
assert %{target: ["is invalid"]} = TestSupport.errors_on(changeset)
Expand Down

0 comments on commit 06ec947

Please sign in to comment.