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.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +5 -0
  3. data/.github/workflows/lml-conformance.yml +71 -0
  4. data/.gitignore +7 -0
  5. data/README.adoc +1 -0
  6. data/docs/_guides/lml-cross-language.adoc +128 -0
  7. data/docs/_guides/native-engines.adoc +10 -0
  8. data/docs/_references/lml-conformance.adoc +78 -0
  9. data/docs/_references/lml-contract.adoc +290 -0
  10. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  11. data/lib/lutaml/model/lml/bundle.rb +171 -0
  12. data/lib/lutaml/model/lml/document.rb +46 -10
  13. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  14. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  15. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  16. data/lib/lutaml/model/lml/exporter.rb +223 -87
  17. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  18. data/lib/lutaml/model/lml/importer.rb +180 -0
  19. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  20. data/lib/lutaml/model/lml/materialization.rb +257 -0
  21. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  22. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  23. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  24. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  25. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  26. data/lib/lutaml/model/lml.rb +61 -0
  27. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  28. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  29. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  30. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  31. data/lib/lutaml/model/sequence.rb +7 -0
  32. data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
  33. data/lib/lutaml/model/serialize/initialization.rb +2 -0
  34. data/lib/lutaml/model/version.rb +1 -1
  35. data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
  36. data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
  37. data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
  38. data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
  39. data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
  40. data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
  41. data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
  42. data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
  43. data/schema/lml/document.schema.json +179 -0
  44. data/scripts/lml/export_choices.rb +57 -0
  45. data/scripts/lml/export_conformance.rb +23 -0
  46. data/scripts/lml/export_mml.rb +253 -0
  47. data/scripts/lml/export_preservation.rb +421 -0
  48. data/scripts/lml/verify_artifacts.rb +31 -0
  49. data/spec/fixtures/lml_artifacts/model.json +1 -0
  50. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  51. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  52. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  53. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  54. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  55. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  56. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  57. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  58. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  59. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  60. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  61. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  62. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  63. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  64. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  65. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  66. data/spec/fixtures/lml_contract/bundle.json +81 -0
  67. data/spec/fixtures/lml_contract/manifest.json +30 -0
  68. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  69. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  70. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  71. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  72. data/spec/fixtures/lml_mml/inventory.json +1 -0
  73. data/spec/fixtures/lml_mml/manifest.json +1 -0
  74. data/spec/fixtures/lml_mml/oracle.json +1 -0
  75. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  76. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  77. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  78. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  79. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  80. data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
  81. data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
  82. data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
  83. data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
  84. data/spec/lutaml/model/lml_document_spec.rb +279 -0
  85. data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
  86. data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
  87. data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
  88. data/spec/lutaml/model/lml_importer_spec.rb +290 -0
  89. data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
  90. data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
  91. data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
  92. data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
  93. data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
  94. data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
  95. data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
  96. data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
  97. data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
  98. data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
  99. data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
  100. data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
  101. data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
  102. data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
  103. data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
  104. data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
  105. data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
  106. data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
  107. data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
  108. data/spec/lutaml/model/sequence_spec.rb +48 -0
  109. data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
  110. data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
  111. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  112. data/spec/support/lml_conformance.rb +292 -0
  113. data/spec/support/lml_corpus.rb +75 -0
  114. data/spec/support/lml_export_fixtures.rb +190 -0
  115. data/spec/support/lml_graph_fixtures.rb +108 -0
  116. data/spec/support/lml_import_fixtures.rb +52 -0
  117. data/spec/support/lml_mapping_fixtures.rb +170 -0
  118. data/spec/support/lml_materialization_fixtures.rb +53 -0
  119. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  120. data/spec/support/lml_order_fixtures.rb +151 -0
  121. metadata +94 -3
  122. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "bigdecimal"
4
+ require "date"
5
+ require "time"
6
+
3
7
  module Lutaml
4
8
  module Model
5
9
  module Lml
@@ -11,15 +15,74 @@ module Lutaml
11
15
  # Only public introspection is used: `Klass.attributes(register)`,
12
16
  # `Klass.mappings_for(format, register)`, `Klass.mappings` (the
13
17
  # declared-format keys), `Lutaml::Model::Type`'s builtin registry.
18
+ #
19
+ # This class holds what both document versions share (type
20
+ # resolution, reject checks, the mapping walk); the version-specific
21
+ # parts live in Exporter::V1Projector (the v1 Document) and
22
+ # Exporter::V2Projector (the portable v2 Hash), which fill the
23
+ # extension hooks below.
14
24
  class Exporter
25
+ autoload :V1Projector, "#{__dir__}/exporter/v1_projector"
26
+ autoload :V2Projector, "#{__dir__}/exporter/v2_projector"
27
+
15
28
  SERIALIZABLE_BASE = Lutaml::Model::Serialize
29
+ ORDERED_CONTENT_MEMBER = "__lml_content"
30
+ ORDERED_CONTENT = { "member" => ORDERED_CONTENT_MEMBER, "version" => 1 }.freeze
31
+ PORTABLE_REGISTER = /\A[A-Za-z0-9_.:-]+\z/
32
+ RENDER_NAMES = { omitted: "omit", nil: "as_nil", empty: "as_empty",
33
+ blank: "as_blank" }.freeze
34
+ # key_value/rdf DSL blocks fan out to these resolved formats that
35
+ # are not wire formats of the v2 contract (in-memory Hash,
36
+ # line-delimited json/yaml, and the :lml format the LML adapter
37
+ # registers as key-value). Portable output omits one only when
38
+ # it is identical to the json/yaml projection, else refuses it.
39
+ DERIVED_FORMATS = %w[hash jsonl yamls lml].freeze
40
+ # Attribute options the portable document carries (the rest of
41
+ # the option space is behaviour — transforms, patterns, facets,
42
+ # polymorphism, choices, references — with no slot in the v2
43
+ # contract, so a model using one is refused by name instead of
44
+ # exporting a plain attribute that silently loses it).
45
+ PORTABLE_ATTRIBUTE_OPTIONS = %i[default collection values initialize_empty
46
+ required desc].freeze
47
+ REFERENCE_OPTIONS = %i[ref_model_class ref_key_attribute].freeze
48
+ DEFAULT_NAMES = ->(klass) { klass.is_a?(::String) ? klass : klass.name }
49
+
50
+ def self.export(klass, register: nil, names: nil)
51
+ V1Projector.new(klass, register: register, names: names).export
52
+ end
53
+
54
+ # Portable-v2 model document (lutaml-model#1033 contract in
55
+ # 01-DOCUMENT-CONTRACT): tagged types, typed defaults, resolved
56
+ # render policies, namespace modes and the ordered-content
57
+ # descriptor. `export` keeps the v1 document unchanged.
58
+ def self.project(klass, register: nil, names: nil, value_declarations: nil)
59
+ V2Projector.new(klass, register: register, names: names, value_declarations: value_declarations).project
60
+ end
16
61
 
17
- def self.export(klass, register: nil)
18
- new(klass, register: register).export
62
+ # The projector the graph exporter and the bundle gates call:
63
+ # (klass, register string) -> v2 document Hash. "default" is the
64
+ # absence of a register.
65
+ def self.graph_projector(names: nil, value_declarations: nil)
66
+ lambda do |klass, register|
67
+ project(klass, register: register.to_s == GraphExporter::DEFAULT_REGISTER ? nil : register.to_sym,
68
+ names: names, value_declarations: value_declarations)
69
+ end
19
70
  end
20
71
 
21
- def initialize(klass, register: nil)
72
+ # @param names [#call, nil] klass (or a class-name String, as a
73
+ # discriminator case may carry) -> the name the document records
74
+ # for that class (default: the class's own name). Lets a caller
75
+ # whose classes carry other names than the declarations they
76
+ # stand for (a materialized bundle) export in the original
77
+ # identifier space.
78
+ def initialize(klass, register: nil, names: nil, value_declarations: nil)
79
+ unless value_declarations.nil? || value_declarations.is_a?(ValueDeclarations)
80
+ raise ArgumentError, "value_declarations must be a ValueDeclarations registry"
81
+ end
82
+
83
+ @value_declarations = value_declarations
22
84
  @klass = klass
85
+ @names = names || DEFAULT_NAMES
23
86
  # A nil caller register resolves through the model's own
24
87
  # declared default register (lutaml_default_register) — the
25
88
  # same precedence mappings_for applies internally, needed
@@ -29,18 +92,75 @@ module Lutaml
29
92
  (klass.lutaml_default_register if klass.respond_to?(:lutaml_default_register))
30
93
  end
31
94
 
32
- def export
33
- reject_non_serializable!
34
- reject_custom_initialize!
35
- Document.new(
36
- model_name: @klass.name,
37
- register: @register,
38
- attributes: @klass.attributes(@register).map { |_, attr| project_attribute(attr) },
39
- mappings: project_mappings,
40
- )
95
+ private
96
+
97
+ # ---- extension hooks (version-specific) -------------------------
98
+
99
+ def namespace_fields(_mapping) = raise(NotImplementedError)
100
+ def content_rule_extras(_rule) = raise(NotImplementedError)
101
+ def element_rule_extras(_rule) = raise(NotImplementedError)
102
+ def key_value_rule_extras(_rule) = raise(NotImplementedError)
103
+ def render_fields(_rule) = raise(NotImplementedError)
104
+ def any_attribute_rule(_mapping) = nil
105
+ def finish_mappings!(_projected) = nil
106
+ def reject_executable_rule!(_rule) = nil
107
+ def reject_custom_value_type!(_resolved, _attr) = nil
108
+
109
+ # ---- naming ------------------------------------------------------
110
+
111
+ def model_name(klass)
112
+ @names.call(klass)
41
113
  end
42
114
 
43
- private
115
+ def portable_register
116
+ name = (@register || "default").to_s
117
+ return name if name.match?(PORTABLE_REGISTER)
118
+
119
+ raise RejectedError.new(:invalid_document, @klass,
120
+ "register #{RejectedError.show(name)} is not a portable name")
121
+ end
122
+
123
+ def model_id(klass)
124
+ "#{model_name(klass)}@#{portable_register}"
125
+ end
126
+
127
+ def class_path(value)
128
+ return nil if value.nil?
129
+ return model_name(value) if value.is_a?(Class)
130
+
131
+ value.to_s
132
+ end
133
+
134
+ # ---- reject checks ----------------------------------------------
135
+
136
+ # The portable document addresses a model by its class path; an
137
+ # anonymous class has none.
138
+ def reject_anonymous!
139
+ return unless @klass.respond_to?(:name) && (@klass.name.nil? || @klass.name.empty?)
140
+
141
+ raise RejectedError.new(:invalid_document, @klass,
142
+ "anonymous class has no portable model name")
143
+ end
144
+
145
+ def reject_reserved_member!
146
+ return unless @klass.attributes(@register).key?(ORDERED_CONTENT_MEMBER.to_sym)
147
+
148
+ raise RejectedError.new(:reserved_member, @klass,
149
+ "#{ORDERED_CONTENT_MEMBER} is reserved for ordered content")
150
+ end
151
+
152
+ def ordered_content_descriptor
153
+ return nil unless ordered_models?
154
+
155
+ ORDERED_CONTENT
156
+ end
157
+
158
+ def ordered_models?
159
+ @klass.mappings.keys.any? do |format|
160
+ mapping = @klass.mappings_for(format, @register)
161
+ mapping.respond_to?(:ordered?) && (mapping.ordered? || mapping.mixed_content?)
162
+ end
163
+ end
44
164
 
45
165
  def reject_non_serializable!
46
166
  return if @klass.is_a?(Class) && @klass.include?(SERIALIZABLE_BASE)
@@ -70,39 +190,10 @@ module Lutaml
70
190
  "initialize is owned by #{owner}; the body stays hand-written")
71
191
  end
72
192
 
73
- def project_attribute(attr)
74
- {
75
- "name" => attr.name.to_s,
76
- "type" => type_name(attr),
77
- "collection" => collection_field(attr),
78
- "default" => static_default(attr),
79
- "required" => true_or_nil(attr.options[:required]),
80
- "values" => enum_field(attr),
81
- "initialize_empty" => true_or_nil(attr.initialize_empty?),
82
- "polymorphic_class" => class_path(attr.options[:polymorphic_class]),
83
- "reference" => reference_field(attr),
84
- }.compact
85
- end
86
-
87
- def enum_field(attr)
88
- values = attr.enum_values
89
- return nil if values.nil? || values.empty?
90
-
91
- values.map(&:to_s)
92
- end
193
+ # ---- type resolution --------------------------------------------
93
194
 
94
195
  def type_name(attr)
95
- resolved =
96
- begin
97
- attr.type(@register)
98
- rescue UnknownTypeError
99
- # Deferred model references: consumers that declare
100
- # `attribute :x, "Ns::Model"` by string path resolve
101
- # through autoload, which the type resolver refuses to
102
- # materialize mid-finalization. Exporting happens
103
- # post-definition — materialize here.
104
- const_get_or_nil(attr.unresolved_type.to_s)
105
- end
196
+ resolved = resolve_type(attr)
106
197
  if resolved.nil?
107
198
  raise RejectedError.new(:unknown_type, @klass,
108
199
  "attribute #{attr.name} has an unresolved type")
@@ -111,6 +202,17 @@ module Lutaml
111
202
  builtin_name(resolved) || model_name_for(resolved, attr)
112
203
  end
113
204
 
205
+ def resolve_type(attr)
206
+ attr.type(@register)
207
+ rescue UnknownTypeError
208
+ # Deferred model references: consumers that declare
209
+ # `attribute :x, "Ns::Model"` by string path resolve
210
+ # through autoload, which the type resolver refuses to
211
+ # materialize mid-finalization. Exporting happens
212
+ # post-definition — materialize here.
213
+ const_get_or_nil(attr.unresolved_type.to_s)
214
+ end
215
+
114
216
  def const_get_or_nil(path)
115
217
  Object.const_get(path)
116
218
  rescue NameError
@@ -130,13 +232,16 @@ module Lutaml
130
232
  end
131
233
 
132
234
  def model_name_for(resolved, attr)
133
- return resolved.name if resolved.is_a?(Class) &&
235
+ reject_custom_value_type!(resolved, attr)
236
+ return model_name(resolved) if resolved.is_a?(Class) &&
134
237
  resolved.include?(SERIALIZABLE_BASE) && resolved.name
135
238
 
136
239
  raise RejectedError.new(:unknown_type, @klass,
137
240
  "attribute #{attr.name}: #{resolved} has no LML name")
138
241
  end
139
242
 
243
+ # ---- attribute field helpers -------------------------------------
244
+
140
245
  def collection_field(attr)
141
246
  return nil unless attr.collection?
142
247
 
@@ -157,7 +262,7 @@ module Lutaml
157
262
  if default.is_a?(Float) && !default.finite?
158
263
  # JSON's canonical form cannot carry Infinity/NaN.
159
264
  raise RejectedError.new(:non_serializable_default, @klass,
160
- "attribute #{attr.name}: default #{default.inspect}")
265
+ "attribute #{attr.name}: default #{RejectedError.show(default)}")
161
266
  end
162
267
  default
163
268
  when :delegate
@@ -176,25 +281,20 @@ module Lutaml
176
281
  { "model" => class_path(ref) }.compact
177
282
  end
178
283
 
179
- def class_path(value)
180
- return nil if value.nil?
181
- return value.name if value.is_a?(Class)
182
-
183
- value.to_s
184
- end
185
-
186
284
  def true_or_nil(value)
187
285
  value ? true : nil
188
286
  end
189
287
 
288
+ # ---- mappings -----------------------------------------------------
289
+
190
290
  def project_mappings
191
291
  # Export per RESOLVED format, not per DSL block: the
192
292
  # key_value/rdf DSL fan one block out across the registered
193
293
  # formats, and each resolved format carries its own
194
294
  # projection.
195
- @klass.mappings.each_with_object({}) do |(format, _), out|
196
- out[format.to_s] = project_format(format)
197
- end
295
+ projected = @klass.mappings.keys.to_h { |format| [format.to_s, project_format(format)] }
296
+ finish_mappings!(projected)
297
+ projected
198
298
  end
199
299
 
200
300
  def project_format(format)
@@ -208,25 +308,72 @@ module Lutaml
208
308
 
209
309
  def project_xml_mapping(mapping)
210
310
  reject_imperative_element_order!(mapping)
211
- sequence = mapping.element_sequence
212
- sequence = nil if sequence.is_a?(Array) && sequence.empty?
213
311
  {
214
312
  "root" => root_field(mapping),
215
313
  "ordered" => true_or_nil(mapping.ordered?),
216
314
  "mixed_content" => true_or_nil(mapping.mixed_content?),
217
- "sequence" => sequence&.map(&:to_s),
218
- "namespace_uri" => mapping.namespace_uri,
219
- "namespace_prefix" => mapping.namespace_prefix,
220
- "rules" => mapping.mappings.map { |rule| project_xml_rule(rule) },
221
- }.compact
315
+ "sequence" => sequence_field(mapping),
316
+ "rules" => project_xml_rules(mapping),
317
+ }.merge(namespace_fields(mapping)).compact
318
+ end
319
+
320
+ # The explicit rules in declared order, then the catch-all
321
+ # attribute rule (Ruby emits that remainder after the mapped
322
+ # attributes, so it is the last rule).
323
+ def project_xml_rules(mapping)
324
+ rules = mapping.mappings.map { |rule| project_xml_rule(rule) }
325
+ any = any_attribute_rule(mapping)
326
+ rules << any if any
327
+ rules
328
+ end
329
+
330
+ # Both v1 and v2 project every sequence compositor as
331
+ # {kind:"sequence", members:[...]}: member element rules in
332
+ # declared order, with a nested choice as
333
+ # {kind:"choice",min,max,members:[...]} at the position of its
334
+ # first member. (v1 formerly emitted Sequence#to_s, which embeds
335
+ # an object address and made exports differ between runs.)
336
+ def sequence_field(mapping)
337
+ sequences = mapping.element_sequence
338
+ return nil unless sequences.is_a?(Array) && sequences.any?
339
+
340
+ sequences.map { |sequence| project_sequence(sequence) }
341
+ end
342
+
343
+ def project_sequence(sequence)
344
+ members = []
345
+ placed = []
346
+ sequence.attributes.each do |rule|
347
+ choice = sequence.compositors.find { |c| c.attributes.any? { |a| a.name == rule.to } }
348
+ if choice.nil?
349
+ members << project_xml_rule(rule)
350
+ elsif !placed.include?(choice)
351
+ placed << choice
352
+ members << project_choice(choice, sequence)
353
+ end
354
+ end
355
+ { "kind" => "sequence", "members" => members }
356
+ end
357
+
358
+ def project_choice(choice, sequence)
359
+ names = choice.attributes.map(&:name)
360
+ rules = sequence.attributes.select { |rule| names.include?(rule.to) }
361
+ { "kind" => "choice", "min" => choice.min, "max" => choice.max,
362
+ "members" => rules.map { |rule| project_xml_rule(rule) } }
222
363
  end
223
364
 
365
+ # Declarative ordered/mixed content exports (per-instance order is
366
+ # data, carried by the ordered-content descriptor). Only order
367
+ # driven by executable callbacks has no declarative form.
224
368
  def reject_imperative_element_order!(mapping)
225
- sequence = mapping.element_sequence
226
- return if !mapping.ordered? || (sequence.is_a?(Array) && !sequence.empty?)
369
+ return unless mapping.ordered? || mapping.mixed_content?
370
+
371
+ callback = mapping.mappings.find { |rule| rule.custom_methods.to_h.any? }
372
+ callback ||= mapping.all_listeners.first if mapping.respond_to?(:all_listeners)
373
+ return unless callback
227
374
 
228
375
  raise RejectedError.new(:imperative_element_order, @klass,
229
- "ordered content without a declared sequence is instance-driven")
376
+ "ordered content is driven by an executable callback")
230
377
  end
231
378
 
232
379
  def root_field(mapping)
@@ -245,38 +392,27 @@ module Lutaml
245
392
  "kind" => "content",
246
393
  "to" => rule.to&.to_s,
247
394
  "cdata" => true_or_nil(rule.cdata),
248
- "render_nil" => render_nil_field(rule),
249
- }.compact
395
+ }.merge(content_rule_extras(rule)).merge(render_fields(rule)).compact
250
396
  end
251
397
 
398
+ reject_executable_rule!(rule)
252
399
  {
253
400
  "kind" => rule.attribute ? "attribute" : "element",
254
401
  "name" => Array(rule.name).map(&:to_s),
255
402
  "to" => rule.to&.to_s,
256
- "namespace_uri" => xml_rule_namespace(rule),
257
- "prefix" => rule.prefix&.to_s,
258
403
  "cdata" => true_or_nil(rule.cdata),
259
- "render_nil" => render_nil_field(rule),
260
404
  "polymorphic_map" => polymorphic_map_field(rule),
261
- }.compact
405
+ }.merge(element_rule_extras(rule)).merge(render_fields(rule)).compact
262
406
  end
263
407
 
264
- def xml_rule_namespace(rule)
265
- namespace = rule.namespace
266
- return nil unless namespace.is_a?(String) || namespace.is_a?(Symbol)
267
-
268
- namespace.to_s
269
- end
270
-
271
- def render_nil_field(rule)
272
- value = rule.render_nil
408
+ def render_policy_field(value)
273
409
  return nil unless value && value != false
274
410
 
275
411
  value == true ? true : value.to_s
276
412
  end
277
413
 
278
414
  def polymorphic_map_field(rule)
279
- return nil unless rule.polymorphic_map.is_a?(Hash) && rule.polymorphic_map.any?
415
+ return nil unless rule.polymorphic_map.is_a?(::Hash) && rule.polymorphic_map.any?
280
416
 
281
417
  rule.polymorphic_map.to_h { |key, value| [class_path(key), value.to_s] }
282
418
  end
@@ -293,30 +429,30 @@ module Lutaml
293
429
  return nil unless mapping.respond_to?(:root_mappings)
294
430
 
295
431
  root_mappings = mapping.root_mappings
296
- return nil unless root_mappings.is_a?(Hash) && root_mappings.any?
432
+ return nil unless root_mappings.is_a?(::Hash) && root_mappings.any?
297
433
 
298
434
  root_mappings.to_h { |key, value| [key.to_s, value.to_s] }
299
435
  end
300
436
 
301
437
  def project_key_value_rule(rule)
438
+ reject_executable_rule!(rule)
302
439
  {
303
440
  "key" => Array(rule.name).map(&:to_s),
304
441
  "to" => rule.to&.to_s,
305
- "render_nil" => render_nil_field(rule),
306
442
  "child_mappings" => child_mappings_field(rule),
307
- }.compact
443
+ }.merge(key_value_rule_extras(rule)).merge(render_fields(rule)).compact
308
444
  end
309
445
 
310
446
  def child_mappings_field(rule)
311
447
  child_mappings = rule.child_mappings
312
- return nil unless child_mappings.is_a?(Hash) && child_mappings.any?
448
+ return nil unless child_mappings.is_a?(::Hash) && child_mappings.any?
313
449
 
314
450
  child_mappings.to_h { |key, value| [key.to_s, canonicalize_value(value)] }
315
451
  end
316
452
 
317
453
  def canonicalize_value(value)
318
454
  case value
319
- when Hash then value.to_h { |key, item| [key.to_s, canonicalize_value(item)] }
455
+ when ::Hash then value.to_h { |key, item| [key.to_s, canonicalize_value(item)] }
320
456
  when Array then value.map { |item| canonicalize_value(item) }
321
457
  when Symbol then value.to_s
322
458
  else value