lib/json/ld/compact.rb in json-ld-3.1.4 vs lib/json/ld/compact.rb in json-ld-3.1.5

- old
+ new

@@ -17,68 +17,68 @@ # @param [String] property (nil) # Extra validatation # @return [Array, Hash] def compact(element, base: nil, - property: nil) - #if property.nil? - # log_debug("compact") {"element: #{element.inspect}, ec: #{context.inspect}"} - #else - # log_debug("compact") {"property: #{property.inspect}"} - #end + property: nil, + log_depth: nil) + log_debug("compact", depth: log_depth.to_i) {"element: #{element.inspect}, ec: #{context.inspect}"} # If the term definition for active property itself contains a context, use that for compacting values. input_context = self.context case element when Array #log_debug("") {"Array #{element.inspect}"} result = element.map do |item| - compact(item, base: base, property: property) + compact(item, base: base, property: property, log_depth: log_depth.to_i + 1) end.compact # If element has a single member and the active property has no # @container mapping to @list or @set, the compacted value is that # member; otherwise the compacted value is element if result.length == 1 && !context.as_array?(property) && @options[:compactArrays] - #log_debug("=> extract single element: #{result.first.inspect}") + log_debug("=> extract single element", depth: log_depth.to_i) {result.first.inspect} result.first else - #log_debug("=> array result: #{result.inspect}") + log_debug("=> array result", depth: log_depth.to_i) {result.inspect} result end when Hash # Otherwise element is a JSON object. # @null objects are used in framing return nil if element.key?('@null') # Revert any previously type-scoped (non-preserved) context if context.previous_context && !element.key?('@value') && element.keys != %w(@id) + log_debug("revert ec", depth: log_depth.to_i) {"previous context: #{context.previous_context.inspect}"} self.context = context.previous_context end # Look up term definintions from property using the original type-scoped context, if it exists, but apply them to the now current previous context td = input_context.term_definitions[property] if property if td && !td.context.nil? self.context = context.parse(td.context, override_protected: true) + log_debug("prop-scoped", depth: log_depth.to_i) {"context: #{self.context.inspect}"} end if element.key?('@id') || element.key?('@value') result = context.compact_value(property, element, base: @options[:base]) if !result.is_a?(Hash) || context.coerce(property) == '@json' - #log_debug("") {"=> scalar result: #{result.inspect}"} + log_debug("", depth: log_depth.to_i) {"=> scalar result: #{result.inspect}"} 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).include?('@list') return compact(element['@list'], base: base, - property: property) + property: property, + log_depth: log_depth.to_i + 1) end inside_reverse = property == '@reverse' result, nest_result = {}, nil @@ -88,15 +88,16 @@ map {|expanded_type| context.compact_iri(expanded_type, vocab: true)}. sort. each do |term| term_context = input_context.term_definitions[term].context if input_context.term_definitions[term] self.context = context.parse(term_context, propagate: false) unless term_context.nil? + log_debug("type-scoped", depth: log_depth.to_i) {"context: #{self.context.inspect}"} end element.keys.opt_sort(ordered: @options[:ordered]).each do |expanded_property| expanded_value = element[expanded_property] - #log_debug("") {"#{expanded_property}: #{expanded_value.inspect}"} + log_debug("", depth: log_depth.to_i) {"#{expanded_property}: #{expanded_value.inspect}"} if expanded_property == '@id' compacted_value = Array(expanded_value).map do |expanded_id| context.compact_iri(expanded_id, base: @options[:base]) end @@ -122,12 +123,13 @@ next end if expanded_property == '@reverse' compacted_value = compact(expanded_value, base: base, - property: '@reverse') - #log_debug("@reverse") {"compacted_value: #{compacted_value.inspect}"} + property: '@reverse', + log_depth: log_depth.to_i + 1) + log_debug("@reverse", depth: log_depth.to_i) {"compacted_value: #{compacted_value.inspect}"} # handle double-reversed properties compacted_value.each do |prop, value| if context.reverse?(prop) add_value(result, prop, value, property_is_array: context.as_array?(prop) || !@options[:compactArrays]) @@ -135,37 +137,38 @@ end end unless compacted_value.empty? al = context.compact_iri('@reverse') - #log_debug("") {"remainder: #{al} => #{compacted_value.inspect}"} + log_debug("", depth: log_depth.to_i) {"remainder: #{al} => #{compacted_value.inspect}"} result[al] = compacted_value end next end if expanded_property == '@preserve' # Compact using `property` compacted_value = compact(expanded_value, base: base, - property: property) - #log_debug("@preserve") {"compacted_value: #{compacted_value.inspect}"} + property: property, + log_depth: log_depth.to_i + 1) + log_debug("@preserve", depth: log_depth.to_i) {"compacted_value: #{compacted_value.inspect}"} unless compacted_value.is_a?(Array) && compacted_value.empty? result['@preserve'] = compacted_value end next end if expanded_property == '@index' && context.container(property).include?('@index') - #log_debug("@index") {"drop @index"} + log_debug("@index", depth: log_depth.to_i) {"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) - #log_debug(expanded_property) {"#{al} => #{expanded_value.inspect}"} + log_debug(expanded_property, depth: log_depth.to_i) {"#{al} => #{expanded_value.inspect}"} result[al] = expanded_value next end if expanded_value.empty? @@ -209,12 +212,13 @@ when graph?(expanded_item) then expanded_item['@graph'] else expanded_item end compacted_item = compact(value, base: base, - property: item_active_property) - #log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"} + property: item_active_property, + log_depth: log_depth.to_i + 1) + log_debug("", depth: log_depth.to_i) {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"} # handle @list if list?(expanded_item) compacted_item = as_array(compacted_item) unless container.include?('@list') @@ -313,11 +317,12 @@ # if compacted_item contains a single entry who's key maps to @id, then recompact the item without @type if compacted_item.keys.length == 1 && expanded_item.keys.include?('@id') compacted_item = compact({'@id' => expanded_item['@id']}, base: base, - property: item_active_property) + property: item_active_property, + log_depth: log_depth.to_i + 1) end compacted_item end map_key ||= context.compact_iri('@none', vocab: true) add_value(map_object, map_key, compacted_item, @@ -330,10 +335,10 @@ end result else # For other types, the compacted value is the element value - #log_debug("compact") {element.class.to_s} + log_debug("compact", depth: log_depth.to_i) {element.class.to_s} element end ensure self.context = input_context