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,421 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
4
+ require "lutaml/model"
5
+ require "json"
6
+ require "yaml"
7
+ require "digest"
8
+ require "rexml/document"
9
+ require "optparse"
10
+ require "fileutils"
11
+
12
+ # Ruby-authored preservation package; no dependency on another corpus index.
13
+ module LmlPreservation
14
+ GENERATION = "preservation-v1"
15
+ SOURCE_REVISION = "8a8ef040963b7965388b01b58af33af2b0995f91"
16
+ XML_PROFILE = "expanded-names-text-v1"
17
+
18
+ class Namespace < Lutaml::Xml::W3c::XmlNamespace
19
+ uri "urn:lml:preservation"
20
+ prefix_default "p"
21
+ element_form_default :qualified
22
+ end
23
+
24
+ class Namespaces < Lutaml::Model::Serializable
25
+ attribute :qualified, :string
26
+ attribute :plain, :string
27
+ xml do
28
+ root "record"
29
+ namespace Namespace
30
+ map_element "qualified", to: :qualified
31
+ map_element "plain", to: :plain, form: :unqualified
32
+ end
33
+ end
34
+
35
+ class Mixed < Lutaml::Model::Serializable
36
+ attribute :text, :string, collection: true
37
+ attribute :em, :string, collection: true
38
+ xml do
39
+ root "mixed"
40
+ mixed_content
41
+ map_content to: :text
42
+ map_element "em", to: :em
43
+ end
44
+ end
45
+
46
+ class Ordered < Lutaml::Model::Serializable
47
+ attribute :a, :string, collection: true
48
+ attribute :b, :string, collection: true
49
+ xml do
50
+ root "ordered", ordered: true
51
+ map_element "a", to: :a
52
+ map_element "b", to: :b
53
+ end
54
+ end
55
+
56
+ class Cdata < Lutaml::Model::Serializable
57
+ attribute :text, :string
58
+ xml do
59
+ root "cdata"
60
+ map_content to: :text, cdata: true
61
+ end
62
+ end
63
+
64
+ class States < Lutaml::Model::Serializable
65
+ attribute :value, :string
66
+ attribute :fallback, :string, default: "default"
67
+ attribute :items, :string, collection: true, initialize_empty: true
68
+ json do
69
+ map "value", to: :value, render_nil: true, render_empty: true
70
+ map "fallback", to: :fallback, render_default: true
71
+ map "items", to: :items, render_empty: true
72
+ end
73
+ end
74
+
75
+ class Bounded < Lutaml::Model::Serializable
76
+ attribute :items, :string, collection: 1..2
77
+ json { map "items", to: :items }
78
+ end
79
+
80
+ class TypedEnum < Lutaml::Model::Serializable
81
+ attribute :number, :integer, values: [1, 2]
82
+ attribute :flag, :boolean, values: [true, false]
83
+ json do
84
+ map "number", to: :number
85
+ map "flag", to: :flag
86
+ end
87
+ end
88
+
89
+ class Source < Lutaml::Model::Serializable
90
+ attribute :name, :string
91
+ json { map "name", to: :name }
92
+ end
93
+
94
+ Lutaml::Model::GlobalRegister.register(Lutaml::Model::Register.new(:oracle))
95
+
96
+ class Imported < Lutaml::Model::Serializable
97
+ import_model Source, :oracle
98
+ end
99
+
100
+ module_function
101
+
102
+ def canonical(value)
103
+ case value
104
+ when Hash then value.keys.sort.to_h { |key| [key, canonical(value.fetch(key))] }
105
+ when Array then value.map { |item| canonical(item) }
106
+ else value
107
+ end
108
+ end
109
+
110
+ def json(value)
111
+ JSON.generate(canonical(value))
112
+ end
113
+
114
+ def digest(bytes)
115
+ "sha256:#{Digest::SHA256.hexdigest(bytes)}"
116
+ end
117
+
118
+ def inputs
119
+ namespace_input = '<p:record xmlns:p="urn:lml:preservation"><p:qualified>Q</p:qualified><plain>P</plain></p:record>'
120
+ mixed_input = "<mixed>before<em>one</em>between<em>two</em>after</mixed>"
121
+ [
122
+ ["namespace-tree", Namespaces, "xml", "xml_tree", namespace_input],
123
+ ["namespace-bytes", Namespaces, "xml", "bytes", namespace_input],
124
+ ["mixed-order-tree", Mixed, "xml", "xml_tree", mixed_input],
125
+ ["mixed-order-bytes", Mixed, "xml", "bytes", mixed_input],
126
+ ["ordered-tree", Ordered, "xml", "xml_tree", "<ordered><b>one</b><a>two</a><b>three</b></ordered>"],
127
+ ["ordered-bytes", Ordered, "xml", "bytes", "<ordered><b>one</b><a>two</a><b>three</b></ordered>"],
128
+ ["cdata-bytes", Cdata, "xml", "bytes", "<cdata><![CDATA[a < b & c]]></cdata>"],
129
+ ["state-absent", States, "json", "data", "{}"],
130
+ ["state-null", States, "json", "data", '{"value":null,"fallback":null}'],
131
+ ["state-empty", States, "json", "data", '{"value":"","items":[]}'],
132
+ ["state-explicit-default", States, "json", "data", '{"fallback":"default"}'],
133
+ ["cardinality-valid", Bounded, "json", "data", '{"items":["one","two"]}'],
134
+ ["cardinality-invalid", Bounded, "json", "data", '{"items":["one","two","three"]}'],
135
+ ["cardinality-missing", Bounded, "json", "data", '{"items":[]}'],
136
+ ["typed-enum-valid", TypedEnum, "json", "data", '{"number":2,"flag":false}'],
137
+ ["typed-enum-invalid", TypedEnum, "json", "data", '{"number":3,"flag":true}'],
138
+ ["imported-context", Imported, "json", "data", '{"name":"contextual"}'],
139
+ ]
140
+ end
141
+
142
+ def model_id(klass)
143
+ "#{klass.name}@#{klass == Imported ? 'oracle' : 'default'}"
144
+ end
145
+
146
+ def neutral(value)
147
+ case value
148
+ when Lutaml::Model::UninitializedClass then { "state" => "absent" }
149
+ when Integer then value.to_s
150
+ when Array then value.map { |item| neutral(item) }
151
+ else value
152
+ end
153
+ end
154
+
155
+ def register_for(klass)
156
+ return :default unless klass == Imported
157
+
158
+ # GlobalContext.reset! removes contexts while GlobalRegister keeps registers.
159
+ if !Lutaml::Model::GlobalContext.context(:oracle) || !Lutaml::Model::GlobalRegister.lookup(:oracle)
160
+ Lutaml::Model::GlobalRegister.register(Lutaml::Model::Register.new(:oracle))
161
+ end
162
+ :oracle
163
+ end
164
+
165
+ def attributes_for(klass)
166
+ klass.attributes(register_for(klass))
167
+ end
168
+
169
+ def normalized(record, klass, input, format)
170
+ data = attributes_for(klass).keys.to_h { |name| [name.to_s, neutral(record.public_send(name))] }
171
+ if klass == States
172
+ authored = JSON.parse(input)
173
+ data = data.to_h do |name, value|
174
+ state = if authored.key?(name)
175
+ "present"
176
+ elsif name == "fallback"
177
+ "default"
178
+ elsif name == "items"
179
+ "initialized_empty"
180
+ else
181
+ "absent"
182
+ end
183
+ [name, { "state" => state, "value" => value }]
184
+ end
185
+ elsif [Mixed, Ordered].include?(klass) && format == "xml"
186
+ # Preserve real per-instance order through the Ruby serialized tree, and
187
+ # expose portable member/index tokens alongside the parsed member values.
188
+ counters = Hash.new(0)
189
+ data["__lml_content"] = record.element_order.map do |node|
190
+ member = node.element? ? node.name : "text"
191
+ kind = node.element? ? "element" : node.node_type.to_s
192
+ token = { "kind" => kind, "attribute" => member, "index" => counters[member] }
193
+ counters[member] += 1
194
+ token
195
+ end
196
+ end
197
+ data
198
+ end
199
+
200
+ def execute(entry, klass)
201
+ format = entry.fetch("format")
202
+ register = register_for(klass)
203
+ Lutaml::Model::Config.with_adapter(xml: :nokogiri, json: :standard) do
204
+ record = klass.public_send("from_#{format}", entry.fetch("input"), register: register)
205
+ errors = record.validate(register: register)
206
+ if errors.any?
207
+ return invalid_result(klass, format, errors)
208
+ end
209
+
210
+ { "data" => normalized(record, klass, entry.fetch("input"), format),
211
+ "output" => record.public_send("to_#{format}", register: register) }
212
+ end
213
+ rescue Lutaml::Model::CollectionCountOutOfRangeError, Lutaml::Model::InvalidValueError => e
214
+ invalid_result(klass, format, [e])
215
+ end
216
+
217
+ def invalid_result(klass, format, errors)
218
+ { "error" => { "reason" => "invalid_document", "model_id" => model_id(klass),
219
+ "format" => format, "location" => "/input",
220
+ "ruby_errors" => errors.map { |error| error.class.name } } }
221
+ end
222
+
223
+ def cases
224
+ inputs.map do |id, klass, format, comparison, input|
225
+ entry = { "id" => id, "model_id" => model_id(klass), "lml_checksum" => checksum,
226
+ "format" => format, "input" => input, "comparison" => comparison }
227
+ result = execute(entry, klass)
228
+ entry.merge("expected_data" => result["data"], "expected_output" => result["output"],
229
+ "expected_error" => result["error"])
230
+ end
231
+ end
232
+
233
+ def schema
234
+ models = inputs.map { |entry| entry[1] }.uniq.to_h do |klass|
235
+ fields = attributes_for(klass).map do |name, attribute|
236
+ builtin = if klass == TypedEnum && name == :number
237
+ "integer"
238
+ elsif klass == TypedEnum && name == :flag
239
+ "boolean"
240
+ else
241
+ "string"
242
+ end
243
+ collection = if [Mixed, Ordered, Bounded].include?(klass) || name == :items
244
+ { "min" => klass == Bounded ? 1 : 0, "max" => klass == Bounded ? 2 : nil }
245
+ else
246
+ false
247
+ end
248
+ field = { "name" => name.to_s, "type" => { "kind" => "builtin", "name" => builtin },
249
+ "required" => false, "collection" => collection,
250
+ "default" => name == :fallback ? { "present" => true, "value" => "default" } : { "present" => false },
251
+ "initialize_empty" => klass == States && name == :items }
252
+ field["values"] = attribute.options[:values] if klass == TypedEnum
253
+ field
254
+ end
255
+ document = { "lml" => 2, "model" => klass.name, "register" => klass == Imported ? "oracle" : "default",
256
+ "attributes" => fields, "mappings" => schema_mappings(klass, fields) }
257
+ document["ordered_content"] = { "member" => "__lml_content", "version" => 1 } if [Mixed, Ordered].include?(klass)
258
+ [model_id(klass), document]
259
+ end
260
+ { "lml_bundle" => 1, "roots" => models.keys.sort, "models" => models,
261
+ "value_types" => {}, "capabilities" => %w[json xml],
262
+ "provenance" => { "source_revision" => SOURCE_REVISION } }
263
+ end
264
+
265
+ def schema_mappings(klass, fields)
266
+ if [States, Bounded, TypedEnum, Imported].include?(klass)
267
+ return { "json" => { "rules" => fields.map do |field|
268
+ rule = { "key" => [field.fetch("name")], "to" => field.fetch("name") }
269
+ if klass == States
270
+ rule["render"] = case field.fetch("name")
271
+ when "value" then { "nil" => true, "empty" => true, "default" => false }
272
+ when "fallback" then { "default" => true }
273
+ else { "empty" => true }
274
+ end
275
+ end
276
+ rule
277
+ end } }
278
+ end
279
+ root = { Namespaces => "record", Mixed => "mixed", Ordered => "ordered", Cdata => "cdata" }.fetch(klass)
280
+ rules = fields.map do |field|
281
+ name = field.fetch("name")
282
+ content = name == "text"
283
+ namespace = if klass == Namespaces
284
+ name == "plain" ? { "mode" => "unqualified" } : { "mode" => "inherit" }
285
+ else
286
+ { "mode" => "inherit" }
287
+ end
288
+ rule = { "kind" => content ? "content" : "element", "to" => name, "namespace" => namespace }
289
+ rule["name"] = [name] unless content
290
+ rule["cdata"] = true if klass == Cdata
291
+ rule
292
+ end
293
+ namespace = klass == Namespaces ? { "mode" => "uri", "uri" => "urn:lml:preservation", "prefix" => "p" } : { "mode" => "inherit" }
294
+ { "xml" => { "root" => { "element" => root }, "namespace" => namespace, "rules" => rules } }
295
+ end
296
+
297
+ def checksum
298
+ digest(json(schema.except("provenance")))
299
+ end
300
+
301
+ def source_expectations
302
+ %w[mixed_content_spec.rb cdata_spec.rb collection_spec.rb register_spec.rb].map do |name|
303
+ path = "spec/lutaml/model/#{name}"
304
+ # Pin repository source text independent of Windows CRLF checkout conversion.
305
+ { "path" => path, "sha256" => digest(File.binread(File.expand_path("../../#{path}", __dir__)).gsub("\r\n", "\n")) }
306
+ end
307
+ end
308
+
309
+ def files
310
+ index = { "corpus" => 1, "generation" => GENERATION, "source_revision" => SOURCE_REVISION,
311
+ "xml_canon_profile" => XML_PROFILE, "wire_profile" => "ruby-native",
312
+ "data_profile" => "exact-values-decimal-v1",
313
+ "source_expectations" => source_expectations, "cases" => cases }
314
+ payload = { "index.json" => json(index), "schema.json" => json(schema), "schema.yaml" => YAML.safe_dump(canonical(schema)) }
315
+ manifest = { "manifest" => 1, "lml_checksum" => checksum, "schema_version" => 2,
316
+ "generator_version" => "ruby-preservation-oracle-v1", "target" => "ruby", "capabilities" => %w[json xml],
317
+ "corpora" => [{ "generation" => GENERATION, "sha256" => digest(payload.fetch("index.json")) }],
318
+ "files" => payload.sort.map { |path, bytes| { "path" => path, "sha256" => digest(bytes) } },
319
+ "dependencies" => [{ "name" => "lutaml-model", "revision" => SOURCE_REVISION,
320
+ "sha256" => digest(File.binread(File.expand_path("../../lutaml-model.gemspec", __dir__)).gsub("\r\n", "\n")) }] }
321
+ payload.merge("manifest.json" => json(manifest))
322
+ end
323
+
324
+ def xml_tree(bytes)
325
+ raise ArgumentError, "forbidden XML declaration" if bytes.match?(/<!DOCTYPE|<!ENTITY|<\?(?!xml\s)/i)
326
+
327
+ element = REXML::Document.new(bytes).root
328
+ xml_element(element)
329
+ end
330
+
331
+ def xml_element(element)
332
+ attrs = element.attributes.each_attribute.filter_map do |attribute|
333
+ next if attribute.expanded_name == "xmlns" || attribute.prefix == "xmlns"
334
+
335
+ [[attribute.namespace, attribute.name], attribute.value]
336
+ end.sort
337
+ children = element.children.each_with_object([]) do |child, result|
338
+ if child.is_a?(REXML::Element)
339
+ result << xml_element(child)
340
+ elsif child.is_a?(REXML::Text)
341
+ result.last.is_a?(String) ? result[-1] += child.value : result << child.value
342
+ else
343
+ raise ArgumentError, "unsupported XML node"
344
+ end
345
+ end
346
+ [[element.namespace, element.name], attrs, children]
347
+ end
348
+
349
+ def compare(entry, result)
350
+ return entry["expected_error"] == result["error"] ? nil : "error mismatch" if entry["expected_error"]
351
+ return "typed data mismatch" unless entry.fetch("expected_data") == result["data"]
352
+
353
+ expected = entry.fetch("expected_output")
354
+ actual = result.fetch("output")
355
+ equal = case entry.fetch("comparison")
356
+ when "bytes" then expected.b == actual.b
357
+ when "xml_tree" then xml_tree(expected) == xml_tree(actual)
358
+ when "data" then JSON.parse(expected) == JSON.parse(actual)
359
+ else false
360
+ end
361
+ equal ? nil : "output mismatch"
362
+ end
363
+
364
+ def check(directory)
365
+ index = JSON.parse(File.read(File.join(directory, "index.json")))
366
+ raise ArgumentError, "invalid index" unless index.is_a?(Hash) && index["cases"].is_a?(Array)
367
+
368
+ expected = cases.to_h { |entry| [entry.fetch("id"), entry] }
369
+ classes = inputs.to_h { |entry| [entry.first, entry[1]] }
370
+ executed = []
371
+ errors = []
372
+ index.fetch("cases").each do |entry|
373
+ unless entry.is_a?(Hash) && entry["id"].is_a?(String)
374
+ errors << { "id" => "package/cases", "error" => "invalid case" }
375
+ next
376
+ end
377
+ id = entry.fetch("id")
378
+ error = if !expected.key?(id)
379
+ "unknown case"
380
+ elsif executed.include?(id)
381
+ "duplicate case"
382
+ else
383
+ executed << id
384
+ entry == expected.fetch(id) ? compare(entry, execute(entry, classes.fetch(id))) : "changed case"
385
+ end
386
+ errors << { "id" => id, "error" => error } if error
387
+ end
388
+ (expected.keys - executed).each { |id| errors << { "id" => id, "error" => "missing case" } }
389
+ files.each do |path, bytes|
390
+ full_path = File.join(directory, path)
391
+ errors << { "id" => "package/#{path}", "error" => "missing or changed file" } unless File.file?(full_path) && File.binread(full_path) == bytes.b
392
+ end
393
+ (Dir.children(directory) - files.keys).each { |path| errors << { "id" => "package/#{path}", "error" => "unknown file" } }
394
+ unless YAML.safe_load_file(File.join(directory, "schema.yaml"), aliases: false) == JSON.parse(File.read(File.join(directory, "schema.json")))
395
+ errors << { "id" => "package/schema.yaml", "error" => "schema equivalence mismatch" }
396
+ end
397
+ { "expected_ids" => expected.keys, "executed_ids" => executed, "expected_count" => expected.size,
398
+ "executed_count" => executed.size, "errors" => errors }
399
+ rescue Errno::ENOENT, JSON::ParserError, Psych::Exception, KeyError, TypeError, ArgumentError => e
400
+ { "expected_ids" => inputs.map(&:first), "executed_ids" => [], "expected_count" => inputs.size,
401
+ "executed_count" => 0, "errors" => [{ "id" => "package", "error" => e.class.name }] +
402
+ inputs.map { |entry| { "id" => entry.first, "error" => "missing case" } } }
403
+ end
404
+
405
+ def main
406
+ options = { check: false, package: File.expand_path("../../spec/fixtures/lml_conformance/preservation", __dir__) }
407
+ OptionParser.new do |parser|
408
+ parser.on("--check") { options[:check] = true }
409
+ parser.on("--package DIRECTORY") { |path| options[:package] = path }
410
+ end.parse!
411
+ unless options[:check]
412
+ FileUtils.mkdir_p(options[:package])
413
+ files.each { |path, bytes| File.binwrite(File.join(options[:package], path), bytes) }
414
+ end
415
+ report = check(options[:package])
416
+ puts json(report)
417
+ report.fetch("errors").empty? ? 0 : 1
418
+ end
419
+ end
420
+
421
+ exit LmlPreservation.main if $PROGRAM_NAME == __FILE__
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require_relative "../../lib/lutaml/model/lml/artifact_verifier"
5
+
6
+ if $PROGRAM_NAME == __FILE__
7
+ options = {}
8
+ parser = OptionParser.new do |cli|
9
+ cli.banner = "Usage: verify_artifacts.rb --manifest FILE --expected-sha256 DIGEST"
10
+ cli.on("--manifest FILE") { |value| options[:manifest] = value }
11
+ cli.on("--expected-sha256 DIGEST") { |value| options[:expected_sha256] = value }
12
+ cli.on("--root DIRECTORY") { |value| options[:root] = value }
13
+ end
14
+ begin
15
+ parser.parse!
16
+ raise OptionParser::MissingArgument, "--manifest" unless options[:manifest]
17
+ raise OptionParser::MissingArgument, "--expected-sha256" unless options[:expected_sha256]
18
+
19
+ manifest = JSON.parse(File.read(options[:manifest], encoding: "UTF-8"))
20
+ root = options[:root] || File.dirname(File.expand_path(options[:manifest]))
21
+ verifier = Lutaml::Model::Lml::ArtifactVerifier.new(root: root,
22
+ expected_sha256: options[:expected_sha256])
23
+ verifier.verify(manifest)
24
+ puts JSON.generate("verified" => true, "target" => manifest["target"],
25
+ "files" => manifest["files"].length)
26
+ rescue OptionParser::ParseError, JSON::ParserError, SystemCallError,
27
+ Lutaml::Model::Lml::ArtifactVerifier::Error => e
28
+ warn JSON.generate("verified" => false, "error" => e.message.scrub)
29
+ exit 1
30
+ end
31
+ end
@@ -0,0 +1 @@
1
+ {"capabilities":["json"],"lml_bundle":1,"models":{"Fixture::Record@default":{"attributes":[],"lml":2,"mappings":{},"model":"Fixture::Record","register":"default"}},"roots":["Fixture::Record@default"],"value_types":{}}
@@ -0,0 +1 @@
1
+ {"capabilities":["json"],"corpora":[{"generation":"fixture-1","sha256":"sha256:114304fa714674f948f4dbb8e903424bf9e9a0d44acddc5a6f81e13f6318f612"}],"dependencies":[{"name":"fixture-dependency","revision":"fixture-v1","sha256":"sha256:c670d63b817369af3647eecbe067fb48b4d5ae6e7867dd25f425e1dbe570cb81"}],"files":[{"path":"model.json","sha256":"sha256:dc9cd182d5db91c9e2e3f36c0d114c1fccb980412cf3535e12ab5f1a91fd7ec5"}],"generator_version":"fixture-v1","lml_checksum":"sha256:dc9cd182d5db91c9e2e3f36c0d114c1fccb980412cf3535e12ab5f1a91fd7ec5","manifest":1,"schema_version":2,"target":"ruby"}
@@ -0,0 +1 @@
1
+ sha256:6c8a922b3cf88601400dc07905925157cd9fb85b7ca3e9d682bb672c372b9324
@@ -0,0 +1 @@
1
+ {"cases":[{"comparison":"data","data_profile":"exact-values-decimal-v1","expected_data":{"count":"42","enabled":false,"label":"café & tea"},"expected_error":null,"expected_output":"{\"label\":\"café & tea\",\"count\":42,\"enabled\":false}","format":"json","id":"core-json","input":"{\"label\":\"café & tea\",\"count\":42,\"enabled\":false}","lml_checksum":"sha256:a7fac433ca7be94a6a66f6b8d0dab8134747860cf8076dffc4aaa92a24d1cb34","model_id":"LmlConformance::Record@default","wire_profile":"ruby-native-v1"},{"comparison":"xml_tree","data_profile":"exact-values-decimal-v1","expected_data":{"count":"42","enabled":false,"label":"café & tea"},"expected_error":null,"expected_output":"<record enabled=\"false\">\n <label>café &amp; tea</label>\n <count>42</count>\n</record>","format":"xml","id":"core-xml-tree","input":"<record enabled=\"false\"><label>café &amp; tea</label><count>42</count></record>","lml_checksum":"sha256:a7fac433ca7be94a6a66f6b8d0dab8134747860cf8076dffc4aaa92a24d1cb34","model_id":"LmlConformance::Record@default","wire_profile":"ruby-native-v1"},{"comparison":"bytes","data_profile":"exact-values-decimal-v1","expected_data":{"count":"7","enabled":true,"label":"plain"},"expected_error":null,"expected_output":"<record enabled=\"true\">\n <label>plain</label>\n <count>7</count>\n</record>","format":"xml","id":"core-xml-bytes","input":"<record enabled=\"true\"><label>plain</label><count>7</count></record>","lml_checksum":"sha256:a7fac433ca7be94a6a66f6b8d0dab8134747860cf8076dffc4aaa92a24d1cb34","model_id":"LmlConformance::Record@default","wire_profile":"ruby-native-v1"},{"comparison":"data","data_profile":"exact-values-decimal-v1","expected_data":null,"expected_error":{"format":"json","location":"/input","model_id":"LmlConformance::Record@default","reason":"invalid_document"},"expected_output":null,"format":"json","id":"core-json-invalid","input":"{","lml_checksum":"sha256:a7fac433ca7be94a6a66f6b8d0dab8134747860cf8076dffc4aaa92a24d1cb34","model_id":"LmlConformance::Record@default","wire_profile":"ruby-native-v1"},{"comparison":"data","data_profile":"exact-values-decimal-v1","expected_data":{"count":"9007199254740993","enabled":false,"label":"beyond binary64"},"expected_error":null,"expected_output":"{\"label\":\"beyond binary64\",\"count\":\"9007199254740993\",\"enabled\":false}","format":"json","id":"core-json-portable-exact","input":"{\"label\":\"beyond binary64\",\"count\":\"9007199254740993\",\"enabled\":false}","lml_checksum":"sha256:a7fac433ca7be94a6a66f6b8d0dab8134747860cf8076dffc4aaa92a24d1cb34","model_id":"LmlConformance::Record@default","wire_profile":"portable-exact-json-v1"}],"corpus":1,"generation":"core-v2","profiles":{"portable-exact-json-v1":{"required_case_ids":["core-json-portable-exact"]},"ruby-native-v1":{"required_case_ids":["core-json","core-xml-tree","core-xml-bytes","core-json-invalid"]}},"source_revision":"8a8ef040963b7965388b01b58af33af2b0995f91","target_profiles":{"ruby":["ruby-native-v1","portable-exact-json-v1"],"rust":["portable-exact-json-v1"],"ts":["portable-exact-json-v1"]},"xml_canon_profile":"expanded-names-text-v1"}
@@ -0,0 +1 @@
1
+ {"capabilities":["json","xml"],"corpora":[{"generation":"core-v2","sha256":"sha256:fd1de9747a78a71d737a4076ebfb0cf4a561d96dba9156a43afb5a337ebad0d2"}],"dependencies":[{"name":"lutaml-model","revision":"8a8ef040963b7965388b01b58af33af2b0995f91","sha256":"sha256:14c85f76a3cf2ebb18aa3c97186840897bf2147c886818a5c73670d3a6db170a"}],"files":[{"path":"index.json","sha256":"sha256:fd1de9747a78a71d737a4076ebfb0cf4a561d96dba9156a43afb5a337ebad0d2"},{"path":"schema.json","sha256":"sha256:ce5cc1c61e4a97bc71b2683b71e56c5488dbd3f41fdfa2802c01c4c2cd34fa1e"},{"path":"schema.yaml","sha256":"sha256:5c6f2cf9ddb907bd2aaff813711a6ed0d35e0ff1563d5b4f893301f803dac8e0"}],"generator_version":"ruby-core-oracle-v2","lml_checksum":"sha256:a7fac433ca7be94a6a66f6b8d0dab8134747860cf8076dffc4aaa92a24d1cb34","manifest":1,"schema_version":2,"target":"ruby"}
@@ -0,0 +1 @@
1
+ {"capabilities":["json","xml"],"lml_bundle":1,"models":{"LmlConformance::Record@default":{"attributes":[{"collection":false,"default":{"present":false},"initialize_empty":false,"name":"label","required":false,"type":{"kind":"builtin","name":"string"}},{"collection":false,"default":{"present":false},"initialize_empty":false,"name":"count","required":false,"type":{"kind":"builtin","name":"integer"}},{"collection":false,"default":{"present":false},"initialize_empty":false,"name":"enabled","required":false,"type":{"kind":"builtin","name":"boolean"}}],"lml":2,"mappings":{"json":{"rules":[{"key":["label"],"to":"label"},{"key":["count"],"to":"count"},{"key":["enabled"],"to":"enabled"}]},"xml":{"namespace":{"mode":"inherit"},"root":{"element":"record"},"rules":[{"kind":"attribute","name":["enabled"],"namespace":{"mode":"inherit"},"to":"enabled"},{"kind":"element","name":["label"],"namespace":{"mode":"inherit"},"to":"label"},{"kind":"element","name":["count"],"namespace":{"mode":"inherit"},"to":"count"}]}},"model":"LmlConformance::Record","register":"default"}},"provenance":{"source_revision":"8a8ef040963b7965388b01b58af33af2b0995f91"},"roots":["LmlConformance::Record@default"],"value_types":{}}
@@ -0,0 +1,79 @@
1
+ ---
2
+ capabilities:
3
+ - json
4
+ - xml
5
+ lml_bundle: 1
6
+ models:
7
+ LmlConformance::Record@default:
8
+ attributes:
9
+ - collection: false
10
+ default:
11
+ present: false
12
+ initialize_empty: false
13
+ name: label
14
+ required: false
15
+ type:
16
+ kind: builtin
17
+ name: string
18
+ - collection: false
19
+ default:
20
+ present: false
21
+ initialize_empty: false
22
+ name: count
23
+ required: false
24
+ type:
25
+ kind: builtin
26
+ name: integer
27
+ - collection: false
28
+ default:
29
+ present: false
30
+ initialize_empty: false
31
+ name: enabled
32
+ required: false
33
+ type:
34
+ kind: builtin
35
+ name: boolean
36
+ lml: 2
37
+ mappings:
38
+ json:
39
+ rules:
40
+ - key:
41
+ - label
42
+ to: label
43
+ - key:
44
+ - count
45
+ to: count
46
+ - key:
47
+ - enabled
48
+ to: enabled
49
+ xml:
50
+ namespace:
51
+ mode: inherit
52
+ root:
53
+ element: record
54
+ rules:
55
+ - kind: attribute
56
+ name:
57
+ - enabled
58
+ namespace:
59
+ mode: inherit
60
+ to: enabled
61
+ - kind: element
62
+ name:
63
+ - label
64
+ namespace:
65
+ mode: inherit
66
+ to: label
67
+ - kind: element
68
+ name:
69
+ - count
70
+ namespace:
71
+ mode: inherit
72
+ to: count
73
+ model: LmlConformance::Record
74
+ register: default
75
+ provenance:
76
+ source_revision: 8a8ef040963b7965388b01b58af33af2b0995f91
77
+ roots:
78
+ - LmlConformance::Record@default
79
+ value_types: {}
@@ -0,0 +1,60 @@
1
+ # rubocop:disable all
2
+ # Generates float_vectors.json: IEEE-754 doubles with the LML `float` wire
3
+ # string Ruby's Lutaml::Model::Lml::DocumentValidator.float_wire produces.
4
+ # Ruby owns this corpus; the TS and Rust ports must reproduce every wire.
5
+ # ruby -Ilib spec/fixtures/lml_conformance/float_vectors.generator.rb OUT.json
6
+ require "json"
7
+ require "lutaml/model"
8
+ require "lutaml/model/lml"
9
+
10
+ DV = Lutaml::Model::Lml::DocumentValidator
11
+
12
+ def from_bits(bits) = [bits].pack("Q>").unpack1("G")
13
+ def bits_of(float) = [float].pack("G").unpack1("Q>")
14
+
15
+ values = []
16
+ add = ->(f) { values << f if f.finite? }
17
+
18
+ # fixed landmarks
19
+ [0.0, -0.0, 1.0, -1.0, 0.5, 0.1, 0.2, 0.3, 1.5, 100.0, 123456.789,
20
+ 1e14, 1e15, 1e16, 1e17, 1e21, 1e22, 1e100, 1e300, 1e-4, 1e-5, 1e-6, 1e-10, 1e-100,
21
+ 9.999999999999998e14, 9.999999999999999e14, 1234567890123456.0, 1151450203773890.8,
22
+ 123456789012345.6, 9007199254740991.0, 9007199254740992.0, 0.00011, 0.000099999,
23
+ Float::MAX, -Float::MAX, Float::MIN, -Float::MIN, Float::EPSILON,
24
+ 5.0e-324, -5.0e-324, 2.2250738585072009e-308, 2.225073858507201e-308].each(&add)
25
+
26
+ # every notation boundary and its immediate neighbours (bit steps)
27
+ [1e-5, 1e-4, 1e15, 1e16, 1e-323, Float::MIN, Float::MAX, 1.0, 10.0, 1e22].each do |b|
28
+ (-20..20).each { |d| add.(from_bits(bits_of(b) + d)); add.(-from_bits(bits_of(b) + d)) }
29
+ end
30
+
31
+ # smallest/largest subnormals and the subnormal/normal seam
32
+ (1..32).each { |n| add.(from_bits(n)); add.(-from_bits(n)) }
33
+ (-16..16).each { |d| add.(from_bits(0x0010000000000000 + d)) }
34
+
35
+ # every power of ten and of two
36
+ (-323..308).each { |e| add.(Float("1e#{e}")) }
37
+ (-1074..1023).each { |e| add.(2.0**e) }
38
+
39
+ rng = Random.new(1033)
40
+ # [1e15, 1e16): 16 and 17 significant digits (integers, .5 and arbitrary fractions)
41
+ 300.times { add.(1e15 + rng.rand * 9e15) }
42
+ 200.times { add.((1e15 + rng.rand(9e15)).floor.to_f) }
43
+ 200.times { add.((1e15 + rng.rand(9e15)).floor + 0.5) }
44
+ 200.times { add.((1e15 + rng.rand(9e14)).floor + rng.rand(10) / 10.0) }
45
+ # around the small boundary
46
+ 200.times { add.(1e-5 * (0.5 + rng.rand * 20)) }
47
+ 200.times { add.(1e-4 * (0.5 + rng.rand * 20)) }
48
+ # uniformly random bit patterns (all magnitudes, both signs)
49
+ 600.times { add.(from_bits(rng.rand(2**64))) }
50
+
51
+ seen = {}
52
+ out = values.filter_map do |f|
53
+ bits = format("%016x", bits_of(f))
54
+ next if seen[bits]
55
+
56
+ seen[bits] = true
57
+ { "bits" => bits, "wire" => DV.float_wire(f) }
58
+ end
59
+ File.binwrite(ARGV.fetch(0), JSON.pretty_generate(out) + "\n")
60
+ puts out.size