lutaml-model 0.8.156 → 0.8.158

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/opal.yml +5 -17
  3. data/.rubocop.yml +10 -0
  4. data/Rakefile +5 -1
  5. data/docs/_guides/opal.adoc +11 -0
  6. data/docs/_pages/serialization_adapters.adoc +47 -2
  7. data/lib/compat/opal/monitor.rb +31 -0
  8. data/lib/compat/opal/moxml/adapter/customized_rexml/entity_reference.rb +9 -0
  9. data/lib/compat/opal/moxml/adapter/customized_rexml/formatter.rb +225 -0
  10. data/lib/compat/opal/moxml/adapter/customized_rexml.rb +11 -0
  11. data/lib/compat/opal/moxml_boot.rb +66 -71
  12. data/lib/compat/opal/objectspace_shim.rb +23 -0
  13. data/lib/compat/opal/rexml/namespace.rb +59 -0
  14. data/lib/compat/opal/rexml/parsers/baseparser.rb +1016 -0
  15. data/lib/compat/opal/rexml/source.rb +214 -0
  16. data/lib/compat/opal/rexml/text.rb +426 -0
  17. data/lib/compat/opal/rexml/xmltokens.rb +45 -0
  18. data/lib/compat/opal/rexml_compat.rb +85 -0
  19. data/lib/lutaml/model/adapter_resolver.rb +29 -0
  20. data/lib/lutaml/model/cached_type_resolver/concurrent_map_cache.rb +3 -1
  21. data/lib/lutaml/model/cached_type_resolver/mutex_hash_cache.rb +3 -1
  22. data/lib/lutaml/model/cached_type_resolver.rb +13 -22
  23. data/lib/lutaml/model/collection.rb +54 -5
  24. data/lib/lutaml/model/mapping/mapping_rule.rb +28 -4
  25. data/lib/lutaml/model/serialize/builder.rb +1 -1
  26. data/lib/lutaml/model/serialize/format_conversion.rb +26 -1
  27. data/lib/lutaml/model/serialize.rb +6 -1
  28. data/lib/lutaml/model/services/rule_value_extractor.rb +39 -35
  29. data/lib/lutaml/model/utils.rb +8 -1
  30. data/lib/lutaml/model/version.rb +1 -1
  31. data/lib/lutaml/xml/adapter/oga_adapter.rb +4 -0
  32. data/lib/lutaml/xml/adapter/rexml_adapter.rb +4 -0
  33. data/lib/lutaml/xml/leptris/plan_hydrator.rb +14 -7
  34. data/lib/lutaml/xml/leptris/plan_order.rb +24 -7
  35. data/lib/lutaml/xml/model_transform.rb +21 -3
  36. data/lib/lutaml/xml/parse_session.rb +4 -3
  37. data/lib/lutaml/xml/xml_attribute.rb +31 -11
  38. data/lutaml-model.gemspec +6 -4
  39. data/spec/lutaml/model/collection_scalar_maps_spec.rb +58 -0
  40. data/spec/lutaml/xml/opal_xml_spec.rb +10 -0
  41. metadata +16 -4
@@ -31,7 +31,7 @@ module Lutaml
31
31
  # raw engine DOM, the retention the eager default exists
32
32
  # to avoid)
33
33
  def call(model_class, plan, value, parent: nil, node: nil,
34
- register: nil, declaration_plan_mode: :eager)
34
+ register: nil, declaration_plan_mode: :eager, intern: nil)
35
35
  # The child model's declared lutaml_default_register takes
36
36
  # precedence over the ambient register — the same
37
37
  # resolve_for_child contract the interpretive path applies.
@@ -41,12 +41,19 @@ module Lutaml
41
41
  register = Lutaml::Model::Register.resolve_for_child(
42
42
  model_class, register || Lutaml::Model::Config.default_register
43
43
  )
44
+ # Per-parse intern table: element_order entries (and the
45
+ # namespace snapshot below) mint one FFI string per child
46
+ # per node; a 2M-node document then retains millions of
47
+ # duplicate name/URI strings inside the summaries. One
48
+ # shared table per parse collapses them to distinct strings.
49
+ intern ||= {}
44
50
  buckets = if node && plan[:needs_nodes]
45
51
  element_buckets(node, plan[:contested_names])
46
52
  end
47
53
  attr_kwargs = attributes_kwargs(plan, value, node)
48
54
  child_kwargs, children, delegates, run_children =
49
- children_kwargs(model_class, plan, value, buckets, register)
55
+ children_kwargs(model_class, plan, value, buckets, register,
56
+ intern)
50
57
  plan[:collection_defaults].each do |name|
51
58
  next if attr_kwargs.key?(name) || child_kwargs.key?(name)
52
59
 
@@ -71,7 +78,7 @@ module Lutaml
71
78
  # read-only consumers skip them via the config.
72
79
  if node && plan[:ordered] &&
73
80
  Lutaml::Model::Config.instance.element_order_tracking
74
- instance.element_order = PlanOrder.build(node)
81
+ instance.element_order = PlanOrder.build(node, intern)
75
82
  end
76
83
  # Root declaration-plan parity (the interpretive
77
84
  # transform's eager mode): consumers and the interpretive
@@ -284,7 +291,7 @@ module Lutaml
284
291
  # parent/root links once the parent exists; delegate values
285
292
  # wait for the instance (their target object must exist).
286
293
  def children_kwargs(_model_class, plan, value, buckets = nil,
287
- register = nil)
294
+ register = nil, intern = {})
288
295
  register ||= Lutaml::Model::Config.default_register
289
296
  grouped, tagged, run_children = group_children(value,
290
297
  plan[:row_tags])
@@ -362,7 +369,7 @@ module Lutaml
362
369
  items = (selected || []).map do |n|
363
370
  call(child_type, child_plan,
364
371
  child_plan[:descriptor].walk(n), node: n,
365
- register: child_register)
372
+ register: child_register, intern: intern)
366
373
  end
367
374
  unless items.empty?
368
375
  children.concat(items)
@@ -424,7 +431,7 @@ module Lutaml
424
431
  items = selected.map do |n|
425
432
  call(child_type, child_plan,
426
433
  child_plan[:descriptor].walk(n), node: n,
427
- register: child_register)
434
+ register: child_register, intern: intern)
428
435
  end
429
436
  else
430
437
  values = grouped.fetch(name, [])
@@ -439,7 +446,7 @@ module Lutaml
439
446
  while pos < count
440
447
  items[pos] = call(child_type, child_plan, values[pos],
441
448
  node: cursor && cursor[pos],
442
- register: child_register)
449
+ register: child_register, intern: intern)
443
450
  pos += 1
444
451
  end
445
452
  end
@@ -14,22 +14,39 @@ module Lutaml
14
14
  CDATA_MARKER = "#cdata-section"
15
15
  COMMENT_MARKER = "comment"
16
16
 
17
+ # The per-parse intern table's entry ceiling: names, namespace
18
+ # URIs, prefixes and short text runs repeat across millions of
19
+ # entries on large documents; the table collapses them to
20
+ # distinct strings so the summaries retain one copy instead of
21
+ # one FFI mint per child. Only strings up to INTERN_LIMIT are
22
+ # interned — longer text is document data and stays un-interned
23
+ # to keep the table bounded.
24
+ INTERN_LIMIT = 32
25
+
17
26
  class << self
18
- def build(node)
19
- node.children.filter_map { |child| entry_for(child) }
27
+ def build(node, intern = {})
28
+ node.children.filter_map { |child| entry_for(child, intern) }
20
29
  .each(&:freeze).freeze
21
30
  end
22
31
 
23
32
  private
24
33
 
25
- def entry_for(child)
34
+ def intern_string(intern, value)
35
+ return value if value.nil?
36
+
37
+ intern[value] ||= value
38
+ end
39
+
40
+ def entry_for(child, intern)
26
41
  case child
27
42
  when ::Leptris::XML::CDATA
28
43
  Element.new("Text", CDATA_MARKER,
29
44
  text_content: child.content, node_type: :cdata)
30
45
  when ::Leptris::XML::Text
46
+ text = child.text
47
+ text = intern_string(intern, text) if text.length <= INTERN_LIMIT
31
48
  Element.new("Text", TEXT_MARKER,
32
- text_content: child.text, node_type: :text)
49
+ text_content: text, node_type: :text)
33
50
  when ::Leptris::XML::Comment
34
51
  Element.new("Comment", COMMENT_MARKER,
35
52
  text_content: child.content, node_type: :comment)
@@ -42,10 +59,10 @@ module Lutaml
42
59
  # Leptris::XML::Namespace is a single object (href/prefix),
43
60
  # not the Nokogiri-style prefix hash — hash accessors blow
44
61
  # up ordered+namespaced plan parses.
45
- Element.new("Element", child.name,
62
+ Element.new("Element", intern_string(intern, child.name),
46
63
  node_type: :element,
47
- namespace_uri: ns&.href,
48
- namespace_prefix: ns&.prefix)
64
+ namespace_uri: intern_string(intern, ns&.href),
65
+ namespace_prefix: intern_string(intern, ns&.prefix))
49
66
  end
50
67
  end
51
68
  end
@@ -35,6 +35,18 @@ module Lutaml
35
35
  Lutaml::Model::RuntimeCompatibility.require_native("concurrent")
36
36
  Concurrent::Map.new
37
37
  end
38
+
39
+ # Rule name -> its local suffix after the last colon. The
40
+ # cross-namespace fallback probes this suffix per rule name per
41
+ # child per element (11M substring mints on a 2M-node parse);
42
+ # the suffix is static per spelling, so it is shared here with
43
+ # the same backing as NAMESPACED_NAME_PARTS.
44
+ RULE_LOCAL_NAMES = if Lutaml::Model.opal?
45
+ {}
46
+ else
47
+ Lutaml::Model::RuntimeCompatibility.require_native("concurrent")
48
+ Concurrent::Map.new
49
+ end
38
50
  include NestedCollectionAttribute
39
51
 
40
52
  # Performance: Frozen empty hash to reduce allocations
@@ -278,8 +290,11 @@ module Lutaml
278
290
  xml_mapping = options[:mappings] || mappings_for(:xml, effective_register)
279
291
  mappings = xml_mapping.mappings(effective_register)
280
292
  # Catch-all rule (map_any_element): iterated like any other,
281
- # claiming every child no explicit rule matches.
282
- mappings += [xml_mapping.any_element_rule].compact
293
+ # claiming every child no explicit rule matches. Skipped when
294
+ # absent — the unconditional array concat minted one fresh
295
+ # Array per hydrated instance against the frozen cached list.
296
+ catch_all = xml_mapping.any_element_rule
297
+ mappings = mappings + [catch_all] if catch_all
283
298
  default_namespace = options[:default_namespace]
284
299
  ordered_option = options[:ordered]
285
300
  mixed_content_option = options[:mixed_content]
@@ -1264,7 +1279,10 @@ _effective_register)
1264
1279
  # Only match by unprefixed name if child doesn't have an explicit namespace prefix
1265
1280
  # This prevents cross-namespace matching when elements have the same local name
1266
1281
  !child_ns_prefix && rule_names.any? do |rn|
1267
- ((colon = rn.rindex(":")) ? rn[(colon + 1)..] : rn) == child.unprefixed_name
1282
+ (RULE_LOCAL_NAMES[rn] ||= begin
1283
+ colon = rn.rindex(":")
1284
+ colon ? rn[(colon + 1)..] : rn
1285
+ end) == child.unprefixed_name
1268
1286
  end
1269
1287
  elsif !rule_namespace_set && (!child_ns_prefix || rule.raw == :element)
1270
1288
  # For simple types (String, etc.) with no namespace constraint,
@@ -132,7 +132,8 @@ module Lutaml
132
132
  end
133
133
 
134
134
  def local_names(attr)
135
- colon = attr.name.rindex(":")
136
- split_local = colon ? attr.name[(colon + 1)..] : attr.name
137
- [attr.unprefixed_name, split_local].compact.uniq
135
+ # Memoized on the attribute instance (name is fixed); this global
136
+ # kept for its callers — the per-call Array/substring churn lived
137
+ # here before the memo moved into XmlAttribute#local_names.
138
+ attr.local_names
138
139
  end
@@ -13,24 +13,44 @@ module Lutaml
13
13
  @namespace_prefix = namespace_prefix
14
14
  end
15
15
 
16
+ # Memoized: name/namespace/prefix are fixed at construction, and
17
+ # these spellings are probed per element per rule during a parse
18
+ # (5M+ substring mints on a 2M-node document before the memo).
16
19
  def unprefixed_name
17
- if namespace_prefix
18
- name[((name.rindex(":") || -1) + 1)..]
19
- else
20
- name
21
- end
20
+ @unprefixed_name ||= if namespace_prefix
21
+ name[((name.rindex(":") || -1) + 1)..]
22
+ else
23
+ name
24
+ end
22
25
  end
23
26
 
24
27
  def namespaced_name
25
28
  # For type-namespaced attributes (those with xml_namespace declaration),
26
29
  # return the prefixed name (e.g., "xml:lang", "ex:attr")
27
30
  # rather than URI:name format (e.g., "http://www.w3.org/XML/1998/namespace:lang")
28
- if namespace_prefix
29
- name
30
- elsif namespace
31
- "#{namespace}:#{unprefixed_name}"
32
- else
33
- unprefixed_name
31
+ @namespaced_name ||= if namespace_prefix
32
+ name
33
+ elsif namespace
34
+ "#{namespace}:#{unprefixed_name}"
35
+ else
36
+ unprefixed_name
37
+ end
38
+ end
39
+
40
+ # The local-name spellings this attribute can match under (its
41
+ # unprefixed form and its split-local form when prefixed). Probed
42
+ # per candidate per rule in the parse session; static per
43
+ # attribute instance, so it is computed once here. Unprefixed
44
+ # attributes (the common case) take the single-spelling fast
45
+ # path: one Array, no substrings.
46
+ def local_names
47
+ @local_names ||= begin
48
+ colon = name.rindex(":")
49
+ if colon
50
+ [unprefixed_name, name[(colon + 1)..]].uniq
51
+ else
52
+ [name]
53
+ end
34
54
  end
35
55
  end
36
56
  end
data/lutaml-model.gemspec CHANGED
@@ -45,10 +45,12 @@ Gem::Specification.new do |spec|
45
45
  # cross-language program's phase 4); the CRuby facade faces this
46
46
  # gem uses are unchanged, and the full suite is green on the 0.6
47
47
  # line (6386/0 with 0.6.0 resolved). Owner call (2026-10-31):
48
- # forward-pin the 0.6 series, matching canon (lutaml/canon#298).
49
- # The Opal boot lane (#914) keeps its own moxml ~> 0.5.x pin until
50
- # it migrates to the moxml-ts wasm binding.
51
- spec.add_dependency "moxml", "~> 0.6.0"
48
+ # forward-pin the 0.6 series, matching canon (lutaml/canon#298);
49
+ # floored at 0.6.1 per the maintainer's directive. The Opal lane's
50
+ # workspace-local 0.5.x pin is retired with this floor — it cannot
51
+ # resolve against it — and the boot's lib/compat/opal append is
52
+ # directory-guarded in the Rakefile for both lines.
53
+ spec.add_dependency "moxml", "~> 0.6.1"
52
54
  spec.add_dependency "ostruct"
53
55
  spec.add_dependency "rubyzip", "~> 3.7"
54
56
  spec.add_dependency "thor", "~> 1.4"
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1086: on a Collection subclass, scalar `map` declarations
6
+ # in key_value were silently ignored alongside `instances` — the scalar
7
+ # attributes neither serialized nor deserialized (data loss, no error).
8
+ # The document is hash-shaped (instance key plus scalar keys) when
9
+ # scalar mappings are declared; collections without scalar mappings keep
10
+ # the legacy bare items array.
11
+ module CollectionScalarMapsSpec
12
+ class Entry < Lutaml::Model::Serializable
13
+ attribute :name, :string
14
+ key_value { map "name", to: :name }
15
+ end
16
+
17
+ class StampToy < Lutaml::Model::Collection
18
+ instances :entries, Entry
19
+ attribute :stamp, :integer, default: -> { 0 }
20
+ key_value do
21
+ map "entries", to: :entries
22
+ map "stamp", to: :stamp
23
+ end
24
+ end
25
+ end
26
+
27
+ RSpec.describe "Collection with scalar key_value mappings (#1086)" do
28
+ let(:toy) do
29
+ collection = CollectionScalarMapsSpec::StampToy.new
30
+ collection.entries = [CollectionScalarMapsSpec::Entry.new(name: "x")]
31
+ collection.stamp = 42
32
+ collection
33
+ end
34
+
35
+ it "serializes the scalars alongside the instances" do
36
+ expect(toy.to_yaml).to include("stamp: 42")
37
+ expect(toy.to_yaml).to include("entries:")
38
+ end
39
+
40
+ it "deserializes the scalars from the hash-shaped document" do
41
+ parsed = CollectionScalarMapsSpec::StampToy.from_yaml(toy.to_yaml)
42
+ expect(parsed.stamp).to eq(42)
43
+ expect(parsed.entries.size).to eq(1)
44
+ expect(parsed.entries.first.name).to eq("x")
45
+ end
46
+
47
+ it "keeps the legacy bare-array document parseable" do
48
+ parsed = CollectionScalarMapsSpec::StampToy.from_yaml("---\n- name: y\n")
49
+ expect(parsed).to be_a(CollectionScalarMapsSpec::StampToy)
50
+ expect(parsed.entries.size).to eq(1)
51
+ expect(parsed.entries.first.name).to eq("y")
52
+ end
53
+
54
+ it "round-trips through the hash shape" do
55
+ parsed = CollectionScalarMapsSpec::StampToy.from_yaml(toy.to_yaml)
56
+ expect(parsed.to_yaml).to eq(toy.to_yaml)
57
+ end
58
+ end
@@ -6,12 +6,21 @@ require "spec_helper"
6
6
  # REXML smoke runs. Both adapters are pure Ruby and work under Opal;
7
7
  # this shared example is the contract that any future Opal-capable
8
8
  # adapter would also need to satisfy.
9
+ # moxml 0.6's packaged REXML adapter references CustomizedRexml
10
+ # (Formatter/EntityReference) but no longer ships the module, and the
11
+ # vendored 0.5 formatter writes empty output against it under Opal
12
+ # (tracked in lutaml/moxml). REXML stays parse-only under Opal until
13
+ # that ships; Oga carries the serialize contract.
14
+ REXML_SERIALIZE_SKIP = "moxml 0.6 REXML adapter lacks CustomizedRexml " \
15
+ "under Opal (lutaml/moxml tracking)"
16
+
9
17
  RSpec.shared_examples "opal XML adapter round-trip" do |adapter_name|
10
18
  before do
11
19
  Lutaml::Model::Config.xml_adapter_type = adapter_name
12
20
  end
13
21
 
14
22
  it "round-trips XML through parse and serialize" do
23
+ skip(REXML_SERIALIZE_SKIP) if adapter_name == :rexml
15
24
  klass = Class.new do
16
25
  include Lutaml::Model::Serialize
17
26
 
@@ -105,6 +114,7 @@ RSpec.shared_examples "opal XML adapter round-trip" do |adapter_name|
105
114
  end
106
115
 
107
116
  it "serializes to XML with correct structure" do
117
+ skip(REXML_SERIALIZE_SKIP) if adapter_name == :rexml
108
118
  item = Class.new do
109
119
  include Lutaml::Model::Serialize
110
120
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.156
4
+ version: 0.8.158
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-01 00:00:00.000000000 Z
11
+ date: 2026-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: 0.6.0
95
+ version: 0.6.1
96
96
  type: :runtime
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: 0.6.0
102
+ version: 0.6.1
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: ostruct
105
105
  requirement: !ruby/object:Gem::Requirement
@@ -310,7 +310,18 @@ files:
310
310
  - lib/compat/opal/generate_boot.rb
311
311
  - lib/compat/opal/js_bundle_entry.rb
312
312
  - lib/compat/opal/lutaml_model_boot.rb
313
+ - lib/compat/opal/monitor.rb
314
+ - lib/compat/opal/moxml/adapter/customized_rexml.rb
315
+ - lib/compat/opal/moxml/adapter/customized_rexml/entity_reference.rb
316
+ - lib/compat/opal/moxml/adapter/customized_rexml/formatter.rb
313
317
  - lib/compat/opal/moxml_boot.rb
318
+ - lib/compat/opal/objectspace_shim.rb
319
+ - lib/compat/opal/rexml/namespace.rb
320
+ - lib/compat/opal/rexml/parsers/baseparser.rb
321
+ - lib/compat/opal/rexml/source.rb
322
+ - lib/compat/opal/rexml/text.rb
323
+ - lib/compat/opal/rexml/xmltokens.rb
324
+ - lib/compat/opal/rexml_compat.rb
314
325
  - lib/compat/opal/yaml_compat.rb
315
326
  - lib/lutaml/hash_format.rb
316
327
  - lib/lutaml/hash_format/adapter.rb
@@ -1742,6 +1753,7 @@ files:
1742
1753
  - spec/lutaml/model/collection_mutation_spec.rb
1743
1754
  - spec/lutaml/model/collection_reader_liveness_spec.rb
1744
1755
  - spec/lutaml/model/collection_register_spec.rb
1756
+ - spec/lutaml/model/collection_scalar_maps_spec.rb
1745
1757
  - spec/lutaml/model/collection_single_value_coercion_spec.rb
1746
1758
  - spec/lutaml/model/collection_spec.rb
1747
1759
  - spec/lutaml/model/collection_state_nesting_spec.rb