mirror of
https://github.com/sloanelybutsurely/typeid-elixir.git
synced 2024-11-28 01:42:54 -05:00
Use homerolled UUID module (#9)
* remove uniq, use homerolled uuid module * update changelog
This commit is contained in:
parent
d6814de257
commit
303a474437
5 changed files with 37 additions and 7 deletions
|
@ -1,8 +1,9 @@
|
|||
# Changelog
|
||||
|
||||
## 0.2.1
|
||||
## main
|
||||
|
||||
- Include CHANGELOG in docs
|
||||
- Remove dependency on `Uniq`
|
||||
|
||||
## 0.2.0
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ defmodule TypeID do
|
|||
@moduledoc File.cwd!() |> Path.join("README.md") |> File.read!()
|
||||
|
||||
alias TypeID.Base32
|
||||
alias TypeID.UUID
|
||||
|
||||
@enforce_keys [:prefix, :suffix]
|
||||
defstruct @enforce_keys
|
||||
|
@ -26,7 +27,7 @@ defmodule TypeID do
|
|||
@spec new(prefix :: String.t()) :: t()
|
||||
def new(prefix) do
|
||||
suffix =
|
||||
Uniq.UUID.uuid7(:raw)
|
||||
UUID.uuid7()
|
||||
|> Base32.encode()
|
||||
|
||||
%__MODULE__{prefix: prefix, suffix: suffix}
|
||||
|
@ -110,7 +111,7 @@ defmodule TypeID do
|
|||
def uuid(%__MODULE__{} = tid) do
|
||||
tid
|
||||
|> uuid_bytes()
|
||||
|> Uniq.UUID.to_string(:default)
|
||||
|> UUID.binary_to_string()
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -180,7 +181,7 @@ defmodule TypeID do
|
|||
"""
|
||||
@spec from_uuid!(prefix :: String.t(), uuid :: String.t()) :: t() | no_return()
|
||||
def from_uuid!(prefix, uuid) do
|
||||
uuid_bytes = Uniq.UUID.string_to_binary!(uuid)
|
||||
uuid_bytes = UUID.string_to_binary(uuid)
|
||||
from_uuid_bytes!(prefix, uuid_bytes)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
defmodule TypeID.UUID do
|
||||
@moduledoc false
|
||||
|
||||
def uuid7(opts \\ []) do
|
||||
time = Keyword.get_lazy(opts, :time, fn -> System.system_time(:millisecond) end)
|
||||
<<rand_a::12, _::6, rand_b::62>> = :crypto.strong_rand_bytes(10)
|
||||
|
@ -6,7 +8,7 @@ defmodule TypeID.UUID do
|
|||
<<time::big-unsigned-integer-size(48), 7::4, rand_a::12, 2::2, rand_b::62>>
|
||||
end
|
||||
|
||||
def to_string(
|
||||
def binary_to_string(
|
||||
<<a1::4, a2::4, a3::4, a4::4, a5::4, a6::4, a7::4, a8::4, b1::4, b2::4, b3::4, b4::4,
|
||||
c1::4, c2::4, c3::4, c4::4, d1::4, d2::4, d3::4, d4::4, e1::4, e2::4, e3::4, e4::4,
|
||||
e5::4, e6::4, e7::4, e8::4, e9::4, e10::4, e11::4, e12::4>>
|
||||
|
@ -16,6 +18,16 @@ defmodule TypeID.UUID do
|
|||
e(e5), e(e6), e(e7), e(e8), e(e9), e(e10), e(e11), e(e12)>>
|
||||
end
|
||||
|
||||
def string_to_binary(
|
||||
<<a1, a2, a3, a4, a5, a6, a7, a8, ?-, b1, b2, b3, b4, ?-, c1, c2, c3, c4, ?-, d1, d2, d3,
|
||||
d4, ?-, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12>>
|
||||
) do
|
||||
<<d(a1)::4, d(a2)::4, d(a3)::4, d(a4)::4, d(a5)::4, d(a6)::4, d(a7)::4, d(a8)::4, d(b1)::4,
|
||||
d(b2)::4, d(b3)::4, d(b4)::4, d(c1)::4, d(c2)::4, d(c3)::4, d(c4)::4, d(d1)::4, d(d2)::4,
|
||||
d(d3)::4, d(d4)::4, d(e1)::4, d(e2)::4, d(e3)::4, d(e4)::4, d(e5)::4, d(e6)::4, d(e7)::4,
|
||||
d(e8)::4, d(e9)::4, d(e10)::4, d(e11)::4, d(e12)::4>>
|
||||
end
|
||||
|
||||
@compile {:inline, [e: 1]}
|
||||
defp e(0), do: ?0
|
||||
defp e(1), do: ?1
|
||||
|
@ -33,4 +45,22 @@ defmodule TypeID.UUID do
|
|||
defp e(13), do: ?d
|
||||
defp e(14), do: ?e
|
||||
defp e(15), do: ?f
|
||||
|
||||
@compile {:inline, [d: 1]}
|
||||
defp d(?0), do: 0
|
||||
defp d(?1), do: 1
|
||||
defp d(?2), do: 2
|
||||
defp d(?3), do: 3
|
||||
defp d(?4), do: 4
|
||||
defp d(?5), do: 5
|
||||
defp d(?6), do: 6
|
||||
defp d(?7), do: 7
|
||||
defp d(?8), do: 8
|
||||
defp d(?9), do: 9
|
||||
defp d(?a), do: 10
|
||||
defp d(?b), do: 11
|
||||
defp d(?c), do: 12
|
||||
defp d(?d), do: 13
|
||||
defp d(?e), do: 14
|
||||
defp d(?f), do: 15
|
||||
end
|
||||
|
|
1
mix.exs
1
mix.exs
|
@ -42,7 +42,6 @@ defmodule TypeID.MixProject do
|
|||
|
||||
defp deps do
|
||||
[
|
||||
{:uniq, "~> 0.5.4"},
|
||||
{:ex_doc, "~> 0.27", only: :dev, runtime: false},
|
||||
{:yaml_elixir, "~> 2.9", only: [:dev, :test], runtime: false}
|
||||
]
|
||||
|
|
1
mix.lock
1
mix.lock
|
@ -5,7 +5,6 @@
|
|||
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
|
||||
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
|
||||
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
|
||||
"uniq": {:hex, :uniq, "0.5.4", "0602d0c75682f14863c1edea48920bd3d278b29ff7801d56e040e5f9f51ae0e2", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "856ec690d713f97373a3746afada340143c7e37a6488fca5afa720bfb08f0fe5"},
|
||||
"yamerl": {:hex, :yamerl, "0.10.0", "4ff81fee2f1f6a46f1700c0d880b24d193ddb74bd14ef42cb0bcf46e81ef2f8e", [:rebar3], [], "hexpm", "346adb2963f1051dc837a2364e4acf6eb7d80097c0f53cbdc3046ec8ec4b4e6e"},
|
||||
"yaml_elixir": {:hex, :yaml_elixir, "2.9.0", "9a256da867b37b8d2c1ffd5d9de373a4fda77a32a45b452f1708508ba7bbcb53", [:mix], [{:yamerl, "~> 0.10", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "0cb0e7d4c56f5e99a6253ed1a670ed0e39c13fc45a6da054033928607ac08dfc"},
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue