Skip to content

Commit

Permalink
Revert splitting of lines in comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rustra committed Jun 21, 2020
1 parent 9ad0e7c commit 48df5fc
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 233 deletions.
162 changes: 46 additions & 116 deletions lib/json/ld/compaction.ex

Large diffs are not rendered by default.

66 changes: 24 additions & 42 deletions lib/json/ld/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ defmodule JSON.LD.Context do
def update(%__MODULE__{} = active, local, remote, options),
do: update(active, [local], remote, 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
defp do_update(%__MODULE__{}, nil, _remote, options),
do: new(options)
Expand Down Expand Up @@ -117,18 +113,7 @@ defmodule JSON.LD.Context do
end

# 3.4) - 3.8)
defp do_update(%__MODULE__{} = active, local, remote, _) when is_map(local),
do: do_update_local(active, local, remote)

# 3.3) If context is not a JSON object, an invalid local context error has been detected
# and processing is aborted.
defp do_update(_, local, _, _) do
raise JSON.LD.InvalidLocalContextError,
message: "#{inspect(local)} is not a valid @context value"
end

@spec do_update_local(t, map, remote) :: t
defp do_update_local(%__MODULE__{} = active, local, remote) when is_map(local) do
defp do_update(%__MODULE__{} = active, local, remote, _) when is_map(local) do
with {base, local} <- Map.pop(local, "@base", false),
{vocab, local} <- Map.pop(local, "@vocab", false),
{language, local} <- Map.pop(local, "@language", false) do
Expand All @@ -140,6 +125,12 @@ defmodule JSON.LD.Context do
end
end

# 3.3) If context is not a JSON object, an invalid local context error has been detected and processing is aborted.
defp do_update(_, local, _, _) do
raise JSON.LD.InvalidLocalContextError,
message: "#{inspect(local)} is not a valid @context value"
end

@spec set_base(t, boolean, remote) :: t
defp set_base(active, false, _),
do: active
Expand All @@ -149,8 +140,7 @@ defmodule JSON.LD.Context do

defp set_base(active, base, _) do
cond do
# TODO: this slightly differs from the spec, due to our false special value for
# base_iri; add more tests
# TODO: this slightly differs from the spec, due to our false special value for base_iri; add more tests
is_nil(base) or IRI.absolute?(base) ->
%__MODULE__{active | base_iri: base}

Expand Down Expand Up @@ -208,8 +198,7 @@ defmodule JSON.LD.Context do
end

@doc """
Expands the given input according to the steps in the JSON-LD Create Term Definition
Algorithm.
Expands the given input according to the steps in the JSON-LD Create Term Definition Algorithm.
see <https://www.w3.org/TR/json-ld-api/#create-term-definition>
"""
Expand Down Expand Up @@ -244,10 +233,10 @@ defmodule JSON.LD.Context do

@spec do_create_term_definition(t, map, String.t(), value, map) :: {t, map}
defp do_create_term_definition(active, _local, term, nil, defined) do
# (if Map.has_key?(active.term_defs, term),
# do: put_in(active, [:term_defs, term], nil),
# else: raise "NotImplemented"),
{
# (if Map.has_key?(active.term_defs, term),
# do: put_in(active, [:term_defs, term], nil),
# else: raise "NotImplemented"),
%__MODULE__{active | term_defs: Map.put(active.term_defs, term, nil)},
Map.put(defined, term, true)
}
Expand Down Expand Up @@ -282,8 +271,7 @@ defmodule JSON.LD.Context do
{definition, active, defined}
end

# 18 / 11.6) Set the term definition of term in active context to definition and set
# the value associated with defined's key term to true.
# 18 / 11.6) Set the term definition of term in active context to definition and set the value associated with defined's key term to true.
{
%__MODULE__{active | term_defs: Map.put(active.term_defs, term, definition)},
Map.put(defined, term, true)
Expand Down Expand Up @@ -329,18 +317,18 @@ defmodule JSON.LD.Context do
defined
) do
cond do
# 11.1 )
# 11.1)
Map.has_key?(value, "@id") ->
raise JSON.LD.InvalidReversePropertyError,
message: "#{inspect(reverse)} is not a valid reverse property"

# 11.2)
not is_binary(reverse) ->
# 11.2)
raise JSON.LD.InvalidIRIMappingError,
message: "Expected String for @reverse value. got #{inspect(reverse)}"

# 11.3)
true ->
# 11.3)
{expanded_reverse, active, defined} =
expand_iri(reverse, active, false, true, local, defined)

Expand Down Expand Up @@ -380,6 +368,7 @@ defmodule JSON.LD.Context do
{TermDefinition.t(), t, map}
defp do_create_id_definition(definition, active, local, term, %{"@id" => id}, defined)
when id != term do
# 13.1)
if is_binary(id) do
# 13.2)
{expanded_id, active, defined} = expand_iri(id, active, false, true, local, defined)
Expand All @@ -397,16 +386,14 @@ defmodule JSON.LD.Context do
"#{inspect(id)} is not a valid IRI mapping; resulting IRI mapping should be a keyword, absolute IRI or blank node"
end
else
# 13.1)
raise JSON.LD.InvalidIRIMappingError,
message: "expected value of @id to be a string, but got #{inspect(id)}"
end
end

defp do_create_id_definition(definition, active, local, term, _, defined) do
# 14)
# TODO: The W3C spec seems to contain an error by requiring only to check for a collon.
# What's when an absolute IRI is given and an "http" term is defined in the context?
# TODO: The W3C spec seems to contain an error by requiring only to check for a collon. What's when an absolute IRI is given and an "http" term is defined in the context?
if String.contains?(term, ":") do
case compact_iri_parts(term) do
[prefix, suffix] ->
Expand Down Expand Up @@ -486,20 +473,16 @@ defmodule JSON.LD.Context do
"""
@spec inverse(t) :: map
def inverse(%__MODULE__{} = context) do
# 2) Initialize default language to @none. If the active context has a default
# language, set default language to it.
# 2) Initialize default language to @none. If the active context has a default language, set default language to it.
default_language = context.default_language || "@none"

# 3) For each key term and value term definition in the active context, ordered by
# shortest term first (breaking ties by choosing the lexicographically least term)
# 3) For each key term and value term definition in the active context, ordered by shortest term first (breaking ties by choosing the lexicographically least term)
context.term_defs
|> Enum.sort_by(fn {term, _} -> String.length(term) end)
|> Enum.reduce(%{}, fn {term, term_def}, result ->
# 3.1) If the term definition is null, term cannot be selected during compaction,
# so continue to the next term.
# 3.1) If the term definition is null, term cannot be selected during compaction, so continue to the next term.
if term_def do
# 3.2) Initialize container to @none. If there is a container mapping in term
# definition, set container to its associated value.
# 3.2) Initialize container to @none. If there is a container mapping in term definition, set container to its associated value.
container = term_def.container_mapping || "@none"

# 3.3) Initialize iri to the value of the IRI mapping for the term definition.
Expand All @@ -510,8 +493,7 @@ defmodule JSON.LD.Context do

{type_map, language_map} =
case term_def do
# 3.8) If the term definition indicates that the term represents a reverse
# property
# 3.8) If the term definition indicates that the term represents a reverse property
%TermDefinition{reverse_property: true} ->
{Map.put_new(type_map, "@reverse", term), language_map}

Expand Down
5 changes: 2 additions & 3 deletions lib/json/ld/document_loader/default.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule JSON.LD.DocumentLoader.Default do
@behaviour JSON.LD.DocumentLoader

alias HTTPoison.{AsyncResponse, Response}

alias JSON.LD.DocumentLoader.RemoteDocument
alias JSON.LD.Options

Expand All @@ -14,7 +12,8 @@ defmodule JSON.LD.DocumentLoader.Default do
end
end

@spec http_get(String.t()) :: {:ok, Response.t() | AsyncResponse.t()} | {:error, any}
@spec http_get(String.t()) ::
{:ok, HTTPoison.Response.t() | HTTPoison.AsyncResponse.t()} | {:error, any}
defp http_get(url) do
HTTPoison.get(url, [accept: "application/ld+json"], follow_redirect: true)
rescue
Expand Down
6 changes: 2 additions & 4 deletions lib/json/ld/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ defmodule JSON.LD.Encoder do
|> update_node_usages
end

# This function is necessary because we have no references and must update the
# node member of the usage maps with later enhanced usages
# This function is necessary because we have no references and must update the node member of the usage maps with later enhanced usages
@spec update_node_usages(map) :: map
defp update_node_usages(node_map) do
Enum.reduce(node_map, node_map, fn
Expand All @@ -201,8 +200,7 @@ defmodule JSON.LD.Encoder do
end)
end

# This function is necessary because we have no references and use this
# instead to update the head by path
# This function is necessary because we have no references and use this instead to update the head by path
@spec update_head(map, [String.t()], map, map) :: map
defp update_head(graph_object, path, old, new) do
update_in(graph_object, path, fn objects ->
Expand Down
39 changes: 13 additions & 26 deletions lib/json/ld/exceptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ end

defmodule JSON.LD.ListOfListsError do
@moduledoc """
A list of lists was detected. List of lists are not supported in this version of
JSON-LD due to the algorithmic complexity.
A list of lists was detected. List of lists are not supported in this version of JSON-LD due to the algorithmic complexity.
"""
defexception code: "list of lists", message: nil
end
Expand Down Expand Up @@ -43,8 +42,7 @@ end

defmodule JSON.LD.MultipleContextLinkHeadersError do
@moduledoc """
Multiple HTTP Link Headers [RFC5988] using the http://www.w3.org/ns/json-ld#context
link relation have been detected.
Multiple HTTP Link Headers [RFC5988] using the http://www.w3.org/ns/json-ld#context link relation have been detected.
"""
defexception code: "multiple context link headers", message: nil
end
Expand Down Expand Up @@ -79,8 +77,7 @@ end

defmodule JSON.LD.InvalidVocabMappingError do
@moduledoc """
An invalid vocabulary mapping has been detected, i.e., it is neither an absolute IRI
nor null.
An invalid vocabulary mapping has been detected, i.e., it is neither an absolute IRI nor null.
"""
defexception code: "invalid vocab mapping", message: nil
end
Expand Down Expand Up @@ -136,40 +133,35 @@ end

defmodule JSON.LD.InvalidTypeMappingError do
@moduledoc """
An @type member in a term definition was encountered whose value could not be
expanded to an absolute IRI.
An @type member in a term definition was encountered whose value could not be expanded to an absolute IRI.
"""
defexception code: "invalid type mapping", message: nil
end

defmodule JSON.LD.InvalidLanguageMappingError do
@moduledoc """
An @language member in a term definition was encountered whose value was neither
a string nor null and thus invalid.
An @language member in a term definition was encountered whose value was neither a string nor null and thus invalid.
"""
defexception code: "invalid language mapping", message: nil
end

defmodule JSON.LD.CollidingKeywordsError do
@moduledoc """
Two properties which expand to the same keyword have been detected. This might occur
if a keyword and an alias thereof are used at the same time.
Two properties which expand to the same keyword have been detected. This might occur if a keyword and an alias thereof are used at the same time.
"""
defexception code: "colliding keywords", message: nil
end

defmodule JSON.LD.InvalidContainerMappingError do
@moduledoc """
An @container member was encountered whose value was not one of the following strings:
@list, @set, or @index.
An @container member was encountered whose value was not one of the following strings: @list, @set, or @index.
"""
defexception code: "invalid container mapping", message: nil
end

defmodule JSON.LD.InvalidTypeValueError do
@moduledoc """
An invalid value for an @type member has been detected, i.e., the value was neither
a string nor an array of strings.
An invalid value for an @type member has been detected, i.e., the value was neither a string nor an array of strings.
"""
defexception code: "invalid type value", message: nil
end
Expand All @@ -183,8 +175,7 @@ end

defmodule JSON.LD.InvalidValueObjectValueError do
@moduledoc """
An invalid value for the @value member of a value object has been detected,
i.e., it is neither a scalar nor null.
An invalid value for the @value member of a value object has been detected, i.e., it is neither a scalar nor null.
"""
defexception code: "invalid value object value", message: nil
end
Expand Down Expand Up @@ -219,16 +210,14 @@ end

defmodule JSON.LD.InvalidLanguageMapValueError do
@moduledoc """
An invalid value in a language map has been detected. It has to be a string or
an array of strings.
An invalid value in a language map has been detected. It has to be a string or an array of strings.
"""
defexception code: "invalid language map value", message: nil
end

defmodule JSON.LD.CompactionToListOfListsError do
@moduledoc """
The compacted document contains a list of lists as multiple lists have been compacted
to the same term.
The compacted document contains a list of lists as multiple lists have been compacted to the same term.
"""
defexception code: "compaction to list of lists", message: nil
end
Expand All @@ -242,16 +231,14 @@ end

defmodule JSON.LD.InvalidReverseValueError do
@moduledoc """
An invalid value for an @reverse member has been detected, i.e., the value was not
a JSON object.
An invalid value for an @reverse member has been detected, i.e., the value was not a JSON object.
"""
defexception code: "invalid @reverse value", message: nil
end

defmodule JSON.LD.InvalidReversePropertyValueError do
@moduledoc """
An invalid value for a reverse property has been detected. The value of an inverse
property must be a node object.
An invalid value for a reverse property has been detected. The value of an inverse property must be a node object.
"""
defexception code: "invalid reverse property value", message: nil
end
23 changes: 7 additions & 16 deletions lib/json/ld/expansion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,11 @@ defmodule JSON.LD.Expansion do
else
value = do_expand(active_context, active_property, value, options)

# Spec FIXME: need to be sure that result is a list
# [from RDF.rb implementation]
# Spec FIXME: need to be sure that result is a list [from RDF.rb implementation]
value = if is_list(value), do: value, else: [value]

# If expanded value is a list object, a list of lists error
# has been detected and processing is aborted.
# Spec FIXME: Also look at each object if result is a list
# [from RDF.rb implementation]
# If expanded value is a list object, a list of lists error has been detected and processing is aborted.
# Spec FIXME: Also look at each object if result is a list [from RDF.rb implementation]
if Enum.any?(value, fn v -> Map.has_key?(v, "@list") end) do
raise JSON.LD.ListOfListsError,
message: "List of lists in #{inspect(value)}"
Expand All @@ -205,9 +202,7 @@ defmodule JSON.LD.Expansion do
# 7.4.11.1)
expanded_value = do_expand(active_context, "@reverse", value, options)

# 7.4.11.2) If expanded value contains an @reverse member,
# i.e., properties that are reversed twice, execute for each of its
# property and item the following steps:
# 7.4.11.2) If expanded value contains an @reverse member, i.e., properties that are reversed twice, execute for each of its property and item the following steps:
new_result =
if Map.has_key?(expanded_value, "@reverse") do
Enum.reduce(
Expand Down Expand Up @@ -269,9 +264,7 @@ defmodule JSON.LD.Expansion do

expanded_value =
cond do
# 7.5) Otherwise, if key's container mapping in active context is
# @language and value is a JSON object then value is expanded from
# a language map as follows:
# 7.5) Otherwise, if key's container mapping in active context is @language and value is a JSON object then value is expanded from a language map as follows:
is_map(value) && term_def && term_def.container_mapping == "@language" ->
value
|> Enum.sort_by(fn {language, _} -> language end)
Expand Down Expand Up @@ -325,8 +318,7 @@ defmodule JSON.LD.Expansion do
expanded_value
end

# 7.10) Otherwise, if the term definition associated to key indicates that
# it is a reverse property
# 7.10) Otherwise, if the term definition associated to key indicates that it is a reverse property
# Spec FIXME: this is not an otherwise [from RDF.rb implementation]
# 7.11)
if term_def && term_def.reverse_property do
Expand Down Expand Up @@ -420,8 +412,7 @@ defmodule JSON.LD.Expansion do
else: result

# 12) If active property is null or @graph, drop free-floating values as follows:
# Spec FIXME: Due to case 10) we might land with a list here; other implementations
# deal with that, by just returning in step 10)
# Spec FIXME: Due to case 10) we might land with a list here; other implementations deal with that, by just returning in step 10)
result =
if is_map(result) and active_property in [nil, "@graph"] and
(Enum.empty?(result) or
Expand Down

0 comments on commit 48df5fc

Please sign in to comment.