Skip to content

Commit 5bef73b

Browse files
committed
first approach to support legacy uuid type
1 parent 0dcddb7 commit 5bef73b

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

lib/bson/types.ex

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,32 @@ defmodule BSON.Binary do
99
}
1010
defstruct [binary: nil, subtype: :generic]
1111

12-
defimpl Inspect do
13-
def inspect(%BSON.Binary{binary: value, subtype: :generic}, _opts) do
14-
"#BSON.Binary<#{Base.encode16(value, case: :lower)}>"
15-
end
16-
def inspect(%BSON.Binary{binary: value, subtype: :uuid}, _opts) do
12+
defimpl String.Chars, for: BSON.Binary do
13+
def to_string(%BSON.Binary{binary: value, subtype: subtype}) when subtype in [:uuid, :uuid_old] do
1714
p1 = binary_part(value, 0, 4)
1815
p2 = binary_part(value, 4, 2)
1916
p3 = binary_part(value, 6, 2)
2017
p4 = binary_part(value, 8, 2)
2118
p5 = binary_part(value, 10, 6)
22-
"#BSON.UUID<#{Base.encode16(p1, case: :lower)}-#{Base.encode16(p2, case: :lower)}-#{Base.encode16(p3, case: :lower)}-#{Base.encode16(p4, case: :lower)}-#{Base.encode16(p5, case: :lower)}>"
19+
"#{Base.encode16(p1, case: :lower)}-#{Base.encode16(p2, case: :lower)}-#{Base.encode16(p3, case: :lower)}-#{Base.encode16(p4, case: :lower)}-#{Base.encode16(p5, case: :lower)}"
20+
end
21+
def to_string(%BSON.Binary{binary: value}) do
22+
Base.encode16(value, case: :lower)
23+
end
24+
end
25+
26+
defimpl Inspect do
27+
def inspect(%BSON.Binary{subtype: :generic} = binary, _opts) do
28+
"#BSON.Binary<#{to_string(binary)}>"
29+
end
30+
def inspect(%BSON.Binary{subtype: :uuid} = binary, _opts) do
31+
"#BSON.UUID<#{to_string(binary)}>"
32+
end
33+
def inspect(%BSON.Binary{subtype: :uuid_old} = binary, _opts) do
34+
"#BSON.LUUID<#{to_string(binary)}>"
2335
end
24-
def inspect(%BSON.Binary{binary: value, subtype: subtype}, _opts) do
25-
"#BSON.Binary<#{Base.encode16(value, case: :lower)}, #{subtype}>"
36+
def inspect(%BSON.Binary{subtype: subtype} = binary, _opts) do
37+
"#BSON.Binary<#{to_string(binary)}, #{subtype}>"
2638
end
2739
end
2840
end

0 commit comments

Comments
 (0)