Skip to content

Commit

Permalink
Added the ability to render UUIDs as a :slug. (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Simmons Jr. MSc authored and zyro committed Nov 7, 2018
1 parent 55da696 commit 81b1004
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: elixir
elixir:
- 1.3.4
- 1.7.3
otp_release:
- 18.0
- 21.0
sudo: false
31 changes: 25 additions & 6 deletions lib/uuid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ defmodule UUID do
iex> UUID.uuid1(:raw)
<<205, 253, 175, 68, 238, 53, 17, 227, 132, 107, 20, 16, 159, 241, 163, 4>>
iex> UUID.uuid1(:slug)
"zf2vRO41EeOEaxQQn_GjBA"
```
"""
Expand Down Expand Up @@ -263,6 +265,8 @@ defmodule UUID do
iex> UUID.uuid1(:raw)
<<205, 253, 175, 68, 238, 53, 17, 227, 132, 107, 20, 16, 159, 241, 163, 4>>
iex> UUID.uuid1(:slug)
"zf2vRO41EeOEaxQQn_GjBA"
```
"""
Expand Down Expand Up @@ -308,6 +312,8 @@ defmodule UUID do
iex> UUID.uuid3("cdfdaf44-ee35-11e3-846b-14109ff1a304", "my.domain.com")
"8808f33a-3e11-3708-919e-15fba88908db"
iex> UUID.uuid3(:dns, "my.domain.com", :slug)
"A78HBrfpM7iu5cYUKoFkeA"
```
"""
Expand Down Expand Up @@ -364,6 +370,8 @@ defmodule UUID do
iex> UUID.uuid4(:raw)
<<251, 73, 160, 236, 214, 12, 77, 32, 146, 100, 59, 76, 254, 39, 33, 6>>
iex> UUID.uuid4(:slug)
"-0mg7NYMTSCSZDtM_ichBg"
```
"""
Expand Down Expand Up @@ -406,6 +414,8 @@ defmodule UUID do
iex> UUID.uuid5("fb49a0ec-d60c-4d20-9264-3b4cfe272106", "my.domain.com")
"822cab19-df58-5eb4-98b5-c96c15c76d32"
iex> UUID.uuid5("fb49a0ec-d60c-4d20-9264-3b4cfe272106", "my.domain.com", :slug)
"giyrGd9YXrSYtclsFcdtMg"
```
"""
Expand Down Expand Up @@ -461,21 +471,24 @@ defmodule UUID do
defp uuid_to_string(<<_::128>> = u, :raw) do
u
end
defp uuid_to_string(_u, format) when format in [:default, :hex, :urn] do
defp uuid_to_string(<<u::128>>, :slug) do
Base.url_encode64(<<u::128>>, [padding: false])
end
defp uuid_to_string(_u, format) when format in [:default, :hex, :urn, :slug] do
raise ArgumentError, message:
"Invalid binary data; Expected: <<uuid::128>>"
end
defp uuid_to_string(_u, format) do
raise ArgumentError, message:
"Invalid format #{format}; Expected: :default|:hex|:urn"
"Invalid format #{format}; Expected: :default|:hex|:urn|:slug"
end

# Extract the type (:default etc) and pure byte value from a UUID String.
defp uuid_string_to_hex_pair(<<uuid::128>>) do
{:raw, <<uuid::128>>}
end
defp uuid_string_to_hex_pair(<<uuid::binary>>) do
uuid = String.downcase(uuid)
defp uuid_string_to_hex_pair(<<uuid_in::binary>>) do
uuid = String.downcase(uuid_in)
{type, hex_str} = case uuid do
<<u0::64, ?-, u1::32, ?-, u2::32, ?-, u3::32, ?-, u4::96>> ->
{:default, <<u0::64, u1::32, u2::32, u3::32, u4::96>>}
Expand All @@ -484,8 +497,14 @@ defmodule UUID do
<<@urn, u0::64, ?-, u1::32, ?-, u2::32, ?-, u3::32, ?-, u4::96>> ->
{:urn, <<u0::64, u1::32, u2::32, u3::32, u4::96>>}
_ ->
raise ArgumentError, message:
"Invalid argument; Not a valid UUID: #{uuid}"
case uuid_in do
_ when byte_size(uuid_in) == 22 ->
case Base.url_decode64(uuid_in <> "==") do
{:ok, decoded} -> {:slug, Base.encode16(decoded)}
_ -> raise ArgumentError, message: "Invalid argument; Not a valid UUID: #{uuid}"
end
_ -> raise ArgumentError, message: "Invalid argument; Not a valid UUID: #{uuid}"
end
end

try do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule UUID.Mixfile do
[app: :elixir_uuid,
name: "UUID",
version: @version,
elixir: "~> 1.3",
elixir: "~> 1.7",
docs: [extras: ["README.md", "CHANGELOG.md"],
main: "readme",
source_ref: "v#{@version}"],
Expand Down

0 comments on commit 81b1004

Please sign in to comment.