From 2c0a468ce26550022122a3561ba1e6f60e4c0154 Mon Sep 17 00:00:00 2001 From: Sloane Perrault <sloane@perrault.email> Date: Fri, 7 Jul 2023 12:56:25 -0400 Subject: [PATCH] `String.Chars` impl --- CHANGELOG.md | 4 ++++ lib/type_id.ex | 4 ++++ test/type_id/string_char_test.exs | 13 +++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 test/type_id/string_char_test.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index 8539a24..d8f5c43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## main +- Implements `String.Chars` protocol + +## 0.3.0 + - **BREAKING:** `type/1` has been renamed to `prefix/1` - `Ecto.ParameterizedType` implementation - `new/2` now accepts an optional keyword list to specify the UUID `time:` in unix milliseconds diff --git a/lib/type_id.ex b/lib/type_id.ex index f74db2c..d7a7cdf 100644 --- a/lib/type_id.ex +++ b/lib/type_id.ex @@ -336,3 +336,7 @@ defimpl Inspect, for: TypeID do concat(["#TypeID<\"", TypeID.to_string(tid), "\">"]) end end + +defimpl String.Chars, for: TypeID do + defdelegate to_string(tid), to: TypeID +end diff --git a/test/type_id/string_char_test.exs b/test/type_id/string_char_test.exs new file mode 100644 index 0000000..a0c217d --- /dev/null +++ b/test/type_id/string_char_test.exs @@ -0,0 +1,13 @@ +defmodule TypeID.StringCharTest do + use ExUnit.Case + + test "implicit cast to string" do + assert {:ok, tid} = TypeID.from_string("test_01h4rm6n03esc96rwqtnq2fr5a") + assert "cast? test_01h4rm6n03esc96rwqtnq2fr5a" == "cast? #{tid}" + end + + test "Kernel.to_string/1" do + assert {:ok, tid} = TypeID.from_string("test_01h4rm6n03esc96rwqtnq2fr5a") + assert "test_01h4rm6n03esc96rwqtnq2fr5a" == to_string(tid) + end +end