-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a82122d
commit f1b5e08
Showing
7 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
if Code.ensure_loaded?(UUID) do | ||
defmodule RDF.BlankNode.Generator.UUID do | ||
@moduledoc """ | ||
An implementation of a `RDF.BlankNode.Generator.Algorithm` which returns `RDF.BlankNode`s with random UUID identifiers. | ||
This module is only available if the optional `:elixir_uuid` dependency is available. | ||
""" | ||
|
||
@behaviour RDF.BlankNode.Generator.Algorithm | ||
|
||
defstruct prefix: "b" | ||
|
||
@type t :: %__MODULE__{prefix: String.t()} | ||
|
||
alias RDF.BlankNode | ||
|
||
@doc """ | ||
Creates a struct with the state of the algorithm. | ||
## Options | ||
- `prefix`: a string prepended to the generated blank node identifier | ||
""" | ||
def new(attrs \\ []) do | ||
struct(__MODULE__, attrs) | ||
end | ||
|
||
@impl BlankNode.Generator.Algorithm | ||
def generate(%__MODULE__{} = state) do | ||
{bnode(state, uuid()), state} | ||
end | ||
|
||
@impl BlankNode.Generator.Algorithm | ||
def generate_for(%__MODULE__{} = state, value) do | ||
{bnode(state, uuid_for(value)), state} | ||
end | ||
|
||
defp bnode(%__MODULE__{prefix: nil}, uuid), do: BlankNode.new(uuid) | ||
defp bnode(%__MODULE__{} = state, uuid), do: BlankNode.new(state.prefix <> uuid) | ||
|
||
defp uuid, do: UUID.uuid4(:hex) | ||
|
||
# 74fdf771-1a01-5e8f-a491-1c9e61f1adc6 | ||
@uuid_namespace UUID.uuid5(:url, "https://rdf-elixir.dev/blank-node-generator/uuid") | ||
|
||
defp uuid_for(value) when is_binary(value), | ||
do: UUID.uuid5(@uuid_namespace, value, :hex) | ||
|
||
defp uuid_for(value), | ||
do: value |> :erlang.term_to_binary() |> uuid_for() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule RDF.BlankNode.Generator.UUIDTest do | ||
use RDF.Test.Case | ||
|
||
import RDF, only: [bnode: 1] | ||
|
||
alias RDF.BlankNode.Generator.UUID | ||
|
||
describe "generate/1" do | ||
test "without prefix" do | ||
assert {%BlankNode{}, %UUID{}} = UUID.generate(%UUID{}) | ||
end | ||
|
||
test "with prefix" do | ||
assert {%BlankNode{value: "b" <> _}, %UUID{prefix: "b"}} = | ||
UUID.generate(%UUID{prefix: "b"}) | ||
end | ||
end | ||
|
||
describe "generate_for/2" do | ||
test "returns the same id for the same value" do | ||
assert {%BlankNode{value: "b12df7fcd25a15b2d84b9db6d881aefdc"}, %UUID{}} = | ||
UUID.generate_for(%UUID{}, "foo") | ||
|
||
assert UUID.generate_for(%UUID{}, "foo") == | ||
UUID.generate_for(%UUID{}, "foo") | ||
|
||
assert UUID.generate_for(%UUID{}, {:foo, "foo", ~U[2024-07-13 02:21:45.085932Z]}) == | ||
UUID.generate_for(%UUID{}, {:foo, "foo", ~U[2024-07-13 02:21:45.085932Z]}) | ||
end | ||
|
||
test "with prefix" do | ||
assert {%BlankNode{value: "x03c964986571507facc25bcf9ae8e8a6"}, %UUID{prefix: "x"}} = | ||
UUID.generate_for(%UUID{prefix: "x"}, "bar") | ||
|
||
assert UUID.generate_for(%UUID{prefix: "x"}, "foo") == | ||
{bnode("x12df7fcd25a15b2d84b9db6d881aefdc"), %UUID{prefix: "x"}} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters