lutaml-model 0.8.203 → 0.8.205

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +5 -0
  3. data/.github/workflows/lml-conformance.yml +71 -0
  4. data/.gitignore +7 -0
  5. data/README.adoc +1 -0
  6. data/docs/_guides/lml-cross-language.adoc +128 -0
  7. data/docs/_guides/native-engines.adoc +10 -0
  8. data/docs/_references/lml-conformance.adoc +78 -0
  9. data/docs/_references/lml-contract.adoc +290 -0
  10. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  11. data/lib/lutaml/model/lml/bundle.rb +171 -0
  12. data/lib/lutaml/model/lml/document.rb +46 -10
  13. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  14. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  15. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  16. data/lib/lutaml/model/lml/exporter.rb +223 -87
  17. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  18. data/lib/lutaml/model/lml/importer.rb +180 -0
  19. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  20. data/lib/lutaml/model/lml/materialization.rb +257 -0
  21. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  22. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  23. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  24. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  25. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  26. data/lib/lutaml/model/lml.rb +61 -0
  27. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  28. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  29. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  30. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  31. data/lib/lutaml/model/sequence.rb +7 -0
  32. data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
  33. data/lib/lutaml/model/serialize/initialization.rb +2 -0
  34. data/lib/lutaml/model/version.rb +1 -1
  35. data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
  36. data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
  37. data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
  38. data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
  39. data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
  40. data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
  41. data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
  42. data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
  43. data/schema/lml/document.schema.json +179 -0
  44. data/scripts/lml/export_choices.rb +57 -0
  45. data/scripts/lml/export_conformance.rb +23 -0
  46. data/scripts/lml/export_mml.rb +253 -0
  47. data/scripts/lml/export_preservation.rb +421 -0
  48. data/scripts/lml/verify_artifacts.rb +31 -0
  49. data/spec/fixtures/lml_artifacts/model.json +1 -0
  50. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  51. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  52. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  53. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  54. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  55. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  56. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  57. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  58. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  59. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  60. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  61. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  62. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  63. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  64. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  65. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  66. data/spec/fixtures/lml_contract/bundle.json +81 -0
  67. data/spec/fixtures/lml_contract/manifest.json +30 -0
  68. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  69. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  70. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  71. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  72. data/spec/fixtures/lml_mml/inventory.json +1 -0
  73. data/spec/fixtures/lml_mml/manifest.json +1 -0
  74. data/spec/fixtures/lml_mml/oracle.json +1 -0
  75. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  76. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  77. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  78. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  79. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  80. data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
  81. data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
  82. data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
  83. data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
  84. data/spec/lutaml/model/lml_document_spec.rb +279 -0
  85. data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
  86. data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
  87. data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
  88. data/spec/lutaml/model/lml_importer_spec.rb +290 -0
  89. data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
  90. data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
  91. data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
  92. data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
  93. data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
  94. data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
  95. data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
  96. data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
  97. data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
  98. data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
  99. data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
  100. data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
  101. data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
  102. data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
  103. data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
  104. data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
  105. data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
  106. data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
  107. data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
  108. data/spec/lutaml/model/sequence_spec.rb +48 -0
  109. data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
  110. data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
  111. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  112. data/spec/support/lml_conformance.rb +292 -0
  113. data/spec/support/lml_corpus.rb +75 -0
  114. data/spec/support/lml_export_fixtures.rb +190 -0
  115. data/spec/support/lml_graph_fixtures.rb +108 -0
  116. data/spec/support/lml_import_fixtures.rb +52 -0
  117. data/spec/support/lml_mapping_fixtures.rb +170 -0
  118. data/spec/support/lml_materialization_fixtures.rb +53 -0
  119. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  120. data/spec/support/lml_order_fixtures.rb +151 -0
  121. metadata +94 -3
  122. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -0,0 +1,292 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+ require "json"
5
+ require "yaml"
6
+ require "digest"
7
+ require "rexml/document"
8
+
9
+ # Independent Ruby oracle and versioned replay protocol for the core package.
10
+ module LmlConformance
11
+ GENERATION = "core-v2"
12
+ SOURCE_REVISION = "8a8ef040963b7965388b01b58af33af2b0995f91"
13
+ XML_PROFILE = "expanded-names-text-v1"
14
+ NATIVE_PROFILE = "ruby-native-v1"
15
+ PORTABLE_PROFILE = "portable-exact-json-v1"
16
+ MODEL_ID = "LmlConformance::Record@default"
17
+
18
+ class Record < Lutaml::Model::Serializable
19
+ attribute :label, :string
20
+ attribute :count, :integer
21
+ attribute :enabled, :boolean
22
+
23
+ xml do
24
+ root "record"
25
+ map_attribute "enabled", to: :enabled
26
+ map_element "label", to: :label
27
+ map_element "count", to: :count
28
+ end
29
+
30
+ json do
31
+ map "label", to: :label
32
+ map "count", to: :count
33
+ map "enabled", to: :enabled
34
+ end
35
+ end
36
+
37
+ module_function
38
+
39
+ def canonical(value)
40
+ case value
41
+ when Hash then value.keys.sort.to_h { |key| [key, canonical(value.fetch(key))] }
42
+ when Array then value.map { |item| canonical(item) }
43
+ else value
44
+ end
45
+ end
46
+
47
+ def json(value)
48
+ JSON.generate(canonical(value))
49
+ end
50
+
51
+ def digest(bytes)
52
+ "sha256:#{Digest::SHA256.hexdigest(bytes)}"
53
+ end
54
+
55
+ # Contract-authored v2 schema; does not depend on the parallel exporter work.
56
+ def schema
57
+ fields = { "label" => "string", "count" => "integer", "enabled" => "boolean" }
58
+ {
59
+ "lml_bundle" => 1, "roots" => [MODEL_ID], "value_types" => {},
60
+ "capabilities" => %w[json xml],
61
+ "provenance" => { "source_revision" => SOURCE_REVISION },
62
+ "models" => { MODEL_ID => {
63
+ "lml" => 2, "model" => "LmlConformance::Record", "register" => "default",
64
+ "attributes" => fields.map do |name, type|
65
+ { "name" => name, "type" => { "kind" => "builtin", "name" => type },
66
+ "required" => false, "collection" => false,
67
+ "default" => { "present" => false }, "initialize_empty" => false }
68
+ end,
69
+ "mappings" => {
70
+ "json" => { "rules" => fields.keys.map { |name| { "key" => [name], "to" => name } } },
71
+ "xml" => { "root" => { "element" => "record" },
72
+ "namespace" => { "mode" => "inherit" },
73
+ "rules" => %w[enabled label count].map do |name|
74
+ { "kind" => name == "enabled" ? "attribute" : "element",
75
+ "name" => [name], "to" => name,
76
+ "namespace" => { "mode" => "inherit" } }
77
+ end },
78
+ }
79
+ } }
80
+ }
81
+ end
82
+
83
+ def schema_checksum
84
+ digest(json(schema.except("provenance")))
85
+ end
86
+
87
+ def normalized(record)
88
+ { "label" => record.label, "count" => record.count.to_s, "enabled" => record.enabled }
89
+ end
90
+
91
+ def case_inputs
92
+ [
93
+ ["core-json", "json", "data", '{"label":"café & tea","count":42,"enabled":false}'],
94
+ ["core-xml-tree", "xml", "xml_tree", '<record enabled="false"><label>café &amp; tea</label><count>42</count></record>'],
95
+ ["core-xml-bytes", "xml", "bytes", '<record enabled="true"><label>plain</label><count>7</count></record>'],
96
+ ["core-json-invalid", "json", "data", "{"],
97
+ ["core-json-portable-exact", "json", "data", '{"label":"beyond binary64","count":"9007199254740993","enabled":false}', PORTABLE_PROFILE],
98
+ ]
99
+ end
100
+
101
+ def execute(entry)
102
+ format = entry.fetch("format")
103
+ raise ArgumentError, "unsupported_format" unless %w[json xml].include?(format)
104
+
105
+ profile = entry.fetch("wire_profile")
106
+ raise ArgumentError, "unknown wire profile" unless [NATIVE_PROFILE, PORTABLE_PROFILE].include?(profile)
107
+ raise ArgumentError, "portable profile requires JSON" if profile == PORTABLE_PROFILE && format != "json"
108
+
109
+ input = entry.fetch("input")
110
+ if profile == PORTABLE_PROFILE
111
+ # This opt-in boundary is for the three-field Record oracle only. Native
112
+ # Ruby mappings stay unchanged; exact JSON integers never pass via Float.
113
+ parsed = JSON.parse(input)
114
+ count = parsed.fetch("count")
115
+ unless count.is_a?(String) && count.match?(/\A-?(?:0|[1-9][0-9]*)\z/)
116
+ raise ArgumentError, "portable integer requires decimal string"
117
+ end
118
+
119
+ # The native String cast accepts floating/exponential forms via Float.
120
+ # Supply an already exact Ruby Integer at this opt-in wire boundary.
121
+ parsed["count"] = Integer(count, 10)
122
+ input = JSON.generate(parsed)
123
+ end
124
+
125
+ Lutaml::Model::Config.with_adapter(xml: :nokogiri) do
126
+ record = Record.public_send("from_#{format}", input)
127
+ data = normalized(record)
128
+ output = profile == PORTABLE_PROFILE ? JSON.generate(data) : record.public_send("to_#{format}")
129
+ { "data" => data, "output" => output }
130
+ end
131
+ rescue JSON::ParserError, Lutaml::Model::InvalidFormatError
132
+ { "error" => { "reason" => "invalid_document", "model_id" => MODEL_ID,
133
+ "format" => format, "location" => "/input" } }
134
+ end
135
+
136
+ def oracle_cases
137
+ case_inputs.map do |id, format, comparison, input, profile|
138
+ entry = { "id" => id, "model_id" => MODEL_ID, "lml_checksum" => schema_checksum,
139
+ "format" => format, "input" => input, "comparison" => comparison,
140
+ "wire_profile" => profile || NATIVE_PROFILE, "data_profile" => "exact-values-decimal-v1" }
141
+ result = execute(entry)
142
+ entry.merge("expected_data" => result["data"], "expected_output" => result["output"],
143
+ "expected_error" => result["error"])
144
+ end
145
+ end
146
+
147
+ def profiles
148
+ [NATIVE_PROFILE, PORTABLE_PROFILE].to_h do |profile|
149
+ ids = case_inputs.filter_map { |entry| entry.first if (entry[4] || NATIVE_PROFILE) == profile }
150
+ [profile, { "required_case_ids" => ids }]
151
+ end
152
+ end
153
+
154
+ def target_profiles
155
+ { "ruby" => [NATIVE_PROFILE, PORTABLE_PROFILE], "ts" => [PORTABLE_PROFILE], "rust" => [PORTABLE_PROFILE] }
156
+ end
157
+
158
+ # Consumers report all required profile/case coverage, including omissions.
159
+ # Selecting another profile never silently certifies a target's required set.
160
+ def coverage(index, target:, executed_ids:)
161
+ required_profiles = index.fetch("target_profiles").fetch(target)
162
+ required_ids = required_profiles.flat_map { |profile| index.fetch("profiles").fetch(profile).fetch("required_case_ids") }
163
+ errors = (required_ids - executed_ids).map { |id| { "id" => id, "error" => "required case not executed" } }
164
+ { "required_profiles" => required_profiles, "required_ids" => required_ids, "errors" => errors }
165
+ end
166
+
167
+ def files
168
+ index = { "corpus" => 1, "generation" => GENERATION, "source_revision" => SOURCE_REVISION,
169
+ "xml_canon_profile" => XML_PROFILE, "profiles" => profiles,
170
+ "target_profiles" => target_profiles, "cases" => oracle_cases }
171
+ payload = { "index.json" => json(index), "schema.json" => json(schema),
172
+ "schema.yaml" => YAML.safe_dump(canonical(schema)) }
173
+ manifest = {
174
+ "manifest" => 1, "lml_checksum" => schema_checksum, "schema_version" => 2,
175
+ "generator_version" => "ruby-core-oracle-v2", "target" => "ruby",
176
+ "capabilities" => %w[json xml],
177
+ "corpora" => [{ "generation" => GENERATION, "sha256" => digest(payload.fetch("index.json")) }],
178
+ "files" => payload.sort.map { |path, bytes| { "path" => path, "sha256" => digest(bytes) } },
179
+ "dependencies" => [{ "name" => "lutaml-model", "revision" => SOURCE_REVISION,
180
+ # Pin repository source text independent of Windows CRLF checkout conversion.
181
+ "sha256" => digest(File.binread(File.expand_path("../../lutaml-model.gemspec", __dir__)).gsub("\r\n", "\n")) }]
182
+ }
183
+ payload.merge("manifest.json" => json(manifest))
184
+ end
185
+
186
+ # Expanded element/attribute names, sorted attributes, significant text and
187
+ # child order; namespace declarations and prefix spelling are not tree data.
188
+ # CDATA and text have the same tree meaning; bytes preserve lexical differences.
189
+ def xml_tree(bytes)
190
+ raise ArgumentError, "DTD and processing instructions are forbidden" if bytes.match?(/<!DOCTYPE|<!ENTITY|<\?(?!xml\s)/i)
191
+
192
+ root = REXML::Document.new(bytes).root
193
+ raise ArgumentError, "missing XML root" unless root
194
+
195
+ xml_element(root)
196
+ end
197
+
198
+ def xml_element(element)
199
+ attributes = element.attributes.each_attribute.filter_map do |attribute|
200
+ next if attribute.expanded_name == "xmlns" || attribute.prefix == "xmlns"
201
+
202
+ [[attribute.namespace, attribute.name], attribute.value]
203
+ end.sort
204
+ children = element.children.each_with_object([]) do |child, result|
205
+ if child.is_a?(REXML::Element)
206
+ result << xml_element(child)
207
+ elsif child.is_a?(REXML::Text)
208
+ if result.last.is_a?(String)
209
+ result[-1] += child.value
210
+ else
211
+ result << child.value
212
+ end
213
+ else
214
+ raise ArgumentError, "unsupported XML node"
215
+ end
216
+ end
217
+ [[element.namespace, element.name], attributes, children]
218
+ end
219
+
220
+ def compare(entry, actual)
221
+ return actual["error"] == entry["expected_error"] ? nil : "error mismatch" if entry["expected_error"]
222
+ return "unexpected error" if actual["error"]
223
+ return "typed data mismatch" unless actual["data"] == entry.fetch("expected_data")
224
+
225
+ expected = entry.fetch("expected_output")
226
+ output = actual.fetch("output")
227
+ equal = case entry.fetch("comparison")
228
+ when "data" then JSON.parse(output) == JSON.parse(expected)
229
+ when "xml_tree" then xml_tree(output) == xml_tree(expected)
230
+ when "bytes" then output.b == expected.b
231
+ else return "unknown comparison"
232
+ end
233
+ equal ? nil : "output mismatch"
234
+ end
235
+
236
+ def replay(index)
237
+ raise ArgumentError, "invalid index" unless index.is_a?(Hash)
238
+
239
+ expected = oracle_cases.to_h { |entry| [entry.fetch("id"), entry] }
240
+ entries = index.fetch("cases")
241
+ raise ArgumentError, "invalid cases" unless entries.is_a?(Array)
242
+
243
+ executed = []
244
+ errors = []
245
+ entries.each do |entry|
246
+ unless entry.is_a?(Hash) && entry["id"].is_a?(String)
247
+ errors << { "id" => "package/cases", "error" => "invalid case" }
248
+ next
249
+ end
250
+ id = entry.fetch("id")
251
+ if !expected.key?(id)
252
+ errors << { "id" => id, "error" => "unknown case" }
253
+ elsif executed.include?(id)
254
+ errors << { "id" => id, "error" => "duplicate case" }
255
+ else
256
+ executed << id
257
+ error = entry == expected.fetch(id) ? compare(entry, execute(entry)) : "changed case"
258
+ errors << { "id" => id, "error" => error } if error
259
+ end
260
+ end
261
+ (expected.keys - executed).each { |id| errors << { "id" => id, "error" => "missing case" } }
262
+ profile_coverage = coverage(index, target: "ruby", executed_ids: executed)
263
+ { "expected_ids" => expected.keys, "executed_ids" => executed,
264
+ "expected_count" => expected.size, "executed_count" => executed.size,
265
+ "required_profiles" => profile_coverage.fetch("required_profiles"),
266
+ "errors" => errors + profile_coverage.fetch("errors") }
267
+ end
268
+
269
+ def check(directory)
270
+ report = replay(JSON.parse(File.read(File.join(directory, "index.json"))))
271
+ files.each do |path, bytes|
272
+ full_path = File.join(directory, path)
273
+ next if File.file?(full_path) && File.binread(full_path) == bytes.b
274
+
275
+ report["errors"] << { "id" => "package/#{path}", "error" => "missing or changed file" }
276
+ end
277
+ (Dir.children(directory) - files.keys).sort.each do |path|
278
+ report["errors"] << { "id" => "package/#{path}", "error" => "unknown file" }
279
+ end
280
+ json_schema = JSON.parse(File.read(File.join(directory, "schema.json")))
281
+ yaml_schema = YAML.safe_load_file(File.join(directory, "schema.yaml"), aliases: false)
282
+ unless json_schema == yaml_schema
283
+ report["errors"] << { "id" => "package/schema.yaml", "error" => "schema equivalence mismatch" }
284
+ end
285
+ report
286
+ rescue Errno::ENOENT, JSON::ParserError, Psych::Exception, KeyError, TypeError, ArgumentError => e
287
+ { "expected_ids" => case_inputs.map(&:first), "executed_ids" => [],
288
+ "expected_count" => case_inputs.size, "executed_count" => 0,
289
+ "errors" => [{ "id" => "package", "error" => e.class.name }] +
290
+ case_inputs.map { |entry| { "id" => entry.first, "error" => "missing case" } } }
291
+ end
292
+ end
@@ -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