lutaml-model 0.8.203 → 0.8.205
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitattributes +5 -0
- data/.github/workflows/lml-conformance.yml +71 -0
- data/.gitignore +7 -0
- data/README.adoc +1 -0
- data/docs/_guides/lml-cross-language.adoc +128 -0
- data/docs/_guides/native-engines.adoc +10 -0
- data/docs/_references/lml-conformance.adoc +78 -0
- data/docs/_references/lml-contract.adoc +290 -0
- data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
- data/lib/lutaml/model/lml/bundle.rb +171 -0
- data/lib/lutaml/model/lml/document.rb +46 -10
- data/lib/lutaml/model/lml/document_validator.rb +840 -0
- data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
- data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
- data/lib/lutaml/model/lml/exporter.rb +223 -87
- data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
- data/lib/lutaml/model/lml/importer.rb +180 -0
- data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
- data/lib/lutaml/model/lml/materialization.rb +257 -0
- data/lib/lutaml/model/lml/ordered_content.rb +577 -0
- data/lib/lutaml/model/lml/portable_json.rb +385 -0
- data/lib/lutaml/model/lml/rejected_error.rb +64 -15
- data/lib/lutaml/model/lml/round_trip.rb +86 -0
- data/lib/lutaml/model/lml/value_declarations.rb +84 -0
- data/lib/lutaml/model/lml.rb +61 -0
- data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
- data/lib/lutaml/model/schema/registry_generator.rb +1 -1
- data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
- data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
- data/lib/lutaml/model/sequence.rb +7 -0
- data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
- data/lib/lutaml/model/serialize/initialization.rb +2 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
- data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
- data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
- data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
- data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
- data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
- data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
- data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
- data/schema/lml/document.schema.json +179 -0
- data/scripts/lml/export_choices.rb +57 -0
- data/scripts/lml/export_conformance.rb +23 -0
- data/scripts/lml/export_mml.rb +253 -0
- data/scripts/lml/export_preservation.rb +421 -0
- data/scripts/lml/verify_artifacts.rb +31 -0
- data/spec/fixtures/lml_artifacts/model.json +1 -0
- data/spec/fixtures/lml_artifacts/valid.json +1 -0
- data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
- data/spec/fixtures/lml_conformance/core/index.json +1 -0
- data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
- data/spec/fixtures/lml_conformance/core/schema.json +1 -0
- data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
- data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
- data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
- data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
- data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
- data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
- data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
- data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
- data/spec/fixtures/lml_contract/bundle.json +81 -0
- data/spec/fixtures/lml_contract/manifest.json +30 -0
- data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
- data/spec/fixtures/lml_contract/v1_unit.json +1 -0
- data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
- data/spec/fixtures/lml_contract/v2_node.json +32 -0
- data/spec/fixtures/lml_mml/inventory.json +1 -0
- data/spec/fixtures/lml_mml/manifest.json +1 -0
- data/spec/fixtures/lml_mml/oracle.json +1 -0
- data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
- data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
- data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
- data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
- data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
- data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
- data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
- data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
- data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
- data/spec/lutaml/model/lml_document_spec.rb +279 -0
- data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
- data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
- data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
- data/spec/lutaml/model/lml_importer_spec.rb +290 -0
- data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
- data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
- data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
- data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
- data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
- data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
- data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
- data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
- data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
- data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
- data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
- data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
- data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
- data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
- data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
- data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
- data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
- data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
- data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
- data/spec/lutaml/model/sequence_spec.rb +48 -0
- data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
- data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
- data/spec/support/lml_any_attribute_fixtures.rb +26 -0
- data/spec/support/lml_conformance.rb +292 -0
- data/spec/support/lml_corpus.rb +75 -0
- data/spec/support/lml_export_fixtures.rb +190 -0
- data/spec/support/lml_graph_fixtures.rb +108 -0
- data/spec/support/lml_import_fixtures.rb +52 -0
- data/spec/support/lml_mapping_fixtures.rb +170 -0
- data/spec/support/lml_materialization_fixtures.rb +53 -0
- data/spec/support/lml_model_choices_fixtures.rb +150 -0
- data/spec/support/lml_order_fixtures.rb +151 -0
- metadata +94 -3
- data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
|
@@ -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
|
-
|
|
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
|
-
|
|
270
|
-
|
|
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[
|
|
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
|
|
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
|
-
|
|
310
|
-
|
|
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
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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)
|
|
322
395
|
end
|
|
323
396
|
when :collection_native, :collection_cb
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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
|
-
|
|
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,
|
|
336
|
-
(nested_plans[[item.class,
|
|
337
|
-
|
|
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
|
-
|
|
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,
|
|
438
|
+
doc, reg, ns_ctx, form,
|
|
439
|
+
scope: child_scope) || return
|
|
347
440
|
else
|
|
348
|
-
build(child, item, child_plan, doc, reg, ns_ctx,
|
|
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,7 +452,9 @@ 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
|
|
455
|
+
Array(value).each do |run|
|
|
456
|
+
append_text(element, run.to_s, doc, cdata: rule.cdata)
|
|
457
|
+
end
|
|
362
458
|
end
|
|
363
459
|
end
|
|
364
460
|
end
|
|
@@ -436,10 +532,10 @@ module Lutaml
|
|
|
436
532
|
# without element_order (built by Ruby, not parsed) falls
|
|
437
533
|
# back to declaration order — the interpretive behavior.
|
|
438
534
|
def build_ordered(element, instance, plan, doc, reg = nil,
|
|
439
|
-
ns_ctx = nil, inherited_ns = nil)
|
|
535
|
+
ns_ctx = nil, inherited_ns = nil, scope: nil)
|
|
440
536
|
if instance.element_order.nil?
|
|
441
537
|
return build(element, instance, plan, doc, reg, ns_ctx,
|
|
442
|
-
inherited_ns)
|
|
538
|
+
inherited_ns, scope: scope)
|
|
443
539
|
end
|
|
444
540
|
|
|
445
541
|
reg ||= register
|
|
@@ -464,11 +560,14 @@ module Lutaml
|
|
|
464
560
|
# appended emission (silent drops are data loss). The
|
|
465
561
|
# reconciler speaks CompiledRule — the interpretive
|
|
466
562
|
# transformation's memoized set; the plan's MappingRule
|
|
467
|
-
# rows cannot drive it.
|
|
468
|
-
#
|
|
469
|
-
#
|
|
470
|
-
#
|
|
471
|
-
|
|
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)
|
|
472
571
|
compiled_rules_for(instance.class, reg)
|
|
473
572
|
end
|
|
474
573
|
if rules
|
|
@@ -482,13 +581,20 @@ module Lutaml
|
|
|
482
581
|
content_attr = content_entry && content_entry[1]
|
|
483
582
|
content_value = content_attr &&
|
|
484
583
|
value_of(instance, content_attr, content_entry[4])
|
|
485
|
-
text_node_count = order.count
|
|
584
|
+
text_node_count = order.count do |o|
|
|
585
|
+
order_entry?(o) && o.type == "Text"
|
|
586
|
+
end
|
|
486
587
|
use_content_index = content_value.is_a?(Array) &&
|
|
487
588
|
content_value.length == text_node_count
|
|
488
589
|
content_cdata = content_entry && content_entry[0].cdata
|
|
489
590
|
text_node_index = 0
|
|
490
591
|
|
|
491
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
|
+
|
|
492
598
|
case object.type
|
|
493
599
|
when "Text"
|
|
494
600
|
text = if content_attr && !content_value.nil?
|
|
@@ -511,63 +617,81 @@ module Lutaml
|
|
|
511
617
|
when "Comment"
|
|
512
618
|
element.add_child(doc.create_comment(object.text_content))
|
|
513
619
|
when "Element"
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
# binding — order entries written by consumers carry no
|
|
517
|
-
# namespace). Contested names dispatch strictly by
|
|
518
|
-
# (URI, local), falling back to URI-less rows.
|
|
519
|
-
name = object.name
|
|
520
|
-
# Order entries may carry names with no mapped row (the
|
|
521
|
-
# parse records every child in document order); the
|
|
522
|
-
# layout cache kills its default proc to stay
|
|
523
|
-
# mutation-free, so a miss reads nil and drops below.
|
|
524
|
-
keys = name_keys[name]
|
|
525
|
-
if keys&.length == 1
|
|
526
|
-
dispatch_key = keys.first
|
|
527
|
-
entry = rows_by_name[dispatch_key]
|
|
528
|
-
else
|
|
529
|
-
dispatch_key = [object.namespace_uri, name]
|
|
530
|
-
entry = rows_by_name[dispatch_key]
|
|
531
|
-
unless entry
|
|
532
|
-
dispatch_key = [nil, name]
|
|
533
|
-
entry = rows_by_name[dispatch_key]
|
|
534
|
-
end
|
|
535
|
-
end
|
|
620
|
+
dispatch_key = dispatch_key_for(object, rows_by_name, name_keys)
|
|
621
|
+
entry = rows_by_name[dispatch_key]
|
|
536
622
|
next unless entry
|
|
537
623
|
|
|
538
|
-
|
|
624
|
+
row_rule, attr, kind, _spelling, delegate, row_ns = entry
|
|
539
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)
|
|
540
629
|
case kind
|
|
541
630
|
when :collection_native, :collection_cb
|
|
542
|
-
index = element_indices[dispatch_key]
|
|
543
631
|
items = Array(value)
|
|
544
|
-
|
|
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)
|
|
545
636
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
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
|
|
550
648
|
when :nested, :ordered_deferred
|
|
551
649
|
item = if attr.collection?
|
|
552
650
|
index = element_indices[dispatch_key]
|
|
553
|
-
|
|
554
|
-
|
|
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
|
|
555
663
|
else
|
|
664
|
+
element_indices[dispatch_key] += 1 unless value.nil?
|
|
556
665
|
value
|
|
557
666
|
end
|
|
558
|
-
|
|
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
|
|
559
677
|
|
|
560
678
|
child_plan = nested_plans_for(plan)[[item.class, object.name]] ||
|
|
561
679
|
(nested_plans_for(plan)[[item.class, object.name]] = PlanCompiler.compile(item.class, reg))
|
|
562
|
-
child_ns = child_plan
|
|
680
|
+
child_ns = nested_child_ns(child_plan, row_ns, inherited_ns)
|
|
563
681
|
child = element.create_child(
|
|
564
|
-
ns_ctx
|
|
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),
|
|
565
687
|
)
|
|
566
688
|
if child_plan[:ordered] || !item.element_order.nil?
|
|
567
689
|
return nil unless build_ordered(child, item, child_plan,
|
|
568
|
-
doc, reg, ns_ctx, child_ns
|
|
690
|
+
doc, reg, ns_ctx, child_ns,
|
|
691
|
+
scope: child_scope)
|
|
569
692
|
else
|
|
570
|
-
build(child, item, child_plan, doc, reg, ns_ctx, child_ns
|
|
693
|
+
build(child, item, child_plan, doc, reg, ns_ctx, child_ns,
|
|
694
|
+
scope: child_scope) || return
|
|
571
695
|
end
|
|
572
696
|
when :raw
|
|
573
697
|
element.add_child(value.to_s) unless value.nil?
|
|
@@ -576,40 +700,150 @@ module Lutaml
|
|
|
576
700
|
# build).
|
|
577
701
|
return nil unless Array(value).empty?
|
|
578
702
|
when :scalar
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
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
|
|
582
717
|
end
|
|
583
718
|
end
|
|
584
719
|
end
|
|
585
720
|
return nil if !fallback_rules.empty? &&
|
|
586
721
|
!emit_uncovered_values(element, instance, plan, fallback_rules,
|
|
587
|
-
doc, reg, ns_ctx, inherited_ns
|
|
722
|
+
doc, reg, ns_ctx, inherited_ns,
|
|
723
|
+
scope: scope)
|
|
588
724
|
|
|
589
725
|
true
|
|
590
726
|
end
|
|
591
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
|
+
|
|
592
805
|
# OrderReconciler fallback: element rules the reconciled order
|
|
593
806
|
# could not place but that still hold a value — emit them
|
|
594
807
|
# appended, the ordinary way (the interpretive
|
|
595
808
|
# apply_remaining_rules contract for the plan row model).
|
|
596
809
|
def emit_uncovered_values(element, instance, plan, fallback, doc,
|
|
597
|
-
reg, ns_ctx, inherited_ns)
|
|
810
|
+
reg, ns_ctx, inherited_ns, scope: nil)
|
|
598
811
|
by_attr = {}
|
|
599
812
|
plan[:rows].each { |row| by_attr[row[1].name] ||= row if row[1] }
|
|
600
|
-
|
|
601
|
-
|
|
813
|
+
ns_index = ns_ctx && ns_index_for(plan)
|
|
814
|
+
fallback.each do |compiled_rule|
|
|
815
|
+
entry = by_attr[compiled_rule.attribute_name]
|
|
602
816
|
next unless entry
|
|
603
817
|
|
|
604
|
-
|
|
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
|
|
605
821
|
value = value_of(instance, attr, delegate)
|
|
606
|
-
|
|
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
|
+
|
|
607
837
|
Array(value).each do |item|
|
|
838
|
+
next if item.nil? && nil_omitted?(row_rule)
|
|
839
|
+
|
|
608
840
|
case kind
|
|
609
841
|
when :collection_native, :collection_cb, :scalar
|
|
610
|
-
add_leaf(element,
|
|
611
|
-
|
|
612
|
-
|
|
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)
|
|
613
847
|
when :nested, :ordered_deferred
|
|
614
848
|
next if item.nil?
|
|
615
849
|
|
|
@@ -617,16 +851,20 @@ module Lutaml
|
|
|
617
851
|
nested_plans_for(plan)[[item.class, name]] ||
|
|
618
852
|
(nested_plans_for(plan)[[item.class, name]] =
|
|
619
853
|
PlanCompiler.compile(item.class, reg))
|
|
620
|
-
child_ns = child_plan
|
|
854
|
+
child_ns = nested_child_ns(child_plan, ns_form, inherited_ns)
|
|
621
855
|
child = element.create_child(
|
|
622
|
-
ns_ctx
|
|
856
|
+
spell(ns_ctx, name, child_ns),
|
|
857
|
+
)
|
|
858
|
+
child_scope = subtree_scope(
|
|
859
|
+
scope, qualify!(child, child_ns, blank, scope, ns_ctx)
|
|
623
860
|
)
|
|
624
861
|
if child_plan[:ordered] || !item.element_order.nil?
|
|
625
862
|
return nil unless build_ordered(child, item, child_plan,
|
|
626
|
-
doc, reg, ns_ctx, child_ns
|
|
863
|
+
doc, reg, ns_ctx, child_ns,
|
|
864
|
+
scope: child_scope)
|
|
627
865
|
else
|
|
628
866
|
built = build(child, item, child_plan, doc, reg, ns_ctx,
|
|
629
|
-
child_ns)
|
|
867
|
+
child_ns, scope: child_scope)
|
|
630
868
|
return nil unless built
|
|
631
869
|
end
|
|
632
870
|
end
|
|
@@ -698,25 +936,183 @@ module Lutaml
|
|
|
698
936
|
|
|
699
937
|
spelled = prefix ? "#{prefix}:#{name}" : name
|
|
700
938
|
element.set_attribute(spelled,
|
|
701
|
-
|
|
939
|
+
scalar_text(value, attr, reg || register).to_s)
|
|
702
940
|
end
|
|
703
941
|
|
|
704
942
|
# Partition rows (#88) re-emit their discriminator: the wire
|
|
705
943
|
# element carries the when_attribute pairs so the round trip
|
|
706
944
|
# re-routes to the same attribute on reparse.
|
|
707
|
-
|
|
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)
|
|
708
1016
|
child = element.create_child(name)
|
|
709
1017
|
attrs&.each { |k, v| child[k.to_s] = v.to_s }
|
|
710
|
-
|
|
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)
|
|
1023
|
+
append_text(child, value.to_s, doc, cdata: cdata)
|
|
1024
|
+
end
|
|
711
1025
|
child
|
|
712
1026
|
end
|
|
713
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
|
+
|
|
714
1110
|
# Text attach: the address face (leptris#408 adoption)
|
|
715
1111
|
# passes raw node addresses — the pointer face mints an
|
|
716
1112
|
# FFI::Pointer wrapper per fresh node, the last per-row
|
|
717
1113
|
# Ruby-side allocation on the DOM build.
|
|
718
|
-
def append_text(parent, text, doc)
|
|
719
|
-
node = doc.create_text_node(text)
|
|
1114
|
+
def append_text(parent, text, doc, cdata: false)
|
|
1115
|
+
node = cdata ? doc.create_cdata(text) : doc.create_text_node(text)
|
|
720
1116
|
status = ::Leptris::XML::FFI.leptris_element_add_child_addr(
|
|
721
1117
|
parent.c_address, node.c_address
|
|
722
1118
|
)
|
|
@@ -728,6 +1124,41 @@ module Lutaml
|
|
|
728
1124
|
node
|
|
729
1125
|
end
|
|
730
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
|
+
|
|
731
1162
|
def value_of(instance, attr, delegate)
|
|
732
1163
|
if delegate
|
|
733
1164
|
target = instance.public_send(delegate)
|