From 303a474437309a568f29f4d66d6c9cf1423e54d2 Mon Sep 17 00:00:00 2001 From: Sloane Date: Thu, 6 Jul 2023 14:23:58 -0400 Subject: [PATCH] Use homerolled UUID module (#9) * remove uniq, use homerolled uuid module * update changelog --- CHANGELOG.md | 3 ++- lib/type_id.ex | 7 ++++--- lib/type_id/uuid.ex | 32 +++++++++++++++++++++++++++++++- mix.exs | 1 - mix.lock | 1 - 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6ec4f6..0823cd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # Changelog -## 0.2.1 +## main - Include CHANGELOG in docs +- Remove dependency on `Uniq` ## 0.2.0 diff --git a/lib/type_id.ex b/lib/type_id.ex index 36ff22c..d6df103 100644 --- a/lib/type_id.ex +++ b/lib/type_id.ex @@ -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 diff --git a/lib/type_id/uuid.ex b/lib/type_id/uuid.ex index accf008..56bc23d 100644 --- a/lib/type_id/uuid.ex +++ b/lib/type_id/uuid.ex @@ -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) <> = :crypto.strong_rand_bytes(10) @@ -6,7 +8,7 @@ defmodule TypeID.UUID do <> end - def to_string( + def binary_to_string( <> @@ -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( + <> + ) do + <> + 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 diff --git a/mix.exs b/mix.exs index 7941313..dcb1bfd 100644 --- a/mix.exs +++ b/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} ] diff --git a/mix.lock b/mix.lock index cf556f8..9e8772c 100644 --- a/mix.lock +++ b/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"}, }