lib/json/ld/compact.rb in json-ld-3.1.0 vs lib/json/ld/compact.rb in json-ld-3.1.1

- old
+ new

@@ -3,16 +3,11 @@ module JSON::LD module Compact include Utils # The following constant is used to reduce object allocations in #compact below - CONTAINER_MAPPING_ID = %w(@id).freeze - CONTAINER_MAPPING_INDEX = %w(@index).freeze - CONTAINER_MAPPING_LANGUAGE = %w(@language).freeze CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE = Set.new(%w(@language @index @id @type)).freeze - CONTAINER_MAPPING_LIST = %w(@list).freeze - CONTAINER_MAPPING_TYPE = %w(@type).freeze EXPANDED_PROPERTY_DIRECTION_INDEX_LANGUAGE_VALUE = %w(@direction @index @language @value).freeze ## # This algorithm compacts a JSON-LD document, such that the given context is applied. This must result in shortening any applicable IRIs to terms or compact IRIs, any applicable keywords to keyword aliases, and any applicable JSON-LD values expressed in expanded form to simple values such as strings or numbers. # @@ -69,11 +64,11 @@ return result end end # If expanded property is @list and we're contained within a list container, recursively compact this item to an array - if list?(element) && context.container(property) == CONTAINER_MAPPING_LIST + if list?(element) && context.container(property).include?('@list') return compact(element['@list'], property: property, ordered: ordered) end inside_reverse = property == '@reverse' result, nest_result = {}, nil @@ -108,12 +103,11 @@ kw_alias = context.compact_iri('@type', vocab: true) as_array = compacted_value.length > 1 || (context.as_array?(kw_alias) && !value?(element) && context.processingMode('json-ld-1.1')) - compacted_value = compacted_value.first unless as_array - result[kw_alias] = compacted_value + add_value(result, kw_alias, compacted_value, property_is_array: as_array) next end if expanded_property == '@reverse' compacted_value = compact(expanded_value, property: '@reverse', ordered: ordered) @@ -126,11 +120,11 @@ compacted_value.delete(prop) end end unless compacted_value.empty? - al = context.compact_iri('@reverse', quiet: true) + al = context.compact_iri('@reverse') #log_debug("") {"remainder: #{al} => #{compacted_value.inspect}"} result[al] = compacted_value end next end @@ -144,18 +138,18 @@ result['@preserve'] = compacted_value end next end - if expanded_property == '@index' && context.container(property) == CONTAINER_MAPPING_INDEX + if expanded_property == '@index' && context.container(property).include?('@index') #log_debug("@index") {"drop @index"} next end # Otherwise, if expanded property is @direction, @index, @value, or @language: if EXPANDED_PROPERTY_DIRECTION_INDEX_LANGUAGE_VALUE.include?(expanded_property) - al = context.compact_iri(expanded_property, vocab: true, quiet: true) + al = context.compact_iri(expanded_property, vocab: true) #log_debug(expanded_property) {"#{al} => #{expanded_value.inspect}"} result[al] = expanded_value next end @@ -207,15 +201,15 @@ #log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"} # handle @list if list?(expanded_item) compacted_item = as_array(compacted_item) - unless container == CONTAINER_MAPPING_LIST - al = context.compact_iri('@list', vocab: true, quiet: true) + unless container.include?('@list') + al = context.compact_iri('@list', vocab: true) compacted_item = {al => compacted_item} if expanded_item.has_key?('@index') - key = context.compact_iri('@index', vocab: true, quiet: true) + key = context.compact_iri('@index', vocab: true) compacted_item[key] = expanded_item['@index'] end else add_value(nest_result, item_active_property, compacted_item, value_is_array: true, allow_duplicate: true) @@ -229,76 +223,75 @@ (container.include?('@id') || container.include?('@index') && simple_graph?(expanded_item)) # container includes @graph and @id map_object = nest_result[item_active_property] ||= {} # If there is no @id, create a blank node identifier to use as an index map_key = if container.include?('@id') && expanded_item['@id'] - context.compact_iri(expanded_item['@id'], quiet: true) + context.compact_iri(expanded_item['@id']) elsif container.include?('@index') && expanded_item['@index'] - context.compact_iri(expanded_item['@index'], quiet: true) + context.compact_iri(expanded_item['@index']) else - context.compact_iri('@none', vocab: true, quiet: true) + context.compact_iri('@none', vocab: true) end add_value(map_object, map_key, compacted_item, property_is_array: as_array) elsif container.include?('@graph') && simple_graph?(expanded_item) # container includes @graph but not @id or @index and value is a simple graph object if compacted_item.is_a?(Array) && compacted_item.length > 1 # Mutple objects in the same graph can't be represented directly, as they would be interpreted as two different graphs. Need to wrap in @included. - included_key = context.compact_iri('@included', vocab: true).to_s + included_key = context.compact_iri('@included', vocab: true) compacted_item = {included_key => compacted_item} end # Drop through, where compacted_item will be added add_value(nest_result, item_active_property, compacted_item, property_is_array: as_array) else # container does not include @graph or otherwise does not match one of the previous cases, redo compacted_item - al = context.compact_iri('@graph', vocab: true, quiet: true) + al = context.compact_iri('@graph', vocab: true) compacted_item = {al => compacted_item} if expanded_item['@id'] - al = context.compact_iri('@id', vocab: true, quiet: true) - compacted_item[al] = context.compact_iri(expanded_item['@id'], vocab: false, quiet: true).to_s + al = context.compact_iri('@id', vocab: true) + compacted_item[al] = context.compact_iri(expanded_item['@id'], vocab: false) end if expanded_item.has_key?('@index') - key = context.compact_iri('@index', vocab: true, quiet: true) + key = context.compact_iri('@index', vocab: true) compacted_item[key] = expanded_item['@index'] end add_value(nest_result, item_active_property, compacted_item, property_is_array: as_array) end - elsif container.any? { |key| CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE.include?(key) } && !container.include?('@graph') + elsif container.intersect?(CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE) && !container.include?('@graph') map_object = nest_result[item_active_property] ||= {} c = container.first - container_key = context.compact_iri(c, vocab: true, quiet: true) - compacted_item = case container - when CONTAINER_MAPPING_ID + container_key = context.compact_iri(c, vocab: true) + compacted_item = case + when container.include?('@id') map_key = compacted_item[container_key] compacted_item.delete(container_key) compacted_item - when CONTAINER_MAPPING_INDEX + when container.include?('@index') index_key = context.term_definitions[item_active_property].index || '@index' if index_key == '@index' map_key = expanded_item['@index'] - compacted_item.delete(container_key) if compacted_item.is_a?(Hash) else - container_key = context.compact_iri(index_key, vocab: true, quiet: true) + container_key = context.compact_iri(index_key, vocab: true) map_key, *others = Array(compacted_item[container_key]) if map_key.is_a?(String) case others.length when 0 then compacted_item.delete(container_key) when 1 then compacted_item[container_key] = others.first else compacted_item[container_key] = others end else - map_key = context.compact_iri('@none', vocab: true, quiet: true) + map_key = context.compact_iri('@none', vocab: true) end end # Note, if compacted_item is a node reference and key is @id-valued, then this could be compacted further. compacted_item - when CONTAINER_MAPPING_LANGUAGE + when container.include?('@language') map_key = expanded_item['@language'] value?(expanded_item) ? expanded_item['@value'] : compacted_item - when CONTAINER_MAPPING_TYPE + when container.include?('@type') map_key, *types = Array(compacted_item[container_key]) case types.length when 0 then compacted_item.delete(container_key) when 1 then compacted_item[container_key] = types.first else compacted_item[container_key] = types @@ -308,10 +301,10 @@ if compacted_item.keys.length == 1 && expanded_item.keys.include?('@id') compacted_item = compact({'@id' => expanded_item['@id']}, property: item_active_property) end compacted_item end - map_key ||= context.compact_iri('@none', vocab: true, quiet: true) + map_key ||= context.compact_iri('@none', vocab: true) add_value(map_object, map_key, compacted_item, property_is_array: as_array) else add_value(nest_result, item_active_property, compacted_item, property_is_array: as_array)