lutaml-model 0.8.123 → 0.8.124
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/lib/compat/opal/lutaml_model_boot.rb +2 -0
- data/lib/lutaml/key_value/transform.rb +6 -0
- data/lib/lutaml/model/error/unsupported_version_error.rb +14 -0
- data/lib/lutaml/model/evolution_steps.rb +85 -0
- data/lib/lutaml/model/serialize/attribute_definition.rb +161 -0
- data/lib/lutaml/model/serialize.rb +12 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/model.rb +3 -0
- data/spec/lutaml/model/child_mappings_absent_key_spec.rb +53 -0
- data/spec/lutaml/model/evolution_spec.rb +204 -0
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f4c03c6aacd7f243f8c919c1561cada699b4d6a06fa1fa7f48e7847787dcbef2
|
|
4
|
+
data.tar.gz: bdadc3e2abadd6df36b913d19ec281771e5ea4b68ec74f58b1566f93a6053242
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '09ac4538324403ddc363c1b75ecedb9499a5361c399894287c83ec1ffb52cd4169cc6ea050f1dd4242c6d834b5df55845e17fa1b0c2e7b10b21c6639a52c7c26'
|
|
7
|
+
data.tar.gz: 8322eeba63f6c0b4ff546629bb81801a80d9b19b3ee13ab1fd72d5265c52b7f3248f28c498b59f3b6eb135fbe78cb73632f0cdd609cab821a5a792fd374251a6
|
|
@@ -59,6 +59,7 @@ require "lutaml/model/context_registry"
|
|
|
59
59
|
require "lutaml/model/deep_dupable"
|
|
60
60
|
require "lutaml/model/error"
|
|
61
61
|
require "lutaml/model/errors"
|
|
62
|
+
require "lutaml/model/evolution_steps"
|
|
62
63
|
require "lutaml/model/finalization_cache"
|
|
63
64
|
require "lutaml/model/format_registry"
|
|
64
65
|
require "lutaml/model/global_context"
|
|
@@ -283,6 +284,7 @@ require "lutaml/model/error/unknown_discriminator_error"
|
|
|
283
284
|
require "lutaml/model/error/unknown_sequence_mapping_error"
|
|
284
285
|
require "lutaml/model/error/unknown_type_error"
|
|
285
286
|
require "lutaml/model/error/unresolvable_type_error"
|
|
287
|
+
require "lutaml/model/error/unsupported_version_error"
|
|
286
288
|
require "lutaml/model/error/validation_error"
|
|
287
289
|
require "lutaml/model/error/validation_failed_error"
|
|
288
290
|
require "lutaml/model/liquid/indexed_access"
|
|
@@ -753,6 +753,12 @@ partition = nil)
|
|
|
753
753
|
end
|
|
754
754
|
|
|
755
755
|
value = rule.transform_value(attr, value, :from, format)
|
|
756
|
+
# Hash/child-mapping rules translate a hash document; an absent
|
|
757
|
+
# or explicit-null key has nothing to translate — the attribute
|
|
758
|
+
# keeps its uninitialized/default state (a nil here previously
|
|
759
|
+
# crashed translate_mappings).
|
|
760
|
+
return if value.nil? && (rule.hash_mappings || rule.child_mappings)
|
|
761
|
+
|
|
756
762
|
value = translate_mappings(value, rule.hash_mappings, attr, format,
|
|
757
763
|
instance)
|
|
758
764
|
unless rule.hash_mappings
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Lutaml
|
|
2
|
+
module Model
|
|
3
|
+
# Raised when version detection reports a model version that no class
|
|
4
|
+
# in the detected model's family declares (#265).
|
|
5
|
+
class UnsupportedVersionError < Error
|
|
6
|
+
def initialize(model_class, version)
|
|
7
|
+
super(
|
|
8
|
+
"no model in the #{model_class} family declares version " \
|
|
9
|
+
"#{version.inspect}",
|
|
10
|
+
)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
# Step collector for the `evolution from:` DSL (#265). Each declared
|
|
6
|
+
# step is a closure applied in declaration order to the source
|
|
7
|
+
# document's wire hash; the evolved hash is then parsed by the
|
|
8
|
+
# target model. All steps are wire-level: they relocate, merge and
|
|
9
|
+
# drop keys of the old document — the model pipeline never sees a
|
|
10
|
+
# half-migrated shape.
|
|
11
|
+
class EvolutionSteps
|
|
12
|
+
def initialize
|
|
13
|
+
@steps = []
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
attr_reader :steps
|
|
17
|
+
|
|
18
|
+
# Accept a document written with a former wire name for the
|
|
19
|
+
# current attribute. The current name wins when both appear.
|
|
20
|
+
def wire_rename(from_key, to:)
|
|
21
|
+
from_key = from_key.to_s
|
|
22
|
+
to = to.to_s
|
|
23
|
+
@steps << lambda do |hash|
|
|
24
|
+
next hash unless hash.key?(from_key)
|
|
25
|
+
|
|
26
|
+
hash[to] = hash.delete(from_key) unless hash.key?(to)
|
|
27
|
+
hash
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Relocate a former top-level key into a nested position, given
|
|
32
|
+
# as a dot path ("metadata.name") or an array of segments.
|
|
33
|
+
# Intermediate objects are created as needed.
|
|
34
|
+
def wire_move(from_key, to:)
|
|
35
|
+
path = to.is_a?(::Array) ? to.map(&:to_s) : to.split(".").map(&:to_s)
|
|
36
|
+
from_key = from_key.to_s
|
|
37
|
+
@steps << lambda do |hash|
|
|
38
|
+
value = hash.delete(from_key)
|
|
39
|
+
next hash if value.nil?
|
|
40
|
+
|
|
41
|
+
target = hash
|
|
42
|
+
path[0...-1].each do |segment|
|
|
43
|
+
target = (target[segment] ||= {})
|
|
44
|
+
end
|
|
45
|
+
target[path[-1]] = value
|
|
46
|
+
hash
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Combine former keys into one. With `with:`, it receives the
|
|
51
|
+
# source values in declared order and returns the new value;
|
|
52
|
+
# without it, the sources must all be hashes and are merged in
|
|
53
|
+
# declared order.
|
|
54
|
+
def wire_merge(from_keys, to:, with: nil)
|
|
55
|
+
keys = from_keys.map(&:to_s)
|
|
56
|
+
to = to.to_s
|
|
57
|
+
@steps << lambda do |hash|
|
|
58
|
+
values = keys.map { |key| hash.delete(key) }
|
|
59
|
+
hash[to] =
|
|
60
|
+
if with
|
|
61
|
+
with.call(*values)
|
|
62
|
+
elsif values.all?(::Hash)
|
|
63
|
+
values.reduce { |merged, value| merged.merge(value) }
|
|
64
|
+
else
|
|
65
|
+
raise ArgumentError,
|
|
66
|
+
"wire_merge requires with: when the merged values " \
|
|
67
|
+
"are not all hashes"
|
|
68
|
+
end
|
|
69
|
+
hash
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Retire former keys explicitly. Documentation as much as
|
|
74
|
+
# behavior: an undeclared unknown key keeps the map_unmapped /
|
|
75
|
+
# drop semantics; a declared one states the vocabulary removed it.
|
|
76
|
+
def wire_drop(*keys)
|
|
77
|
+
dropped = keys.map(&:to_s)
|
|
78
|
+
@steps << lambda do |hash|
|
|
79
|
+
dropped.each { |key| hash.delete(key) }
|
|
80
|
+
hash
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -521,6 +521,167 @@ module Lutaml
|
|
|
521
521
|
end
|
|
522
522
|
end
|
|
523
523
|
|
|
524
|
+
# Model evolution (#265): `version 1` (or a semver string)
|
|
525
|
+
# stamps the model schema version. Subclasses inherit the
|
|
526
|
+
# nearest ancestor's version; a family is the ancestor chain of
|
|
527
|
+
# version-declared classes, and detection routes within it.
|
|
528
|
+
def version(value = nil)
|
|
529
|
+
return @lutaml_model_version = value if value
|
|
530
|
+
|
|
531
|
+
return @lutaml_model_version if defined?(@lutaml_model_version)
|
|
532
|
+
|
|
533
|
+
if superclass.is_a?(::Class) &&
|
|
534
|
+
superclass < ::Lutaml::Model::Serializable
|
|
535
|
+
return superclass.version
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
nil
|
|
539
|
+
end
|
|
540
|
+
|
|
541
|
+
alias model_version version
|
|
542
|
+
|
|
543
|
+
# Wire-level version detection for the family (#265): the block
|
|
544
|
+
# receives the parsed document hash and returns the version the
|
|
545
|
+
# document was written against. Declared on the family head;
|
|
546
|
+
# `from_<format>` routes to the detected version's class before
|
|
547
|
+
# parsing. Opt-in — without a detector, parsing is unchanged.
|
|
548
|
+
def detect_version(&block)
|
|
549
|
+
return @lutaml_version_detector = block if block
|
|
550
|
+
|
|
551
|
+
return @lutaml_version_detector if defined?(@lutaml_version_detector)
|
|
552
|
+
|
|
553
|
+
if superclass.is_a?(::Class) &&
|
|
554
|
+
superclass < ::Lutaml::Model::Serializable
|
|
555
|
+
return superclass.detect_version
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
nil
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
# Declares wire-level migration steps from a former model
|
|
562
|
+
# version (#265). The block is evaluated on an EvolutionSteps
|
|
563
|
+
# collector (wire_rename, wire_move, wire_merge, wire_drop);
|
|
564
|
+
# the steps are applied by `evolve`, in declaration order, to a
|
|
565
|
+
# copy of the source document's wire hash before the target
|
|
566
|
+
# model parses it.
|
|
567
|
+
def evolution(from:, &block)
|
|
568
|
+
unless from.is_a?(::Class) && from < ::Lutaml::Model::Serializable
|
|
569
|
+
raise ArgumentError,
|
|
570
|
+
"evolution from: must be a Lutaml::Model::Serializable " \
|
|
571
|
+
"model class"
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
collector = Lutaml::Model::EvolutionSteps.new
|
|
575
|
+
collector.instance_eval(&block)
|
|
576
|
+
lutaml_evolutions[from] = collector.steps.freeze
|
|
577
|
+
nil
|
|
578
|
+
end
|
|
579
|
+
|
|
580
|
+
def lutaml_evolutions
|
|
581
|
+
@lutaml_evolutions ||= {}
|
|
582
|
+
end
|
|
583
|
+
|
|
584
|
+
# The class of the family that declares `version == value`.
|
|
585
|
+
# Walks the ancestor chain nearest-first; raises
|
|
586
|
+
# UnsupportedVersionError when no family member declares it.
|
|
587
|
+
def version_class_for(value)
|
|
588
|
+
klass = self
|
|
589
|
+
while klass.is_a?(::Class) &&
|
|
590
|
+
klass < ::Lutaml::Model::Serializable
|
|
591
|
+
return klass if klass.version == value
|
|
592
|
+
|
|
593
|
+
klass = klass.superclass
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
raise ::Lutaml::Model::UnsupportedVersionError.new(self, value)
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
# Migrate a document or instance of a former model version into
|
|
600
|
+
# this one (#265). The source's wire hash (its own `to_hash`
|
|
601
|
+
# for an instance) passes through the declared evolution chain
|
|
602
|
+
# — every family link between the source version and this
|
|
603
|
+
# class, in order — and the result parses as this model.
|
|
604
|
+
#
|
|
605
|
+
# @param source [Hash, Serializable] the old document or model
|
|
606
|
+
# @param from [Class] the source model class
|
|
607
|
+
# @param format [Symbol] the key-value format used to parse
|
|
608
|
+
# the evolved hash
|
|
609
|
+
def evolve(source, from:)
|
|
610
|
+
chain = evolution_chain_from(from)
|
|
611
|
+
wire = if source.is_a?(::Hash)
|
|
612
|
+
Lutaml::Model::Utils.deep_dup(source)
|
|
613
|
+
else
|
|
614
|
+
source.to_hash
|
|
615
|
+
end
|
|
616
|
+
chain.each do |link_source|
|
|
617
|
+
steps = evolution_steps_for(link_source)
|
|
618
|
+
wire = steps.reduce(wire) { |hash, step| step.call(hash) }
|
|
619
|
+
end
|
|
620
|
+
from_hash(wire)
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
# Family members nearest-first, self included.
|
|
624
|
+
def evolution_family
|
|
625
|
+
members = []
|
|
626
|
+
klass = self
|
|
627
|
+
while klass.is_a?(::Class) &&
|
|
628
|
+
klass < ::Lutaml::Model::Serializable
|
|
629
|
+
members << klass
|
|
630
|
+
klass = klass.superclass
|
|
631
|
+
end
|
|
632
|
+
members
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
# Shortest declared-evolution path from the source class to
|
|
636
|
+
# self, as the list of link source classes. Edges are the
|
|
637
|
+
# family's `evolution from:` declarations. (Public: also serves
|
|
638
|
+
# introspection of declared family links.)
|
|
639
|
+
def evolution_chain_from(source_class)
|
|
640
|
+
return [] if source_class == self
|
|
641
|
+
|
|
642
|
+
edges = {}
|
|
643
|
+
evolution_family.each do |member|
|
|
644
|
+
member.lutaml_evolutions.each_key do |source|
|
|
645
|
+
(edges[source] ||= []) << member
|
|
646
|
+
end
|
|
647
|
+
end
|
|
648
|
+
|
|
649
|
+
parents = { source_class => nil }
|
|
650
|
+
frontier = [source_class]
|
|
651
|
+
until frontier.empty? || parents[self]
|
|
652
|
+
current = frontier.shift
|
|
653
|
+
(edges[current] || []).each do |target|
|
|
654
|
+
next if parents.key?(target)
|
|
655
|
+
|
|
656
|
+
parents[target] = current
|
|
657
|
+
frontier << target
|
|
658
|
+
end
|
|
659
|
+
end
|
|
660
|
+
|
|
661
|
+
unless parents[self]
|
|
662
|
+
raise ArgumentError,
|
|
663
|
+
"no evolution path from #{source_class} to #{self} " \
|
|
664
|
+
"is declared"
|
|
665
|
+
end
|
|
666
|
+
|
|
667
|
+
path = []
|
|
668
|
+
cursor = self
|
|
669
|
+
while cursor != source_class
|
|
670
|
+
path.unshift(cursor)
|
|
671
|
+
cursor = parents[cursor]
|
|
672
|
+
end
|
|
673
|
+
path.unshift(source_class)[0...-1]
|
|
674
|
+
end
|
|
675
|
+
|
|
676
|
+
# The family member (nearest-first) that declared steps for the
|
|
677
|
+
# given link source.
|
|
678
|
+
def evolution_steps_for(link_source)
|
|
679
|
+
member = evolution_family.find do |klass|
|
|
680
|
+
klass.lutaml_evolutions.key?(link_source)
|
|
681
|
+
end
|
|
682
|
+
member.lutaml_evolutions[link_source]
|
|
683
|
+
end
|
|
684
|
+
|
|
524
685
|
# Define an attribute for the model
|
|
525
686
|
#
|
|
526
687
|
# @param name [Symbol] The attribute name
|
|
@@ -75,6 +75,18 @@ module Lutaml
|
|
|
75
75
|
|
|
76
76
|
def self.register_from_format_method(format)
|
|
77
77
|
ClassMethods.define_method(:"from_#{format}") do |data, options = {}|
|
|
78
|
+
# #265 version routing: with a family detector declared, the
|
|
79
|
+
# document's version picks the family class that parses it.
|
|
80
|
+
# Terminates because the routed class resolves the same
|
|
81
|
+
# detected version to itself and parses directly.
|
|
82
|
+
detector = detect_version
|
|
83
|
+
if detector
|
|
84
|
+
parsed = ::Lutaml::Model::Config.adapter_for(format)
|
|
85
|
+
.parse(data, options)
|
|
86
|
+
detected = detector.call(parsed)
|
|
87
|
+
routed = version_class_for(detected) unless detected.nil?
|
|
88
|
+
return routed.public_send(:"from_#{format}", data, options) if routed && routed != self
|
|
89
|
+
end
|
|
78
90
|
from(format, data, options)
|
|
79
91
|
end
|
|
80
92
|
|
data/lib/lutaml/model/version.rb
CHANGED
data/lib/lutaml/model.rb
CHANGED
|
@@ -46,6 +46,7 @@ module Lutaml
|
|
|
46
46
|
autoload :NamespaceBinding, "#{__dir__}/model/register/namespace_binding"
|
|
47
47
|
autoload :ModelTreeImporter, "#{__dir__}/model/register/model_tree_importer"
|
|
48
48
|
autoload :Transformation, "#{__dir__}/model/transformation"
|
|
49
|
+
autoload :EvolutionSteps, "#{__dir__}/model/evolution_steps"
|
|
49
50
|
autoload :CompiledRule, "#{__dir__}/model/compiled_rule"
|
|
50
51
|
autoload :FormatRegistry, "#{__dir__}/model/format_registry"
|
|
51
52
|
autoload :OneEntryCache, "#{__dir__}/model/one_entry_cache"
|
|
@@ -161,6 +162,8 @@ module Lutaml
|
|
|
161
162
|
autoload :NoRootMappingError, "#{__dir__}/model/error/no_root_mapping_error"
|
|
162
163
|
autoload :ImportModelWithRootError,
|
|
163
164
|
"#{__dir__}/model/error/import_model_with_root_error"
|
|
165
|
+
autoload :UnsupportedVersionError,
|
|
166
|
+
"#{__dir__}/model/error/unsupported_version_error"
|
|
164
167
|
autoload :InvalidChoiceRangeError,
|
|
165
168
|
"#{__dir__}/model/error/invalid_choice_range_error"
|
|
166
169
|
autoload :UnknownSequenceMappingError,
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# A child_mappings rule whose wire key is absent crashed translate_mappings
|
|
6
|
+
# with an internal NoMethodError (nil.map) wrapped as InvalidFormatError.
|
|
7
|
+
# An absent key means the attribute keeps its uninitialized/default state,
|
|
8
|
+
# exactly like every other rule kind.
|
|
9
|
+
RSpec.describe "child_mappings rule with an absent key" do
|
|
10
|
+
let(:schema_class) do
|
|
11
|
+
Class.new(Lutaml::Model::Serializable) do
|
|
12
|
+
attribute :id, :string
|
|
13
|
+
attribute :path, :string
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
let(:model_class) do
|
|
18
|
+
schemas = schema_class
|
|
19
|
+
Class.new(Lutaml::Model::Serializable) do
|
|
20
|
+
attribute :type, :string
|
|
21
|
+
attribute :schemas, schemas, collection: true
|
|
22
|
+
|
|
23
|
+
key_value do
|
|
24
|
+
map "type", to: :type
|
|
25
|
+
map "schemas", to: :schemas,
|
|
26
|
+
child_mappings: {
|
|
27
|
+
id: :key,
|
|
28
|
+
path: %i[path link],
|
|
29
|
+
}
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
before { stub_const("ChildMapAbsentModel", model_class) }
|
|
35
|
+
|
|
36
|
+
it "parses without the key, leaving the attribute unset" do
|
|
37
|
+
model = ChildMapAbsentModel.from_json('{"type":"object"}')
|
|
38
|
+
|
|
39
|
+
expect(model.type).to eq("object")
|
|
40
|
+
expect(model.schemas).to be_nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "still translates when the key is present" do
|
|
44
|
+
model = ChildMapAbsentModel.from_json(
|
|
45
|
+
'{"type":"object","schemas":{' \
|
|
46
|
+
'"sch1":{"path":{"link":"https://example.com"}}}}',
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
expect(model.schemas.size).to eq(1)
|
|
50
|
+
expect(model.schemas.first.id).to eq("sch1")
|
|
51
|
+
expect(model.schemas.first.path).to eq("https://example.com")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# #265: model evolution — version metadata, wire-level migration steps
|
|
6
|
+
# between model versions, and family version detection. The migration
|
|
7
|
+
# steps operate on the source document's wire hash (deep-copied); the
|
|
8
|
+
# evolved hash parses as the target model through the hash format.
|
|
9
|
+
RSpec.describe "model evolution" do
|
|
10
|
+
let(:v1_class) do
|
|
11
|
+
Class.new(Lutaml::Model::Serializable) do
|
|
12
|
+
version 1
|
|
13
|
+
|
|
14
|
+
attribute :author, :string
|
|
15
|
+
attribute :name, :string
|
|
16
|
+
attribute :first_name, :string
|
|
17
|
+
attribute :last_name, :string
|
|
18
|
+
attribute :tag, :string
|
|
19
|
+
attribute :deprecated_field, :string
|
|
20
|
+
|
|
21
|
+
key_value do
|
|
22
|
+
map "author", to: :author
|
|
23
|
+
map "name", to: :name
|
|
24
|
+
map "first_name", to: :first_name
|
|
25
|
+
map "last_name", to: :last_name
|
|
26
|
+
map "tag", to: :tag
|
|
27
|
+
map "deprecated_field", to: :deprecated_field
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
let(:v2_class) do
|
|
33
|
+
v1 = v1_class
|
|
34
|
+
Class.new(v1) do
|
|
35
|
+
version 2
|
|
36
|
+
|
|
37
|
+
attribute :creator, :string
|
|
38
|
+
attribute :full_name, :string
|
|
39
|
+
attribute :tags, :string, collection: true
|
|
40
|
+
attribute :metadata_name, :string
|
|
41
|
+
|
|
42
|
+
key_value do
|
|
43
|
+
map "creator", to: :creator
|
|
44
|
+
map "full_name", to: :full_name
|
|
45
|
+
map "tags", to: :tags
|
|
46
|
+
map "metadata", to: :metadata_name,
|
|
47
|
+
child_mappings: { metadata_name: :name }
|
|
48
|
+
map "deprecated_field", to: :deprecated_field, serialize: false
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
evolution from: v1 do
|
|
52
|
+
wire_rename "author", to: "creator"
|
|
53
|
+
wire_rename "tag", to: "tags"
|
|
54
|
+
wire_merge %w[first_name last_name], to: "full_name",
|
|
55
|
+
with: ->(first, last) { [first, last].compact.join(" ") }
|
|
56
|
+
wire_drop "first_name", "last_name", "deprecated_field"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
before do
|
|
62
|
+
stub_const("EvolutionDocV1", v1_class)
|
|
63
|
+
stub_const("EvolutionDocV2", v2_class)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "stamps and inherits the model version" do
|
|
67
|
+
expect(EvolutionDocV1.model_version).to eq(1)
|
|
68
|
+
expect(EvolutionDocV2.model_version).to eq(2)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "migrates a v1 hash into a v2 model through declared steps" do
|
|
72
|
+
v2 = EvolutionDocV2.evolve(
|
|
73
|
+
{ "author" => "Ruth", "first_name" => "Ada", "last_name" => "L" },
|
|
74
|
+
from: EvolutionDocV1,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
expect(v2).to be_a(EvolutionDocV2)
|
|
78
|
+
expect(v2.creator).to eq("Ruth")
|
|
79
|
+
expect(v2.full_name).to eq("Ada L")
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it "migrates a v1 instance through its own wire hash" do
|
|
83
|
+
old = EvolutionDocV1.from_json(
|
|
84
|
+
'{"author":"Ruth","tag":"core","deprecated_field":"gone"}',
|
|
85
|
+
)
|
|
86
|
+
v2 = EvolutionDocV2.evolve(old, from: EvolutionDocV1)
|
|
87
|
+
|
|
88
|
+
expect(v2.creator).to eq("Ruth")
|
|
89
|
+
expect(v2.tags).to eq(["core"])
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it "does not mutate the caller's source hash" do
|
|
93
|
+
source = { "author" => "Ruth", "first_name" => "Ada" }
|
|
94
|
+
EvolutionDocV2.evolve(source, from: EvolutionDocV1)
|
|
95
|
+
|
|
96
|
+
expect(source).to eq("author" => "Ruth", "first_name" => "Ada")
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "relocates keys with wire_move" do
|
|
100
|
+
mover = Class.new(EvolutionDocV1) do
|
|
101
|
+
version 3
|
|
102
|
+
attribute :meta, :hash
|
|
103
|
+
|
|
104
|
+
key_value do
|
|
105
|
+
map "meta", to: :meta
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
evolution from: EvolutionDocV2 do
|
|
109
|
+
wire_move "creator", to: "meta.creator"
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
stub_const("EvolutionDocV3", mover)
|
|
113
|
+
|
|
114
|
+
v3 = mover.evolve(
|
|
115
|
+
{ "creator" => "Ruth", "name" => "n" },
|
|
116
|
+
from: EvolutionDocV2,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
expect(v3.meta).to eq("creator" => "Ruth")
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "composes chains v1 -> v2 -> v3" do
|
|
123
|
+
mover = Class.new(EvolutionDocV2) do
|
|
124
|
+
version 3
|
|
125
|
+
|
|
126
|
+
evolution from: EvolutionDocV2 do
|
|
127
|
+
wire_drop "tag"
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
stub_const("EvolutionDocV3Chain", mover)
|
|
131
|
+
|
|
132
|
+
v3 = mover.evolve(
|
|
133
|
+
{ "author" => "Ruth", "first_name" => "Ada", "tag" => "x" },
|
|
134
|
+
from: EvolutionDocV1,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
expect(v3).to be_a(EvolutionDocV3Chain)
|
|
138
|
+
expect(v3.creator).to eq("Ruth")
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it "raises when no evolution path is declared" do
|
|
142
|
+
unrelated = Class.new(Lutaml::Model::Serializable) do
|
|
143
|
+
version 9
|
|
144
|
+
attribute :x, :string
|
|
145
|
+
key_value { map "x", to: :x }
|
|
146
|
+
end
|
|
147
|
+
stub_const("EvolutionUnrelated", unrelated)
|
|
148
|
+
|
|
149
|
+
expect do
|
|
150
|
+
EvolutionUnrelated.evolve({ "x" => 1 }, from: EvolutionDocV1)
|
|
151
|
+
end.to raise_error(
|
|
152
|
+
ArgumentError,
|
|
153
|
+
/no evolution path from EvolutionDocV1 to EvolutionUnrelated/,
|
|
154
|
+
)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
describe "version detection" do
|
|
158
|
+
let(:detecting_class) do
|
|
159
|
+
Class.new(v2_class) do
|
|
160
|
+
detect_version do |doc|
|
|
161
|
+
doc["schema_version"] || 1
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
before { stub_const("EvolutionDetecting", detecting_class) }
|
|
167
|
+
|
|
168
|
+
it "routes an older document to its own version's model" do
|
|
169
|
+
parsed = EvolutionDetecting.from_json(
|
|
170
|
+
'{"schema_version":1,"author":"Ruth"}',
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
expect(parsed).to be_a(EvolutionDocV1)
|
|
174
|
+
expect(parsed.author).to eq("Ruth")
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
it "parses a current document as itself" do
|
|
178
|
+
parsed = EvolutionDetecting.from_json(
|
|
179
|
+
'{"schema_version":2,"creator":"Ruth"}',
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
expect(parsed).to be_a(EvolutionDetecting)
|
|
183
|
+
expect(parsed.creator).to eq("Ruth")
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it "raises on a version no family member declares" do
|
|
187
|
+
expect do
|
|
188
|
+
EvolutionDetecting.from_json('{"schema_version":42}')
|
|
189
|
+
end.to raise_error(
|
|
190
|
+
Lutaml::Model::UnsupportedVersionError,
|
|
191
|
+
/version 42/,
|
|
192
|
+
)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
describe "models without evolution declarations" do
|
|
197
|
+
it "parses unchanged" do
|
|
198
|
+
model = v1_class.from_json('{"author":"Ruth"}')
|
|
199
|
+
|
|
200
|
+
expect(model.author).to eq("Ruth")
|
|
201
|
+
expect(v1_class.model_version).to eq(1)
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lutaml-model
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.124
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
@@ -500,9 +500,11 @@ files:
|
|
|
500
500
|
- lib/lutaml/model/error/unknown_sequence_mapping_error.rb
|
|
501
501
|
- lib/lutaml/model/error/unknown_type_error.rb
|
|
502
502
|
- lib/lutaml/model/error/unresolvable_type_error.rb
|
|
503
|
+
- lib/lutaml/model/error/unsupported_version_error.rb
|
|
503
504
|
- lib/lutaml/model/error/validation_error.rb
|
|
504
505
|
- lib/lutaml/model/error/validation_failed_error.rb
|
|
505
506
|
- lib/lutaml/model/errors.rb
|
|
507
|
+
- lib/lutaml/model/evolution_steps.rb
|
|
506
508
|
- lib/lutaml/model/finalization_cache.rb
|
|
507
509
|
- lib/lutaml/model/format_registry.rb
|
|
508
510
|
- lib/lutaml/model/global_context.rb
|
|
@@ -1716,6 +1718,7 @@ files:
|
|
|
1716
1718
|
- spec/lutaml/model/cardinality_strict_spec.rb
|
|
1717
1719
|
- spec/lutaml/model/cast_element_form_spec.rb
|
|
1718
1720
|
- spec/lutaml/model/cdata_spec.rb
|
|
1721
|
+
- spec/lutaml/model/child_mappings_absent_key_spec.rb
|
|
1719
1722
|
- spec/lutaml/model/choice_restrict_spec.rb
|
|
1720
1723
|
- spec/lutaml/model/choice_spec.rb
|
|
1721
1724
|
- spec/lutaml/model/cli_spec.rb
|
|
@@ -1750,6 +1753,7 @@ files:
|
|
|
1750
1753
|
- spec/lutaml/model/dynamic_attribute_spec.rb
|
|
1751
1754
|
- spec/lutaml/model/encoding_spec.rb
|
|
1752
1755
|
- spec/lutaml/model/enum_spec.rb
|
|
1756
|
+
- spec/lutaml/model/evolution_spec.rb
|
|
1753
1757
|
- spec/lutaml/model/except_spec.rb
|
|
1754
1758
|
- spec/lutaml/model/finalization_cache_spec.rb
|
|
1755
1759
|
- spec/lutaml/model/from_json_scalar_cast_spec.rb
|