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.
- checksums.yaml +4 -4
- data/.gitattributes +5 -0
- data/.github/workflows/lml-conformance.yml +71 -0
- data/.gitignore +7 -0
- data/README.adoc +1 -0
- data/docs/_guides/lml-cross-language.adoc +128 -0
- data/docs/_guides/native-engines.adoc +10 -0
- data/docs/_references/lml-conformance.adoc +78 -0
- data/docs/_references/lml-contract.adoc +290 -0
- data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
- data/lib/lutaml/model/lml/bundle.rb +171 -0
- data/lib/lutaml/model/lml/document.rb +46 -10
- data/lib/lutaml/model/lml/document_validator.rb +840 -0
- data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
- data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
- data/lib/lutaml/model/lml/exporter.rb +223 -87
- data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
- data/lib/lutaml/model/lml/importer.rb +180 -0
- data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
- data/lib/lutaml/model/lml/materialization.rb +257 -0
- data/lib/lutaml/model/lml/ordered_content.rb +577 -0
- data/lib/lutaml/model/lml/portable_json.rb +385 -0
- data/lib/lutaml/model/lml/rejected_error.rb +64 -15
- data/lib/lutaml/model/lml/round_trip.rb +86 -0
- data/lib/lutaml/model/lml/value_declarations.rb +84 -0
- data/lib/lutaml/model/lml.rb +61 -0
- data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
- data/lib/lutaml/model/schema/registry_generator.rb +1 -1
- data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
- data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
- data/lib/lutaml/model/sequence.rb +7 -0
- data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
- data/lib/lutaml/model/serialize/initialization.rb +2 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
- data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
- data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
- data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
- data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
- data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
- data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
- data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
- data/schema/lml/document.schema.json +179 -0
- data/scripts/lml/export_choices.rb +57 -0
- data/scripts/lml/export_conformance.rb +23 -0
- data/scripts/lml/export_mml.rb +253 -0
- data/scripts/lml/export_preservation.rb +421 -0
- data/scripts/lml/verify_artifacts.rb +31 -0
- data/spec/fixtures/lml_artifacts/model.json +1 -0
- data/spec/fixtures/lml_artifacts/valid.json +1 -0
- data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
- data/spec/fixtures/lml_conformance/core/index.json +1 -0
- data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
- data/spec/fixtures/lml_conformance/core/schema.json +1 -0
- data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
- data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
- data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
- data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
- data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
- data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
- data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
- data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
- data/spec/fixtures/lml_contract/bundle.json +81 -0
- data/spec/fixtures/lml_contract/manifest.json +30 -0
- data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
- data/spec/fixtures/lml_contract/v1_unit.json +1 -0
- data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
- data/spec/fixtures/lml_contract/v2_node.json +32 -0
- data/spec/fixtures/lml_mml/inventory.json +1 -0
- data/spec/fixtures/lml_mml/manifest.json +1 -0
- data/spec/fixtures/lml_mml/oracle.json +1 -0
- data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
- data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
- data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
- data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
- data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
- data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
- data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
- data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
- data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
- data/spec/lutaml/model/lml_document_spec.rb +279 -0
- data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
- data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
- data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
- data/spec/lutaml/model/lml_importer_spec.rb +290 -0
- data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
- data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
- data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
- data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
- data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
- data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
- data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
- data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
- data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
- data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
- data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
- data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
- data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
- data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
- data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
- data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
- data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
- data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
- data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
- data/spec/lutaml/model/sequence_spec.rb +48 -0
- data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
- data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
- data/spec/support/lml_any_attribute_fixtures.rb +26 -0
- data/spec/support/lml_conformance.rb +292 -0
- data/spec/support/lml_corpus.rb +75 -0
- data/spec/support/lml_export_fixtures.rb +190 -0
- data/spec/support/lml_graph_fixtures.rb +108 -0
- data/spec/support/lml_import_fixtures.rb +52 -0
- data/spec/support/lml_mapping_fixtures.rb +170 -0
- data/spec/support/lml_materialization_fixtures.rb +53 -0
- data/spec/support/lml_model_choices_fixtures.rb +150 -0
- data/spec/support/lml_order_fixtures.rb +151 -0
- metadata +94 -3
- data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
# digest and pathname are required lazily: Opal boots this file and ships
|
|
5
|
+
# neither.
|
|
6
|
+
|
|
7
|
+
module Lutaml
|
|
8
|
+
module Model
|
|
9
|
+
module Lml
|
|
10
|
+
# Verifies a caller-pinned manifest before any target artifact is loaded.
|
|
11
|
+
class ArtifactVerifier
|
|
12
|
+
class Error < StandardError
|
|
13
|
+
attr_reader :reason, :location
|
|
14
|
+
|
|
15
|
+
def initialize(reason, location)
|
|
16
|
+
@reason = reason
|
|
17
|
+
@location = location
|
|
18
|
+
super("#{reason}: #{location}")
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
TARGETS = %w[ruby ts rust wasm].freeze
|
|
23
|
+
INVALID_PATH_PARTS = ["", ".", ".."].freeze
|
|
24
|
+
MAX_DEPTH = 100
|
|
25
|
+
DIGEST = /\Asha256:[0-9a-f]{64}\z/
|
|
26
|
+
|
|
27
|
+
def initialize(root:, expected_sha256:, schema_version: 2,
|
|
28
|
+
target: nil, lml_checksum: nil, generator_version: nil,
|
|
29
|
+
corpora: nil, dependencies: nil)
|
|
30
|
+
require "pathname"
|
|
31
|
+
@root = real_root(root)
|
|
32
|
+
@expected_sha256 = expected_sha256
|
|
33
|
+
@expected = {
|
|
34
|
+
"schema_version" => schema_version, "target" => target,
|
|
35
|
+
"lml_checksum" => lml_checksum, "generator_version" => generator_version,
|
|
36
|
+
"corpora" => corpora, "dependencies" => dependencies
|
|
37
|
+
}.compact
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def verify(manifest)
|
|
41
|
+
fail_with(:invalid_document, "/") unless manifest.is_a?(::Hash)
|
|
42
|
+
validate_header(manifest)
|
|
43
|
+
actual = self.class.checksum(manifest)
|
|
44
|
+
unless digest?(@expected_sha256) && actual == @expected_sha256
|
|
45
|
+
fail_with(:checksum_mismatch, "/manifest")
|
|
46
|
+
end
|
|
47
|
+
@expected.each do |key, value|
|
|
48
|
+
next if manifest[key] == value
|
|
49
|
+
|
|
50
|
+
fail_with(key == "schema_version" ? :unsupported_version : :checksum_mismatch,
|
|
51
|
+
"/#{key}")
|
|
52
|
+
end
|
|
53
|
+
verify_files(manifest["files"])
|
|
54
|
+
deep_freeze(manifest)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.checksum(value)
|
|
58
|
+
require "digest"
|
|
59
|
+
"sha256:#{Digest::SHA256.hexdigest(JSON.generate(canonical(value)))}"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.canonical(value, depth = 0)
|
|
63
|
+
if (value.is_a?(::Hash) || value.is_a?(::Array)) && depth >= MAX_DEPTH
|
|
64
|
+
raise Error.new(:invalid_document, "nesting deeper than #{MAX_DEPTH} levels")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
case value
|
|
68
|
+
when ::Hash
|
|
69
|
+
unless value.keys.all?(::String)
|
|
70
|
+
raise Error.new(:invalid_document, "object keys must be strings")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
value.keys.sort.to_h { |key| [key, canonical(value.fetch(key), depth + 1)] }
|
|
74
|
+
when ::Array then value.map { |item| canonical(item, depth + 1) }
|
|
75
|
+
when ::String
|
|
76
|
+
raise Error.new(:invalid_document, "invalid UTF-8") unless value.encoding == Encoding::UTF_8 && value.valid_encoding?
|
|
77
|
+
|
|
78
|
+
value
|
|
79
|
+
when ::Integer, ::TrueClass, ::FalseClass, ::NilClass then value
|
|
80
|
+
else
|
|
81
|
+
raise Error.new(:invalid_document, "non-neutral value")
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def validate_header(manifest)
|
|
88
|
+
fail_with(:unsupported_version, "/manifest") unless manifest["manifest"] == 1
|
|
89
|
+
unless manifest["schema_version"].is_a?(::Integer) && manifest["schema_version"] == @expected["schema_version"]
|
|
90
|
+
fail_with(:unsupported_version, "/schema_version")
|
|
91
|
+
end
|
|
92
|
+
fail_with(:invalid_document, "/target") unless TARGETS.include?(manifest["target"])
|
|
93
|
+
fail_with(:invalid_document, "/lml_checksum") unless digest?(manifest["lml_checksum"])
|
|
94
|
+
unless manifest["generator_version"].is_a?(::String) && !manifest["generator_version"].empty?
|
|
95
|
+
fail_with(:invalid_document, "/generator_version")
|
|
96
|
+
end
|
|
97
|
+
capabilities = manifest["capabilities"]
|
|
98
|
+
unless capabilities.is_a?(::Array) && capabilities.all?(::String) &&
|
|
99
|
+
capabilities == capabilities.sort.uniq
|
|
100
|
+
fail_with(:invalid_document, "/capabilities")
|
|
101
|
+
end
|
|
102
|
+
validate_pins(manifest["corpora"], "corpora", "generation")
|
|
103
|
+
validate_pins(manifest["dependencies"], "dependencies", "name")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def validate_pins(records, field, identity)
|
|
107
|
+
fail_with(:invalid_document, "/#{field}") unless records.is_a?(::Array)
|
|
108
|
+
names = records.map.with_index do |record, index|
|
|
109
|
+
unless record.is_a?(::Hash) && record[identity].is_a?(::String) &&
|
|
110
|
+
!record[identity].empty? && digest?(record["sha256"])
|
|
111
|
+
fail_with(:invalid_document, "/#{field}/#{index}")
|
|
112
|
+
end
|
|
113
|
+
if field == "dependencies" && (!record["revision"].is_a?(::String) || record["revision"].empty?)
|
|
114
|
+
fail_with(:invalid_document, "/#{field}/#{index}/revision")
|
|
115
|
+
end
|
|
116
|
+
record.fetch(identity)
|
|
117
|
+
end
|
|
118
|
+
fail_with(:invalid_document, "/#{field}") unless names == names.sort.uniq
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def verify_files(records)
|
|
122
|
+
unless records.is_a?(::Array) && !records.empty?
|
|
123
|
+
fail_with(:invalid_document, "/files")
|
|
124
|
+
end
|
|
125
|
+
paths = records.map.with_index do |record, index|
|
|
126
|
+
unless record.is_a?(::Hash) && digest?(record["sha256"])
|
|
127
|
+
fail_with(:invalid_document, "/files/#{index}")
|
|
128
|
+
end
|
|
129
|
+
path = artifact_path(record["path"], index)
|
|
130
|
+
require "digest"
|
|
131
|
+
actual = file_digest(path, index)
|
|
132
|
+
fail_with(:checksum_mismatch, "/files/#{index}/sha256") unless actual == record["sha256"]
|
|
133
|
+
record.fetch("path")
|
|
134
|
+
end
|
|
135
|
+
fail_with(:invalid_document, "/files") unless paths == paths.sort.uniq
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# An empty root is refused by name: Windows resolves "" to the
|
|
139
|
+
# working directory instead of raising.
|
|
140
|
+
def real_root(root)
|
|
141
|
+
fail_with(:invalid_document, "/root") if root.to_s.empty?
|
|
142
|
+
|
|
143
|
+
Pathname.new(root).realpath
|
|
144
|
+
rescue SystemCallError, TypeError, ArgumentError
|
|
145
|
+
fail_with(:invalid_document, "/root")
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def file_digest(path, index)
|
|
149
|
+
"sha256:#{Digest::SHA256.file(path).hexdigest}"
|
|
150
|
+
rescue SystemCallError
|
|
151
|
+
fail_with(:invalid_document, "/files/#{index}/path")
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def artifact_path(relative, index)
|
|
155
|
+
location = "/files/#{index}/path"
|
|
156
|
+
unless relative.is_a?(::String) && !relative.empty? &&
|
|
157
|
+
!relative.include?("\\") && !relative.include?("\0") &&
|
|
158
|
+
!relative.match?(/\A(?:[A-Za-z]:|\/)/) &&
|
|
159
|
+
relative.split("/", -1).none? { |part| INVALID_PATH_PARTS.include?(part) }
|
|
160
|
+
fail_with(:invalid_document, location)
|
|
161
|
+
end
|
|
162
|
+
path = @root.join(relative).realpath
|
|
163
|
+
unless path.file? && path.to_s.start_with?("#{@root}#{File::SEPARATOR}") &&
|
|
164
|
+
exact_case?(relative)
|
|
165
|
+
fail_with(:invalid_document, location)
|
|
166
|
+
end
|
|
167
|
+
path
|
|
168
|
+
rescue Errno::ENOENT, Errno::ENOTDIR, Errno::ELOOP
|
|
169
|
+
fail_with(:invalid_document, location)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Case-exact on every filesystem: a case-insensitive one (macOS,
|
|
173
|
+
# Windows) resolves A.TXT to a.txt, so each component must appear
|
|
174
|
+
# with the stored spelling in its directory listing.
|
|
175
|
+
def exact_case?(relative)
|
|
176
|
+
current = @root
|
|
177
|
+
relative.split("/").all? do |part|
|
|
178
|
+
found = current.children(false).map(&:to_s).include?(part)
|
|
179
|
+
current = current.join(part)
|
|
180
|
+
found
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def digest?(value)
|
|
185
|
+
value.is_a?(::String) && value.encoding.ascii_compatible? && DIGEST.match?(value)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def deep_freeze(value)
|
|
189
|
+
case value
|
|
190
|
+
when ::Hash then value.each do |key, item|
|
|
191
|
+
key.freeze
|
|
192
|
+
deep_freeze(item)
|
|
193
|
+
end
|
|
194
|
+
when ::Array then value.each { |item| deep_freeze(item) }
|
|
195
|
+
end
|
|
196
|
+
value.freeze
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def fail_with(reason, location)
|
|
200
|
+
raise Error.new(reason, location)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "rejected_error"
|
|
5
|
+
require_relative "document_validator"
|
|
6
|
+
|
|
7
|
+
module Lutaml
|
|
8
|
+
module Model
|
|
9
|
+
module Lml
|
|
10
|
+
# An immutable version-1 LML bundle: a closed graph of v2 model
|
|
11
|
+
# documents keyed by stable "Model@register" IDs. Construction
|
|
12
|
+
# normalises ordering (models/value_types key-sorted, roots and
|
|
13
|
+
# capabilities sorted unique) and deep-freezes a private copy, so a
|
|
14
|
+
# caller mutating its input cannot change a pinned identity.
|
|
15
|
+
class Bundle
|
|
16
|
+
BUNDLE_VERSION = 1
|
|
17
|
+
|
|
18
|
+
attr_reader :roots, :models, :value_types, :capabilities, :provenance
|
|
19
|
+
|
|
20
|
+
def initialize(roots:, models:, value_types: {}, capabilities: nil,
|
|
21
|
+
provenance: nil)
|
|
22
|
+
@models = freeze_tree(sort_keys(stringify(models)))
|
|
23
|
+
reject_duplicate_model_names!
|
|
24
|
+
@value_types = freeze_tree(sort_keys(stringify(value_types)))
|
|
25
|
+
@roots = frozen_strings(roots)
|
|
26
|
+
@capabilities = frozen_strings(capabilities || derive_capabilities)
|
|
27
|
+
@provenance = provenance.nil? ? nil : freeze_tree(neutral_provenance(provenance))
|
|
28
|
+
freeze
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_h
|
|
32
|
+
{
|
|
33
|
+
"lml_bundle" => BUNDLE_VERSION,
|
|
34
|
+
"roots" => roots,
|
|
35
|
+
"models" => models,
|
|
36
|
+
"value_types" => value_types,
|
|
37
|
+
"capabilities" => capabilities,
|
|
38
|
+
"provenance" => provenance,
|
|
39
|
+
}.compact
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def to_json(*)
|
|
43
|
+
JSON.generate(to_h)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Recursively key-sorted compact UTF-8 JSON (no BOM, no trailing
|
|
47
|
+
# newline; "/" and non-ASCII are not escaped).
|
|
48
|
+
def canonical_json(include_provenance: true)
|
|
49
|
+
hash = to_h
|
|
50
|
+
hash = hash.except("provenance") unless include_provenance
|
|
51
|
+
JSON.generate(canonicalize(hash))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Semantic identity: provenance is excluded.
|
|
55
|
+
def checksum
|
|
56
|
+
require "digest" # lazy: Opal boots this file and ships no digest
|
|
57
|
+
"sha256:#{Digest::SHA256.hexdigest(canonical_json(include_provenance: false))}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Checksum of one full model document.
|
|
61
|
+
def model_checksum(id)
|
|
62
|
+
model = models.fetch(id.to_s)
|
|
63
|
+
require "digest"
|
|
64
|
+
"sha256:#{Digest::SHA256.hexdigest(JSON.generate(canonicalize(model)))}"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def ==(other)
|
|
68
|
+
other.is_a?(Bundle) && canonical_json == other.canonical_json
|
|
69
|
+
end
|
|
70
|
+
alias eql? ==
|
|
71
|
+
|
|
72
|
+
def hash
|
|
73
|
+
canonical_json.hash
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
# One Ruby class per model name: the importer cannot materialize
|
|
79
|
+
# the same model under two registers, so the bundle refuses to
|
|
80
|
+
# exist rather than export what it cannot import.
|
|
81
|
+
def reject_duplicate_model_names!
|
|
82
|
+
names = models.values.map { |doc| doc["model"] }
|
|
83
|
+
duplicate = names.tally.find { |_, count| count > 1 }&.first
|
|
84
|
+
return unless duplicate
|
|
85
|
+
|
|
86
|
+
raise RejectedError.new(:duplicate_id, duplicate,
|
|
87
|
+
"#{duplicate} is declared by more than one model")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Provenance is held to the importer's neutral rules at
|
|
91
|
+
# construction, so a bundle that exports always imports.
|
|
92
|
+
def neutral_provenance(provenance)
|
|
93
|
+
tree = stringify(provenance)
|
|
94
|
+
DocumentValidator.validate!(
|
|
95
|
+
{ "lml_bundle" => BUNDLE_VERSION, "roots" => [], "models" => {}, "value_types" => {},
|
|
96
|
+
"capabilities" => [], "provenance" => tree }, kind: :bundle
|
|
97
|
+
)["provenance"]
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def derive_capabilities
|
|
101
|
+
models.values.flat_map do |doc|
|
|
102
|
+
formats = (doc["mappings"] || {}).keys
|
|
103
|
+
doc.key?("choices") ? formats + ["model.choices.v1"] : formats
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Private copies only: the caller's strings are never frozen or
|
|
108
|
+
# shared, so mutating them later cannot change this bundle.
|
|
109
|
+
def frozen_strings(list)
|
|
110
|
+
list.map { |item| item.to_s.dup }.uniq.sort.each(&:freeze).freeze
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def stringify(node, depth = 0)
|
|
114
|
+
if (node.is_a?(::Hash) || node.is_a?(Array)) && depth >= DocumentValidator::MAX_DEPTH
|
|
115
|
+
raise RejectedError.new(:invalid_document, nil,
|
|
116
|
+
"nesting deeper than #{DocumentValidator::MAX_DEPTH} levels")
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
case node
|
|
120
|
+
when ::Hash then stringify_hash(node, depth)
|
|
121
|
+
when Array then node.map { |item| stringify(item, depth + 1) }
|
|
122
|
+
when ::String then portable(node).dup
|
|
123
|
+
else node
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def stringify_hash(hash, depth)
|
|
128
|
+
hash.each_with_object({}) do |(key, value), out|
|
|
129
|
+
name = portable(key.to_s).dup
|
|
130
|
+
if out.key?(name)
|
|
131
|
+
raise RejectedError.new(:invalid_document, nil, "duplicate key #{RejectedError.show(name)}")
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
out[name] = stringify(value, depth + 1)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def portable(string)
|
|
139
|
+
return string if DocumentValidator.portable_string?(string)
|
|
140
|
+
|
|
141
|
+
raise RejectedError.new(:invalid_document, nil, "string is not valid UTF-8")
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def sort_keys(hash)
|
|
145
|
+
hash.sort_by { |key, _| key }.to_h
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def canonicalize(node)
|
|
149
|
+
case node
|
|
150
|
+
when ::Hash
|
|
151
|
+
node.sort_by { |key, _| key.to_s }
|
|
152
|
+
.to_h { |key, value| [key.to_s, canonicalize(value)] }
|
|
153
|
+
when Array then node.map { |item| canonicalize(item) }
|
|
154
|
+
else node
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def freeze_tree(node)
|
|
159
|
+
case node
|
|
160
|
+
when ::Hash
|
|
161
|
+
node.each_value { |value| freeze_tree(value) }
|
|
162
|
+
node.each_key(&:freeze)
|
|
163
|
+
when Array then node.each { |item| freeze_tree(item) }
|
|
164
|
+
when ::String then node.freeze
|
|
165
|
+
end
|
|
166
|
+
node.freeze
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "rejected_error"
|
|
4
|
+
require_relative "document_validator"
|
|
5
|
+
|
|
3
6
|
module Lutaml
|
|
4
7
|
module Model
|
|
5
8
|
module Lml
|
|
@@ -11,34 +14,51 @@ module Lutaml
|
|
|
11
14
|
class Document
|
|
12
15
|
LML_VERSION = 1
|
|
13
16
|
|
|
14
|
-
attr_reader :model_name, :attributes, :mappings, :register
|
|
17
|
+
attr_reader :model_name, :attributes, :mappings, :register, :version
|
|
15
18
|
|
|
16
|
-
def initialize(model_name:, attributes:, mappings:, register: nil
|
|
19
|
+
def initialize(model_name:, attributes:, mappings:, register: nil,
|
|
20
|
+
version: LML_VERSION, extras: {})
|
|
17
21
|
@model_name = model_name
|
|
18
22
|
@attributes = attributes
|
|
19
23
|
@mappings = mappings
|
|
20
24
|
@register = register
|
|
25
|
+
@version = version
|
|
26
|
+
@extras = extras
|
|
21
27
|
end
|
|
22
28
|
|
|
29
|
+
# Validates against the contract and keeps the supplied version:
|
|
30
|
+
# v1 documents stay v1 (same canonical form and checksum); v2
|
|
31
|
+
# documents keep every declared member. An unsupported version
|
|
32
|
+
# or malformed document raises RejectedError, never a rewrite.
|
|
23
33
|
def self.from_hash(hash)
|
|
24
|
-
|
|
34
|
+
tree = DocumentValidator.validate!(hash, kind: :document)
|
|
35
|
+
if tree["lml"] == LML_VERSION
|
|
36
|
+
return new(
|
|
37
|
+
model_name: tree["model"],
|
|
38
|
+
attributes: tree["attributes"] || [],
|
|
39
|
+
mappings: tree["mappings"] || {},
|
|
40
|
+
register: tree["register"],
|
|
41
|
+
)
|
|
42
|
+
end
|
|
25
43
|
|
|
26
44
|
new(
|
|
27
|
-
model_name:
|
|
28
|
-
attributes:
|
|
29
|
-
mappings:
|
|
30
|
-
register:
|
|
45
|
+
model_name: tree["model"],
|
|
46
|
+
attributes: tree["attributes"],
|
|
47
|
+
mappings: tree["mappings"],
|
|
48
|
+
register: tree["register"],
|
|
49
|
+
version: tree["lml"],
|
|
50
|
+
extras: tree.slice("ordered_content"),
|
|
31
51
|
)
|
|
32
52
|
end
|
|
33
53
|
|
|
34
54
|
def to_h
|
|
35
55
|
{
|
|
36
|
-
"lml" =>
|
|
56
|
+
"lml" => version,
|
|
37
57
|
"model" => model_name,
|
|
38
58
|
"register" => register,
|
|
39
59
|
"attributes" => attributes,
|
|
40
60
|
"mappings" => mappings,
|
|
41
|
-
}.compact
|
|
61
|
+
}.merge(@extras).compact
|
|
42
62
|
end
|
|
43
63
|
|
|
44
64
|
def to_json(*)
|
|
@@ -63,7 +83,7 @@ module Lutaml
|
|
|
63
83
|
# declarations, independent of key order or format.
|
|
64
84
|
def canonical_json
|
|
65
85
|
require "json"
|
|
66
|
-
JSON.generate(canonicalize(to_h))
|
|
86
|
+
JSON.generate(version == LML_VERSION ? canonicalize(to_h) : canonicalize_sorted(to_h))
|
|
67
87
|
end
|
|
68
88
|
|
|
69
89
|
def checksum
|
|
@@ -82,6 +102,10 @@ module Lutaml
|
|
|
82
102
|
|
|
83
103
|
private
|
|
84
104
|
|
|
105
|
+
# v1 identity is frozen: inside Lutaml::Model the bare `Hash` below
|
|
106
|
+
# resolves to Lutaml::Model::Hash, so this never sorts plain hashes
|
|
107
|
+
# and v1 canonical form keeps the export's insertion order. Existing
|
|
108
|
+
# v1 checksums depend on that, so it must not change.
|
|
85
109
|
def canonicalize(node)
|
|
86
110
|
case node
|
|
87
111
|
when Hash
|
|
@@ -93,6 +117,18 @@ module Lutaml
|
|
|
93
117
|
end
|
|
94
118
|
end
|
|
95
119
|
|
|
120
|
+
# v2 canonical form: recursively key-sorted, array order kept.
|
|
121
|
+
def canonicalize_sorted(node)
|
|
122
|
+
case node
|
|
123
|
+
when ::Hash
|
|
124
|
+
node.keys.sort.to_h { |key| [key, canonicalize_sorted(node[key])] }
|
|
125
|
+
when ::Array
|
|
126
|
+
node.map { |item| canonicalize_sorted(item) }
|
|
127
|
+
else
|
|
128
|
+
node
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
96
132
|
def deep_stringify_keys(node)
|
|
97
133
|
case node
|
|
98
134
|
when Hash
|