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.
@@ -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
@@ -62,10 +97,10 @@ module Lutaml
62
97
  # (namespaced spellings are instance-dependent; ordered,
63
98
  # wildcard, raw, and callback shapes are not
64
99
  # ops-expressible).
65
- # element_order is the order contract: a model carrying it —
66
- # parse-captured or programmatically tracked (#977) — must
67
- # serialize through the ordered walk, not the flat
68
- # schema-order native ops layout.
100
+ # element_order is the order contract: a model carrying a
101
+ # tracked one (#977) serializes through the ordered walk
102
+ # below, not the flat schema-order native ops layout — the
103
+ # native gate refuses those instances.
69
104
  return NativeEmission.emit(instance, plan, register) if
70
105
  NativeEmission.compatible?(plan, instance, register)
71
106
 
@@ -90,10 +125,14 @@ module Lutaml
90
125
  root.set_attribute("xsi:schemaLocation",
91
126
  instance.raw_schema_location)
92
127
  end
93
- built = if plan[:ordered] || !instance.element_order.nil?
94
- build_ordered(root, instance, plan, doc, register, ns_ctx)
128
+ built = if ordered_walk?(plan, instance)
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
- if child_plan[:ordered] || !item.element_order.nil?
436
+ if ordered_walk?(child_plan, item)
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
@@ -358,8 +452,14 @@ module Lutaml
358
452
  # deferred rows ride the fast lane untouched.
359
453
  return nil unless Array(value).empty?
360
454
  when :content, :content_deferred
361
- Array(value).each do |run|
362
- append_text(element, run.to_s, doc, cdata: rule.cdata)
455
+ if value.is_a?(::Array)
456
+ # The interpretive apply_mixed_content: the runs
457
+ # interleave with the element children emitted so far
458
+ # — the first before the first element, each next one
459
+ # after an element, a trailing run appended.
460
+ interleave_runs(element, value, doc, cdata: rule.cdata)
461
+ else
462
+ append_text(element, value.to_s, doc, cdata: rule.cdata)
363
463
  end
364
464
  end
365
465
  end
@@ -438,10 +538,10 @@ module Lutaml
438
538
  # without element_order (built by Ruby, not parsed) falls
439
539
  # back to declaration order — the interpretive behavior.
440
540
  def build_ordered(element, instance, plan, doc, reg = nil,
441
- ns_ctx = nil, inherited_ns = nil)
541
+ ns_ctx = nil, inherited_ns = nil, scope: nil)
442
542
  if instance.element_order.nil?
443
543
  return build(element, instance, plan, doc, reg, ns_ctx,
444
- inherited_ns)
544
+ inherited_ns, scope: scope)
445
545
  end
446
546
 
447
547
  reg ||= register
@@ -466,11 +566,14 @@ module Lutaml
466
566
  # appended emission (silent drops are data loss). The
467
567
  # reconciler speaks CompiledRule — the interpretive
468
568
  # 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?
569
+ # rows cannot drive it. A pristine instance skips the scan
570
+ # only when its order covers every value: the mutation
571
+ # flag cannot see in-place collection growth (`items <<
572
+ # x` never reaches a setter), so coverage is measured
573
+ # against the values first.
574
+ rules = if instance.mutated_after_parse? ||
575
+ order_uncovered?(instance, plan, rows_by_name,
576
+ name_keys, instance.element_order, reg)
474
577
  compiled_rules_for(instance.class, reg)
475
578
  end
476
579
  if rules
@@ -484,13 +587,20 @@ module Lutaml
484
587
  content_attr = content_entry && content_entry[1]
485
588
  content_value = content_attr &&
486
589
  value_of(instance, content_attr, content_entry[4])
487
- text_node_count = order.count { |o| o.type == "Text" }
590
+ text_node_count = order.count do |o|
591
+ order_entry?(o) && o.type == "Text"
592
+ end
488
593
  use_content_index = content_value.is_a?(Array) &&
489
594
  content_value.length == text_node_count
490
595
  content_cdata = content_entry && content_entry[0].cdata
491
596
  text_node_index = 0
492
597
 
493
598
  order.each do |object|
599
+ # element_order is a public accessor: entries that are not
600
+ # order entries (no #type) exist in the wild and are
601
+ # skipped like the interpretive applier skips them.
602
+ next unless order_entry?(object)
603
+
494
604
  case object.type
495
605
  when "Text"
496
606
  text = if content_attr && !content_value.nil?
@@ -513,106 +623,260 @@ module Lutaml
513
623
  when "Comment"
514
624
  element.add_child(doc.create_comment(object.text_content))
515
625
  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
626
+ dispatch_key = dispatch_key_for(object, rows_by_name, name_keys)
627
+ entry = rows_by_name[dispatch_key]
538
628
  next unless entry
539
629
 
540
- _, attr, kind, _spelling, delegate, row_ns = entry
630
+ row_rule, attr, kind, _spelling, delegate, row_ns = entry
541
631
  value = value_of(instance, attr, delegate)
632
+ blank = blank_row_form?(row_ns, plan)
633
+ row_ns = spelled_form(row_ns, inherited_ns, plan)
634
+ spelled = spell(ns_ctx, object.name, row_ns)
542
635
  case kind
543
636
  when :collection_native, :collection_cb
544
- index = element_indices[dispatch_key]
545
637
  items = Array(value)
546
- next unless index < items.length
638
+ index = element_indices[dispatch_key]
639
+ if index < items.length
640
+ element_indices[dispatch_key] += 1
641
+ next if items[index].nil? && nil_omitted?(row_rule)
547
642
 
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)
643
+ qualify!(add_leaf(element, spelled,
644
+ scalar_text(items[index], attr, reg), doc,
645
+ attrs: row_rule.when_attribute, cdata: row_rule.cdata),
646
+ row_ns, blank, scope, ns_ctx)
647
+ elsif index.zero? && (placeholder = placeholder_for(row_rule, value))
648
+ element_indices[dispatch_key] += 1
649
+ qualify!(add_leaf(element, spelled, nil, doc,
650
+ attrs: row_rule.when_attribute,
651
+ xsi_nil: placeholder == :xsi_nil),
652
+ row_ns, blank, scope, ns_ctx)
653
+ end
552
654
  when :nested, :ordered_deferred
553
655
  item = if attr.collection?
554
656
  index = element_indices[dispatch_key]
555
- element_indices[dispatch_key] += 1
556
- Array(value)[index]
657
+ items = Array(value)
658
+ if index < items.length
659
+ element_indices[dispatch_key] += 1
660
+ items[index]
661
+ elsif index.zero? &&
662
+ (placeholder = placeholder_for(row_rule, value))
663
+ element_indices[dispatch_key] += 1
664
+ qualify!(add_leaf(element, spelled, nil, doc,
665
+ xsi_nil: placeholder == :xsi_nil),
666
+ row_ns, blank, scope, ns_ctx)
667
+ next
668
+ end
557
669
  else
670
+ element_indices[dispatch_key] += 1 unless value.nil?
558
671
  value
559
672
  end
560
- next unless item
673
+ if item.nil?
674
+ # A singular nil renders its policy's leaf, or nothing.
675
+ placeholder = placeholder_for(row_rule, value)
676
+ next unless placeholder
677
+
678
+ qualify!(add_leaf(element, spelled, nil, doc,
679
+ xsi_nil: placeholder == :xsi_nil),
680
+ row_ns, blank, scope, ns_ctx)
681
+ next
682
+ end
561
683
 
562
684
  child_plan = nested_plans_for(plan)[[item.class, object.name]] ||
563
685
  (nested_plans_for(plan)[[item.class, object.name]] = PlanCompiler.compile(item.class, reg))
564
- child_ns = child_plan[:tree][:ns] || row_ns || inherited_ns
686
+ child_ns = nested_child_ns(child_plan, row_ns, inherited_ns)
565
687
  child = element.create_child(
566
- ns_ctx ? ns_ctx.spell(object.name, child_ns) : object.name,
688
+ spell(ns_ctx, object.name, child_ns),
567
689
  )
568
- if child_plan[:ordered] || !item.element_order.nil?
690
+ child_scope = subtree_scope(
691
+ scope,
692
+ qualify!(child, child_ns, blank, scope, ns_ctx),
693
+ )
694
+ if ordered_walk?(child_plan, item)
569
695
  return nil unless build_ordered(child, item, child_plan,
570
- doc, reg, ns_ctx, child_ns)
696
+ doc, reg, ns_ctx, child_ns,
697
+ scope: child_scope)
571
698
  else
572
- build(child, item, child_plan, doc, reg, ns_ctx, child_ns) || return
699
+ build(child, item, child_plan, doc, reg, ns_ctx, child_ns,
700
+ scope: child_scope) || return
573
701
  end
574
702
  when :raw
575
- element.add_child(value.to_s) unless value.nil?
703
+ # One captured fragment per order entry — the
704
+ # interpretive walk's per-value raw injection. A
705
+ # collection's fragments were injected whole at the
706
+ # first entry (the rest arriving as a Ruby array
707
+ # spelling).
708
+ items = value.is_a?(::Array) ? value : [value]
709
+ index = element_indices[dispatch_key]
710
+ next unless index < items.length
711
+
712
+ element_indices[dispatch_key] += 1
713
+ raw = items[index]
714
+ element.add_child(raw.to_s) unless raw.nil? || raw.to_s.empty?
576
715
  when :child_deferred
577
716
  # Valued cycle-point children: interpretive lane (see
578
717
  # build).
579
718
  return nil unless Array(value).empty?
580
719
  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)
720
+ placeholder = placeholder_for(row_rule, value)
721
+ next if placeholder.nil? && value.nil?
722
+
723
+ element_indices[dispatch_key] += 1
724
+ if placeholder
725
+ qualify!(add_leaf(element, spelled, nil, doc,
726
+ xsi_nil: placeholder == :xsi_nil),
727
+ row_ns, blank, scope, ns_ctx)
728
+ else
729
+ qualify!(add_leaf(element, spelled,
730
+ scalar_text(value, attr, reg), doc,
731
+ cdata: row_rule.cdata),
732
+ row_ns, blank, scope, ns_ctx)
733
+ end
584
734
  end
585
735
  end
586
736
  end
587
737
  return nil if !fallback_rules.empty? &&
588
738
  !emit_uncovered_values(element, instance, plan, fallback_rules,
589
- doc, reg, ns_ctx, inherited_ns)
739
+ doc, reg, ns_ctx, inherited_ns,
740
+ scope: scope)
590
741
 
591
742
  true
592
743
  end
593
744
 
745
+ # The (URI, local) dispatch key an order entry resolves to.
746
+ # A uniquely-named row absorbs the entry at any qualification
747
+ # (the interpretive matcher's lenient binding — order entries
748
+ # written by consumers carry no namespace). Contested names
749
+ # dispatch strictly by (URI, local), falling back to URI-less
750
+ # rows. Order entries may carry names with no mapped row (the
751
+ # parse records every child in document order); the layout
752
+ # cache kills its default proc to stay mutation-free, so a
753
+ # miss reads nil.
754
+ def dispatch_key_for(object, rows_by_name, name_keys)
755
+ keys = name_keys[object.name]
756
+ if keys&.length == 1
757
+ keys.first
758
+ else
759
+ key = [object.namespace_uri, object.name]
760
+ rows_by_name[key] ? key : [nil, object.name]
761
+ end
762
+ end
763
+
764
+ def order_entry?(object)
765
+ RECONCILER.order_entry?(object)
766
+ end
767
+
768
+ # Whether an instance serializes through the ordered walk: an
769
+ # ordered/mixed plan, or an instance carrying a programmatically
770
+ # tracked element_order (the order-tracker pattern, lutaml-model
771
+ # #977) whatever its mapping declares. A parse-built record on
772
+ # an unordered plan does not reroute — sequence validation reads
773
+ # it, and the interpretive writer emits mapping order there. The
774
+ # plan lane's parse-built record is PlanOrder.build's frozen
775
+ # array; a tracked one is mutable (entries are appended).
776
+ def ordered_walk?(plan, instance)
777
+ return true if plan[:ordered]
778
+ return false unless instance.is_a?(::Lutaml::Model::Serialize)
779
+
780
+ order = instance.element_order
781
+ !order.nil? && !order.empty? && !order.frozen?
782
+ end
783
+
784
+ # [attribute name, serialized name] -> CompiledRule for one
785
+ # transformation, identity-keyed by its frozen rule array.
786
+ def compiled_rule_index_for(klass, reg)
787
+ rules = compiled_rules_for(klass, reg)
788
+ return nil unless rules
789
+
790
+ COMPILED_RULE_INDEX[rules] ||= rules.each_with_object({}) do |r, h|
791
+ h[[r.attribute_name, r.serialized_name.to_s]] ||= r
792
+ end
793
+ end
794
+
795
+ # Whether the parse-built order leaves a row's value
796
+ # unemitted. Consumed entries are counted per dispatch key
797
+ # exactly as the walk dispatches them; a shortfall then goes
798
+ # to the reconciler's own expected count, which knows the
799
+ # default and derived exemptions (a parsed default is never
800
+ # a deficit).
801
+ def order_uncovered?(instance, _plan, rows_by_name, name_keys,
802
+ order, reg)
803
+ consumed = ::Hash.new(0)
804
+ order.each do |object|
805
+ next unless order_entry?(object) && object.type == "Element"
806
+
807
+ key = dispatch_key_for(object, rows_by_name, name_keys)
808
+ consumed[key] += 1 if rows_by_name[key]
809
+ end
810
+ compiled = nil
811
+ rows_by_name.each do |key, (rule, attr, kind, _spelling, delegate, _ns)|
812
+ next if emission_count(rule, attr, kind,
813
+ value_of(instance, attr, delegate)) <= consumed[key]
814
+
815
+ compiled ||= compiled_rule_index_for(instance.class, reg)
816
+ compiled_rule = compiled &&
817
+ compiled[[attr.name, rule.name.to_s]]
818
+ next unless compiled_rule
819
+
820
+ return true if RECONCILER.expected_element_count(compiled_rule,
821
+ instance) > consumed[key]
822
+ end
823
+ false
824
+ end
825
+
826
+ # How many entries the walk emits for a row's current value:
827
+ # one per item, or the single leaf a nil/empty value renders
828
+ # under its policy.
829
+ def emission_count(rule, attr, kind, value)
830
+ return 0 unless PLACEHOLDER_KINDS.include?(kind)
831
+ return 1 if placeholder_for(rule, value)
832
+ return 0 if value.nil?
833
+ return 1 unless attr.collection?
834
+
835
+ Array(value).length
836
+ end
837
+
594
838
  # OrderReconciler fallback: element rules the reconciled order
595
839
  # could not place but that still hold a value — emit them
596
840
  # appended, the ordinary way (the interpretive
597
841
  # apply_remaining_rules contract for the plan row model).
598
842
  def emit_uncovered_values(element, instance, plan, fallback, doc,
599
- reg, ns_ctx, inherited_ns)
843
+ reg, ns_ctx, inherited_ns, scope: nil)
600
844
  by_attr = {}
601
845
  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]
846
+ ns_index = ns_ctx && ns_index_for(plan)
847
+ fallback.each do |compiled_rule|
848
+ entry = by_attr[compiled_rule.attribute_name]
604
849
  next unless entry
605
850
 
606
- _rule, attr, kind, _spelling, delegate, row_ns = entry
851
+ # The fallback speaks CompiledRule; the wire name, the
852
+ # render policy and the cdata flag ride the plan's own row.
853
+ row_rule, attr, kind, spelling, delegate, _type = entry
607
854
  value = value_of(instance, attr, delegate)
608
- name = rule.name.to_s
855
+ next unless render_value?(row_rule, value, instance)
856
+
857
+ name = row_name(row_rule, spelling)
858
+ row_ns = (ns_index && ns_index[name]) || inherited_ns
859
+ blank = blank_row_form?(row_ns, plan)
860
+ ns_form = spelled_form(row_ns, inherited_ns, plan)
861
+ spelled = spell(ns_ctx, name, ns_form)
862
+ placeholder = placeholder_for(row_rule, value)
863
+ if placeholder
864
+ qualify!(add_leaf(element, spelled, nil, doc,
865
+ xsi_nil: placeholder == :xsi_nil),
866
+ ns_form, blank, scope, ns_ctx)
867
+ next
868
+ end
869
+
609
870
  Array(value).each do |item|
871
+ next if item.nil? && nil_omitted?(row_rule)
872
+
610
873
  case kind
611
874
  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)
875
+ qualify!(add_leaf(element, spelled,
876
+ scalar_text(item, attr, reg), doc,
877
+ attrs: row_rule.when_attribute,
878
+ cdata: row_rule.cdata),
879
+ ns_form, blank, scope, ns_ctx)
616
880
  when :nested, :ordered_deferred
617
881
  next if item.nil?
618
882
 
@@ -620,16 +884,20 @@ module Lutaml
620
884
  nested_plans_for(plan)[[item.class, name]] ||
621
885
  (nested_plans_for(plan)[[item.class, name]] =
622
886
  PlanCompiler.compile(item.class, reg))
623
- child_ns = child_plan[:tree][:ns] || row_ns || inherited_ns
887
+ child_ns = nested_child_ns(child_plan, ns_form, inherited_ns)
624
888
  child = element.create_child(
625
- ns_ctx ? ns_ctx.spell(name, child_ns) : name,
889
+ spell(ns_ctx, name, child_ns),
626
890
  )
627
- if child_plan[:ordered] || !item.element_order.nil?
891
+ child_scope = subtree_scope(
892
+ scope, qualify!(child, child_ns, blank, scope, ns_ctx)
893
+ )
894
+ if ordered_walk?(child_plan, item)
628
895
  return nil unless build_ordered(child, item, child_plan,
629
- doc, reg, ns_ctx, child_ns)
896
+ doc, reg, ns_ctx, child_ns,
897
+ scope: child_scope)
630
898
  else
631
899
  built = build(child, item, child_plan, doc, reg, ns_ctx,
632
- child_ns)
900
+ child_ns, scope: child_scope)
633
901
  return nil unless built
634
902
  end
635
903
  end
@@ -701,27 +969,183 @@ module Lutaml
701
969
 
702
970
  spelled = prefix ? "#{prefix}:#{name}" : name
703
971
  element.set_attribute(spelled,
704
- attr.serialize(value, :xml, reg || register).to_s)
972
+ scalar_text(value, attr, reg || register).to_s)
705
973
  end
706
974
 
707
975
  # Partition rows (#88) re-emit their discriminator: the wire
708
976
  # element carries the when_attribute pairs so the round trip
709
977
  # re-routes to the same attribute on reparse.
710
- def add_leaf(element, name, value, doc, attrs: nil, cdata: false)
978
+ # A nested child's namespace form. A row spelled :unqualified wins
979
+ # over the child plan's own namespace: under a parent whose
980
+ # elementFormDefault is not :qualified, locally declared elements
981
+ # stay bare even when the child model declares the namespace
982
+ # (the interpretive writer's determine_element_namespace scope).
983
+ def nested_child_ns(child_plan, row_ns, inherited_ns)
984
+ return row_ns if %i[unqualified blank].include?(row_ns)
985
+
986
+ child_plan[:tree][:ns] || row_ns || inherited_ns
987
+ end
988
+
989
+ # Whether a row's element is in NO namespace: :blank (see
990
+ # blank_row?), :none (an explicit `namespace: nil` rule), or
991
+ # the :unqualified row of a namespace-less model.
992
+ def blank_row_form?(form, plan)
993
+ case form
994
+ when :blank, :none
995
+ true
996
+ when :unqualified
997
+ plan[:tree][:ns].nil?
998
+ else
999
+ false
1000
+ end
1001
+ end
1002
+
1003
+ # The ns form a row is spelled through: a :blank row follows
1004
+ # the enclosing namespace's form — the root's prefix format
1005
+ # under a prefix-preserved instance, the default-namespace
1006
+ # format (with the xmlns="" opt-out) otherwise.
1007
+ def spelled_form(form, inherited_ns, plan)
1008
+ form == :blank ? (inherited_ns || plan[:tree][:ns]) : form
1009
+ end
1010
+
1011
+ # Applies the blank-namespace rule (BlankScope) to a freshly
1012
+ # created child spelled through `form`: returns the scope its
1013
+ # own children are built under when the opt-out changed it,
1014
+ # nil otherwise (callers building a subtree start it on a
1015
+ # fresh first-child slot).
1016
+ def qualify!(child, form, blank, scope, ns_ctx)
1017
+ return unless scope
1018
+
1019
+ if blank
1020
+ # Bare, or spelled through a default-namespace form: the
1021
+ # child opts out, on the first such child of each parent
1022
+ # under pretty output.
1023
+ default_spelled = !form.is_a?(::Hash) || (ns_ctx &&
1024
+ ns_ctx.prefix_for(form[:exact]).nil?)
1025
+ if default_spelled && scope.root_default &&
1026
+ !(scope.pretty && scope.done)
1027
+ child.set_attribute("xmlns", "")
1028
+ scope.done = true
1029
+ BlankScope.new(scope.root_default, true, scope.pretty)
1030
+ end
1031
+ elsif scope.blank && form.is_a?(::Hash) && ns_ctx &&
1032
+ ns_ctx.prefix_for(form[:exact]).nil?
1033
+ # A default-namespace child inside an opted-out subtree
1034
+ # re-declares its namespace here: the root's declaration
1035
+ # no longer reaches it.
1036
+ child.set_attribute("xmlns", form[:exact].to_s)
1037
+ BlankScope.new(scope.root_default, false, scope.pretty)
1038
+ end
1039
+ end
1040
+
1041
+ # The scope a nested child's subtree is built under.
1042
+ def subtree_scope(scope, changed)
1043
+ changed || BlankScope.new(scope.root_default, scope.blank,
1044
+ scope.pretty)
1045
+ end
1046
+
1047
+ def add_leaf(element, name, value, doc, attrs: nil, cdata: false,
1048
+ xsi_nil: false)
711
1049
  child = element.create_child(name)
712
1050
  attrs&.each { |k, v| child[k.to_s] = v.to_s }
713
- unless value.nil?
1051
+ child.set_attribute("xsi:nil", "true") if xsi_nil
1052
+ # An uninitialized value rendered under its value_map is an
1053
+ # empty element (the interpretive serialize_value yields no
1054
+ # text for it).
1055
+ if !value.nil? && !::Lutaml::Model::Utils.uninitialized?(value)
714
1056
  append_text(child, value.to_s, doc, cdata: cdata)
715
1057
  end
716
1058
  child
717
1059
  end
718
1060
 
1061
+ # A collection row's emission (the interpretive
1062
+ # apply_collection_element contract): one leaf per item — a
1063
+ # nil item goes unrendered when the value_map omits nil — and
1064
+ # for a nil or empty value the single leaf its policy renders,
1065
+ # or nothing.
1066
+ def emit_collection(element, spelled, ns_form, blank, rule, attr,
1067
+ value, doc, reg, scope, ns_ctx)
1068
+ items = Array(value)
1069
+ if items.empty? && (placeholder = placeholder_for(rule, value))
1070
+ return qualify!(add_leaf(element, spelled, nil, doc,
1071
+ attrs: rule.when_attribute,
1072
+ xsi_nil: placeholder == :xsi_nil),
1073
+ ns_form, blank, scope, ns_ctx)
1074
+ end
1075
+
1076
+ items.each do |item|
1077
+ next if item.nil? && nil_omitted?(rule)
1078
+
1079
+ qualify!(add_leaf(element, spelled,
1080
+ scalar_text(item, attr, reg), doc,
1081
+ attrs: rule.when_attribute, cdata: rule.cdata),
1082
+ ns_form, blank, scope, ns_ctx)
1083
+ end
1084
+ end
1085
+
1086
+ # What a nil or empty element value still renders, per the
1087
+ # rule's value_map :to (render_nil / render_empty — the
1088
+ # interpretive apply_nil_collection, apply_empty_collection
1089
+ # and apply_nil_marker contract): :xsi_nil (one leaf carrying
1090
+ # xsi:nil="true"), :blank (one bare leaf), or nil (nothing).
1091
+ def placeholder_for(rule, value)
1092
+ to_map = rule.value_map(:to)
1093
+ mode = if ::Lutaml::Model::Utils.uninitialized?(value)
1094
+ # An uninitialized value its value_map renders: the
1095
+ # interpretive apply_nil_marker_for_uninitialized
1096
+ # marks xsi:nil under :nil, else emits a bare leaf.
1097
+ to_map[:omitted] == :nil ? :nil : :blank
1098
+ elsif value.nil?
1099
+ to_map[:nil]
1100
+ elsif ::Lutaml::Model::Utils.empty?(value)
1101
+ to_map[:empty]
1102
+ else
1103
+ return nil
1104
+ end
1105
+ return :xsi_nil if mode == :nil
1106
+ return :blank if mode == :blank
1107
+ return :blank if value.nil? && mode == :empty
1108
+
1109
+ nil
1110
+ end
1111
+
1112
+ # Shared with NativeEmission's gate: the order-entry predicate,
1113
+ # the render-policy placeholder and the coverage pre-check
1114
+ # decide what the ops lane cannot serve.
1115
+ public :order_entry?, :ordered_walk?, :placeholder_for, :order_uncovered?
1116
+
1117
+ # Whether a row's value reaches the output at all — the
1118
+ # interpretive should_skip_value? contract (RenderPolicy): an
1119
+ # uninitialized, nil, or empty value is decided by its value_map
1120
+ # keys alone; every other value by the default guard.
1121
+ def render_value?(rule, value, instance)
1122
+ to_map = rule.value_map(:to)
1123
+ if ::Lutaml::Model::Utils.uninitialized?(value)
1124
+ return !(to_map[:omitted].nil? || to_map[:omitted] == :omitted)
1125
+ elsif value.nil?
1126
+ return to_map[:nil] != :omitted
1127
+ elsif ::Lutaml::Model::Utils.empty?(value)
1128
+ return to_map[:empty] != :omitted
1129
+ end
1130
+
1131
+ rule.render?(value, instance)
1132
+ end
1133
+ public :render_value?
1134
+
1135
+ def nil_omitted?(rule)
1136
+ rule.value_map(:to)[:nil] == :omitted
1137
+ end
1138
+
1139
+ def spell(ns_ctx, name, ns_form)
1140
+ ns_ctx ? ns_ctx.spell(name, ns_form) : name
1141
+ end
1142
+
719
1143
  # Text attach: the address face (leptris#408 adoption)
720
1144
  # passes raw node addresses — the pointer face mints an
721
1145
  # FFI::Pointer wrapper per fresh node, the last per-row
722
1146
  # Ruby-side allocation on the DOM build.
723
1147
  def append_text(parent, text, doc, cdata: false)
724
- node = cdata ? doc.create_cdata(text) : doc.create_text_node(text)
1148
+ node = text_node(doc, text, cdata)
725
1149
  status = ::Leptris::XML::FFI.leptris_element_add_child_addr(
726
1150
  parent.c_address, node.c_address
727
1151
  )
@@ -733,6 +1157,62 @@ module Lutaml
733
1157
  node
734
1158
  end
735
1159
 
1160
+ def text_node(doc, text, cdata)
1161
+ cdata ? doc.create_cdata(text) : doc.create_text_node(text)
1162
+ end
1163
+
1164
+ # The interpretive apply_mixed_content over the children emitted
1165
+ # so far: the runs interleave with them — the first before the
1166
+ # first child, each next one after a child, a trailing run
1167
+ # appended (the #1164 tail).
1168
+ def interleave_runs(element, runs, doc, cdata: false)
1169
+ existing = element.children
1170
+ nodes = runs.map do |run|
1171
+ run.nil? || run.to_s.empty? ? nil : text_node(doc, run.to_s, cdata)
1172
+ end
1173
+ interleaved = [nodes[0]]
1174
+ existing.each_with_index do |child, index|
1175
+ interleaved << child << nodes[index + 1]
1176
+ end
1177
+ interleaved.concat(nodes[(existing.size + 1)..] || [])
1178
+ element.children = interleaved.compact
1179
+ end
1180
+
1181
+ # Whether a Value type hooks the XML format through its own
1182
+ # methods (instance to_xml, class from_xml) instead of a
1183
+ # registered format serializer. The interpretive writer calls
1184
+ # those hooks (serialize_custom_value, from_format); the plan
1185
+ # lanes must too — or decline the shape.
1186
+ def custom_xml_type?(type)
1187
+ return false unless type.is_a?(Class) &&
1188
+ type < ::Lutaml::Model::Type::Value
1189
+
1190
+ CUSTOM_XML_TYPE[type] ||=
1191
+ !type.instance_method(:to_xml).owner.equal?(
1192
+ ::Lutaml::Model::Type::Value,
1193
+ ) ||
1194
+ !type.method(:from_xml).owner.equal?(
1195
+ ::Lutaml::Model::Type::Value.singleton_class,
1196
+ )
1197
+ end
1198
+ public :custom_xml_type?
1199
+
1200
+ # The interpretive writer's XML spelling of a scalar (its
1201
+ # serialize_value -> serialize_custom_value): a type hooking
1202
+ # to_xml is spelled through the wrapped instance; every other
1203
+ # type takes the attribute serializer's own path (its String
1204
+ # fast path included).
1205
+ def scalar_text(value, attr, reg)
1206
+ type = attr.type(reg)
1207
+ if custom_xml_type?(type)
1208
+ return value.to_xml if value.is_a?(type)
1209
+
1210
+ return type.new(value).to_xml
1211
+ end
1212
+
1213
+ attr.serialize(value, :xml, reg)
1214
+ end
1215
+
736
1216
  def value_of(instance, attr, delegate)
737
1217
  if delegate
738
1218
  target = instance.public_send(delegate)