lutaml-model 0.8.198 → 0.8.200
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/docs/_references/plan-schema-spec.adoc +20 -0
- data/lib/compat/opal/lutaml_model_boot.rb +0 -1
- data/lib/lutaml/key_value/transform.rb +236 -107
- data/lib/lutaml/key_value/transformation.rb +82 -7
- data/lib/lutaml/lml.rb +0 -1
- data/lib/lutaml/model/attribute.rb +81 -4
- data/lib/lutaml/model/schema/xml_compiler/spec_builder.rb +7 -8
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/schema/xsd/override.rb +63 -22
- data/lib/lutaml/xml/schema/xsd/schema.rb +22 -19
- data/spec/lml/lml_spec_setup.rb +9 -6
- data/spec/lutaml/key_value/group_instantiate_spec.rb +11 -6
- data/spec/lutaml/key_value/recursive_plan_spec.rb +163 -0
- data/spec/lutaml/key_value/transformation/value_serializer_spec.rb +10 -7
- data/spec/lutaml/model/polymorphic_scalar_member_spec.rb +113 -0
- data/spec/lutaml/xml/schema/xsd_override_assembly_spec.rb +116 -39
- data/spec/lutaml/xml/schema/xsd_override_cycle_spec.rb +150 -0
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9a7958849b1b037485644838e6cd7b3264546a8f25fdfd8d29d694595fb2c4be
|
|
4
|
+
data.tar.gz: 16e38f14e5d3c06b55073d626dcf79e8fb93f33c1775bf2ef2288d62d614c617
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 290f49e6e4d3801291845ffd8ef58822363f70fdc8a8ee17013757a5a5afc3b10ff73187bf75a8e11a20271750534f3e6c0b888569dc1f81f71db12e36d906b9
|
|
7
|
+
data.tar.gz: 2df50e5220736a2d7468cd5ae6713e819cce37bfcfaacf7c7248901db6f31023d01a92015f7dd1bbb02db1876f66b9def21ea2309de15414f777d714227aeb15
|
|
@@ -150,6 +150,16 @@ The in-memory precedent for this tuple is the shipped row shape
|
|
|
150
150
|
replaces the Ruby object references (rule, attr, type, class) with
|
|
151
151
|
portable identifiers.
|
|
152
152
|
|
|
153
|
+
The plan graph is a graph, not a tree. A recursive model graph — a tree
|
|
154
|
+
whose nodes reference their own type, directly or through other model
|
|
155
|
+
types — compiles as a self-referential plan: the `model` row's nested
|
|
156
|
+
plan reference may be the enclosing plan. The wire form therefore
|
|
157
|
+
identifies each plan and references plans by identifier; it never
|
|
158
|
+
expands a nested plan inline, which would be unbounded for a tree shape
|
|
159
|
+
(#1297). A plan compile session commits its plans only when the
|
|
160
|
+
outermost build succeeds, so a plan that references rows an incomplete
|
|
161
|
+
outer build never finished is never published.
|
|
162
|
+
|
|
153
163
|
=== Row kinds (XML lane; extracted from the shipped kinds)
|
|
154
164
|
|
|
155
165
|
The XML lane's compiled kinds are already enumerated in code
|
|
@@ -210,6 +220,16 @@ unmapped merges exactly as `kv_group_build` does today. The
|
|
|
210
220
|
Ruby-visible object graph, validation surface, and error classes are
|
|
211
221
|
byte-for-byte the fallback's.
|
|
212
222
|
|
|
223
|
+
Executor contract (the reference the crate's executor mirrors): the
|
|
224
|
+
plan runs on an explicit frame stack, one frame per model under
|
|
225
|
+
construction, so hydration depth is bound by the heap and never by the
|
|
226
|
+
VM stack (#1297). Children complete before the parent instance is
|
|
227
|
+
constructed; siblings hydrate in row order and collection items in
|
|
228
|
+
document order; each child is linked to its parent, and to its root
|
|
229
|
+
when it has none, once the parent instance exists. A child shape the
|
|
230
|
+
plan does not cover unwinds the whole build to the fallback, which owns
|
|
231
|
+
that shape's semantics and errors.
|
|
232
|
+
|
|
213
233
|
Parse errors: the engine raises the strict-parse contract (the
|
|
214
234
|
`strict:` kwarg lineage — malformed input is a format error, never a
|
|
215
235
|
best-effort tree), mapped to `Lutaml::Model::InvalidFormatError` at
|
|
@@ -66,7 +66,6 @@ require "lutaml/lml/symbol_index"
|
|
|
66
66
|
require "lutaml/lml/transform"
|
|
67
67
|
require "lutaml/lml/types"
|
|
68
68
|
require "lutaml/lml/validator"
|
|
69
|
-
require "lutaml/lml/version"
|
|
70
69
|
require "lutaml/lml/view_resolution"
|
|
71
70
|
require "lutaml/lml/view_resolver"
|
|
72
71
|
require "lutaml/lml/yaml_parser"
|
|
@@ -152,10 +152,10 @@ module Lutaml
|
|
|
152
152
|
# ---- TODO.max-perf/37: group-then-instantiate fast path ----
|
|
153
153
|
|
|
154
154
|
# [model class, format, register] -> rows or false (ineligible).
|
|
155
|
-
# Rows: [[rule, attr, kind, child_rows]] with kind
|
|
156
|
-
# :model; child_rows is the child
|
|
157
|
-
#
|
|
158
|
-
#
|
|
155
|
+
# Rows: [[rule, attr, kind, child_rows, child_register]] with kind
|
|
156
|
+
# :scalar, :model, :child_map or :unmapped; child_rows is the child
|
|
157
|
+
# model's own row set. A recursive model graph compiles as a
|
|
158
|
+
# self-referential plan: child_rows may be the plan itself.
|
|
159
159
|
# Concurrent::Map under threaded MRI, plain Hash under Opal
|
|
160
160
|
# (Concurrent is unavailable there) — the RULE_RECORDS pattern.
|
|
161
161
|
# Writes are idempotent (the same deterministic plan is computed),
|
|
@@ -193,24 +193,43 @@ module Lutaml
|
|
|
193
193
|
plan = KV_GROUP_PLANS[key]
|
|
194
194
|
return plan unless plan.nil?
|
|
195
195
|
|
|
196
|
-
#
|
|
197
|
-
#
|
|
198
|
-
#
|
|
199
|
-
#
|
|
200
|
-
#
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
196
|
+
# A recursive model graph (a tree: the model references itself,
|
|
197
|
+
# directly or through other models) compiles as a
|
|
198
|
+
# self-referential plan: the build in progress publishes its row
|
|
199
|
+
# list to the builds it spawns, so the cycle point references
|
|
200
|
+
# the same rows instead of refusing the model (#1297). Both the
|
|
201
|
+
# publication and the session's results are thread-local: a
|
|
202
|
+
# concurrent build never sees them, and a session commits only
|
|
203
|
+
# when its outermost build succeeds — an inner plan referencing
|
|
204
|
+
# rows a failed outer build never finished must not survive.
|
|
205
|
+
building = (Thread.current[:kv_group_plan_building] ||= {})
|
|
206
|
+
return building[key] if building.key?(key)
|
|
207
|
+
|
|
208
|
+
rows = []
|
|
209
|
+
building[key] = rows
|
|
205
210
|
begin
|
|
206
|
-
|
|
207
|
-
|
|
211
|
+
built = build_kv_group_plan(model_class, format, register, rows)
|
|
212
|
+
(Thread.current[:kv_group_plan_session] ||= []) << [key, built]
|
|
208
213
|
ensure
|
|
209
|
-
|
|
214
|
+
building.delete(key)
|
|
215
|
+
if building.empty?
|
|
216
|
+
session = Thread.current[:kv_group_plan_session]
|
|
217
|
+
Thread.current[:kv_group_plan_session] = nil
|
|
218
|
+
# Only the outermost verdict decides: an inner plan that
|
|
219
|
+
# completed references the outer rows, so a failed outer
|
|
220
|
+
# build (false, an exception) commits nothing at all. A
|
|
221
|
+
# concurrent build that committed first keeps its plan.
|
|
222
|
+
if built
|
|
223
|
+
session&.each do |built_key, result|
|
|
224
|
+
KV_GROUP_PLANS[built_key] = result unless KV_GROUP_PLANS.key?(built_key)
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
210
228
|
end
|
|
229
|
+
built
|
|
211
230
|
end
|
|
212
231
|
|
|
213
|
-
def self.build_kv_group_plan(model_class, format, register)
|
|
232
|
+
def self.build_kv_group_plan(model_class, format, register, rows = [])
|
|
214
233
|
return false unless model_class.is_a?(Class) &&
|
|
215
234
|
model_class.include?(Lutaml::Model::Serialize)
|
|
216
235
|
|
|
@@ -229,8 +248,6 @@ module Lutaml
|
|
|
229
248
|
"map_unmapped target :#{unmapped_target} is not a " \
|
|
230
249
|
"declared attribute of #{model_class}"
|
|
231
250
|
end
|
|
232
|
-
rows = nil
|
|
233
|
-
claimed = nil
|
|
234
251
|
mapping.mappings(register).each do |rule|
|
|
235
252
|
eligible = !rule.name.nil? && !rule.multiple_mappings? &&
|
|
236
253
|
rule.delegate.nil? &&
|
|
@@ -274,7 +291,6 @@ module Lutaml
|
|
|
274
291
|
rule.hash_mappings)
|
|
275
292
|
return false unless child_plan
|
|
276
293
|
|
|
277
|
-
rows ||= []
|
|
278
294
|
rows << [rule, attr, :child_map, child_plan, nil, nil]
|
|
279
295
|
next
|
|
280
296
|
end
|
|
@@ -285,7 +301,6 @@ module Lutaml
|
|
|
285
301
|
child_rows = kv_group_plan(type, format, child_register)
|
|
286
302
|
return false unless child_rows
|
|
287
303
|
|
|
288
|
-
rows ||= []
|
|
289
304
|
# child_register rides the row: the plan was BUILT under it,
|
|
290
305
|
# and rebuilding the resolution per model row per document
|
|
291
306
|
# cost a Concurrent::Map hop each time (TODO.perf 14).
|
|
@@ -293,7 +308,6 @@ module Lutaml
|
|
|
293
308
|
elsif type.is_a?(Class) && type < Lutaml::Model::Type::Value &&
|
|
294
309
|
!attr.value_policy.whole_value?(type) &&
|
|
295
310
|
!Lutaml::Model::Attribute.custom_from_probe?(type)
|
|
296
|
-
rows ||= []
|
|
297
311
|
rows << [rule, attr, :scalar, nil, nil, nil]
|
|
298
312
|
else
|
|
299
313
|
return false
|
|
@@ -305,11 +319,10 @@ module Lutaml
|
|
|
305
319
|
Array(rule.name).each { |name| claimed[name.to_s] = true } if rule.name
|
|
306
320
|
end
|
|
307
321
|
claimed.freeze
|
|
308
|
-
|
|
309
|
-
nil]
|
|
322
|
+
rows << [nil, nil, :unmapped, unmapped_target, claimed, nil]
|
|
310
323
|
end
|
|
311
324
|
# No eligible rows at all (empty mapping) — nothing to gain.
|
|
312
|
-
rows
|
|
325
|
+
rows.empty? ? nil : rows
|
|
313
326
|
end
|
|
314
327
|
|
|
315
328
|
# Class-level twin of the walk's child-map compile: the registry
|
|
@@ -338,30 +351,150 @@ child_mappings)
|
|
|
338
351
|
nil
|
|
339
352
|
end
|
|
340
353
|
|
|
354
|
+
# One model under construction by the explicit-stack group build:
|
|
355
|
+
# its row scan, the positional values collected so far and the
|
|
356
|
+
# child frame it needs next. One frame per nesting level, reused
|
|
357
|
+
# across siblings, so hydration depth is bound by the heap rather
|
|
358
|
+
# than the VM stack (#1297).
|
|
359
|
+
GroupBuildFrame = ::Struct.new(
|
|
360
|
+
:transform, :rows, :doc, :format, :register, :options,
|
|
361
|
+
:slots, :children, :absent, :unmapped, :index,
|
|
362
|
+
:pending_index, :pending_items, :pending_cursor, :pending_built,
|
|
363
|
+
:request_transform, :request_rows, :request_register, :request_doc,
|
|
364
|
+
:instance
|
|
365
|
+
) do
|
|
366
|
+
def start(transform, rows, doc, format, register, options)
|
|
367
|
+
self.transform = transform
|
|
368
|
+
self.rows = rows
|
|
369
|
+
self.doc = doc
|
|
370
|
+
self.format = format
|
|
371
|
+
self.register = register
|
|
372
|
+
self.options = options
|
|
373
|
+
# Positional values, not per-row [name, value] pairs: rows are
|
|
374
|
+
# a fixed per-class list, so the writer to apply lives at the
|
|
375
|
+
# row's index and the pair arrays were the lane's biggest
|
|
376
|
+
# allocation (~6 arrays per node on hydrate-heavy corpora).
|
|
377
|
+
self.slots = Array.new(rows.size, MISSING)
|
|
378
|
+
# Lazy: the scalar hot path never pushes a child; the shared
|
|
379
|
+
# wiring pass tolerates nil.
|
|
380
|
+
self.children = nil
|
|
381
|
+
# Lazy: the present-key hot path never pushes, and this runs
|
|
382
|
+
# once per hydrated model instance.
|
|
383
|
+
self.absent = nil
|
|
384
|
+
self.unmapped = nil
|
|
385
|
+
self.index = 0
|
|
386
|
+
clear_pending
|
|
387
|
+
self.instance = nil
|
|
388
|
+
self
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
# A :model row hydrates through child frames, one per document:
|
|
392
|
+
# singular rows have a single pending item.
|
|
393
|
+
def begin_pending(row_index, items, transform, rows, register)
|
|
394
|
+
self.pending_index = row_index
|
|
395
|
+
self.pending_items = items
|
|
396
|
+
self.pending_cursor = 0
|
|
397
|
+
self.pending_built = []
|
|
398
|
+
self.request_transform = transform
|
|
399
|
+
self.request_rows = rows
|
|
400
|
+
self.request_register = register
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
def pending?
|
|
404
|
+
!pending_items.nil?
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
def pending_remaining?
|
|
408
|
+
pending_cursor < pending_items.size
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
# The next child document, or nil for a shape the plan does not
|
|
412
|
+
# cover (a non-Hash item where a model was expected).
|
|
413
|
+
def next_child_document
|
|
414
|
+
item = pending_items[pending_cursor]
|
|
415
|
+
item.is_a?(::Hash) ? item : nil
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
# A finished child joins the row being hydrated; the row's value
|
|
419
|
+
# slot is written once its last child is in.
|
|
420
|
+
def deliver(child)
|
|
421
|
+
pending_built << child
|
|
422
|
+
(self.children ||= []) << child
|
|
423
|
+
self.pending_cursor += 1
|
|
424
|
+
complete_pending! unless pending_remaining?
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
def complete_pending!
|
|
428
|
+
collection = rows[pending_index][1].collection?
|
|
429
|
+
slots[pending_index] = collection ? pending_built : pending_built.first
|
|
430
|
+
clear_pending
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
def clear_pending
|
|
434
|
+
self.pending_index = nil
|
|
435
|
+
self.pending_items = nil
|
|
436
|
+
self.pending_cursor = 0
|
|
437
|
+
self.pending_built = nil
|
|
438
|
+
end
|
|
439
|
+
end
|
|
440
|
+
|
|
341
441
|
# Build values bottom-up, then ONE instance per model: present
|
|
342
442
|
# keys through the casting setters, ABSENT rules through the real
|
|
343
443
|
# per-rule walk (their extractor/defaults/sentinel semantics are
|
|
344
444
|
# the walk's own — reproducing them here would fork them). The
|
|
345
445
|
# per-rule walk is eliminated exactly for the present-key hot
|
|
346
|
-
# path.
|
|
347
|
-
#
|
|
446
|
+
# path. Children hydrate through frames on an explicit stack, not
|
|
447
|
+
# recursion: depth is not bound by the VM stack (#1297). Returns
|
|
448
|
+
# nil (caller falls back) on any shape the plan does not cover,
|
|
449
|
+
# e.g. a non-Hash item where a model was expected.
|
|
348
450
|
def kv_group_build(rows, doc, format, register, options)
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
451
|
+
frames = []
|
|
452
|
+
frame = GroupBuildFrame.new.start(self, rows, doc, format, register,
|
|
453
|
+
options)
|
|
454
|
+
frames << frame
|
|
455
|
+
loop do
|
|
456
|
+
# Each frame is stepped by its own transform, so the walk's
|
|
457
|
+
# private per-rule machinery stays private.
|
|
458
|
+
case frame.transform.kv_frame_step(frame)
|
|
459
|
+
when :child
|
|
460
|
+
# The request rides the frame being stepped; read it before
|
|
461
|
+
# the new frame takes the loop variable.
|
|
462
|
+
child = frames[frames.size] ||= GroupBuildFrame.new
|
|
463
|
+
frame = child.start(frame.request_transform, frame.request_rows,
|
|
464
|
+
frame.request_doc, format,
|
|
465
|
+
frame.request_register, options)
|
|
466
|
+
when :fallback
|
|
467
|
+
return nil
|
|
468
|
+
else
|
|
469
|
+
instance = frame.instance
|
|
470
|
+
frames.pop
|
|
471
|
+
return instance if frames.empty?
|
|
472
|
+
|
|
473
|
+
frame = frames.last
|
|
474
|
+
frame.deliver(instance)
|
|
475
|
+
end
|
|
476
|
+
end
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
# Advances a frame one step: the next child frame it needs
|
|
480
|
+
# (:child), the finished instance (:done), or a shape the plan does
|
|
481
|
+
# not cover (:fallback).
|
|
482
|
+
def kv_frame_step(frame)
|
|
483
|
+
if frame.pending?
|
|
484
|
+
if frame.pending_remaining?
|
|
485
|
+
return request_child(frame)
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
frame.complete_pending!
|
|
489
|
+
end
|
|
490
|
+
rows = frame.rows
|
|
491
|
+
i = frame.index
|
|
492
|
+
while i < rows.size
|
|
493
|
+
row = rows[i]
|
|
494
|
+
rule = row[0]
|
|
495
|
+
kind = row[2]
|
|
363
496
|
if kind == :unmapped
|
|
364
|
-
unmapped = [
|
|
497
|
+
frame.unmapped = [row[3], row[4]]
|
|
365
498
|
i += 1
|
|
366
499
|
next
|
|
367
500
|
end
|
|
@@ -369,9 +502,9 @@ child_mappings)
|
|
|
369
502
|
# One hash pass per row: fetch with a sentinel distinguishes
|
|
370
503
|
# absent keys from present-nil values (both read as nil), so
|
|
371
504
|
# the separate string_or_symbol_key? probe goes away.
|
|
372
|
-
v = Lutaml::Model::Utils.fetch_str_or_sym(doc, rule.name, MISSING)
|
|
505
|
+
v = Lutaml::Model::Utils.fetch_str_or_sym(frame.doc, rule.name, MISSING)
|
|
373
506
|
if v.equal?(MISSING)
|
|
374
|
-
(absent ||= []) << rule
|
|
507
|
+
(frame.absent ||= []) << rule
|
|
375
508
|
i += 1
|
|
376
509
|
next
|
|
377
510
|
end
|
|
@@ -383,89 +516,95 @@ child_mappings)
|
|
|
383
516
|
# parent/root wiring rides the shared children pass. A
|
|
384
517
|
# non-Hash value falls back to the walk, which owns that
|
|
385
518
|
# error.
|
|
386
|
-
return
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
v.each do |child_key, child_value|
|
|
391
|
-
child_hash = {}
|
|
392
|
-
type.child_mappings.each_key do |attr_name|
|
|
393
|
-
child_hash[type.wire_names[attr_name]] =
|
|
394
|
-
extract_compiled_child_value(type.paths[attr_name],
|
|
395
|
-
child_key, child_value)
|
|
396
|
-
end
|
|
397
|
-
child_hash.update(child_value) if keys_only
|
|
398
|
-
built << self.class.data_to_model(type.attr_type,
|
|
399
|
-
child_hash, format, { mappings: type.child_rules })
|
|
400
|
-
end
|
|
401
|
-
values[i] = built
|
|
402
|
-
(children ||= []).concat(built)
|
|
519
|
+
return :fallback unless v.is_a?(::Hash)
|
|
520
|
+
|
|
521
|
+
frame.slots[i] = kv_build_child_map(row[3], v, frame.format)
|
|
522
|
+
(frame.children ||= []).concat(frame.slots[i])
|
|
403
523
|
i += 1
|
|
404
524
|
next
|
|
405
525
|
end
|
|
406
526
|
|
|
407
527
|
if kind == :scalar
|
|
408
|
-
|
|
528
|
+
frame.slots[i] = v
|
|
409
529
|
i += 1
|
|
410
530
|
next
|
|
411
531
|
end
|
|
412
532
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
533
|
+
# A present-but-nil collection reaches the per-rule walk,
|
|
534
|
+
# which owns the sentinel interplay for that edge
|
|
535
|
+
# (render_nil :as_empty semantics).
|
|
536
|
+
return :fallback if row[1].collection? && v.nil?
|
|
537
|
+
|
|
538
|
+
items = if row[1].collection?
|
|
539
|
+
v.is_a?(::Array) ? v : [v]
|
|
540
|
+
else
|
|
541
|
+
[v]
|
|
542
|
+
end
|
|
543
|
+
frame.index = i + 1
|
|
544
|
+
frame.begin_pending(i, items,
|
|
545
|
+
self.class.cached_transform(row[3], row[5]),
|
|
546
|
+
row[4], row[5])
|
|
547
|
+
return request_child(frame)
|
|
548
|
+
end
|
|
549
|
+
frame.index = i
|
|
550
|
+
frame.instance = kv_finalize_frame(frame)
|
|
551
|
+
:done
|
|
552
|
+
end
|
|
423
553
|
|
|
424
|
-
|
|
425
|
-
format, child_register,
|
|
426
|
-
options)
|
|
427
|
-
return nil if child.nil?
|
|
554
|
+
private
|
|
428
555
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
return nil unless v.is_a?(::Hash)
|
|
556
|
+
# :child when the frame's pending row has a document the plan
|
|
557
|
+
# covers, :fallback otherwise.
|
|
558
|
+
def request_child(frame)
|
|
559
|
+
document = frame.next_child_document
|
|
560
|
+
return :fallback unless document
|
|
435
561
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
return nil if child.nil?
|
|
562
|
+
frame.request_doc = document
|
|
563
|
+
:child
|
|
564
|
+
end
|
|
440
565
|
|
|
441
|
-
|
|
442
|
-
|
|
566
|
+
def kv_build_child_map(plan, value, format)
|
|
567
|
+
built = []
|
|
568
|
+
keys_only = plan.keys_only && value.values.all?(::Hash)
|
|
569
|
+
value.each do |child_key, child_value|
|
|
570
|
+
child_hash = {}
|
|
571
|
+
plan.child_mappings.each_key do |attr_name|
|
|
572
|
+
child_hash[plan.wire_names[attr_name]] =
|
|
573
|
+
extract_compiled_child_value(plan.paths[attr_name],
|
|
574
|
+
child_key, child_value)
|
|
443
575
|
end
|
|
444
|
-
|
|
576
|
+
child_hash.update(child_value) if keys_only
|
|
577
|
+
built << self.class.data_to_model(plan.attr_type, child_hash,
|
|
578
|
+
format, { mappings: plan.child_rules })
|
|
445
579
|
end
|
|
446
|
-
|
|
580
|
+
built
|
|
581
|
+
end
|
|
582
|
+
|
|
583
|
+
def kv_finalize_frame(frame)
|
|
584
|
+
instance = model_class.allocate_for_walk(frame.register)
|
|
585
|
+
values = frame.slots
|
|
447
586
|
i = 0
|
|
448
587
|
# Index access, not block-parameter splat: `*_rest` re-sliced a
|
|
449
588
|
# fresh Array per row per instance (the walk's largest our-side
|
|
450
589
|
# allocation line on the synthetic KV corpus).
|
|
451
|
-
rows.each do |row|
|
|
590
|
+
frame.rows.each do |row|
|
|
452
591
|
v = values[i]
|
|
453
592
|
i += 1
|
|
454
593
|
next if v.equal?(MISSING) || row[2] == :unmapped
|
|
455
594
|
|
|
456
595
|
instance.public_send(row[0].kv_setter, v)
|
|
457
596
|
end
|
|
458
|
-
absent&.each do |rule|
|
|
459
|
-
process_mapping_rule(doc, instance, format, rule, options, nil)
|
|
597
|
+
frame.absent&.each do |rule|
|
|
598
|
+
process_mapping_rule(frame.doc, instance, frame.format, rule, frame.options, nil)
|
|
460
599
|
end
|
|
461
|
-
children&.each do |child|
|
|
600
|
+
frame.children&.each do |child|
|
|
462
601
|
child.lutaml_parent = instance
|
|
463
602
|
child.lutaml_root ||= instance.lutaml_root || instance
|
|
464
603
|
end
|
|
465
|
-
if unmapped
|
|
466
|
-
target, claimed = unmapped
|
|
604
|
+
if frame.unmapped
|
|
605
|
+
target, claimed = frame.unmapped
|
|
467
606
|
extras = nil
|
|
468
|
-
doc.each do |key, value|
|
|
607
|
+
frame.doc.each do |key, value|
|
|
469
608
|
next if claimed.key?(key.to_s)
|
|
470
609
|
|
|
471
610
|
(extras ||= {})[key] = value
|
|
@@ -479,16 +618,6 @@ child_hash, format, { mappings: type.child_rules })
|
|
|
479
618
|
instance
|
|
480
619
|
end
|
|
481
620
|
|
|
482
|
-
def self.kv_group_instance(model_class, rows, doc, format, register,
|
|
483
|
-
options)
|
|
484
|
-
# cached_transform is per (class, register); format rides the
|
|
485
|
-
# call, not the cache.
|
|
486
|
-
cached_transform(model_class, register)
|
|
487
|
-
.kv_group_build(rows, doc, format, register, options)
|
|
488
|
-
end
|
|
489
|
-
|
|
490
|
-
private
|
|
491
|
-
|
|
492
621
|
def process_rule!(instance, rule, hash, format, _mappings, options)
|
|
493
622
|
return handle_delegate(instance, rule, hash, format) if rule.delegate
|
|
494
623
|
|
|
@@ -1069,12 +1069,59 @@ child_mappings, options)
|
|
|
1069
1069
|
parent[rule.serialized_name] = child_value unless child_value.nil?
|
|
1070
1070
|
end
|
|
1071
1071
|
|
|
1072
|
-
#
|
|
1073
|
-
#
|
|
1074
|
-
#
|
|
1075
|
-
#
|
|
1076
|
-
#
|
|
1077
|
-
|
|
1072
|
+
# Whether the value is a listed scalar member of the attribute's
|
|
1073
|
+
# polymorphic list: a Type::Value member admits non-model scalars
|
|
1074
|
+
# (and, with :array, arrays of them); a list of model classes
|
|
1075
|
+
# only admits model instances, so a scalar under it still raises
|
|
1076
|
+
# in the nested-model validation.
|
|
1077
|
+
def polymorphic_scalar_member?(rule, value)
|
|
1078
|
+
list = polymorphic_list_for(rule)
|
|
1079
|
+
return false unless list.is_a?(::Array)
|
|
1080
|
+
|
|
1081
|
+
scalar_member = list.any? do |member|
|
|
1082
|
+
member.is_a?(Class) && member < Lutaml::Model::Type::Value
|
|
1083
|
+
end
|
|
1084
|
+
return false unless scalar_member
|
|
1085
|
+
|
|
1086
|
+
case value
|
|
1087
|
+
when ::Hash, Lutaml::Model::Serialize
|
|
1088
|
+
false
|
|
1089
|
+
when ::Array
|
|
1090
|
+
value.all? do |item|
|
|
1091
|
+
!item.is_a?(::Hash) && !item.is_a?(Lutaml::Model::Serialize)
|
|
1092
|
+
end
|
|
1093
|
+
else
|
|
1094
|
+
true
|
|
1095
|
+
end
|
|
1096
|
+
end
|
|
1097
|
+
|
|
1098
|
+
# The attribute's polymorphic list, read off the declaring
|
|
1099
|
+
# class (the compiled rule carries the type, not the
|
|
1100
|
+
# attribute).
|
|
1101
|
+
def polymorphic_list_for(rule)
|
|
1102
|
+
polymorphic_attribute_for(rule)&.options&.[](:polymorphic)
|
|
1103
|
+
end
|
|
1104
|
+
|
|
1105
|
+
# Whether the attribute's list admits this Array of members on a
|
|
1106
|
+
# singular attribute: each item then takes its own lane — scalars the
|
|
1107
|
+
# scalar lane, model instances the nested one (#1292).
|
|
1108
|
+
def polymorphic_member_array?(rule, value)
|
|
1109
|
+
!union_rule?(rule) && polymorphic_attribute_for(rule)
|
|
1110
|
+
&.polymorphic_member_array?(value)
|
|
1111
|
+
end
|
|
1112
|
+
|
|
1113
|
+
def polymorphic_attribute_for(rule)
|
|
1114
|
+
klass = model_class
|
|
1115
|
+
return unless klass.is_a?(Class) &&
|
|
1116
|
+
klass.include?(Lutaml::Model::Serialize)
|
|
1117
|
+
|
|
1118
|
+
klass.attributes[rule.attribute_name.to_sym]
|
|
1119
|
+
end
|
|
1120
|
+
|
|
1121
|
+
def union_rule?(rule)
|
|
1122
|
+
Lutaml::Model::Type::Union.rule?(rule)
|
|
1123
|
+
end
|
|
1124
|
+
|
|
1078
1125
|
def coerce_accepted_nested_value(value, declared_class)
|
|
1079
1126
|
NestedValueCoercion.coerce(value, declared_class)
|
|
1080
1127
|
end
|
|
@@ -1088,9 +1135,22 @@ child_mappings, options)
|
|
|
1088
1135
|
build_child_transformation(type_class)
|
|
1089
1136
|
end
|
|
1090
1137
|
|
|
1138
|
+
# Create value for an item (handles nested models and primitives)
|
|
1139
|
+
#
|
|
1140
|
+
# @param rule [CompiledRule] The rule
|
|
1141
|
+
# @param value [Object] The value
|
|
1142
|
+
# @param options [Hash] Options
|
|
1143
|
+
# @return [Object] The value (could be Hash, primitive, or KeyValueElement)
|
|
1091
1144
|
def create_value_for_item(rule, value, options)
|
|
1092
1145
|
return nil if value.nil?
|
|
1093
1146
|
|
|
1147
|
+
# A singular attribute whose polymorphic list admits an Array of
|
|
1148
|
+
# members serializes item by item: the scalar members ride the
|
|
1149
|
+
# scalar lane below, the model instances the nested one (#1292).
|
|
1150
|
+
if polymorphic_member_array?(rule, value)
|
|
1151
|
+
return value.map { |item| create_value_for_item(rule, item, options) }
|
|
1152
|
+
end
|
|
1153
|
+
|
|
1094
1154
|
# Union: dispatch on the value's own class (stateless, like polymorphism).
|
|
1095
1155
|
# A model member serializes via its class; a scalar member emits as-is.
|
|
1096
1156
|
union = Lutaml::Model::Type::Union.rule?(rule)
|
|
@@ -1101,8 +1161,20 @@ child_mappings, options)
|
|
|
1101
1161
|
rule.attribute_type < Lutaml::Model::Serialize
|
|
1102
1162
|
end
|
|
1103
1163
|
|
|
1164
|
+
# A polymorphic list admits scalar members (Type::String,
|
|
1165
|
+
# :array, ...) alongside model classes: a value that is a
|
|
1166
|
+
# listed scalar member serializes through the scalar lane
|
|
1167
|
+
# (#1292). The nested-model validation below demands a Serialize
|
|
1168
|
+
# instance for every value, which rejected the scalar members
|
|
1169
|
+
# the declaration explicitly allowed.
|
|
1170
|
+
if is_nested_model && polymorphic_scalar_member?(rule, value)
|
|
1171
|
+
is_nested_model = false
|
|
1172
|
+
|
|
1173
|
+
end
|
|
1174
|
+
|
|
1104
1175
|
if is_nested_model && !value.is_a?(Lutaml::Model::Serialize)
|
|
1105
1176
|
# The KV mirror of the XML lane's #971 collapse: a tolerated
|
|
1177
|
+
|
|
1106
1178
|
# singular attribute holding an over-assigned collection
|
|
1107
1179
|
# serializes its leading value instead of raising.
|
|
1108
1180
|
attr = if model_class.respond_to?(:attributes)
|
|
@@ -1237,7 +1309,10 @@ child_mappings, options)
|
|
|
1237
1309
|
|
|
1238
1310
|
# Validate that value is an instance of the expected Serializable type
|
|
1239
1311
|
# When attribute_type is a Serializable class, value must be an instance of that class
|
|
1240
|
-
|
|
1312
|
+
# A listed scalar member of a polymorphic list skips the nested
|
|
1313
|
+
# lane (the dispatch in create_value_for_item and here must agree:
|
|
1314
|
+
# #1292).
|
|
1315
|
+
if plan[:nested] && !polymorphic_scalar_member?(rule, value)
|
|
1241
1316
|
unless value.is_a?(rule.attribute_type)
|
|
1242
1317
|
value = coerce_accepted_nested_value(value, rule.attribute_type)
|
|
1243
1318
|
unless value.is_a?(rule.attribute_type)
|
data/lib/lutaml/lml.rb
CHANGED
|
@@ -83,7 +83,6 @@ module Lutaml
|
|
|
83
83
|
autoload :Diagnostic, "lutaml/lml/diagnostic"
|
|
84
84
|
autoload :SourcePosition, "lutaml/lml/source_position"
|
|
85
85
|
autoload :Positioned, "lutaml/lml/positioned"
|
|
86
|
-
autoload :VERSION, "lutaml/lml/version"
|
|
87
86
|
|
|
88
87
|
# Type classes (Lutaml::Model::Type::Value subclasses, not models)
|
|
89
88
|
autoload :LiteralValue, "lutaml/lml/literal_value"
|