Skip to content

Commit dc663b2

Browse files
authored
implements String.Chars for BSON.ObjectId (#184)
1 parent e989801 commit dc663b2

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/bson/types.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ defmodule BSON.ObjectId do
138138
"#BSON.ObjectId<#{encoded}>"
139139
end
140140
end
141+
142+
defimpl String.Chars do
143+
def to_string(id), do: BSON.ObjectId.encode!(id)
144+
end
141145
end
142146

143147
defmodule BSON.Regex do

test/bson/types_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ defmodule BSON.TypesTest do
4242
assert BSON.ObjectId.decode("") == :error
4343
end
4444

45+
test "to_string BSON.ObjectId" do
46+
assert to_string(@objectid) == @string
47+
end
48+
4549
test "inspect BSON.Regex" do
4650
value = %BSON.Regex{pattern: "abc"}
4751
assert inspect(value) == "#BSON.Regex<\"abc\", \"\">"

0 commit comments

Comments
 (0)