lutaml-model 0.8.204 → 0.8.205
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/docs/_guides/native-engines.adoc +10 -0
- data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
- data/lib/lutaml/model/serialize/initialization.rb +2 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/leptris/native_emission.rb +81 -3
- data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
- data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
- data/lib/lutaml/xml/leptris/plan_serializer.rb +526 -100
- data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
- data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
- data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
- data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
- data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
- metadata +3 -2
|
@@ -18,6 +18,7 @@ module Lutaml
|
|
|
18
18
|
# Row kinds whose child type/plan resolve once per plan (see
|
|
19
19
|
# child_row_plans).
|
|
20
20
|
CHILD_ROW_KINDS = %i[nested ordered_deferred].freeze
|
|
21
|
+
XSI_URI = "http://www.w3.org/2001/XMLSchema-instance"
|
|
21
22
|
|
|
22
23
|
class << self
|
|
23
24
|
# plan: the compiled plan entry for model_class
|
|
@@ -50,15 +51,15 @@ module Lutaml
|
|
|
50
51
|
buckets = if node && plan[:needs_nodes]
|
|
51
52
|
element_buckets(node, plan[:contested_names])
|
|
52
53
|
end
|
|
53
|
-
attr_kwargs = attributes_kwargs(plan, value, node)
|
|
54
|
+
attr_kwargs = attributes_kwargs(plan, value, node, register)
|
|
54
55
|
child_kwargs, children, delegates, run_children =
|
|
55
56
|
children_kwargs(model_class, plan, value, buckets, register,
|
|
56
|
-
intern)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
intern, node: node)
|
|
58
|
+
# A collection absent from the document keeps the
|
|
59
|
+
# deserialization seed's lazy sentinel and its default mark
|
|
60
|
+
# — the interpretive parse's state. The reader materializes
|
|
61
|
+
# [], and a later nil write leaves the untouched collection
|
|
62
|
+
# empty rather than nil (the parsed-model mutation contract).
|
|
62
63
|
# TODO.max-perf/33: the bulk constructor — allocate +
|
|
63
64
|
# compiled writers, from_hash semantics — measured ~30x on
|
|
64
65
|
# hydration-heavy corpora versus the generic constructor.
|
|
@@ -97,7 +98,7 @@ module Lutaml
|
|
|
97
98
|
end
|
|
98
99
|
interpret_deferred(model_class, plan, value, instance, node,
|
|
99
100
|
buckets, run_children)
|
|
100
|
-
route_delegates(delegates, instance)
|
|
101
|
+
route_delegates(delegates, instance, register)
|
|
101
102
|
CollectionConsolidation.call(instance, register)
|
|
102
103
|
instance
|
|
103
104
|
end
|
|
@@ -188,7 +189,8 @@ module Lutaml
|
|
|
188
189
|
Lutaml::Model::Config.default_register
|
|
189
190
|
end
|
|
190
191
|
|
|
191
|
-
def attributes_kwargs(plan, value, node = nil)
|
|
192
|
+
def attributes_kwargs(plan, value, node = nil, register = nil)
|
|
193
|
+
register ||= Lutaml::Model::Config.default_register
|
|
192
194
|
kwargs = {}
|
|
193
195
|
sole_claimants = sole_claimant_names(plan)
|
|
194
196
|
ns_attr_names = plan[:ns_attr_names]
|
|
@@ -219,7 +221,8 @@ module Lutaml
|
|
|
219
221
|
if rule.as_list && rule.as_list[:import]
|
|
220
222
|
v = rule.as_list[:import].call(v)
|
|
221
223
|
end
|
|
222
|
-
kwargs[attr.name.to_sym] =
|
|
224
|
+
kwargs[attr.name.to_sym] = import_transformed(rule, attr,
|
|
225
|
+
xml_hooked_cast(attr, v, register))
|
|
223
226
|
end
|
|
224
227
|
kwargs
|
|
225
228
|
end
|
|
@@ -250,11 +253,10 @@ module Lutaml
|
|
|
250
253
|
false
|
|
251
254
|
end
|
|
252
255
|
|
|
253
|
-
#
|
|
254
|
-
#
|
|
255
|
-
# rule
|
|
256
|
-
#
|
|
257
|
-
# other directive means uninitialized, i.e. no binding.
|
|
256
|
+
# An empty capture binds through the rule's value map: a
|
|
257
|
+
# blank element through the :empty key — the :empty directive
|
|
258
|
+
# (the plain-rule default) keeps it as "", :nil binds nil, and
|
|
259
|
+
# any other directive means uninitialized, i.e. no binding.
|
|
258
260
|
def mapped_empty_value(rule)
|
|
259
261
|
case rule.value_map(:from)[:empty]
|
|
260
262
|
when :nil then nil
|
|
@@ -263,14 +265,54 @@ module Lutaml
|
|
|
263
265
|
end
|
|
264
266
|
end
|
|
265
267
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
+
# An xsi:nil="true" element binds through the :nil key
|
|
269
|
+
# (Attribute#apply_value_map's contract — the interpretive
|
|
270
|
+
# nil_element? read).
|
|
271
|
+
def mapped_nil_value(rule, attr)
|
|
272
|
+
case rule.value_map(:from)[:nil]
|
|
273
|
+
when :nil then nil
|
|
274
|
+
when :empty then attr.collection? ? [] : ""
|
|
275
|
+
else :skip
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# An absent element's binding through the value_map :omitted key
|
|
280
|
+
# when it names a typed empty ("" or the empty collection). The
|
|
281
|
+
# default :nil directive leaves the attribute unbound — the
|
|
282
|
+
# constructor's default seeding and a writer-assigned value
|
|
283
|
+
# (#922) own an absent attribute, never an explicit nil.
|
|
284
|
+
def mapped_omitted_value(rule, attr)
|
|
285
|
+
if rule.value_map(:from)[:omitted] == :empty
|
|
286
|
+
attr.collection? ? [] : ""
|
|
287
|
+
else
|
|
288
|
+
:skip
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def empty_capture_value(rule, attr, marked)
|
|
293
|
+
marked ? mapped_nil_value(rule, attr) : mapped_empty_value(rule)
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# A collection's captured items. One blank or nil-marked
|
|
297
|
+
# element is an empty collection (the interpretive
|
|
298
|
+
# normalized_value_for_attr rule). A multi-item capture is
|
|
299
|
+
# left alone by the interpretive value map — items bind raw,
|
|
300
|
+
# a blank element staying "" — except that a nil-marked
|
|
301
|
+
# element reads nil (the nil_element? rule).
|
|
302
|
+
def capture_collection_values(values, rule, attr, name, node)
|
|
303
|
+
return values unless attr.collection?
|
|
304
|
+
return [] if values.length == 1 &&
|
|
305
|
+
(values[0].nil? || values[0] == "")
|
|
306
|
+
return values unless values.include?(nil) || values.include?("")
|
|
268
307
|
|
|
269
|
-
|
|
308
|
+
marks = xsi_nil_marks(node)
|
|
270
309
|
out = []
|
|
271
|
-
values.
|
|
272
|
-
if item.nil?
|
|
273
|
-
out <<
|
|
310
|
+
values.each_with_index do |item, rank|
|
|
311
|
+
if marked?(marks, name, rank) && (item.nil? || item == "")
|
|
312
|
+
out << nil
|
|
313
|
+
elsif item.nil?
|
|
314
|
+
bound = mapped_empty_value(rule)
|
|
315
|
+
out << bound unless bound == :skip
|
|
274
316
|
else
|
|
275
317
|
out << item
|
|
276
318
|
end
|
|
@@ -278,6 +320,73 @@ module Lutaml
|
|
|
278
320
|
out
|
|
279
321
|
end
|
|
280
322
|
|
|
323
|
+
# The interpretive reader's two transform lanes
|
|
324
|
+
# (handle_transform_method): a hash/proc transform on the rule or
|
|
325
|
+
# the attribute rides ImportTransformer (transform_dispatch
|
|
326
|
+
# :import); a class transform rides transform_value.
|
|
327
|
+
def import_transformed(rule, attr, value)
|
|
328
|
+
if Lutaml::Model::MappingRule.transform_dispatch(rule, attr) == :import
|
|
329
|
+
return Lutaml::Model::ImportTransformer.call(value, rule, attr,
|
|
330
|
+
context: nil)
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
if rule.transform.is_a?(Class)
|
|
334
|
+
rule.transform_value(attr, value, :from, :xml)
|
|
335
|
+
else
|
|
336
|
+
value
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# The interpretive reader casts a Value type that hooks the XML
|
|
341
|
+
# format through its own class method (from_xml) via the
|
|
342
|
+
# attribute's format-aware cast (normalize_xml_value); plain
|
|
343
|
+
# scalars stay raw for the constructor's cast.
|
|
344
|
+
def xml_hooked_cast(attr, raw, register)
|
|
345
|
+
return raw if raw.nil?
|
|
346
|
+
return raw unless PlanSerializer.custom_xml_type?(attr.type(register))
|
|
347
|
+
|
|
348
|
+
attr.cast(raw, :xml, register)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# Child elements carrying xsi:nil="true", as
|
|
352
|
+
# {wire name => {same-name sibling rank => true}} — the
|
|
353
|
+
# engine walk does not surface the mark, so it is read from
|
|
354
|
+
# the source node on the first empty capture that needs it.
|
|
355
|
+
# Without a source node (a nested model hydrated from walk
|
|
356
|
+
# values alone) nothing is marked.
|
|
357
|
+
def xsi_nil_marks(node)
|
|
358
|
+
return {} unless node
|
|
359
|
+
|
|
360
|
+
marks = {}
|
|
361
|
+
ranks = ::Hash.new(0)
|
|
362
|
+
node.element_children.each do |child|
|
|
363
|
+
rank = ranks[child.name]
|
|
364
|
+
ranks[child.name] = rank + 1
|
|
365
|
+
next unless child["xsi:nil"] == "true"
|
|
366
|
+
|
|
367
|
+
(marks[child.name] ||= {})[rank] = true
|
|
368
|
+
end
|
|
369
|
+
marks
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
def marked?(marks, name, rank)
|
|
373
|
+
!!marks&.dig(name, rank)
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
# Whether a document carries xsi:nil="true" below the root's
|
|
377
|
+
# own children. A nested model hydrated from walk values alone
|
|
378
|
+
# has no source node to read the mark from, so such a
|
|
379
|
+
# document rides the interpretive lane; only a document that
|
|
380
|
+
# declares the instance namespace pays the scan. Both plan
|
|
381
|
+
# entry points (from_xml, PlanWalk) consult it.
|
|
382
|
+
def xsi_nil_below_root?(root)
|
|
383
|
+
return false unless root.namespace_definitions
|
|
384
|
+
.any? { |ns| ns.href == XSI_URI }
|
|
385
|
+
|
|
386
|
+
root.xpath("*//@xsi:nil").any? { |attr| attr.value == "true" }
|
|
387
|
+
end
|
|
388
|
+
public :xsi_nil_below_root?
|
|
389
|
+
|
|
281
390
|
# Wire-name strings are plan-frozen; materializing them per
|
|
282
391
|
# hydration allocated two or three strings per attribute row
|
|
283
392
|
# per instance.
|
|
@@ -292,7 +401,7 @@ module Lutaml
|
|
|
292
401
|
# parent/root links once the parent exists; delegate values
|
|
293
402
|
# wait for the instance (their target object must exist).
|
|
294
403
|
def children_kwargs(_model_class, plan, value, buckets = nil,
|
|
295
|
-
register = nil, intern = {})
|
|
404
|
+
register = nil, intern = {}, node: nil)
|
|
296
405
|
register ||= Lutaml::Model::Config.default_register
|
|
297
406
|
grouped, tagged, run_children = group_children(value,
|
|
298
407
|
plan[:row_tags])
|
|
@@ -300,6 +409,9 @@ module Lutaml
|
|
|
300
409
|
kwargs = {}
|
|
301
410
|
children = []
|
|
302
411
|
delegates = []
|
|
412
|
+
# xsi:nil marks of the source node's children — read on the
|
|
413
|
+
# first empty capture that needs them (see xsi_nil_marks).
|
|
414
|
+
marks = nil
|
|
303
415
|
# Lazy: models without multi-spelling rows never touch it, and
|
|
304
416
|
# the block-backed Hash is a fresh object per hydrated row.
|
|
305
417
|
spellings = nil
|
|
@@ -324,27 +436,47 @@ module Lutaml
|
|
|
324
436
|
# Raw passthrough: the model constructor is the single
|
|
325
437
|
# cast authority — pre-casting here doubled every cast.
|
|
326
438
|
# Class transforms still apply before assignment.
|
|
327
|
-
|
|
439
|
+
explicit_nil = false
|
|
440
|
+
captures = tag ? tagged[tag] : grouped[name]
|
|
441
|
+
if captures && captures.size > 1
|
|
328
442
|
# Interpretive parity: several captures into a
|
|
329
|
-
# non-collection attribute arrive as the full array
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
443
|
+
# non-collection attribute arrive as the full array —
|
|
444
|
+
# the over-count reaches `.validate` (Issue #185).
|
|
445
|
+
v = []
|
|
446
|
+
captures.each_with_index do |val, rank|
|
|
447
|
+
raw = val.string_value
|
|
448
|
+
if raw.nil? || raw == ""
|
|
449
|
+
marks ||= xsi_nil_marks(node)
|
|
450
|
+
bound = empty_capture_value(rule, attr,
|
|
451
|
+
marked?(marks, name, rank))
|
|
452
|
+
v << bound unless bound == :skip
|
|
453
|
+
else
|
|
454
|
+
v << xml_hooked_cast(attr, raw, register)
|
|
455
|
+
end
|
|
334
456
|
end
|
|
335
457
|
else
|
|
336
|
-
first =
|
|
337
|
-
tagged[tag]&.first
|
|
338
|
-
else
|
|
339
|
-
grouped.dig(name, 0)
|
|
340
|
-
end
|
|
458
|
+
first = captures&.first
|
|
341
459
|
v = first&.string_value
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
460
|
+
if first && (v.nil? || v == "")
|
|
461
|
+
marks ||= xsi_nil_marks(node)
|
|
462
|
+
explicit_nil = marked?(marks, name, 0)
|
|
463
|
+
v = empty_capture_value(rule, attr, explicit_nil)
|
|
464
|
+
elsif captures.nil? || captures.empty?
|
|
465
|
+
# No capture at all: the attribute's omitted state
|
|
466
|
+
# binds through the value_map :omitted key (the
|
|
467
|
+
# interpretive apply_value_map on the uninitialized
|
|
468
|
+
# sentinel) — nil, the typed empty, or nothing.
|
|
469
|
+
v = mapped_omitted_value(rule, attr)
|
|
470
|
+
end
|
|
347
471
|
end
|
|
472
|
+
# A bound nil is a state of its own (the nil-marked
|
|
473
|
+
# capture): it is assigned, not left to the default.
|
|
474
|
+
next if v == :skip
|
|
475
|
+
next if v.nil? && !explicit_nil
|
|
476
|
+
|
|
477
|
+
v = import_transformed(rule, attr,
|
|
478
|
+
xml_hooked_cast(attr, v, register))
|
|
479
|
+
assign(kwargs, delegates, delegate, rule, attr, v)
|
|
348
480
|
when :wildcard_any
|
|
349
481
|
# interpreted post-instance (interpret_deferred)
|
|
350
482
|
when :content
|
|
@@ -391,7 +523,8 @@ module Lutaml
|
|
|
391
523
|
next if elements.empty?
|
|
392
524
|
|
|
393
525
|
results = elements.map do |element|
|
|
394
|
-
attr.cast(element, :xml, register
|
|
526
|
+
attr.cast(element, :xml, register,
|
|
527
|
+
**deferred_cast_options(rule))
|
|
395
528
|
end
|
|
396
529
|
unless results.empty?
|
|
397
530
|
children.concat(results)
|
|
@@ -402,10 +535,10 @@ module Lutaml
|
|
|
402
535
|
values = grouped[name].to_a.map do |item|
|
|
403
536
|
item&.string_value
|
|
404
537
|
end
|
|
405
|
-
values =
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
538
|
+
values = capture_collection_values(values, rule, attr, name,
|
|
539
|
+
node)
|
|
540
|
+
values = values.map { |item| xml_hooked_cast(attr, item, register) }
|
|
541
|
+
values = import_transformed(rule, attr, values)
|
|
409
542
|
unless values.empty?
|
|
410
543
|
assign(kwargs, delegates, delegate, rule, attr, values)
|
|
411
544
|
end
|
|
@@ -415,8 +548,15 @@ module Lutaml
|
|
|
415
548
|
else
|
|
416
549
|
native_collection(value, name)
|
|
417
550
|
end
|
|
418
|
-
values = mapped_capture_values(values, rule) unless values.nil?
|
|
419
551
|
unless values.nil?
|
|
552
|
+
values = capture_collection_values(values, rule, attr, name,
|
|
553
|
+
node)
|
|
554
|
+
end
|
|
555
|
+
unless values.nil?
|
|
556
|
+
values = values.map do |item|
|
|
557
|
+
xml_hooked_cast(attr, item, register)
|
|
558
|
+
end
|
|
559
|
+
values = import_transformed(rule, attr, values)
|
|
420
560
|
assign(kwargs, delegates, delegate, rule, attr, values)
|
|
421
561
|
end
|
|
422
562
|
when :spelling
|
|
@@ -537,8 +677,7 @@ module Lutaml
|
|
|
537
677
|
|
|
538
678
|
results = deferred_elements(value, wire_name, buckets).map do |element|
|
|
539
679
|
attr.cast(element, :xml, register,
|
|
540
|
-
|
|
541
|
-
lutaml_root: instance.lutaml_root || instance)
|
|
680
|
+
**deferred_cast_options(rule, instance))
|
|
542
681
|
end
|
|
543
682
|
next if results.empty?
|
|
544
683
|
|
|
@@ -580,9 +719,7 @@ module Lutaml
|
|
|
580
719
|
when :polymorphic
|
|
581
720
|
results = deferred_elements(value, wire_name, buckets).map do |element|
|
|
582
721
|
attr.cast(element, :xml, register,
|
|
583
|
-
|
|
584
|
-
lutaml_parent: instance,
|
|
585
|
-
lutaml_root: instance.lutaml_root || instance)
|
|
722
|
+
**deferred_cast_options(rule, instance))
|
|
586
723
|
end
|
|
587
724
|
next if results.empty?
|
|
588
725
|
|
|
@@ -592,6 +729,20 @@ module Lutaml
|
|
|
592
729
|
end
|
|
593
730
|
end
|
|
594
731
|
|
|
732
|
+
# The interpretive cast options for a deferred subtree
|
|
733
|
+
# (value_for_rule's base options): the parent links, and the
|
|
734
|
+
# rule's polymorphic resolution — a child plan that could not
|
|
735
|
+
# compile must not drop it (the subclass identity).
|
|
736
|
+
def deferred_cast_options(rule, instance = nil)
|
|
737
|
+
options = {}
|
|
738
|
+
if instance
|
|
739
|
+
options[:lutaml_parent] = instance
|
|
740
|
+
options[:lutaml_root] = instance.lutaml_root || instance
|
|
741
|
+
end
|
|
742
|
+
options[:polymorphic] = rule.polymorphic if rule.polymorphic
|
|
743
|
+
options
|
|
744
|
+
end
|
|
745
|
+
|
|
595
746
|
# Wrapper elements for deferred rows: the ORIGINAL source
|
|
596
747
|
# nodes bridged through moxml's wrapper cache (wrapper
|
|
597
748
|
# construction only, no parse), falling back to a fragment
|
|
@@ -633,14 +784,16 @@ module Lutaml
|
|
|
633
784
|
@bridge_context ||= Moxml::Context.new(:leptris)
|
|
634
785
|
end
|
|
635
786
|
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
787
|
+
# Rows carry the delegate NAME (the interpretive
|
|
788
|
+
# MappingRule#deserialize contract: the target attribute's
|
|
789
|
+
# type instantiates an absent target).
|
|
790
|
+
def route_delegates(delegates, instance, register)
|
|
791
|
+
attributes = instance.class.attributes(register)
|
|
792
|
+
delegates.each do |_rule, delegate, attr, v|
|
|
793
|
+
target = instance.public_send(delegate)
|
|
639
794
|
unless target
|
|
640
|
-
|
|
641
|
-
#
|
|
642
|
-
target = delegate_attr.type(register).new
|
|
643
|
-
instance.public_send(:"#{rule.delegate}=", target)
|
|
795
|
+
target = attributes[delegate].type(register).new
|
|
796
|
+
instance.public_send(:"#{delegate}=", target)
|
|
644
797
|
end
|
|
645
798
|
target.public_send(:"#{attr.name}=", v)
|
|
646
799
|
end
|