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.
@@ -28,6 +28,34 @@ module Lutaml
28
28
  content_deferred].freeze
29
29
  PLAN_SERIALIZABLE_KINDS = SERIALIZABLE_KINDS
30
30
  CONTENT_KINDS = %i[content content_deferred].freeze
31
+ # Rows whose nil/empty value may still render one element, per
32
+ # the rule's value_map :to (render_nil / render_empty).
33
+ PLACEHOLDER_KINDS = %i[scalar collection_native collection_cb
34
+ nested ordered_deferred].freeze
35
+ XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace"
36
+
37
+ # Default-namespace scope of one element's children — the
38
+ # interpretive planner's blank-namespace rule (W3C XML
39
+ # Namespaces 1.0 §6.2): in a document whose root declares a
40
+ # default namespace, a child in no namespace (:blank row) opts out
41
+ # with xmlns="" — on the first such child of each parent under
42
+ # pretty output (the interpretive writer's sibling rule), on
43
+ # every one otherwise. The rule keys on the root's declaration,
44
+ # not on the in-scope state: the interpretive writer opts a blank
45
+ # child out again inside an already opted-out subtree. What the
46
+ # opt-out does change is scope: a child spelled in
47
+ # default-namespace form inside it re-declares its namespace.
48
+ class BlankScope
49
+ attr_reader :root_default, :blank, :pretty
50
+ attr_accessor :done
51
+
52
+ def initialize(root_default, blank, pretty)
53
+ @root_default = root_default
54
+ @blank = blank
55
+ @pretty = pretty
56
+ @done = false
57
+ end
58
+ end
31
59
 
32
60
  # Reconciler host: the interpretive support modules are written
33
61
  # against the Transformation's combined surface (the reconciler
@@ -41,10 +69,17 @@ module Lutaml
41
69
  include(TransformationSupport::SkipLogic)
42
70
  end
43
71
  RECONCILER = ReconcilerHost.new.freeze
72
+ # [attribute name, serialized name] -> CompiledRule per frozen rule
73
+ # array (the coverage pre-check's row -> rule lookup).
74
+ COMPILED_RULE_INDEX = {}.compare_by_identity
75
+ # Type -> whether it hooks the XML format through its own methods.
76
+ CUSTOM_XML_TYPE = {}.compare_by_identity
44
77
 
45
78
  class << self
46
- # plan: the compiler's entry for instance's class
47
- def call(instance, plan, register = nil)
79
+ # plan: the compiler's entry for instance's class. pretty:
80
+ # the to_xml option — it selects the first-child-only form of
81
+ # the blank-namespace rule, as the interpretive writer's does.
82
+ def call(instance, plan, register = nil, pretty: false)
48
83
  # The child model's declared lutaml_default_register takes
49
84
  # precedence over the ambient register — the resolve_for_child
50
85
  # contract (#876); without it, child plans and attribute
@@ -91,9 +126,13 @@ module Lutaml
91
126
  instance.raw_schema_location)
92
127
  end
93
128
  built = if plan[:ordered] || !instance.element_order.nil?
94
- build_ordered(root, instance, plan, doc, register, ns_ctx)
129
+ build_ordered(root, instance, plan, doc, register, ns_ctx,
130
+ scope: BlankScope.new(ns_ctx.default_namespace,
131
+ false, pretty))
95
132
  else
96
- build(root, instance, plan, doc, register, ns_ctx)
133
+ build(root, instance, plan, doc, register, ns_ctx,
134
+ scope: BlankScope.new(ns_ctx.default_namespace,
135
+ false, pretty))
97
136
  end
98
137
  return nil unless built
99
138
 
@@ -153,12 +192,28 @@ module Lutaml
153
192
  @hoisted[uri] = prefix
154
193
  @forms[uri] = { prefix: prefix, uri: uri }
155
194
  end
195
+ # The writer's own root decision (its rule engine's
196
+ # DefaultPreferenceRule over the built tree): an explicitly
197
+ # :unqualified elementFormDefault never selects the prefix
198
+ # format by itself — only prefixed attribute needs do (W3C).
199
+ # The planner's type-analysis path prefers the prefix format
200
+ # here so children can omit xmlns; the writer does not.
201
+ model_uri = plan[:tree][:ns] && plan[:tree][:ns][:exact]
202
+ ns_class = mapping.namespace_class
203
+ if ns_class&.element_form_default_set? &&
204
+ ns_class.element_form_default == :unqualified &&
205
+ ns_class.attribute_form_default != :qualified &&
206
+ @hoisted.key?(model_uri) && @hoisted[model_uri] &&
207
+ needs.type_attribute_namespaces
208
+ .none? { |ns| ns.uri == model_uri }
209
+ @hoisted[model_uri] = nil
210
+ @forms[model_uri] = { prefix: nil, uri: model_uri }
211
+ end
156
212
  # Format preservation (the interpretive writer's
157
213
  # rule): an instance parsed from a PREFIXED source keeps
158
214
  # its prefixes — the model's own namespace hoists with
159
215
  # the instance's recorded prefix even when the fresh
160
216
  # planner preferred the default-ns format.
161
- model_uri = plan[:tree][:ns] && plan[:tree][:ns][:exact]
162
217
  if ns_prefix && !ns_prefix.empty? && model_uri &&
163
218
  @hoisted.key?(model_uri)
164
219
  @hoisted[model_uri] = ns_prefix
@@ -203,12 +258,18 @@ module Lutaml
203
258
  !ns_class.prefix_default.to_s.empty?
204
259
  @model_prefix = ns_class.prefix_default.to_s
205
260
  end
261
+ @default_namespace = @hoisted.any? { |_uri, prefix| prefix.nil? }
206
262
  end
207
263
 
208
264
  def unspellable?
209
265
  @unspellable
210
266
  end
211
267
 
268
+ # Whether the root declares a default namespace — every
269
+ # element then has one in scope until a :blank child opts
270
+ # out with xmlns="".
271
+ attr_reader :default_namespace
272
+
212
273
  def collect(tree)
213
274
  check(tree[:ns])
214
275
  (tree[:attributes] || []).each { |a| check(a[:ns]) }
@@ -266,10 +327,12 @@ module Lutaml
266
327
  def declare_on(root, _attr_form_prefix = nil)
267
328
  @hoisted.sort_by { |_uri, prefix| [prefix ? 0 : 1, prefix.to_s] }
268
329
  .each do |uri, prefix|
269
- key = uri
270
- next if key.nil? || @declared[key]
330
+ # The xml prefix is implicitly bound (Namespaces 1.0
331
+ # §3) — the interpretive writer never declares it.
332
+ next if uri.nil? || uri == XML_NAMESPACE_URI ||
333
+ @declared[uri]
271
334
 
272
- @declared[key] = true
335
+ @declared[uri] = true
273
336
  root.add_namespace_definition(prefix, uri)
274
337
  end
275
338
  end
@@ -291,14 +354,14 @@ module Lutaml
291
354
  end
292
355
 
293
356
  def build(element, instance, plan, doc, reg = nil, ns_ctx = nil,
294
- inherited_ns = nil)
357
+ inherited_ns = nil, scope: nil)
295
358
  reg ||= register
296
359
  write_attributes(element, instance, plan, reg, ns_ctx)
297
360
  ns_index = ns_index_for(plan)
298
361
  nested_plans = nested_plans_for(plan)
299
362
  plan[:rows].each do |rule, attr, kind, spelling, delegate|
300
363
  value = value_of(instance, attr, delegate)
301
- next unless rule.render?(value, instance)
364
+ next unless render_value?(rule, value, instance)
302
365
 
303
366
  case kind
304
367
  when :scalar
@@ -306,46 +369,77 @@ module Lutaml
306
369
  # several occurrences serializes one element per item,
307
370
  # as the interpretive writer does. A plain scalar skips
308
371
  # the Array() wrap — the common case.
309
- row_ns = ns_index[row_name(rule, spelling)] || inherited_ns
310
- if value.is_a?(::Array)
372
+ name = row_name(rule, spelling)
373
+ form = spelled_form(ns_index[name] || inherited_ns,
374
+ inherited_ns, plan)
375
+ blank = blank_row_form?(ns_index[name] || inherited_ns, plan)
376
+ spelled = spell(ns_ctx, name, form)
377
+ placeholder = placeholder_for(rule, value)
378
+ if placeholder
379
+ qualify!(add_leaf(element, spelled, nil, doc,
380
+ attrs: rule.when_attribute,
381
+ xsi_nil: placeholder == :xsi_nil),
382
+ form, blank, scope, ns_ctx)
383
+ elsif !value.is_a?(::Array)
384
+ qualify!(add_leaf(element, spelled,
385
+ scalar_text(value, attr, reg), doc,
386
+ attrs: rule.when_attribute, cdata: rule.cdata),
387
+ form, blank, scope, ns_ctx)
388
+ else
311
389
  value.each do |item|
312
- add_leaf(element,
313
- ns_ctx ? ns_ctx.spell(row_name(rule, spelling), row_ns) : row_name(rule, spelling),
314
- attr.serialize(item, :xml, reg), doc,
315
- attrs: rule.when_attribute, cdata: rule.cdata)
390
+ qualify!(add_leaf(element, spelled,
391
+ scalar_text(item, attr, reg), doc,
392
+ attrs: rule.when_attribute, cdata: rule.cdata),
393
+ form, blank, scope, ns_ctx)
316
394
  end
317
- else
318
- add_leaf(element,
319
- ns_ctx ? ns_ctx.spell(row_name(rule, spelling), row_ns) : row_name(rule, spelling),
320
- attr.serialize(value, :xml, reg), doc,
321
- attrs: rule.when_attribute, cdata: rule.cdata)
322
395
  end
323
396
  when :collection_native, :collection_cb
324
- row_ns = ns_index[rule.name.to_s] || inherited_ns
325
- Array(value).each do |item|
326
- add_leaf(element,
327
- ns_ctx ? ns_ctx.spell(rule.name.to_s, row_ns) : rule.name.to_s,
328
- attr.serialize(item, :xml, reg), doc,
329
- attrs: rule.when_attribute, cdata: rule.cdata)
330
- end
397
+ name = rule.name.to_s
398
+ form = spelled_form(ns_index[name] || inherited_ns,
399
+ inherited_ns, plan)
400
+ emit_collection(element, spell(ns_ctx, name, form), form,
401
+ blank_row_form?(ns_index[name] || inherited_ns,
402
+ plan),
403
+ rule, attr, value, doc, reg, scope, ns_ctx)
331
404
  when :nested, :ordered_deferred
332
- Array(value).each do |item|
405
+ name = row_name(rule, spelling)
406
+ items = Array(value)
407
+ placeholder = placeholder_for(rule, value) if items.empty?
408
+ if placeholder
409
+ form = spelled_form(ns_index[name] || inherited_ns,
410
+ inherited_ns, plan)
411
+ qualify!(add_leaf(element, spell(ns_ctx, name, form),
412
+ nil, doc, xsi_nil: placeholder == :xsi_nil),
413
+ form, blank_row_form?(ns_index[name] || inherited_ns,
414
+ plan),
415
+ scope, ns_ctx)
416
+ end
417
+ items.each do |item|
418
+ next unless item
419
+
333
420
  # Keyed by [class, row name]: distinct rows (sz, szCs)
334
421
  # can share one class with different root names.
335
- child_plan = nested_plans[[item.class, rule.name.to_s]] ||
336
- (nested_plans[[item.class, rule.name.to_s]] = PlanCompiler.compile(item.class, reg))
337
- child_ns = child_plan[:tree][:ns] || inherited_ns
422
+ child_plan = nested_plans[[item.class, name]] ||
423
+ (nested_plans[[item.class, name]] = PlanCompiler.compile(item.class, reg))
424
+ row_ns = nested_child_ns(child_plan, ns_index[name],
425
+ inherited_ns)
426
+ form = spelled_form(row_ns, inherited_ns, plan)
338
427
  # The ROW's wire name wins over the child plan's
339
428
  # root — distinct rows (sz, szCs) share one class.
340
- child = element.create_child(
341
- ns_ctx ? ns_ctx.spell(row_name(rule, spelling), child_ns) : row_name(rule, spelling),
429
+ child = element.create_child(spell(ns_ctx, name, form))
430
+ child_scope = subtree_scope(
431
+ scope,
432
+ qualify!(child, form, blank_row_form?(row_ns, plan),
433
+ scope, ns_ctx),
342
434
  )
343
435
  ns_ctx&.collect(child_plan[:tree])
344
436
  if child_plan[:ordered] || !item.element_order.nil?
345
437
  build_ordered(child, item, child_plan,
346
- doc, reg, ns_ctx, child_ns) || return
438
+ doc, reg, ns_ctx, form,
439
+ scope: child_scope) || return
347
440
  else
348
- build(child, item, child_plan, doc, reg, ns_ctx, child_ns) || return
441
+ build(child, item, child_plan, doc, reg, ns_ctx, form,
442
+ scope: child_scope) || return
349
443
  end
350
444
  end
351
445
  when :raw
@@ -438,10 +532,10 @@ module Lutaml
438
532
  # without element_order (built by Ruby, not parsed) falls
439
533
  # back to declaration order — the interpretive behavior.
440
534
  def build_ordered(element, instance, plan, doc, reg = nil,
441
- ns_ctx = nil, inherited_ns = nil)
535
+ ns_ctx = nil, inherited_ns = nil, scope: nil)
442
536
  if instance.element_order.nil?
443
537
  return build(element, instance, plan, doc, reg, ns_ctx,
444
- inherited_ns)
538
+ inherited_ns, scope: scope)
445
539
  end
446
540
 
447
541
  reg ||= register
@@ -466,11 +560,14 @@ module Lutaml
466
560
  # appended emission (silent drops are data loss). The
467
561
  # reconciler speaks CompiledRule — the interpretive
468
562
  # transformation's memoized set; the plan's MappingRule
469
- # rows cannot drive it. Pristine parsed instances skip the
470
- # scan entirely: the parse-built order covers every set
471
- # value, and the writer-set flag is the only flow that can
472
- # produce deficits.
473
- rules = if instance.mutated_after_parse?
563
+ # rows cannot drive it. A pristine instance skips the scan
564
+ # only when its order covers every value: the mutation
565
+ # flag cannot see in-place collection growth (`items <<
566
+ # x` never reaches a setter), so coverage is measured
567
+ # against the values first.
568
+ rules = if instance.mutated_after_parse? ||
569
+ order_uncovered?(instance, plan, rows_by_name,
570
+ name_keys, instance.element_order, reg)
474
571
  compiled_rules_for(instance.class, reg)
475
572
  end
476
573
  if rules
@@ -484,13 +581,20 @@ module Lutaml
484
581
  content_attr = content_entry && content_entry[1]
485
582
  content_value = content_attr &&
486
583
  value_of(instance, content_attr, content_entry[4])
487
- text_node_count = order.count { |o| o.type == "Text" }
584
+ text_node_count = order.count do |o|
585
+ order_entry?(o) && o.type == "Text"
586
+ end
488
587
  use_content_index = content_value.is_a?(Array) &&
489
588
  content_value.length == text_node_count
490
589
  content_cdata = content_entry && content_entry[0].cdata
491
590
  text_node_index = 0
492
591
 
493
592
  order.each do |object|
593
+ # element_order is a public accessor: entries that are not
594
+ # order entries (no #type) exist in the wild and are
595
+ # skipped like the interpretive applier skips them.
596
+ next unless order_entry?(object)
597
+
494
598
  case object.type
495
599
  when "Text"
496
600
  text = if content_attr && !content_value.nil?
@@ -513,63 +617,81 @@ module Lutaml
513
617
  when "Comment"
514
618
  element.add_child(doc.create_comment(object.text_content))
515
619
  when "Element"
516
- # A uniquely-named row absorbs the entry at any
517
- # qualification (the interpretive matcher's lenient
518
- # binding — order entries written by consumers carry no
519
- # namespace). Contested names dispatch strictly by
520
- # (URI, local), falling back to URI-less rows.
521
- name = object.name
522
- # Order entries may carry names with no mapped row (the
523
- # parse records every child in document order); the
524
- # layout cache kills its default proc to stay
525
- # mutation-free, so a miss reads nil and drops below.
526
- keys = name_keys[name]
527
- if keys&.length == 1
528
- dispatch_key = keys.first
529
- entry = rows_by_name[dispatch_key]
530
- else
531
- dispatch_key = [object.namespace_uri, name]
532
- entry = rows_by_name[dispatch_key]
533
- unless entry
534
- dispatch_key = [nil, name]
535
- entry = rows_by_name[dispatch_key]
536
- end
537
- end
620
+ dispatch_key = dispatch_key_for(object, rows_by_name, name_keys)
621
+ entry = rows_by_name[dispatch_key]
538
622
  next unless entry
539
623
 
540
- _, attr, kind, _spelling, delegate, row_ns = entry
624
+ row_rule, attr, kind, _spelling, delegate, row_ns = entry
541
625
  value = value_of(instance, attr, delegate)
626
+ blank = blank_row_form?(row_ns, plan)
627
+ row_ns = spelled_form(row_ns, inherited_ns, plan)
628
+ spelled = spell(ns_ctx, object.name, row_ns)
542
629
  case kind
543
630
  when :collection_native, :collection_cb
544
- index = element_indices[dispatch_key]
545
631
  items = Array(value)
546
- next unless index < items.length
632
+ index = element_indices[dispatch_key]
633
+ if index < items.length
634
+ element_indices[dispatch_key] += 1
635
+ next if items[index].nil? && nil_omitted?(row_rule)
547
636
 
548
- element_indices[dispatch_key] += 1
549
- add_leaf(element,
550
- ns_ctx ? ns_ctx.spell(object.name, row_ns) : object.name,
551
- attr.serialize(items[index], :xml, reg), doc)
637
+ qualify!(add_leaf(element, spelled,
638
+ scalar_text(items[index], attr, reg), doc,
639
+ attrs: row_rule.when_attribute, cdata: row_rule.cdata),
640
+ row_ns, blank, scope, ns_ctx)
641
+ elsif index.zero? && (placeholder = placeholder_for(row_rule, value))
642
+ element_indices[dispatch_key] += 1
643
+ qualify!(add_leaf(element, spelled, nil, doc,
644
+ attrs: row_rule.when_attribute,
645
+ xsi_nil: placeholder == :xsi_nil),
646
+ row_ns, blank, scope, ns_ctx)
647
+ end
552
648
  when :nested, :ordered_deferred
553
649
  item = if attr.collection?
554
650
  index = element_indices[dispatch_key]
555
- element_indices[dispatch_key] += 1
556
- Array(value)[index]
651
+ items = Array(value)
652
+ if index < items.length
653
+ element_indices[dispatch_key] += 1
654
+ items[index]
655
+ elsif index.zero? &&
656
+ (placeholder = placeholder_for(row_rule, value))
657
+ element_indices[dispatch_key] += 1
658
+ qualify!(add_leaf(element, spelled, nil, doc,
659
+ xsi_nil: placeholder == :xsi_nil),
660
+ row_ns, blank, scope, ns_ctx)
661
+ next
662
+ end
557
663
  else
664
+ element_indices[dispatch_key] += 1 unless value.nil?
558
665
  value
559
666
  end
560
- next unless item
667
+ if item.nil?
668
+ # A singular nil renders its policy's leaf, or nothing.
669
+ placeholder = placeholder_for(row_rule, value)
670
+ next unless placeholder
671
+
672
+ qualify!(add_leaf(element, spelled, nil, doc,
673
+ xsi_nil: placeholder == :xsi_nil),
674
+ row_ns, blank, scope, ns_ctx)
675
+ next
676
+ end
561
677
 
562
678
  child_plan = nested_plans_for(plan)[[item.class, object.name]] ||
563
679
  (nested_plans_for(plan)[[item.class, object.name]] = PlanCompiler.compile(item.class, reg))
564
- child_ns = child_plan[:tree][:ns] || row_ns || inherited_ns
680
+ child_ns = nested_child_ns(child_plan, row_ns, inherited_ns)
565
681
  child = element.create_child(
566
- ns_ctx ? ns_ctx.spell(object.name, child_ns) : object.name,
682
+ spell(ns_ctx, object.name, child_ns),
683
+ )
684
+ child_scope = subtree_scope(
685
+ scope,
686
+ qualify!(child, child_ns, blank, scope, ns_ctx),
567
687
  )
568
688
  if child_plan[:ordered] || !item.element_order.nil?
569
689
  return nil unless build_ordered(child, item, child_plan,
570
- doc, reg, ns_ctx, child_ns)
690
+ doc, reg, ns_ctx, child_ns,
691
+ scope: child_scope)
571
692
  else
572
- build(child, item, child_plan, doc, reg, ns_ctx, child_ns) || return
693
+ build(child, item, child_plan, doc, reg, ns_ctx, child_ns,
694
+ scope: child_scope) || return
573
695
  end
574
696
  when :raw
575
697
  element.add_child(value.to_s) unless value.nil?
@@ -578,41 +700,150 @@ module Lutaml
578
700
  # build).
579
701
  return nil unless Array(value).empty?
580
702
  when :scalar
581
- add_leaf(element,
582
- ns_ctx ? ns_ctx.spell(object.name, row_ns) : object.name,
583
- attr.serialize(value, :xml, reg), doc)
703
+ placeholder = placeholder_for(row_rule, value)
704
+ next if placeholder.nil? && value.nil?
705
+
706
+ element_indices[dispatch_key] += 1
707
+ if placeholder
708
+ qualify!(add_leaf(element, spelled, nil, doc,
709
+ xsi_nil: placeholder == :xsi_nil),
710
+ row_ns, blank, scope, ns_ctx)
711
+ else
712
+ qualify!(add_leaf(element, spelled,
713
+ scalar_text(value, attr, reg), doc,
714
+ cdata: row_rule.cdata),
715
+ row_ns, blank, scope, ns_ctx)
716
+ end
584
717
  end
585
718
  end
586
719
  end
587
720
  return nil if !fallback_rules.empty? &&
588
721
  !emit_uncovered_values(element, instance, plan, fallback_rules,
589
- doc, reg, ns_ctx, inherited_ns)
722
+ doc, reg, ns_ctx, inherited_ns,
723
+ scope: scope)
590
724
 
591
725
  true
592
726
  end
593
727
 
728
+ # The (URI, local) dispatch key an order entry resolves to.
729
+ # A uniquely-named row absorbs the entry at any qualification
730
+ # (the interpretive matcher's lenient binding — order entries
731
+ # written by consumers carry no namespace). Contested names
732
+ # dispatch strictly by (URI, local), falling back to URI-less
733
+ # rows. Order entries may carry names with no mapped row (the
734
+ # parse records every child in document order); the layout
735
+ # cache kills its default proc to stay mutation-free, so a
736
+ # miss reads nil.
737
+ def dispatch_key_for(object, rows_by_name, name_keys)
738
+ keys = name_keys[object.name]
739
+ if keys&.length == 1
740
+ keys.first
741
+ else
742
+ key = [object.namespace_uri, object.name]
743
+ rows_by_name[key] ? key : [nil, object.name]
744
+ end
745
+ end
746
+
747
+ def order_entry?(object)
748
+ RECONCILER.order_entry?(object)
749
+ end
750
+
751
+ # [attribute name, serialized name] -> CompiledRule for one
752
+ # transformation, identity-keyed by its frozen rule array.
753
+ def compiled_rule_index_for(klass, reg)
754
+ rules = compiled_rules_for(klass, reg)
755
+ return nil unless rules
756
+
757
+ COMPILED_RULE_INDEX[rules] ||= rules.each_with_object({}) do |r, h|
758
+ h[[r.attribute_name, r.serialized_name.to_s]] ||= r
759
+ end
760
+ end
761
+
762
+ # Whether the parse-built order leaves a row's value
763
+ # unemitted. Consumed entries are counted per dispatch key
764
+ # exactly as the walk dispatches them; a shortfall then goes
765
+ # to the reconciler's own expected count, which knows the
766
+ # default and derived exemptions (a parsed default is never
767
+ # a deficit).
768
+ def order_uncovered?(instance, _plan, rows_by_name, name_keys,
769
+ order, reg)
770
+ consumed = ::Hash.new(0)
771
+ order.each do |object|
772
+ next unless order_entry?(object) && object.type == "Element"
773
+
774
+ key = dispatch_key_for(object, rows_by_name, name_keys)
775
+ consumed[key] += 1 if rows_by_name[key]
776
+ end
777
+ compiled = nil
778
+ rows_by_name.each do |key, (rule, attr, kind, _spelling, delegate, _ns)|
779
+ next if emission_count(rule, attr, kind,
780
+ value_of(instance, attr, delegate)) <= consumed[key]
781
+
782
+ compiled ||= compiled_rule_index_for(instance.class, reg)
783
+ compiled_rule = compiled &&
784
+ compiled[[attr.name, rule.name.to_s]]
785
+ next unless compiled_rule
786
+
787
+ return true if RECONCILER.expected_element_count(compiled_rule,
788
+ instance) > consumed[key]
789
+ end
790
+ false
791
+ end
792
+
793
+ # How many entries the walk emits for a row's current value:
794
+ # one per item, or the single leaf a nil/empty value renders
795
+ # under its policy.
796
+ def emission_count(rule, attr, kind, value)
797
+ return 0 unless PLACEHOLDER_KINDS.include?(kind)
798
+ return 1 if placeholder_for(rule, value)
799
+ return 0 if value.nil?
800
+ return 1 unless attr.collection?
801
+
802
+ Array(value).length
803
+ end
804
+
594
805
  # OrderReconciler fallback: element rules the reconciled order
595
806
  # could not place but that still hold a value — emit them
596
807
  # appended, the ordinary way (the interpretive
597
808
  # apply_remaining_rules contract for the plan row model).
598
809
  def emit_uncovered_values(element, instance, plan, fallback, doc,
599
- reg, ns_ctx, inherited_ns)
810
+ reg, ns_ctx, inherited_ns, scope: nil)
600
811
  by_attr = {}
601
812
  plan[:rows].each { |row| by_attr[row[1].name] ||= row if row[1] }
602
- fallback.each do |rule|
603
- entry = by_attr[rule.attribute_name]
813
+ ns_index = ns_ctx && ns_index_for(plan)
814
+ fallback.each do |compiled_rule|
815
+ entry = by_attr[compiled_rule.attribute_name]
604
816
  next unless entry
605
817
 
606
- _rule, attr, kind, _spelling, delegate, row_ns = entry
818
+ # The fallback speaks CompiledRule; the wire name, the
819
+ # render policy and the cdata flag ride the plan's own row.
820
+ row_rule, attr, kind, spelling, delegate, _type = entry
607
821
  value = value_of(instance, attr, delegate)
608
- name = rule.name.to_s
822
+ next unless render_value?(row_rule, value, instance)
823
+
824
+ name = row_name(row_rule, spelling)
825
+ row_ns = (ns_index && ns_index[name]) || inherited_ns
826
+ blank = blank_row_form?(row_ns, plan)
827
+ ns_form = spelled_form(row_ns, inherited_ns, plan)
828
+ spelled = spell(ns_ctx, name, ns_form)
829
+ placeholder = placeholder_for(row_rule, value)
830
+ if placeholder
831
+ qualify!(add_leaf(element, spelled, nil, doc,
832
+ xsi_nil: placeholder == :xsi_nil),
833
+ ns_form, blank, scope, ns_ctx)
834
+ next
835
+ end
836
+
609
837
  Array(value).each do |item|
838
+ next if item.nil? && nil_omitted?(row_rule)
839
+
610
840
  case kind
611
841
  when :collection_native, :collection_cb, :scalar
612
- add_leaf(element,
613
- ns_ctx ? ns_ctx.spell(name, row_ns) : name,
614
- attr.serialize(item, :xml, reg), doc,
615
- cdata: rule.cdata)
842
+ qualify!(add_leaf(element, spelled,
843
+ scalar_text(item, attr, reg), doc,
844
+ attrs: row_rule.when_attribute,
845
+ cdata: row_rule.cdata),
846
+ ns_form, blank, scope, ns_ctx)
616
847
  when :nested, :ordered_deferred
617
848
  next if item.nil?
618
849
 
@@ -620,16 +851,20 @@ module Lutaml
620
851
  nested_plans_for(plan)[[item.class, name]] ||
621
852
  (nested_plans_for(plan)[[item.class, name]] =
622
853
  PlanCompiler.compile(item.class, reg))
623
- child_ns = child_plan[:tree][:ns] || row_ns || inherited_ns
854
+ child_ns = nested_child_ns(child_plan, ns_form, inherited_ns)
624
855
  child = element.create_child(
625
- ns_ctx ? ns_ctx.spell(name, child_ns) : name,
856
+ spell(ns_ctx, name, child_ns),
857
+ )
858
+ child_scope = subtree_scope(
859
+ scope, qualify!(child, child_ns, blank, scope, ns_ctx)
626
860
  )
627
861
  if child_plan[:ordered] || !item.element_order.nil?
628
862
  return nil unless build_ordered(child, item, child_plan,
629
- doc, reg, ns_ctx, child_ns)
863
+ doc, reg, ns_ctx, child_ns,
864
+ scope: child_scope)
630
865
  else
631
866
  built = build(child, item, child_plan, doc, reg, ns_ctx,
632
- child_ns)
867
+ child_ns, scope: child_scope)
633
868
  return nil unless built
634
869
  end
635
870
  end
@@ -701,21 +936,177 @@ module Lutaml
701
936
 
702
937
  spelled = prefix ? "#{prefix}:#{name}" : name
703
938
  element.set_attribute(spelled,
704
- attr.serialize(value, :xml, reg || register).to_s)
939
+ scalar_text(value, attr, reg || register).to_s)
705
940
  end
706
941
 
707
942
  # Partition rows (#88) re-emit their discriminator: the wire
708
943
  # element carries the when_attribute pairs so the round trip
709
944
  # re-routes to the same attribute on reparse.
710
- def add_leaf(element, name, value, doc, attrs: nil, cdata: false)
945
+ # A nested child's namespace form. A row spelled :unqualified wins
946
+ # over the child plan's own namespace: under a parent whose
947
+ # elementFormDefault is not :qualified, locally declared elements
948
+ # stay bare even when the child model declares the namespace
949
+ # (the interpretive writer's determine_element_namespace scope).
950
+ def nested_child_ns(child_plan, row_ns, inherited_ns)
951
+ return row_ns if %i[unqualified blank].include?(row_ns)
952
+
953
+ child_plan[:tree][:ns] || row_ns || inherited_ns
954
+ end
955
+
956
+ # Whether a row's element is in NO namespace: :blank (see
957
+ # blank_row?), :none (an explicit `namespace: nil` rule), or
958
+ # the :unqualified row of a namespace-less model.
959
+ def blank_row_form?(form, plan)
960
+ case form
961
+ when :blank, :none
962
+ true
963
+ when :unqualified
964
+ plan[:tree][:ns].nil?
965
+ else
966
+ false
967
+ end
968
+ end
969
+
970
+ # The ns form a row is spelled through: a :blank row follows
971
+ # the enclosing namespace's form — the root's prefix format
972
+ # under a prefix-preserved instance, the default-namespace
973
+ # format (with the xmlns="" opt-out) otherwise.
974
+ def spelled_form(form, inherited_ns, plan)
975
+ form == :blank ? (inherited_ns || plan[:tree][:ns]) : form
976
+ end
977
+
978
+ # Applies the blank-namespace rule (BlankScope) to a freshly
979
+ # created child spelled through `form`: returns the scope its
980
+ # own children are built under when the opt-out changed it,
981
+ # nil otherwise (callers building a subtree start it on a
982
+ # fresh first-child slot).
983
+ def qualify!(child, form, blank, scope, ns_ctx)
984
+ return unless scope
985
+
986
+ if blank
987
+ # Bare, or spelled through a default-namespace form: the
988
+ # child opts out, on the first such child of each parent
989
+ # under pretty output.
990
+ default_spelled = !form.is_a?(::Hash) || (ns_ctx &&
991
+ ns_ctx.prefix_for(form[:exact]).nil?)
992
+ if default_spelled && scope.root_default &&
993
+ !(scope.pretty && scope.done)
994
+ child.set_attribute("xmlns", "")
995
+ scope.done = true
996
+ BlankScope.new(scope.root_default, true, scope.pretty)
997
+ end
998
+ elsif scope.blank && form.is_a?(::Hash) && ns_ctx &&
999
+ ns_ctx.prefix_for(form[:exact]).nil?
1000
+ # A default-namespace child inside an opted-out subtree
1001
+ # re-declares its namespace here: the root's declaration
1002
+ # no longer reaches it.
1003
+ child.set_attribute("xmlns", form[:exact].to_s)
1004
+ BlankScope.new(scope.root_default, false, scope.pretty)
1005
+ end
1006
+ end
1007
+
1008
+ # The scope a nested child's subtree is built under.
1009
+ def subtree_scope(scope, changed)
1010
+ changed || BlankScope.new(scope.root_default, scope.blank,
1011
+ scope.pretty)
1012
+ end
1013
+
1014
+ def add_leaf(element, name, value, doc, attrs: nil, cdata: false,
1015
+ xsi_nil: false)
711
1016
  child = element.create_child(name)
712
1017
  attrs&.each { |k, v| child[k.to_s] = v.to_s }
713
- unless value.nil?
1018
+ child.set_attribute("xsi:nil", "true") if xsi_nil
1019
+ # An uninitialized value rendered under its value_map is an
1020
+ # empty element (the interpretive serialize_value yields no
1021
+ # text for it).
1022
+ if !value.nil? && !::Lutaml::Model::Utils.uninitialized?(value)
714
1023
  append_text(child, value.to_s, doc, cdata: cdata)
715
1024
  end
716
1025
  child
717
1026
  end
718
1027
 
1028
+ # A collection row's emission (the interpretive
1029
+ # apply_collection_element contract): one leaf per item — a
1030
+ # nil item goes unrendered when the value_map omits nil — and
1031
+ # for a nil or empty value the single leaf its policy renders,
1032
+ # or nothing.
1033
+ def emit_collection(element, spelled, ns_form, blank, rule, attr,
1034
+ value, doc, reg, scope, ns_ctx)
1035
+ items = Array(value)
1036
+ if items.empty? && (placeholder = placeholder_for(rule, value))
1037
+ return qualify!(add_leaf(element, spelled, nil, doc,
1038
+ attrs: rule.when_attribute,
1039
+ xsi_nil: placeholder == :xsi_nil),
1040
+ ns_form, blank, scope, ns_ctx)
1041
+ end
1042
+
1043
+ items.each do |item|
1044
+ next if item.nil? && nil_omitted?(rule)
1045
+
1046
+ qualify!(add_leaf(element, spelled,
1047
+ scalar_text(item, attr, reg), doc,
1048
+ attrs: rule.when_attribute, cdata: rule.cdata),
1049
+ ns_form, blank, scope, ns_ctx)
1050
+ end
1051
+ end
1052
+
1053
+ # What a nil or empty element value still renders, per the
1054
+ # rule's value_map :to (render_nil / render_empty — the
1055
+ # interpretive apply_nil_collection, apply_empty_collection
1056
+ # and apply_nil_marker contract): :xsi_nil (one leaf carrying
1057
+ # xsi:nil="true"), :blank (one bare leaf), or nil (nothing).
1058
+ def placeholder_for(rule, value)
1059
+ to_map = rule.value_map(:to)
1060
+ mode = if ::Lutaml::Model::Utils.uninitialized?(value)
1061
+ # An uninitialized value its value_map renders: the
1062
+ # interpretive apply_nil_marker_for_uninitialized
1063
+ # marks xsi:nil under :nil, else emits a bare leaf.
1064
+ to_map[:omitted] == :nil ? :nil : :blank
1065
+ elsif value.nil?
1066
+ to_map[:nil]
1067
+ elsif ::Lutaml::Model::Utils.empty?(value)
1068
+ to_map[:empty]
1069
+ else
1070
+ return nil
1071
+ end
1072
+ return :xsi_nil if mode == :nil
1073
+ return :blank if mode == :blank
1074
+ return :blank if value.nil? && mode == :empty
1075
+
1076
+ nil
1077
+ end
1078
+
1079
+ # Shared with NativeEmission's gate: the order-entry predicate,
1080
+ # the render-policy placeholder and the coverage pre-check
1081
+ # decide what the ops lane cannot serve.
1082
+ public :order_entry?, :placeholder_for, :order_uncovered?
1083
+
1084
+ # Whether a row's value reaches the output at all — the
1085
+ # interpretive should_skip_value? contract (RenderPolicy): an
1086
+ # uninitialized, nil, or empty value is decided by its value_map
1087
+ # keys alone; every other value by the default guard.
1088
+ def render_value?(rule, value, instance)
1089
+ to_map = rule.value_map(:to)
1090
+ if ::Lutaml::Model::Utils.uninitialized?(value)
1091
+ return !(to_map[:omitted].nil? || to_map[:omitted] == :omitted)
1092
+ elsif value.nil?
1093
+ return to_map[:nil] != :omitted
1094
+ elsif ::Lutaml::Model::Utils.empty?(value)
1095
+ return to_map[:empty] != :omitted
1096
+ end
1097
+
1098
+ rule.render?(value, instance)
1099
+ end
1100
+ public :render_value?
1101
+
1102
+ def nil_omitted?(rule)
1103
+ rule.value_map(:to)[:nil] == :omitted
1104
+ end
1105
+
1106
+ def spell(ns_ctx, name, ns_form)
1107
+ ns_ctx ? ns_ctx.spell(name, ns_form) : name
1108
+ end
1109
+
719
1110
  # Text attach: the address face (leptris#408 adoption)
720
1111
  # passes raw node addresses — the pointer face mints an
721
1112
  # FFI::Pointer wrapper per fresh node, the last per-row
@@ -733,6 +1124,41 @@ module Lutaml
733
1124
  node
734
1125
  end
735
1126
 
1127
+ # Whether a Value type hooks the XML format through its own
1128
+ # methods (instance to_xml, class from_xml) instead of a
1129
+ # registered format serializer. The interpretive writer calls
1130
+ # those hooks (serialize_custom_value, from_format); the plan
1131
+ # lanes must too — or decline the shape.
1132
+ def custom_xml_type?(type)
1133
+ return false unless type.is_a?(Class) &&
1134
+ type < ::Lutaml::Model::Type::Value
1135
+
1136
+ CUSTOM_XML_TYPE[type] ||=
1137
+ !type.instance_method(:to_xml).owner.equal?(
1138
+ ::Lutaml::Model::Type::Value,
1139
+ ) ||
1140
+ !type.method(:from_xml).owner.equal?(
1141
+ ::Lutaml::Model::Type::Value.singleton_class,
1142
+ )
1143
+ end
1144
+ public :custom_xml_type?
1145
+
1146
+ # The interpretive writer's XML spelling of a scalar (its
1147
+ # serialize_value -> serialize_custom_value): a type hooking
1148
+ # to_xml is spelled through the wrapped instance; every other
1149
+ # type takes the attribute serializer's own path (its String
1150
+ # fast path included).
1151
+ def scalar_text(value, attr, reg)
1152
+ type = attr.type(reg)
1153
+ if custom_xml_type?(type)
1154
+ return value.to_xml if value.is_a?(type)
1155
+
1156
+ return type.new(value).to_xml
1157
+ end
1158
+
1159
+ attr.serialize(value, :xml, reg)
1160
+ end
1161
+
736
1162
  def value_of(instance, attr, delegate)
737
1163
  if delegate
738
1164
  target = instance.public_send(delegate)