lutaml-model 0.8.204 → 0.8.206

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.
@@ -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
@@ -47,18 +48,19 @@ module Lutaml
47
48
  # duplicate name/URI strings inside the summaries. One
48
49
  # shared table per parse collapses them to distinct strings.
49
50
  intern ||= {}
50
- buckets = if node && plan[:needs_nodes]
51
+ buckets = if node && plan[:needs_nodes] && plan[:needs_buckets]
51
52
  element_buckets(node, plan[:contested_names])
52
53
  end
53
- attr_kwargs = attributes_kwargs(plan, value, node)
54
- child_kwargs, children, delegates, run_children =
54
+ attr_kwargs = attributes_kwargs(plan, value, node, register)
55
+ child_kwargs, children, delegates, run_children,
56
+ element_children =
55
57
  children_kwargs(model_class, plan, value, buckets, register,
56
- intern)
57
- plan[:collection_defaults].each do |name|
58
- next if attr_kwargs.key?(name) || child_kwargs.key?(name)
59
-
60
- child_kwargs[name] = []
61
- end
58
+ intern, node: node)
59
+ # A collection absent from the document keeps the
60
+ # deserialization seed's lazy sentinel and its default mark
61
+ # — the interpretive parse's state. The reader materializes
62
+ # [], and a later nil write leaves the untouched collection
63
+ # empty rather than nil (the parsed-model mutation contract).
62
64
  # TODO.max-perf/33: the bulk constructor — allocate +
63
65
  # compiled writers, from_hash semantics — measured ~30x on
64
66
  # hydration-heavy corpora versus the generic constructor.
@@ -75,8 +77,11 @@ module Lutaml
75
77
  instance.xml_namespace_prefix = node.prefix
76
78
  end
77
79
  # #909: order summaries are per-instance pinned records —
78
- # read-only consumers skip them via the config.
79
- if node && plan[:ordered] &&
80
+ # read-only consumers skip them via the config. Ordered and
81
+ # sequence models record them: the ordered walk replays
82
+ # them, and sequence validation reads the element names in
83
+ # document order (without the record it silently passes).
84
+ if node && (plan[:ordered] || plan[:sequence]) &&
80
85
  Lutaml::Model::Config.instance.element_order_tracking
81
86
  instance.element_order = PlanOrder.build(node, intern)
82
87
  end
@@ -96,8 +101,9 @@ module Lutaml
96
101
  child.lutaml_root ||= instance.lutaml_root || instance
97
102
  end
98
103
  interpret_deferred(model_class, plan, value, instance, node,
99
- buckets, run_children)
100
- route_delegates(delegates, instance)
104
+ buckets, run_children,
105
+ element_children: element_children)
106
+ route_delegates(delegates, instance, register)
101
107
  CollectionConsolidation.call(instance, register)
102
108
  instance
103
109
  end
@@ -188,7 +194,8 @@ module Lutaml
188
194
  Lutaml::Model::Config.default_register
189
195
  end
190
196
 
191
- def attributes_kwargs(plan, value, node = nil)
197
+ def attributes_kwargs(plan, value, node = nil, register = nil)
198
+ register ||= Lutaml::Model::Config.default_register
192
199
  kwargs = {}
193
200
  sole_claimants = sole_claimant_names(plan)
194
201
  ns_attr_names = plan[:ns_attr_names]
@@ -219,7 +226,8 @@ module Lutaml
219
226
  if rule.as_list && rule.as_list[:import]
220
227
  v = rule.as_list[:import].call(v)
221
228
  end
222
- kwargs[attr.name.to_sym] = v
229
+ kwargs[attr.name.to_sym] = import_transformed(rule, attr,
230
+ xml_hooked_cast(attr, v, register))
223
231
  end
224
232
  kwargs
225
233
  end
@@ -250,11 +258,10 @@ module Lutaml
250
258
  false
251
259
  end
252
260
 
253
- # A captured-but-empty child marshals as a nil string value.
254
- # The interpretive pipeline classifies the raw "" through the
255
- # rule's value map (:empty key) — the :empty directive (the
256
- # plain-rule default) keeps it as "", :nil binds nil, and any
257
- # other directive means uninitialized, i.e. no binding.
261
+ # An empty capture binds through the rule's value map: a
262
+ # blank element through the :empty key — the :empty directive
263
+ # (the plain-rule default) keeps it as "", :nil binds nil, and
264
+ # any other directive means uninitialized, i.e. no binding.
258
265
  def mapped_empty_value(rule)
259
266
  case rule.value_map(:from)[:empty]
260
267
  when :nil then nil
@@ -263,14 +270,54 @@ module Lutaml
263
270
  end
264
271
  end
265
272
 
266
- def mapped_capture_values(values, rule)
267
- return values unless values.include?(nil)
273
+ # An xsi:nil="true" element binds through the :nil key
274
+ # (Attribute#apply_value_map's contract — the interpretive
275
+ # nil_element? read).
276
+ def mapped_nil_value(rule, attr)
277
+ case rule.value_map(:from)[:nil]
278
+ when :nil then nil
279
+ when :empty then attr.collection? ? [] : ""
280
+ else :skip
281
+ end
282
+ end
283
+
284
+ # An absent element's binding through the value_map :omitted key
285
+ # when it names a typed empty ("" or the empty collection). The
286
+ # default :nil directive leaves the attribute unbound — the
287
+ # constructor's default seeding and a writer-assigned value
288
+ # (#922) own an absent attribute, never an explicit nil.
289
+ def mapped_omitted_value(rule, attr)
290
+ if rule.value_map(:from)[:omitted] == :empty
291
+ attr.collection? ? [] : ""
292
+ else
293
+ :skip
294
+ end
295
+ end
296
+
297
+ def empty_capture_value(rule, attr, marked)
298
+ marked ? mapped_nil_value(rule, attr) : mapped_empty_value(rule)
299
+ end
268
300
 
269
- mapped = mapped_empty_value(rule)
301
+ # A collection's captured items. One blank or nil-marked
302
+ # element is an empty collection (the interpretive
303
+ # normalized_value_for_attr rule). A multi-item capture is
304
+ # left alone by the interpretive value map — items bind raw,
305
+ # a blank element staying "" — except that a nil-marked
306
+ # element reads nil (the nil_element? rule).
307
+ def capture_collection_values(values, rule, attr, name, node)
308
+ return values unless attr.collection?
309
+ return [] if values.length == 1 &&
310
+ (values[0].nil? || values[0] == "")
311
+ return values unless values.include?(nil) || values.include?("")
312
+
313
+ marks = xsi_nil_marks(node)
270
314
  out = []
271
- values.each do |item|
272
- if item.nil?
273
- out << mapped unless mapped == :skip
315
+ values.each_with_index do |item, rank|
316
+ if marked?(marks, name, rank) && (item.nil? || item == "")
317
+ out << nil
318
+ elsif item.nil?
319
+ bound = mapped_empty_value(rule)
320
+ out << bound unless bound == :skip
274
321
  else
275
322
  out << item
276
323
  end
@@ -278,6 +325,72 @@ module Lutaml
278
325
  out
279
326
  end
280
327
 
328
+ # The interpretive reader's two transform lanes
329
+ # (handle_transform_method): a hash/proc transform on the rule or
330
+ # the attribute rides ImportTransformer (transform_dispatch
331
+ # :import); a class transform rides transform_value.
332
+ def import_transformed(rule, attr, value)
333
+ if Lutaml::Model::MappingRule.transform_dispatch(rule, attr) == :import
334
+ return Lutaml::Model::ImportTransformer.call(value, rule, attr,
335
+ context: nil)
336
+ end
337
+
338
+ # The interpretive transform_value: class transformers on
339
+ # the rule or the attribute, in order (its own fast path
340
+ # returns the value when there are none).
341
+ rule.transform_value(attr, value, :from, :xml)
342
+ end
343
+
344
+ # The interpretive reader casts a Value type that hooks the XML
345
+ # format through its own class method (from_xml) via the
346
+ # attribute's format-aware cast (normalize_xml_value); plain
347
+ # scalars stay raw for the constructor's cast.
348
+ def xml_hooked_cast(attr, raw, register)
349
+ return raw if raw.nil?
350
+ return raw unless PlanSerializer.custom_xml_type?(attr.type(register))
351
+
352
+ attr.cast(raw, :xml, register)
353
+ end
354
+
355
+ # Child elements carrying xsi:nil="true", as
356
+ # {wire name => {same-name sibling rank => true}} — the
357
+ # engine walk does not surface the mark, so it is read from
358
+ # the source node on the first empty capture that needs it.
359
+ # Without a source node (a nested model hydrated from walk
360
+ # values alone) nothing is marked.
361
+ def xsi_nil_marks(node)
362
+ return {} unless node
363
+
364
+ marks = {}
365
+ ranks = ::Hash.new(0)
366
+ node.element_children.each do |child|
367
+ rank = ranks[child.name]
368
+ ranks[child.name] = rank + 1
369
+ next unless child["xsi:nil"] == "true"
370
+
371
+ (marks[child.name] ||= {})[rank] = true
372
+ end
373
+ marks
374
+ end
375
+
376
+ def marked?(marks, name, rank)
377
+ !!marks&.dig(name, rank)
378
+ end
379
+
380
+ # Whether a document carries xsi:nil="true" below the root's
381
+ # own children. A nested model hydrated from walk values alone
382
+ # has no source node to read the mark from, so such a
383
+ # document rides the interpretive lane; only a document that
384
+ # declares the instance namespace pays the scan. Both plan
385
+ # entry points (from_xml, PlanWalk) consult it.
386
+ def xsi_nil_below_root?(root)
387
+ return false unless root.namespace_definitions
388
+ .any? { |ns| ns.href == XSI_URI }
389
+
390
+ root.xpath("*//@xsi:nil").any? { |attr| attr.value == "true" }
391
+ end
392
+ public :xsi_nil_below_root?
393
+
281
394
  # Wire-name strings are plan-frozen; materializing them per
282
395
  # hydration allocated two or three strings per attribute row
283
396
  # per instance.
@@ -292,7 +405,7 @@ module Lutaml
292
405
  # parent/root links once the parent exists; delegate values
293
406
  # wait for the instance (their target object must exist).
294
407
  def children_kwargs(_model_class, plan, value, buckets = nil,
295
- register = nil, intern = {})
408
+ register = nil, intern = {}, node: nil)
296
409
  register ||= Lutaml::Model::Config.default_register
297
410
  grouped, tagged, run_children = group_children(value,
298
411
  plan[:row_tags])
@@ -300,6 +413,9 @@ module Lutaml
300
413
  kwargs = {}
301
414
  children = []
302
415
  delegates = []
416
+ # xsi:nil marks of the source node's children — read on the
417
+ # first empty capture that needs them (see xsi_nil_marks).
418
+ marks = nil
303
419
  # Lazy: models without multi-spelling rows never touch it, and
304
420
  # the block-backed Hash is a fresh object per hydrated row.
305
421
  spellings = nil
@@ -324,27 +440,47 @@ module Lutaml
324
440
  # Raw passthrough: the model constructor is the single
325
441
  # cast authority — pre-casting here doubled every cast.
326
442
  # Class transforms still apply before assignment.
327
- if tag && (vals = tagged[tag]) && vals.size > 1
443
+ explicit_nil = false
444
+ captures = tag ? tagged[tag] : grouped[name]
445
+ if captures && captures.size > 1
328
446
  # Interpretive parity: several captures into a
329
- # non-collection attribute arrive as the full array.
330
- v = vals.map(&:string_value)
331
- if v.include?(nil)
332
- v = v.map { |item| item.nil? ? mapped_empty_value(rule) : item }
333
- v.reject! { |item| item == :skip }
447
+ # non-collection attribute arrive as the full array —
448
+ # the over-count reaches `.validate` (Issue #185).
449
+ v = []
450
+ captures.each_with_index do |val, rank|
451
+ raw = val.string_value
452
+ if raw.nil? || raw == ""
453
+ marks ||= xsi_nil_marks(node)
454
+ bound = empty_capture_value(rule, attr,
455
+ marked?(marks, name, rank))
456
+ v << bound unless bound == :skip
457
+ else
458
+ v << xml_hooked_cast(attr, raw, register)
459
+ end
334
460
  end
335
461
  else
336
- first = if tag
337
- tagged[tag]&.first
338
- else
339
- grouped.dig(name, 0)
340
- end
462
+ first = captures&.first
341
463
  v = first&.string_value
342
- v = mapped_empty_value(rule) if v.nil? && first
343
- end
344
- unless v.nil? || v == :skip
345
- v = rule.transform_value(attr, v, :from, :xml) if rule.transform.is_a?(Class)
346
- assign(kwargs, delegates, delegate, rule, attr, v)
464
+ if first && (v.nil? || v == "")
465
+ marks ||= xsi_nil_marks(node)
466
+ explicit_nil = marked?(marks, name, 0)
467
+ v = empty_capture_value(rule, attr, explicit_nil)
468
+ elsif captures.nil? || captures.empty?
469
+ # No capture at all: the attribute's omitted state
470
+ # binds through the value_map :omitted key (the
471
+ # interpretive apply_value_map on the uninitialized
472
+ # sentinel) — nil, the typed empty, or nothing.
473
+ v = mapped_omitted_value(rule, attr)
474
+ end
347
475
  end
476
+ # A bound nil is a state of its own (the nil-marked
477
+ # capture): it is assigned, not left to the default.
478
+ next if v == :skip
479
+ next if v.nil? && !explicit_nil
480
+
481
+ v = import_transformed(rule, attr,
482
+ xml_hooked_cast(attr, v, register))
483
+ assign(kwargs, delegates, delegate, rule, attr, v)
348
484
  when :wildcard_any
349
485
  # interpreted post-instance (interpret_deferred)
350
486
  when :content
@@ -391,7 +527,8 @@ module Lutaml
391
527
  next if elements.empty?
392
528
 
393
529
  results = elements.map do |element|
394
- attr.cast(element, :xml, register)
530
+ attr.cast(element, :xml, register,
531
+ **deferred_cast_options(rule))
395
532
  end
396
533
  unless results.empty?
397
534
  children.concat(results)
@@ -402,10 +539,10 @@ module Lutaml
402
539
  values = grouped[name].to_a.map do |item|
403
540
  item&.string_value
404
541
  end
405
- values = mapped_capture_values(values, rule)
406
- if rule.transform.is_a?(Class)
407
- values = values.map { |v| rule.transform_value(attr, v, :from, :xml) }
408
- end
542
+ values = capture_collection_values(values, rule, attr, name,
543
+ node)
544
+ values = values.map { |item| xml_hooked_cast(attr, item, register) }
545
+ values = import_transformed(rule, attr, values)
409
546
  unless values.empty?
410
547
  assign(kwargs, delegates, delegate, rule, attr, values)
411
548
  end
@@ -415,8 +552,15 @@ module Lutaml
415
552
  else
416
553
  native_collection(value, name)
417
554
  end
418
- values = mapped_capture_values(values, rule) unless values.nil?
419
555
  unless values.nil?
556
+ values = capture_collection_values(values, rule, attr, name,
557
+ node)
558
+ end
559
+ unless values.nil?
560
+ values = values.map do |item|
561
+ xml_hooked_cast(attr, item, register)
562
+ end
563
+ values = import_transformed(rule, attr, values)
420
564
  assign(kwargs, delegates, delegate, rule, attr, values)
421
565
  end
422
566
  when :spelling
@@ -472,7 +616,10 @@ module Lutaml
472
616
  attr.collection? ? values : values.first)
473
617
  end
474
618
  end
475
- [kwargs, children, delegates, run_children]
619
+ # grouped holds the captured element rows: non-empty means
620
+ # the source element has element children (the content
621
+ # shape below reads it).
622
+ [kwargs, children, delegates, run_children, !grouped.empty?]
476
623
  end
477
624
 
478
625
  # Deferred islands: raw subtrees become wrapper elements via a
@@ -483,7 +630,8 @@ module Lutaml
483
630
  # node (no fragment parse — the reparse dominated deferred
484
631
  # hydration). Ordered children only land here without a node.
485
632
  def interpret_deferred(model_class, plan, value, instance, node = nil,
486
- buckets = nil, run_children = nil)
633
+ buckets = nil, run_children = nil,
634
+ element_children: false)
487
635
  # Array#each + top counter: no per-node enumerator (see
488
636
  # children_kwargs).
489
637
  rows = plan[:rows]
@@ -514,10 +662,13 @@ module Lutaml
514
662
  next
515
663
  end
516
664
 
517
- # Non-collection content attrs hold the joined text
518
- # (the interpretive path assigns element text, the runs
519
- # concatenated).
520
- instance.public_send(:"#{attr.name}=", runs.join)
665
+ # A singular content attribute takes the interpretive
666
+ # reader's shape: the text runs when the element has
667
+ # non-text children (validate flags a singular target
668
+ # holding them), the joined text otherwise.
669
+ instance.public_send(:"#{attr.name}=",
670
+ content_text(node, runs,
671
+ element_children))
521
672
  when :raw
522
673
  raws = raw_strings(value, wire_name)
523
674
  next if raws.empty?
@@ -528,7 +679,16 @@ module Lutaml
528
679
  elements = deferred_elements(value, wire_name, buckets)
529
680
  next if elements.empty?
530
681
 
531
- args = attr.collection? ? elements : elements.first
682
+ # The interpretive reader's custom-method contract: a
683
+ # singular attribute receives its one element, or the
684
+ # whole array on an over-count (#185); the rule's
685
+ # transform runs first, on that same value.
686
+ args = if attr.collection? || elements.size > 1
687
+ elements
688
+ else
689
+ elements.first
690
+ end
691
+ args = import_transformed(rule, attr, args)
532
692
  rule.deserialize(instance, args,
533
693
  model_class.attributes(register),
534
694
  model_class)
@@ -537,8 +697,7 @@ module Lutaml
537
697
 
538
698
  results = deferred_elements(value, wire_name, buckets).map do |element|
539
699
  attr.cast(element, :xml, register,
540
- lutaml_parent: instance,
541
- lutaml_root: instance.lutaml_root || instance)
700
+ **deferred_cast_options(rule, instance))
542
701
  end
543
702
  next if results.empty?
544
703
 
@@ -580,9 +739,7 @@ module Lutaml
580
739
  when :polymorphic
581
740
  results = deferred_elements(value, wire_name, buckets).map do |element|
582
741
  attr.cast(element, :xml, register,
583
- polymorphic: rule.polymorphic,
584
- lutaml_parent: instance,
585
- lutaml_root: instance.lutaml_root || instance)
742
+ **deferred_cast_options(rule, instance))
586
743
  end
587
744
  next if results.empty?
588
745
 
@@ -592,6 +749,20 @@ module Lutaml
592
749
  end
593
750
  end
594
751
 
752
+ # The interpretive cast options for a deferred subtree
753
+ # (value_for_rule's base options): the parent links, and the
754
+ # rule's polymorphic resolution — a child plan that could not
755
+ # compile must not drop it (the subclass identity).
756
+ def deferred_cast_options(rule, instance = nil)
757
+ options = {}
758
+ if instance
759
+ options[:lutaml_parent] = instance
760
+ options[:lutaml_root] = instance.lutaml_root || instance
761
+ end
762
+ options[:polymorphic] = rule.polymorphic if rule.polymorphic
763
+ options
764
+ end
765
+
595
766
  # Wrapper elements for deferred rows: the ORIGINAL source
596
767
  # nodes bridged through moxml's wrapper cache (wrapper
597
768
  # construction only, no parse), falling back to a fragment
@@ -633,14 +804,16 @@ module Lutaml
633
804
  @bridge_context ||= Moxml::Context.new(:leptris)
634
805
  end
635
806
 
636
- def route_delegates(delegates, instance)
637
- delegates.each do |rule, delegate_attr, attr, v|
638
- target = instance.public_send(rule.delegate)
807
+ # Rows carry the delegate NAME (the interpretive
808
+ # MappingRule#deserialize contract: the target attribute's
809
+ # type instantiates an absent target).
810
+ def route_delegates(delegates, instance, register)
811
+ attributes = instance.class.attributes(register)
812
+ delegates.each do |_rule, delegate, attr, v|
813
+ target = instance.public_send(delegate)
639
814
  unless target
640
- # the interpretive path instantiates absent delegate
641
- # targets; mirror it
642
- target = delegate_attr.type(register).new
643
- instance.public_send(:"#{rule.delegate}=", target)
815
+ target = attributes[delegate].type(register).new
816
+ instance.public_send(:"#{delegate}=", target)
644
817
  end
645
818
  target.public_send(:"#{attr.name}=", v)
646
819
  end
@@ -672,6 +845,21 @@ module Lutaml
672
845
  end
673
846
  end
674
847
 
848
+ # The interpretive XmlElement#text shape for a singular
849
+ # content attribute: the text runs when the source element has
850
+ # non-text children (mixed content), the joined text otherwise.
851
+ # Mapped element children show in the walk's own capture; an
852
+ # unmapped one only on the source node, consulted when the
853
+ # hydration already holds it (the buckets chain is not kept
854
+ # for this — 85 singular content attributes ride every NISO
855
+ # JATS paragraph).
856
+ def content_text(node, runs, element_children)
857
+ return runs if element_children
858
+ return runs.join unless node
859
+
860
+ node.first_element_child.nil? ? runs.join : runs
861
+ end
862
+
675
863
  # Native collection rows echo their producing row's name
676
864
  # (leptris 1.9.178).
677
865
  def native_collection(value, name)