Skip to content

Commit 96e212d

Browse files
authored
feat(trogon_commanded): add is_object_id/1 guard (#329)
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
1 parent 83d42a6 commit 96e212d

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎apps/trogon_commanded/lib/trogon/commanded/object_id.ex‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ defmodule Trogon.Commanded.ObjectId do
6060
alias Trogon.Commanded.ProtoExtension
6161
alias TrogonProto.ObjectId.V1Alpha1.EnumValueOptions, as: ObjectIdEnumValueOptions
6262

63+
@doc """
64+
Returns true if `term` is an ObjectId struct; otherwise returns false.
65+
66+
Allowed in guard tests.
67+
68+
## Examples
69+
70+
iex> is_object_id(%MyApp.UserId{id: "abc-123"})
71+
true
72+
73+
iex> is_object_id(%{id: "abc-123"})
74+
false
75+
"""
76+
defguard is_object_id(term)
77+
when is_map(term) and :erlang.is_map_key(:__object_id__, term) and
78+
:erlang.map_get(:__object_id__, term) == true
79+
6380
@proto_extension_tag 870_010
6481
@proto_extension_name "trogon.object_id.v1alpha1.enum_value"
6582
@default_separator "_"
@@ -204,9 +221,9 @@ defmodule Trogon.Commanded.ObjectId do
204221

205222
defp __generated_struct_and_metadata__(object_type, prefix) do
206223
quote location: :keep do
207-
@type t :: %__MODULE__{id: binary()}
224+
@type t :: %__MODULE__{id: binary(), __object_id__: true}
208225

209-
defstruct [:id]
226+
defstruct id: nil, __object_id__: true
210227

211228
@doc false
212229
@spec object_type() :: String.t()

‎apps/trogon_commanded/test/trogon/commanded/object_id_test.exs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Trogon.Commanded.ObjectIdTest do
22
use ExUnit.Case
33

4+
import Trogon.Commanded.ObjectId
5+
46
alias TestSupport
57

68
@test_uuid "test-value-1"
@@ -626,4 +628,47 @@ defmodule Trogon.Commanded.ObjectIdTest do
626628
assert parsed.id == "abc-123"
627629
end
628630
end
631+
632+
describe "is_object_id/1" do
633+
test "returns true for an ObjectId struct" do
634+
user_id = TestSupport.UserId.new!("abc-123")
635+
assert is_object_id(user_id)
636+
end
637+
638+
test "returns false for a plain map" do
639+
refute is_object_id(%{id: "abc-123"})
640+
end
641+
642+
test "returns false for a non-ObjectId struct" do
643+
refute is_object_id(%URI{path: "/"})
644+
end
645+
646+
test "returns false for non-map values" do
647+
refute is_object_id("user_abc-123")
648+
refute is_object_id(123)
649+
refute is_object_id(nil)
650+
end
651+
652+
test "works in guard clauses" do
653+
user_id = TestSupport.UserId.new!("abc-123")
654+
655+
result =
656+
case user_id do
657+
x when is_object_id(x) -> :object_id
658+
_ -> :other
659+
end
660+
661+
assert result == :object_id
662+
end
663+
664+
test "rejects non-ObjectId in guard clauses" do
665+
result =
666+
case %{id: "abc-123"} do
667+
x when is_object_id(x) -> :object_id
668+
_ -> :other
669+
end
670+
671+
assert result == :other
672+
end
673+
end
629674
end

0 commit comments

Comments
 (0)