Skip to content

Commit cfb418d

Browse files
committed
Don't pass ordered option, get it from @options.
1 parent d42c565 commit cfb418d

File tree

4 files changed

+16
-40
lines changed

4 files changed

+16
-40
lines changed

lib/json/ld/api.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ def self.expand(input, framing: false, **options, &block)
167167
API.new(input, options[:expandContext], **options) do
168168
result = self.expand(self.value, nil, self.context,
169169
base: (RDF::URI(@options[:base]) if @options[:base]),
170-
ordered: @options[:ordered],
171170
framing: framing)
172171
doc_base = @options[:base]
173172
end
@@ -226,8 +225,7 @@ def self.compact(input, context, expanded: false, **options)
226225
API.new(expanded_input, context, no_default_base: true, **options) do
227226
log_debug(".compact") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
228227
result = compact(value,
229-
base: (RDF::URI(@options[:base]) if @options[:base]),
230-
ordered: options[:ordered])
228+
base: (RDF::URI(@options[:base]) if @options[:base]))
231229

232230
# xxx) Add the given context to the output
233231
ctx = self.context.serialize(provided_context: context)
@@ -299,8 +297,7 @@ def self.flatten(input, context, expanded: false, **options)
299297
# Otherwise, return the result of compacting flattened according the Compaction algorithm passing context ensuring that the compaction result uses the @graph keyword (or its alias) at the top-level, even if the context is empty or if there is only one element to put in the @graph array. This ensures that the returned document has a deterministic structure.
300298
compacted = as_array(
301299
compact(flattened,
302-
base: (RDF::URI(options[:base]) if options[:base]),
303-
ordered: options[:ordered]))
300+
base: (RDF::URI(options[:base]) if options[:base])))
304301
kwgraph = self.context.compact_iri('@graph')
305302
flattened = self.context.
306303
serialize(provided_context: context).
@@ -435,8 +432,7 @@ def self.frame(input, frame, expanded: false, **options)
435432

436433
# Compact result
437434
compacted = compact(result,
438-
base: (RDF::URI(options[:base]) if options[:base]),
439-
ordered: options[:ordered])
435+
base: (RDF::URI(options[:base]) if options[:base]))
440436

441437
# @replace `@null` with nil, compacting arrays
442438
compacted = cleanup_null(compacted)

lib/json/ld/compact.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ module Compact
1414
# @param [Array, Hash] element
1515
# @param [String, RDF::URI] base (nil)
1616
# Ensure output objects have keys ordered properly
17-
# @param [Boolean] ordered (true)
1817
# @param [String] property (nil)
1918
# Extra validatation
2019
# @return [Array, Hash]
2120
def compact(element,
2221
base: nil,
23-
ordered: false,
2422
property: nil)
2523
#if property.nil?
2624
# log_debug("compact") {"element: #{element.inspect}, ec: #{context.inspect}"}
@@ -35,7 +33,7 @@ def compact(element,
3533
when Array
3634
#log_debug("") {"Array #{element.inspect}"}
3735
result = element.map do |item|
38-
compact(item, base: base, ordered: ordered, property: property)
36+
compact(item, base: base, property: property)
3937
end.compact
4038

4139
# If element has a single member and the active property has no
@@ -78,7 +76,7 @@ def compact(element,
7876
# If expanded property is @list and we're contained within a list container, recursively compact this item to an array
7977
if list?(element) && context.container(property).include?('@list')
8078
return compact(element['@list'], base: base,
81-
ordered: ordered, property: property)
79+
property: property)
8280
end
8381

8482
inside_reverse = property == '@reverse'
@@ -94,7 +92,7 @@ def compact(element,
9492
self.context = context.parse(term_context, propagate: false) unless term_context.nil?
9593
end
9694

97-
element.keys.opt_sort(ordered: ordered).each do |expanded_property|
95+
element.keys.opt_sort(ordered: @options[:ordered]).each do |expanded_property|
9896
expanded_value = element[expanded_property]
9997
#log_debug("") {"#{expanded_property}: #{expanded_value.inspect}"}
10098

@@ -126,7 +124,7 @@ def compact(element,
126124

127125
if expanded_property == '@reverse'
128126
compacted_value = compact(expanded_value, base: base,
129-
ordered: ordered, property: '@reverse')
127+
property: '@reverse')
130128
#log_debug("@reverse") {"compacted_value: #{compacted_value.inspect}"}
131129
# handle double-reversed properties
132130
compacted_value.each do |prop, value|
@@ -148,7 +146,7 @@ def compact(element,
148146
if expanded_property == '@preserve'
149147
# Compact using `property`
150148
compacted_value = compact(expanded_value, base: base,
151-
ordered: ordered, property: property)
149+
property: property)
152150
#log_debug("@preserve") {"compacted_value: #{compacted_value.inspect}"}
153151

154152
unless compacted_value.is_a?(Array) && compacted_value.empty?
@@ -213,7 +211,7 @@ def compact(element,
213211
end
214212

215213
compacted_item = compact(value, base: base,
216-
ordered: ordered, property: item_active_property)
214+
property: item_active_property)
217215
#log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"}
218216

219217
# handle @list
@@ -317,7 +315,6 @@ def compact(element,
317315
if compacted_item.keys.length == 1 && expanded_item.keys.include?('@id')
318316
compacted_item = compact({'@id' => expanded_item['@id']},
319317
base: base,
320-
ordered: ordered,
321318
property: item_active_property)
322319
end
323320
compacted_item

lib/json/ld/expand.rb

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ module Expand
2727
# Special rules for expanding a frame
2828
# @param [Boolean] from_map
2929
# Expanding from a map, which could be an `@type` map, so don't clear out context term definitions
30-
# @param [Boolean] ordered (true)
31-
# Ensure output objects have keys ordered properly
3230
#
3331
# @return [Array<Hash{String => Object}>]
3432
def expand(input, active_property, context,
3533
base: nil, framing: false,
36-
from_map: false, ordered: true, log_depth: nil)
34+
from_map: false, log_depth: nil)
3735
log_debug("expand", depth: log_depth.to_i) {"input: #{input.inspect}, active_property: #{active_property.inspect}, context: #{context.inspect}"}
3836
framing = false if active_property == '@default'
3937
expanded_active_property = context.expand_iri(active_property, vocab: true, as_string: true, base: base) if active_property
@@ -50,7 +48,6 @@ def expand(input, active_property, context,
5048
# Initialize expanded item to the result of using this algorithm recursively, passing active context, active property, and item as element.
5149
v = expand(v, active_property, context,
5250
base: base,
53-
ordered: ordered,
5451
framing: framing,
5552
from_map: from_map,
5653
log_depth: log_depth.to_i + 1)
@@ -119,7 +116,6 @@ def expand(input, active_property, context,
119116
base: base,
120117
expanded_active_property: expanded_active_property,
121118
framing: framing,
122-
ordered: ordered,
123119
type_key: type_key,
124120
type_scoped_context: type_scoped_context,
125121
log_depth: log_depth.to_i + 1)
@@ -188,7 +184,7 @@ def expand(input, active_property, context,
188184
end
189185

190186
# Re-order result keys if ordering
191-
if ordered
187+
if @options[:ordered]
192188
output_object.keys.sort.each_with_object({}) {|kk, memo| memo[kk] = output_object[kk]}
193189
else
194190
output_object
@@ -221,15 +217,14 @@ def expand_object(input, active_property, context, output_object,
221217
framing:,
222218
type_key:,
223219
type_scoped_context:,
224-
ordered:,
225220
log_depth: nil)
226221
nests = []
227222

228223
input_type = Array(input[type_key]).last
229224
input_type = context.expand_iri(input_type, vocab: true, as_string: true, base: base) if input_type
230225

231226
# Then, proceed and process each property and value in element as follows:
232-
keys = ordered ? input.keys.sort : input.keys
227+
keys = @options[:ordered] ? input.keys.sort : input.keys
233228
keys.each do |key|
234229
# For each key and value in element, ordered lexicographically by key:
235230
value = input[key]
@@ -300,7 +295,6 @@ def expand_object(input, active_property, context, output_object,
300295
included_result = as_array(expand(value, active_property, context,
301296
base: base,
302297
framing: framing,
303-
ordered: ordered,
304298
log_depth: log_depth.to_i + 1))
305299

306300
# Expanded values must be node objects
@@ -362,7 +356,6 @@ def expand_object(input, active_property, context, output_object,
362356
value = expand(value, '@graph', context,
363357
base: base,
364358
framing: framing,
365-
ordered: ordered,
366359
log_depth: log_depth.to_i + 1)
367360
as_array(value)
368361
when '@value'
@@ -458,7 +451,6 @@ def expand_object(input, active_property, context, output_object,
458451
value = expand(value, active_property, context,
459452
base: base,
460453
framing: framing,
461-
ordered: ordered,
462454
log_depth: log_depth.to_i + 1)
463455

464456
# Spec FIXME: need to be sure that result is an array
@@ -470,7 +462,6 @@ def expand_object(input, active_property, context, output_object,
470462
expand(value, active_property, context,
471463
base: base,
472464
framing: framing,
473-
ordered: ordered,
474465
log_depth: log_depth.to_i + 1)
475466
when '@reverse'
476467
# If expanded property is @reverse and value is not a JSON object, an invalid @reverse value error has been detected and processing is aborted.
@@ -482,7 +473,6 @@ def expand_object(input, active_property, context, output_object,
482473
value = expand(value, '@reverse', context,
483474
base: base,
484475
framing: framing,
485-
ordered: ordered,
486476
log_depth: log_depth.to_i + 1)
487477

488478
# 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:
@@ -519,7 +509,6 @@ def expand_object(input, active_property, context, output_object,
519509
[expand(value, expanded_property, context,
520510
base: base,
521511
framing: framing,
522-
ordered: ordered,
523512
log_depth: log_depth.to_i + 1)
524513
].flatten
525514
when '@nest'
@@ -549,7 +538,7 @@ def expand_object(input, active_property, context, output_object,
549538
ary = []
550539

551540
# For each key-value pair language-language value in value, ordered lexicographically by language
552-
keys = ordered ? value.keys.sort : value.keys
541+
keys = @options[:ordered] ? value.keys.sort : value.keys
553542
keys.each do |k|
554543
expanded_k = context.expand_iri(k, vocab: true, as_string: true, base: base)
555544

@@ -594,7 +583,7 @@ def expand_object(input, active_property, context, output_object,
594583
end
595584

596585
# For each key-value in the object:
597-
keys = ordered ? value.keys.sort : value.keys
586+
keys = @options[:ordered] ? value.keys.sort : value.keys
598587
keys.each do |k|
599588
# If container mapping in the active context includes @type, and k is a term in the active context having a local context, use that context when expanding values
600589
map_context = container_context.term_definitions[k].context if container.include?('@type') && container_context.term_definitions[k]
@@ -612,7 +601,6 @@ def expand_object(input, active_property, context, output_object,
612601
base: base,
613602
framing: framing,
614603
from_map: true,
615-
ordered: ordered,
616604
log_depth: log_depth.to_i + 1)
617605
index_value.each do |item|
618606
case
@@ -653,7 +641,6 @@ def expand_object(input, active_property, context, output_object,
653641
expand(value, key, context,
654642
base: base,
655643
framing: framing,
656-
ordered: ordered,
657644
log_depth: log_depth.to_i + 1)
658645
end
659646

@@ -725,7 +712,6 @@ def expand_object(input, active_property, context, output_object,
725712
base: base,
726713
framing: framing,
727714
expanded_active_property: expanded_active_property,
728-
ordered: ordered,
729715
type_key: type_key,
730716
type_scoped_context: type_scoped_context,
731717
log_depth: log_depth.to_i + 1)

script/tc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ def run_tc(man, tc, options)
158158
exit 1
159159
rescue StandardError => e
160160
result = if tc.positiveTest?
161-
STDERR.puts "#{"exception:" unless options[:quiet]}: #{e}"
162-
if options[:quiet]
161+
STDERR.puts "#{" exception" unless options[:quiet]}: #{e}"
162+
if options[:quiet] || !options[:verbose]
163163
options[:results]['failed'] ||= 0
164164
options[:results]['failed'] += 1
165165
else
@@ -216,7 +216,6 @@ opts = GetoptLong.new(
216216
["--quiet", "-q", GetoptLong::NO_ARGUMENT],
217217
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT],
218218
["--stream", GetoptLong::NO_ARGUMENT],
219-
["--validate", GetoptLong::NO_ARGUMENT],
220219
["--verbose", "-v", GetoptLong::NO_ARGUMENT]
221220
)
222221

@@ -228,7 +227,6 @@ def help(options)
228227
puts " --quiet: Minimal output"
229228
puts " --output: Output to specified file"
230229
puts " --stream: Use streaming RDF reader/writer"
231-
puts " --validate: Validate input"
232230
puts " --verbose: Verbose processing"
233231
puts " --help,-?: This message"
234232
exit(0)
@@ -246,7 +244,6 @@ opts.each do |opt, arg|
246244
options[:quiet] = true
247245
logger.level = Logger::FATAL
248246
when '--stream' then options[:stream] = true
249-
when '--validate' then options[:validate] = true
250247
when '--verbose' then options[:verbose] = true
251248
end
252249
end

0 commit comments

Comments
 (0)