Skip to content

Commit

Permalink
When compacting @type or @language maps, account for compacted_item
Browse files Browse the repository at this point in the history
… not being a Hash.

Fixes #62.
  • Loading branch information
gkellogg committed Jul 29, 2024
1 parent 24a67f4 commit be7e7a5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/json/ld/compact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ def compact(element,
else
index_key = context.expand_iri(index_key, vocab: true)
container_key = context.compact_iri(index_key, vocab: true)
map_key, *others = Array(compacted_item[container_key])
map_key, *others = Array(compacted_item.is_a?(Hash) && compacted_item[container_key])
if map_key.is_a?(String)
case others.length
when 0 then compacted_item.delete(container_key)
when 0 then compacted_item.delete(container_key) if compacted_item.is_a?(Hash)
when 1 then compacted_item[container_key] = others.first
else compacted_item[container_key] = others
end
Expand All @@ -316,15 +316,15 @@ def compact(element,
map_key = expanded_item['@language']
value?(expanded_item) ? expanded_item['@value'] : compacted_item
elsif container.include?('@type')
map_key, *types = Array(compacted_item[container_key])
map_key, *types = Array(compacted_item.is_a?(Hash) && compacted_item[container_key])
case types.length
when 0 then compacted_item.delete(container_key)
when 0 then compacted_item.delete(container_key) if compacted_item.is_a?(Hash)
when 1 then compacted_item[container_key] = types.first
else compacted_item[container_key] = types
end

# 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.key?('@id')
if compacted_item.is_a?(Hash) && compacted_item.keys.length == 1 && expanded_item.key?('@id')
compacted_item = compact({ '@id' => expanded_item['@id'] },
base: base,
property: item_active_property,
Expand Down
34 changes: 34 additions & 0 deletions spec/compact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3448,6 +3448,40 @@
}
}),
processingMode: "json-ld-1.1"
},
"ruby-rdf/json-ld#62": {
input: %({
"@context": {
"@vocab": "http://schema.org/"
},
"@type": "Event",
"location": {
"@id": "http://kg.artsdata.ca/resource/K11-200"
}
}),
context: %({
"@context": {
"@vocab": "http://schema.org/",
"location": {
"@type": "@id",
"@container": "@type"
}
}
}),
output: %({
"@context": {
"@vocab": "http://schema.org/",
"location": {
"@type": "@id",
"@container": "@type"
}
},
"@type": "Event",
"location": {
"@none": "http://kg.artsdata.ca/resource/K11-200"
}
}),
processingMode: "json-ld-1.1"
}
}.each do |title, params|
it title do
Expand Down
35 changes: 35 additions & 0 deletions spec/frame_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,41 @@
}
}),
processingMode: "json-ld-1.1"
},
"ruby-rdf/json-ld#62": {
input: %({
"@context": {
"@vocab": "http://schema.org/"
},
"@type": "Event",
"location": {
"@id": "http://kg.artsdata.ca/resource/K11-200"
}
}),
frame: %({
"@context": {
"@vocab": "http://schema.org/",
"location": {
"@type": "@id",
"@container": "@type"
}
},
"@type": "Event"
}),
output: %({
"@context": {
"@vocab": "http://schema.org/",
"location": {
"@type": "@id",
"@container": "@type"
}
},
"@type": "Event",
"location": {
"@none": "http://kg.artsdata.ca/resource/K11-200"
}
}),
processingMode: "json-ld-1.1"
}
}.each do |title, params|
it title do
Expand Down

0 comments on commit be7e7a5

Please sign in to comment.