Add missing option conversion on context creation

This commit is contained in:
Marcel Otto 2021-04-24 20:51:02 +02:00
parent 4f903a04d4
commit aa7a8e08b2
3 changed files with 12 additions and 7 deletions

View file

@ -5,7 +5,7 @@ defmodule JSON.LD.Compaction do
alias JSON.LD.{Context, Options} alias JSON.LD.{Context, Options}
@spec compact(map | [map], map | nil, Options.t() | Enum.t()) :: map @spec compact(map | [map], map | nil, Options.convertible()) :: map
def compact(input, context, options \\ %Options{}) do def compact(input, context, options \\ %Options{}) do
options = Options.new(options) options = Options.new(options)
active_context = JSON.LD.context(context, options) active_context = JSON.LD.context(context, options)

View file

@ -31,27 +31,30 @@ defmodule JSON.LD.Context do
def base(%__MODULE__{base_iri: base_iri}), def base(%__MODULE__{base_iri: base_iri}),
do: base_iri do: base_iri
@spec new(Options.t()) :: t @spec new(Options.convertible()) :: t
def new(options \\ %Options{}), def new(options \\ %Options{}),
do: %__MODULE__{api_base_iri: Options.new(options).base} do: %__MODULE__{api_base_iri: Options.new(options).base}
@spec create(map, Options.t()) :: t @spec create(map, Options.convertible()) :: t
def create(%{"@context" => json_ld_context}, options), def create(%{"@context" => json_ld_context}, options),
do: options |> new() |> update(json_ld_context, [], options) do: options |> new() |> update(json_ld_context, [], options)
@spec update(t, [local] | local, remote, Options.t()) :: t @spec update(t, [local] | local, remote, Options.convertible()) :: t
def update(active, local, remote \\ [], options \\ %Options{}) def update(active, local, remote \\ [], options \\ %Options{})
def update(%__MODULE__{} = active, local, remote, options) when is_list(local) do def update(%__MODULE__{} = active, local, remote, %Options{} = options) when is_list(local) do
Enum.reduce(local, active, fn local, result -> Enum.reduce(local, active, fn local, result ->
do_update(result, local, remote, options) do_update(result, local, remote, options)
end) end)
end end
# 2) If local context is not an array, set it to an array containing only local context. # 2) If local context is not an array, set it to an array containing only local context.
def update(%__MODULE__{} = active, local, remote, options), def update(%__MODULE__{} = active, local, remote, %Options{} = options),
do: update(active, [local], remote, options) do: update(active, [local], remote, options)
def update(%__MODULE__{} = active, local, remote, options),
do: update(active, local, remote, Options.new(options))
# 3.1) If context is null, set result to a newly-initialized active context and continue with the next context. The base IRI of the active context is set to the IRI of the currently being processed document (which might be different from the currently being processed context), if available; otherwise to null. If set, the base option of a JSON-LD API Implementation overrides the base IRI. # 3.1) If context is null, set result to a newly-initialized active context and continue with the next context. The base IRI of the active context is set to the IRI of the currently being processed document (which might be different from the currently being processed context), if available; otherwise to null. If set, the base option of a JSON-LD API Implementation overrides the base IRI.
@spec do_update(t, local, remote, Options.t()) :: t @spec do_update(t, local, remote, Options.t()) :: t
defp do_update(%__MODULE__{}, nil, _remote, options), defp do_update(%__MODULE__{}, nil, _remote, options),

View file

@ -16,6 +16,8 @@ defmodule JSON.LD.Options do
processing_mode: String.t() processing_mode: String.t()
} }
@type convertible :: t | Enum.t()
defstruct base: nil, defstruct base: nil,
compact_arrays: true, compact_arrays: true,
document_loader: nil, document_loader: nil,
@ -28,7 +30,7 @@ defmodule JSON.LD.Options do
@spec new :: t @spec new :: t
def new, do: %__MODULE__{} def new, do: %__MODULE__{}
@spec new(t | Enum.t()) :: t @spec new(convertible) :: t
def new(%__MODULE__{} = options), do: options def new(%__MODULE__{} = options), do: options
def new(options), do: struct(__MODULE__, options) def new(options), do: struct(__MODULE__, options)
end end