lutaml-model 0.8.203 → 0.8.204

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 (112) 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/_references/lml-conformance.adoc +78 -0
  8. data/docs/_references/lml-contract.adoc +290 -0
  9. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  10. data/lib/lutaml/model/lml/bundle.rb +171 -0
  11. data/lib/lutaml/model/lml/document.rb +46 -10
  12. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  13. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  14. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  15. data/lib/lutaml/model/lml/exporter.rb +223 -87
  16. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  17. data/lib/lutaml/model/lml/importer.rb +180 -0
  18. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  19. data/lib/lutaml/model/lml/materialization.rb +257 -0
  20. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  21. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  22. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  23. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  24. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  25. data/lib/lutaml/model/lml.rb +61 -0
  26. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  27. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  28. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  29. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  30. data/lib/lutaml/model/sequence.rb +7 -0
  31. data/lib/lutaml/model/version.rb +1 -1
  32. data/lib/lutaml/xml/leptris/native_emission.rb +4 -0
  33. data/lib/lutaml/xml/leptris/plan_serializer.rb +14 -9
  34. data/schema/lml/document.schema.json +179 -0
  35. data/scripts/lml/export_choices.rb +57 -0
  36. data/scripts/lml/export_conformance.rb +23 -0
  37. data/scripts/lml/export_mml.rb +253 -0
  38. data/scripts/lml/export_preservation.rb +421 -0
  39. data/scripts/lml/verify_artifacts.rb +31 -0
  40. data/spec/fixtures/lml_artifacts/model.json +1 -0
  41. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  42. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  43. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  44. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  45. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  46. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  47. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  48. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  49. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  50. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  51. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  52. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  53. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  54. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  55. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  56. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  57. data/spec/fixtures/lml_contract/bundle.json +81 -0
  58. data/spec/fixtures/lml_contract/manifest.json +30 -0
  59. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  60. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  61. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  62. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  63. data/spec/fixtures/lml_mml/inventory.json +1 -0
  64. data/spec/fixtures/lml_mml/manifest.json +1 -0
  65. data/spec/fixtures/lml_mml/oracle.json +1 -0
  66. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  67. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  68. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  69. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  70. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  71. data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
  72. data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
  73. data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
  74. data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
  75. data/spec/lutaml/model/lml_document_spec.rb +279 -0
  76. data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
  77. data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
  78. data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
  79. data/spec/lutaml/model/lml_importer_spec.rb +290 -0
  80. data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
  81. data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
  82. data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
  83. data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
  84. data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
  85. data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
  86. data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
  87. data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
  88. data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
  89. data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
  90. data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
  91. data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
  92. data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
  93. data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
  94. data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
  95. data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
  96. data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
  97. data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
  98. data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
  99. data/spec/lutaml/model/sequence_spec.rb +48 -0
  100. data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
  101. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  102. data/spec/support/lml_conformance.rb +292 -0
  103. data/spec/support/lml_corpus.rb +75 -0
  104. data/spec/support/lml_export_fixtures.rb +190 -0
  105. data/spec/support/lml_graph_fixtures.rb +108 -0
  106. data/spec/support/lml_import_fixtures.rb +52 -0
  107. data/spec/support/lml_mapping_fixtures.rb +170 -0
  108. data/spec/support/lml_materialization_fixtures.rb +53 -0
  109. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  110. data/spec/support/lml_order_fixtures.rb +151 -0
  111. metadata +92 -2
  112. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "json"
5
+ require "open3"
6
+ require "tmpdir"
7
+
8
+ # The generated mml package's large payloads (lutaml-model#1033) live in
9
+ # lutaml/lutaml-model-corpus. The package directory keeps every small
10
+ # file and the manifest whose sha256 entries verify each fetch.
11
+ module LmlCorpus
12
+ SOURCE = ENV.fetch("LML_CORPUS_SOURCE",
13
+ "https://github.com/lutaml/lutaml-model-corpus.git")
14
+ REVISION = ENV.fetch("LML_CORPUS_REVISION",
15
+ "af328917cd8e29ab52f033d0de91465d3eb7a538")
16
+ PACKAGE = File.expand_path("../fixtures/lml_mml", __dir__)
17
+ PAYLOADS = %w[
18
+ v3-math-nested.json
19
+ v3-math-nested.yaml
20
+ v4-math-nested.json
21
+ v4-math-nested.yaml
22
+ ].freeze
23
+
24
+ module_function
25
+
26
+ def digest(bytes)
27
+ "sha256:#{Digest::SHA256.hexdigest(bytes)}"
28
+ end
29
+
30
+ def expected(package)
31
+ JSON.parse(File.read(File.join(package, "manifest.json")))
32
+ .fetch("files").to_h { |entry| [entry.fetch("path"), entry.fetch("sha256")] }
33
+ end
34
+
35
+ def verified?(package, path, digests)
36
+ full = File.join(package, path)
37
+ File.file?(full) && digest(File.binread(full)) == digests.fetch(path)
38
+ end
39
+
40
+ # Idempotent: fetches only a payload that is missing or fails its
41
+ # digest, and verifies the fetched bytes against the manifest before a
42
+ # reader sees the file. Returns the package directory.
43
+ def materialize!(package = PACKAGE)
44
+ digests = expected(package)
45
+ missing = PAYLOADS.reject { |path| verified?(package, path, digests) }
46
+ return package if missing.empty?
47
+
48
+ Dir.mktmpdir("lml-corpus") do |scratch|
49
+ git = ["git", "--git-dir", File.join(scratch, "corpus.git")]
50
+ run!(*git, "init", "--quiet")
51
+ run!(*git, "fetch", "--quiet", "--depth", "1", SOURCE, REVISION)
52
+ missing.each do |path|
53
+ bytes = read_payload(git, path, digests.fetch(path))
54
+ target = File.join(package, path)
55
+ File.binwrite("#{target}.tmp", bytes)
56
+ File.rename("#{target}.tmp", target)
57
+ end
58
+ end
59
+ package
60
+ end
61
+
62
+ def run!(*command)
63
+ _out, err, status = Open3.capture3(*command)
64
+ raise "corpus fetch failed: #{err.strip}" unless status.success?
65
+ end
66
+
67
+ def read_payload(git, path, sha256)
68
+ bytes, status = Open3.capture2(*git, "show", "FETCH_HEAD:lml_mml/#{path}",
69
+ binmode: true)
70
+ raise "corpus has no payload #{path}" unless status.success?
71
+ return bytes if digest(bytes) == sha256
72
+
73
+ raise "corpus payload #{path} does not match its manifest digest"
74
+ end
75
+ end
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+ require_relative "lml_order_fixtures"
5
+
6
+ # Named fixtures for the LML exporter: the document addresses models by
7
+ # class path, so anonymous classes are unnameable.
8
+ module LmlExportFixtures # rubocop:disable Lint/ConstantDefinitionInBlock -- named fixtures
9
+ # Shaped like Mml::V4::Math: ordered content, element collections,
10
+ # no declared sequence.
11
+ class MathShape < Lutaml::Model::Serializable
12
+ attribute :mi, :string, collection: true
13
+ attribute :mn, :string, collection: true
14
+
15
+ xml do
16
+ root "math", ordered: true
17
+ map_element "mi", to: :mi
18
+ map_element "mn", to: :mn
19
+ end
20
+ end
21
+
22
+ class MixedShape < Lutaml::Model::Serializable
23
+ attribute :content, :string, collection: true
24
+
25
+ xml do
26
+ root "p", mixed: true
27
+ map_content to: :content
28
+ end
29
+ end
30
+
31
+ class OrderedWithCallback < Lutaml::Model::Serializable
32
+ attribute :a, :string
33
+
34
+ xml do
35
+ root "cb", ordered: true
36
+ map_element "a", to: :a, with: { to: :a_to_xml, from: :a_from_xml }
37
+ end
38
+
39
+ def a_to_xml(_model, _parent, _doc); end
40
+
41
+ def a_from_xml(_model, _value); end
42
+ end
43
+
44
+ class ReservedMember < Lutaml::Model::Serializable
45
+ attribute :__lml_content, :string
46
+
47
+ xml do
48
+ root "r"
49
+ map_element "x", to: :__lml_content
50
+ end
51
+ end
52
+
53
+ class RenderPolicies < Lutaml::Model::Serializable
54
+ attribute :x, :string
55
+ attribute :y, :string
56
+
57
+ json do
58
+ map "x", to: :x, render_empty: true
59
+ map "y", to: :y, value_map: { to: { empty: :nil } }
60
+ end
61
+ end
62
+
63
+ class PortableRender < Lutaml::Model::Serializable
64
+ attribute :x, :string
65
+ attribute :count, :integer, default: 3
66
+ attribute :kind, :string, values: %w[a b]
67
+ attribute :items, :string, collection: 1..4
68
+
69
+ xml do
70
+ root "pr"
71
+ map_element "x", to: :x, render_empty: true, render_nil: :as_blank, render_default: true
72
+ map_attribute "count", to: :count
73
+ map_attribute "kind", to: :kind
74
+ map_element "item", to: :items
75
+ end
76
+ end
77
+
78
+ class TransformRule < Lutaml::Model::Serializable
79
+ attribute :x, :string
80
+
81
+ json do
82
+ map "x", to: :x, transform: { export: ->(v) { v } }
83
+ end
84
+ end
85
+
86
+ class FloatAttr < Lutaml::Model::Serializable
87
+ attribute :f, :float
88
+ attribute :ratio, :float, default: 0.71
89
+ attribute :tiny, :float, default: 1.0e-05
90
+ attribute :whole, :float, default: 3
91
+ end
92
+
93
+ class NonFiniteFloatDefault < Lutaml::Model::Serializable
94
+ attribute :f, :float, default: Float::INFINITY
95
+ end
96
+
97
+ class ChildMapped < Lutaml::Model::Serializable
98
+ attribute :schemas, :string, collection: true
99
+
100
+ json do
101
+ map "schemas", to: :schemas, child_mappings: { id: :key, path: %i[path link] }
102
+ end
103
+ end
104
+
105
+ class RootMapped < Lutaml::Model::Serializable
106
+ attribute :fonts, :string, collection: true
107
+
108
+ key_value do
109
+ map to: :fonts, root_mappings: { name: :key, styles: :value }
110
+ end
111
+ end
112
+
113
+ class KeyValueOnly < Lutaml::Model::Serializable
114
+ attribute :name, :string
115
+
116
+ key_value do
117
+ map "name", to: :name
118
+ end
119
+ end
120
+
121
+ class PolyBase < Lutaml::Model::Serializable
122
+ attribute :_class, :string, polymorphic_class: true
123
+
124
+ xml do
125
+ map_attribute "item-type", to: :_class, polymorphic_map: {
126
+ "basic" => "LmlExportFixtures::PolyBase",
127
+ }
128
+ end
129
+ end
130
+
131
+ class PolyHolder < Lutaml::Model::Serializable
132
+ attribute :items, PolyBase, collection: true
133
+
134
+ xml do
135
+ root "holder"
136
+ map_element "item", to: :items, polymorphic: {
137
+ attribute: "type",
138
+ class_map: { "base" => "LmlExportFixtures::PolyBase" },
139
+ }
140
+ end
141
+ end
142
+
143
+ class SequencedShape < Lutaml::Model::Serializable
144
+ attribute :a, :string
145
+ attribute :b, :string
146
+
147
+ xml do
148
+ root "seq"
149
+ sequence do
150
+ map_element "a", to: :a
151
+ map_element "b", to: :b
152
+ end
153
+ end
154
+ end
155
+
156
+ class ChoiceSequence < Lutaml::Model::Serializable
157
+ attribute :a, :string
158
+ attribute :b, :string
159
+ attribute :c, :string
160
+
161
+ xml do
162
+ root "cs"
163
+ sequence do
164
+ map_element "a", to: :a
165
+ choice(min: 1, max: 1) do
166
+ map_element "b", to: :b
167
+ map_element "c", to: :c
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ # Fixture models the portable exporter must refuse, with the reason.
174
+ REFUSED = {
175
+ "LmlExportFixtures::OrderedWithCallback" => :imperative_element_order,
176
+ "LmlExportFixtures::ReservedMember" => :reserved_member,
177
+ "LmlExportFixtures::TransformRule" => :unsupported_mapping,
178
+ "LmlExportFixtures::NonFiniteFloatDefault" => :non_serializable_default,
179
+ "LmlOrderFixtures::ImperativeRule" => :imperative_element_order,
180
+ "LmlOrderFixtures::ReservedMember" => :reserved_member,
181
+ }.freeze
182
+
183
+ # Every Serializable fixture in the export and order fixture modules.
184
+ def self.exportable_models
185
+ [self, LmlOrderFixtures].flat_map do |mod|
186
+ mod.constants.map { |name| mod.const_get(name) }
187
+ .select { |c| c.is_a?(Class) && c < Lutaml::Model::Serializable }
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Named stand-in classes plus an injectable v2 projector for the LML graph
4
+ # exporter specs. The projector is table driven: it returns the declared
5
+ # document for (class, register), exactly the callable shape GraphExporter
6
+ # expects, with no dependency on the real single-model exporter.
7
+ module LmlGraphFixtures
8
+ class Node
9
+ def self.model?
10
+ true
11
+ end
12
+ end
13
+
14
+ class Left
15
+ def self.model?
16
+ true
17
+ end
18
+ end
19
+
20
+ class Right
21
+ def self.model?
22
+ true
23
+ end
24
+ end
25
+
26
+ class Leaf
27
+ def self.model?
28
+ true
29
+ end
30
+ end
31
+
32
+ class Shape
33
+ def self.model?
34
+ true
35
+ end
36
+ end
37
+
38
+ class Circle
39
+ def self.model?
40
+ true
41
+ end
42
+ end
43
+
44
+ class Square
45
+ def self.model?
46
+ true
47
+ end
48
+ end
49
+
50
+ class Holder
51
+ def self.model?
52
+ true
53
+ end
54
+ end
55
+
56
+ class Substitute
57
+ def self.model?
58
+ true
59
+ end
60
+ end
61
+
62
+ def self.model_ref(id)
63
+ { "kind" => "model", "id" => id }
64
+ end
65
+
66
+ def self.attribute(name, type)
67
+ { "name" => name, "type" => type, "required" => false }
68
+ end
69
+
70
+ def self.document(klass, register, attributes = [], extra = {})
71
+ {
72
+ "lml" => 2, "model" => klass.name, "register" => register,
73
+ "attributes" => attributes,
74
+ "mappings" => { "xml" => { "rules" => [] } }
75
+ }.merge(extra)
76
+ end
77
+
78
+ # Returns a projector lambda. +overrides+ maps "Model@register" => hash
79
+ # and replaces the built-in table; +calls+ records every invocation.
80
+ def self.projector(overrides = {}, calls = [])
81
+ table = {
82
+ "#{Node.name}@default" => ->(r) { document(Node, r, [attribute("children", model_ref("#{Node.name}@#{r}"))]) },
83
+ "#{Left.name}@default" => ->(r) { document(Left, r, [attribute("right", model_ref("#{Right.name}@#{r}"))]) },
84
+ "#{Right.name}@default" => ->(r) { document(Right, r, [attribute("left", model_ref("#{Left.name}@#{r}"))]) },
85
+ "#{Leaf.name}@default" => ->(r) { document(Leaf, r) },
86
+ "#{Shape.name}@default" => lambda { |r|
87
+ document(Shape, r, [attribute("kind", { "kind" => "builtin", "name" => "string" }).merge(
88
+ "discriminator" => { "attribute" => "kind",
89
+ "cases" => { "c" => "#{Circle.name}@#{r}", "s" => "#{Square.name}@#{r}" } },
90
+ )])
91
+ },
92
+ "#{Circle.name}@default" => ->(r) { document(Circle, r) },
93
+ "#{Square.name}@default" => ->(r) { document(Square, r) },
94
+ "#{Holder.name}@default" => lambda { |r|
95
+ document(Holder, r, [attribute("size", { "kind" => "value", "id" => "Size" })],
96
+ "value_types" => { "Size" => { "base" => "integer", "facets" => {} } })
97
+ },
98
+ }
99
+ lambda do |klass, register|
100
+ calls << [klass, register]
101
+ key = "#{klass.name}@#{register}"
102
+ return overrides.fetch(key) if overrides.key?(key)
103
+
104
+ builder = table["#{klass.name}@default"] or raise "no fixture for #{key}"
105
+ builder.call(register)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+ require_relative "lml_mapping_fixtures"
5
+
6
+ # Named source models for the LML import/export round-trip specs
7
+ # (lutaml-model#1033 card 03c). The graph mixes namespaces, nested and
8
+ # collection models, ordered/mixed content and discriminator-based
9
+ # polymorphism in all three wire formats.
10
+ module LmlImportFixtures
11
+ class Animal < Lutaml::Model::Serializable
12
+ attribute :kind, :string, polymorphic_class: true
13
+ attribute :name, :string
14
+
15
+ xml do
16
+ element "animal"
17
+ map_attribute "kind", to: :kind
18
+ map_element "name", to: :name
19
+ end
20
+ json do
21
+ map "kind", to: :kind
22
+ map "name", to: :name
23
+ end
24
+ end
25
+
26
+ class Dog < Animal
27
+ end
28
+
29
+ class Kennel < Lutaml::Model::Serializable
30
+ attribute :animals, Animal, collection: true
31
+ attribute :owner, :string
32
+
33
+ xml do
34
+ element "kennel"
35
+ map_element "animal", to: :animals, polymorphic: {
36
+ attribute: "kind", class_map: { "dog" => "LmlImportFixtures::Dog", "animal" => "LmlImportFixtures::Animal" }
37
+ }
38
+ map_element "owner", to: :owner
39
+ end
40
+ json do
41
+ map "animals", to: :animals, polymorphic: {
42
+ attribute: "kind", class_map: { "dog" => "LmlImportFixtures::Dog", "animal" => "LmlImportFixtures::Animal" }
43
+ }
44
+ map "owner", to: :owner
45
+ end
46
+ end
47
+
48
+ GRAPH_ROOTS = [
49
+ LmlMappingFixtures::Doc, LmlMappingFixtures::Shape,
50
+ LmlOrderFixtures::Para, LmlOrderFixtures::Transport, Kennel
51
+ ].freeze
52
+ end
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+ require "lutaml/model/lml/exporter"
5
+ require "lutaml/model/lml/document_validator"
6
+ require_relative "lml_order_fixtures"
7
+
8
+ # Named source models and a fresh-graph helper for the LML
9
+ # MappingCompiler specs (lutaml-model#1033 card 03b). The helper
10
+ # declares attributes the way Materialization (03a) does, so these
11
+ # specs do not depend on 03a being merged.
12
+ module LmlMappingFixtures
13
+ class Ns < Lutaml::Xml::Namespace
14
+ uri "urn:lml-map:a"
15
+ prefix_default "m"
16
+ end
17
+
18
+ class Entry < Lutaml::Model::Serializable
19
+ attribute :name, :string
20
+ attribute :value, :string
21
+
22
+ xml do
23
+ element "entry"
24
+ map_attribute "name", to: :name
25
+ map_content to: :value
26
+ end
27
+ json do
28
+ map "name", to: :name
29
+ map "value", to: :value
30
+ end
31
+ end
32
+
33
+ class Item < Lutaml::Model::Serializable
34
+ attribute :id, :string
35
+ attribute :label, :string
36
+
37
+ xml do
38
+ element "item"
39
+ namespace Ns
40
+ map_attribute "id", to: :id
41
+ map_content to: :label, cdata: true
42
+ end
43
+ json do
44
+ map "id", to: :id
45
+ map "label", to: :label
46
+ end
47
+ end
48
+
49
+ class Doc < Lutaml::Model::Serializable
50
+ attribute :kind, :string, values: %w[a b], default: "a"
51
+ attribute :title, :string
52
+ attribute :note, :string
53
+ attribute :count, :integer, required: true
54
+ attribute :items, Item, collection: true
55
+ attribute :entries, Entry, collection: 0..5
56
+
57
+ xml do
58
+ element "doc"
59
+ namespace Ns
60
+ map_attribute "kind", to: :kind, render_nil: true
61
+ map_element "title", to: :title, render_nil: :as_blank
62
+ map_element "note", to: :note, render_empty: :as_blank, render_default: true,
63
+ namespace: nil
64
+ map_element "count", to: :count
65
+ map_element "item", to: :items
66
+ map_element "entry", to: :entries
67
+ end
68
+ json do
69
+ map "kind", to: :kind
70
+ map "title", to: :title, render_nil: :as_empty
71
+ map "count", to: :count, render_default: true
72
+ map "items", to: :items
73
+ map "entries", to: :entries
74
+ end
75
+ yaml do
76
+ map "title", to: :title
77
+ map "items", to: :items
78
+ end
79
+ end
80
+
81
+ class Circle < Lutaml::Model::Serializable
82
+ attribute :kind, :string
83
+ attribute :radius, :integer
84
+ xml do
85
+ element "circle"
86
+ map_attribute "kind", to: :kind
87
+ map_element "radius", to: :radius
88
+ end
89
+ json do
90
+ map "kind", to: :kind
91
+ map "radius", to: :radius
92
+ end
93
+ end
94
+
95
+ class Shape < Lutaml::Model::Serializable
96
+ attribute :child, Circle
97
+ xml do
98
+ element "shape"
99
+ map_element "circle", to: :child
100
+ end
101
+ json do
102
+ map "child", to: :child
103
+ end
104
+ end
105
+
106
+ SOURCES = [Doc, Item, Entry, Circle, Shape, LmlOrderFixtures::Para,
107
+ LmlOrderFixtures::Emphasis, LmlOrderFixtures::Transport].freeze
108
+
109
+ module_function
110
+
111
+ # Neutral bundle (string keys) of the given source classes.
112
+ def bundle_for(*classes)
113
+ models = classes.to_h do |klass|
114
+ doc = Lutaml::Model::Lml::Exporter.project(klass)
115
+ ["#{doc['model']}@#{doc['register']}", doc]
116
+ end.sort.to_h
117
+ { "lml_bundle" => 1, "roots" => models.keys.sort, "models" => models, "value_types" => {},
118
+ "capabilities" => %w[json xml yaml] }
119
+ end
120
+
121
+ # The exporter does not project rule-level discriminators or
122
+ # child_mappings yet (see the card report), so specs author them.
123
+ def with_discriminator(bundle, id: "LmlMappingFixtures::Shape@default",
124
+ target: "LmlMappingFixtures::Circle@default")
125
+ copy = Marshal.load(Marshal.dump(bundle))
126
+ disc = { "attribute" => "kind", "cases" => { "circle" => target } }
127
+ mappings = copy["models"][id]["mappings"]
128
+ mappings["xml"]["rules"][0]["discriminator"] = disc
129
+ mappings["json"]["rules"][0]["discriminator"] = disc
130
+ copy
131
+ end
132
+
133
+ def with_child_mappings(bundle, id: "LmlMappingFixtures::Doc@default")
134
+ copy = Marshal.load(Marshal.dump(bundle))
135
+ rule = copy["models"][id]["mappings"]["json"]["rules"].find { |r| r["to"] == "entries" }
136
+ rule["child_mappings"] = { "name" => "key", "value" => "value" }
137
+ copy
138
+ end
139
+
140
+ # Fresh classes (attributes only) installed under +holder+, keyed by
141
+ # model id. +prefix+ lets the same bundle be built into two holders.
142
+ def fresh_classes(bundle, holder)
143
+ classes = bundle["models"].to_h do |id, doc|
144
+ klass = Class.new(Lutaml::Model::Serializable)
145
+ holder.const_set(doc["model"].split("::").last, klass)
146
+ [id, klass]
147
+ end
148
+ bundle["models"].each { |id, doc| declare_attributes(classes[id], doc, classes) }
149
+ classes
150
+ end
151
+
152
+ def declare_attributes(klass, doc, classes)
153
+ doc["attributes"].each do |attr|
154
+ type = attr["type"]
155
+ type = type["kind"] == "model" ? classes.fetch(type["id"]) : type["name"].to_sym
156
+ options = {}
157
+ options[:required] = true if attr["required"]
158
+ options[:values] = attr["values"] if attr["values"]
159
+ options[:default] = attr["default"]["value"] if attr.dig("default", "present")
160
+ options[:collection] = collection(attr["collection"]) if attr["collection"]
161
+ klass.attribute(attr["name"].to_sym, type, **options)
162
+ end
163
+ end
164
+
165
+ def collection(spec)
166
+ return true if spec == true
167
+
168
+ spec["min"]..(spec["max"] || Float::INFINITY)
169
+ end
170
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Hand-authored contract (01) bundles for Materialization specs. String
4
+ # keys, as DocumentValidator emits them. No Ruby model constants exist
5
+ # for these ids: materialization must work from the data alone.
6
+ module LmlMaterializationFixtures
7
+ module_function
8
+
9
+ def string_type
10
+ { "kind" => "builtin", "name" => "string" }
11
+ end
12
+
13
+ def model_type(id)
14
+ { "kind" => "model", "id" => id }
15
+ end
16
+
17
+ def model(name, attributes, register: "default")
18
+ { "lml" => 2, "model" => name, "register" => register, "attributes" => attributes }
19
+ end
20
+
21
+ def bundle(models)
22
+ { "lml_bundle" => 1, "roots" => models.keys.sort, "models" => models,
23
+ "value_types" => {}, "capabilities" => [] }
24
+ end
25
+
26
+ # Order -> Line (collection) -> Order (cycle), Order has a self-tree.
27
+ def cyclic
28
+ order = "Shop::Order@default"
29
+ line = "Shop::Line@default"
30
+ bundle(
31
+ order => model("Shop::Order", [
32
+ { "name" => "id", "type" => string_type, "required" => true },
33
+ { "name" => "status", "type" => string_type, "values" => %w[open closed],
34
+ "default" => { "present" => true, "value" => "open" } },
35
+ { "name" => "lines", "type" => model_type(line),
36
+ "collection" => { "min" => 0, "max" => nil } },
37
+ { "name" => "parent", "type" => model_type(order) },
38
+ ]),
39
+ line => model("Shop::Line", [
40
+ { "name" => "sku", "type" => string_type },
41
+ { "name" => "order", "type" => model_type(order) },
42
+ { "name" => "qty", "type" => string_type,
43
+ "collection" => { "min" => 1, "max" => 3 } },
44
+ ]),
45
+ )
46
+ end
47
+
48
+ def with_value_type
49
+ bundle("Shop::Item@default" => model("Shop::Item", [
50
+ { "name" => "code", "type" => { "kind" => "value", "id" => "Shop::Sku" } },
51
+ ]))
52
+ end
53
+ end