lutaml-model 0.8.203 → 0.8.204
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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/_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/version.rb +1 -1
- data/lib/lutaml/xml/leptris/native_emission.rb +4 -0
- data/lib/lutaml/xml/leptris/plan_serializer.rb +14 -9
- 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/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 +92 -2
- data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "bundle"
|
|
4
|
+
require_relative "rejected_error"
|
|
5
|
+
|
|
6
|
+
module Lutaml
|
|
7
|
+
module Model
|
|
8
|
+
module Lml
|
|
9
|
+
# Exports the closed graph of models reachable from some roots as a
|
|
10
|
+
# Bundle (lutaml-model#1033, card 02b).
|
|
11
|
+
#
|
|
12
|
+
# One model is projected by an injected +projector+:
|
|
13
|
+
# projector.call(klass, register) -> v2 document Hash
|
|
14
|
+
# where +register+ is the effective register string. The graph
|
|
15
|
+
# follows model-typed attributes and discriminator cases by stable "Model@register" ID. Each ID is
|
|
16
|
+
# projected once, so cycles terminate; the same ID with a different
|
|
17
|
+
# definition, or a target that cannot be resolved, is rejected.
|
|
18
|
+
# No state is kept between calls, so two registers never
|
|
19
|
+
# contaminate each other.
|
|
20
|
+
class GraphExporter
|
|
21
|
+
REGISTER_FORMAT = /\A[A-Za-z0-9_.:-]+\z/
|
|
22
|
+
DEFAULT_REGISTER = "default"
|
|
23
|
+
# Keys whose values are user data (defaults, enum values, key
|
|
24
|
+
# mappings), never structure: a data hash that merely looks like a
|
|
25
|
+
# {"kind"=>"model"} reference must not be walked as one.
|
|
26
|
+
DATA_KEYS = %w[default values facets child_mappings root_mappings].freeze
|
|
27
|
+
|
|
28
|
+
def self.export(roots, projector:, register: nil, resolver: nil,
|
|
29
|
+
provenance: nil, check_root_registers: false, names: nil)
|
|
30
|
+
new(register: register, projector: projector, resolver: resolver,
|
|
31
|
+
check_root_registers: check_root_registers, names: names)
|
|
32
|
+
.export(roots, provenance: provenance)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Default resolver: the named constant, with the register's
|
|
36
|
+
# type substitution applied when one is registered.
|
|
37
|
+
def self.default_resolver(model_name, register)
|
|
38
|
+
klass = Object.const_get(model_name)
|
|
39
|
+
substitute(klass, register) || klass
|
|
40
|
+
rescue NameError
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.substitute(klass, register)
|
|
45
|
+
return nil if register == DEFAULT_REGISTER
|
|
46
|
+
return nil unless defined?(Lutaml::Model::GlobalRegister)
|
|
47
|
+
|
|
48
|
+
reg = Lutaml::Model::GlobalRegister.lookup(register)
|
|
49
|
+
reg&.substitute(klass)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# +names+ (klass -> String, default the class's own name) is the
|
|
53
|
+
# identifier space the IDs live in; it must match the projector's.
|
|
54
|
+
def initialize(projector:, register: nil, resolver: nil,
|
|
55
|
+
check_root_registers: false, names: nil)
|
|
56
|
+
@names = names || Exporter::DEFAULT_NAMES
|
|
57
|
+
@default_register = register
|
|
58
|
+
@check_root_registers = check_root_registers
|
|
59
|
+
@projector = projector
|
|
60
|
+
@resolver = resolver || self.class.method(:default_resolver)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# +roots+: classes, or [class, register] pairs.
|
|
64
|
+
def export(roots, provenance: nil)
|
|
65
|
+
reject_unregistered!(@default_register)
|
|
66
|
+
documents = {}
|
|
67
|
+
owners = {}
|
|
68
|
+
queue = []
|
|
69
|
+
root_ids = Array(roots).map do |root|
|
|
70
|
+
klass, register = root.is_a?(Array) ? root : [root, nil]
|
|
71
|
+
reject_unregistered!(register) if @check_root_registers
|
|
72
|
+
id = id_for(klass, effective_register(klass, register))
|
|
73
|
+
queue << [id, klass]
|
|
74
|
+
id
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
until queue.empty?
|
|
78
|
+
id, klass = queue.shift
|
|
79
|
+
next if owners[id].equal?(klass)
|
|
80
|
+
|
|
81
|
+
doc = project(id, klass)
|
|
82
|
+
if documents.key?(id)
|
|
83
|
+
next if documents[id] == doc
|
|
84
|
+
|
|
85
|
+
reject(:duplicate_id, id, "#{owners[id]} and #{klass} define it differently")
|
|
86
|
+
end
|
|
87
|
+
owners[id] = klass
|
|
88
|
+
documents[id] = doc
|
|
89
|
+
references(doc).each do |target|
|
|
90
|
+
queue << [target, resolve(target, id)] unless owners.key?(target)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
value_types = collect_value_types(documents)
|
|
95
|
+
check_references!(documents, value_types)
|
|
96
|
+
Bundle.new(roots: root_ids, models: documents,
|
|
97
|
+
value_types: value_types, provenance: provenance)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
# A caller-supplied register (the default, or a root pair's when
|
|
103
|
+
# +check_root_registers+ is set) must exist; silently exporting
|
|
104
|
+
# under an unregistered name would mint Model@nosuch IDs no importer
|
|
105
|
+
# could resolve. Registers a model declares itself are not checked,
|
|
106
|
+
# and the round-trip replays a bundle's own identifiers with
|
|
107
|
+
# +check_root_registers+ off.
|
|
108
|
+
def reject_unregistered!(register)
|
|
109
|
+
register = register.to_s
|
|
110
|
+
return if register.empty? || register == DEFAULT_REGISTER || !REGISTER_FORMAT.match?(register)
|
|
111
|
+
return if defined?(Lutaml::Model::GlobalRegister) && Lutaml::Model::GlobalRegister.lookup(register)
|
|
112
|
+
|
|
113
|
+
reject(:invalid_document, register, "register #{RejectedError.show(register)} is not registered")
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def effective_register(klass, register)
|
|
117
|
+
register ||= @default_register
|
|
118
|
+
if register.nil? && klass.respond_to?(:lutaml_default_register)
|
|
119
|
+
register = klass.lutaml_default_register
|
|
120
|
+
end
|
|
121
|
+
register = register.to_s
|
|
122
|
+
register = DEFAULT_REGISTER if register.empty?
|
|
123
|
+
return register if REGISTER_FORMAT.match?(register)
|
|
124
|
+
|
|
125
|
+
reject(:invalid_document, klass, "register #{RejectedError.show(register)} is not portable")
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def id_for(klass, register)
|
|
129
|
+
name = klass.respond_to?(:name) ? @names.call(klass) : nil
|
|
130
|
+
if name.nil? || name.empty?
|
|
131
|
+
reject(:invalid_document, klass, "anonymous class has no portable model name")
|
|
132
|
+
end
|
|
133
|
+
"#{name}@#{register}"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def split_id(id)
|
|
137
|
+
model, _, register = id.rpartition("@")
|
|
138
|
+
[model, register]
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def resolve(target, referrer)
|
|
142
|
+
model, register = split_id(target)
|
|
143
|
+
klass = @resolver.call(model, register)
|
|
144
|
+
return klass if klass
|
|
145
|
+
|
|
146
|
+
reject(:dangling_reference, target, "referenced from #{referrer}")
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def project(id, klass)
|
|
150
|
+
model, register = split_id(id)
|
|
151
|
+
doc = deep_copy(@projector.call(klass, register))
|
|
152
|
+
unless doc.is_a?(::Hash)
|
|
153
|
+
reject(:invalid_document, id, "projector returned #{doc.class}, not a Hash")
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# A substituted class keeps the schema ID of the type it
|
|
157
|
+
# replaced; anything else must agree with the ID.
|
|
158
|
+
if @names.call(klass) == model
|
|
159
|
+
check_identity!(id, doc, model, register)
|
|
160
|
+
else
|
|
161
|
+
doc["model"] = model
|
|
162
|
+
doc["register"] = register
|
|
163
|
+
end
|
|
164
|
+
doc
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def check_identity!(id, doc, model, register)
|
|
168
|
+
return if doc["model"] == model && doc["register"].to_s == register
|
|
169
|
+
|
|
170
|
+
reject(:invalid_document, id,
|
|
171
|
+
"document identifies as #{doc['model']}@#{doc['register']}")
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Model IDs this document points at: model-typed attributes and
|
|
175
|
+
# discriminator cases.
|
|
176
|
+
def references(node, found = [])
|
|
177
|
+
case node
|
|
178
|
+
when ::Hash
|
|
179
|
+
found << node["id"] if node["kind"] == "model" && node["id"]
|
|
180
|
+
collect_discriminator(node["discriminator"], found)
|
|
181
|
+
node.each { |key, value| references(value, found) unless DATA_KEYS.include?(key) }
|
|
182
|
+
when Array
|
|
183
|
+
node.each { |value| references(value, found) }
|
|
184
|
+
end
|
|
185
|
+
found.uniq
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def collect_discriminator(disc, found)
|
|
189
|
+
return unless disc.is_a?(::Hash) && disc["cases"].is_a?(::Hash)
|
|
190
|
+
|
|
191
|
+
found.concat(disc["cases"].values.grep(::String))
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def value_type_ids(node, found = [])
|
|
195
|
+
case node
|
|
196
|
+
when ::Hash
|
|
197
|
+
found << node["id"] if node["kind"] == "value" && node["id"]
|
|
198
|
+
node.each { |key, value| value_type_ids(value, found) unless DATA_KEYS.include?(key) }
|
|
199
|
+
when Array
|
|
200
|
+
node.each { |value| value_type_ids(value, found) }
|
|
201
|
+
end
|
|
202
|
+
found
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Per-model "value_types" are hoisted to the bundle; the same ID
|
|
206
|
+
# with two different definitions is a duplicate identity.
|
|
207
|
+
def collect_value_types(documents)
|
|
208
|
+
documents.each_with_object({}) do |(id, doc), merged|
|
|
209
|
+
(doc.delete("value_types") || {}).each do |vid, definition|
|
|
210
|
+
if merged.key?(vid) && merged[vid] != definition
|
|
211
|
+
reject(:duplicate_id, id, "value type #{vid} defined twice differently")
|
|
212
|
+
end
|
|
213
|
+
merged[vid] = definition
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def check_references!(documents, value_types)
|
|
219
|
+
documents.each do |id, doc|
|
|
220
|
+
references(doc).each do |target|
|
|
221
|
+
next if documents.key?(target)
|
|
222
|
+
|
|
223
|
+
reject(:dangling_reference, id, "unknown model #{target}")
|
|
224
|
+
end
|
|
225
|
+
value_type_ids(doc).each do |vid|
|
|
226
|
+
next if value_types.key?(vid)
|
|
227
|
+
|
|
228
|
+
reject(:dangling_reference, id, "unknown value type #{vid}")
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def reject(reason, subject, detail)
|
|
234
|
+
raise RejectedError.new(reason, subject, detail)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def deep_copy(node)
|
|
238
|
+
case node
|
|
239
|
+
when ::Hash then node.to_h { |k, v| [k.to_s, deep_copy(v)] }
|
|
240
|
+
when Array then node.map { |item| deep_copy(item) }
|
|
241
|
+
when ::String then node.dup
|
|
242
|
+
else node
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
end
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "strscan"
|
|
5
|
+
require_relative "bundle"
|
|
6
|
+
require_relative "document_validator"
|
|
7
|
+
require_relative "materialization"
|
|
8
|
+
require_relative "mapping_compiler"
|
|
9
|
+
|
|
10
|
+
module Lutaml
|
|
11
|
+
module Model
|
|
12
|
+
module Lml
|
|
13
|
+
# Imports a neutral LML bundle into a fresh Ruby declaration graph
|
|
14
|
+
# (lutaml-model#1033, card 03c): validate, allocate and link the
|
|
15
|
+
# attributes (Materialization), then declare the xml/json/yaml
|
|
16
|
+
# mappings (MappingCompiler). Nothing is evaluated from the
|
|
17
|
+
# document, and the source constants are never consulted.
|
|
18
|
+
class Importer
|
|
19
|
+
# A bounded run of string body (plain characters and escapes). The
|
|
20
|
+
# repetition cap keeps a JavaScript regexp engine from recursing
|
|
21
|
+
# once per escape on a huge string (Opal); the caller loops over runs.
|
|
22
|
+
STRING_RUN = /[^"\\]*(?:\\.[^"\\]*){0,1000}/m
|
|
23
|
+
private_constant :STRING_RUN
|
|
24
|
+
|
|
25
|
+
# @param source [Bundle, Hash, String] a bundle, its Hash form
|
|
26
|
+
# (string keys; symbol keys are refused) or its JSON text
|
|
27
|
+
# @param namespace [Module, nil] unfrozen, named holder for the generated
|
|
28
|
+
# constants. Polymorphic discriminator targets are resolved by
|
|
29
|
+
# Ruby through Object.const_get, so a bundle that declares
|
|
30
|
+
# polymorphism needs a named (installed) namespace.
|
|
31
|
+
# @param type_bindings [Hash{String=>Class}] trusted classes by
|
|
32
|
+
# value-type id
|
|
33
|
+
# @return [Hash{String=>Class}] model id => generated class
|
|
34
|
+
def self.import(source, namespace: nil, type_bindings: {})
|
|
35
|
+
check_namespace!(namespace)
|
|
36
|
+
tree = validate(source)
|
|
37
|
+
classes = Materialization.build(tree, namespace: nil,
|
|
38
|
+
type_bindings: type_bindings)
|
|
39
|
+
return MappingCompiler.apply(classes, tree) unless namespace
|
|
40
|
+
|
|
41
|
+
install_and_compile(classes, tree, namespace)
|
|
42
|
+
rescue RejectedError => e
|
|
43
|
+
raise e.as_import
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.check_namespace!(namespace)
|
|
47
|
+
return if namespace.nil? || (namespace.is_a?(::Module) && !namespace.frozen?)
|
|
48
|
+
|
|
49
|
+
raise RejectedError.new(:invalid_document, nil, "namespace must be an unfrozen Module",
|
|
50
|
+
import: true)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# All or nothing: the constants are installed once every class is
|
|
54
|
+
# built and linked (the mapping compiler resolves polymorphic
|
|
55
|
+
# targets by constant name, so it runs after) and are removed
|
|
56
|
+
# again if the mapping compile refuses, so a retry starts clean.
|
|
57
|
+
def self.install_and_compile(classes, tree, namespace)
|
|
58
|
+
installation = Materialization.install(classes, tree["models"], namespace)
|
|
59
|
+
completed = false
|
|
60
|
+
result = MappingCompiler.apply(classes, tree)
|
|
61
|
+
completed = true
|
|
62
|
+
result
|
|
63
|
+
ensure
|
|
64
|
+
installation&.rollback unless completed
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The validated, immutable form of any accepted bundle source.
|
|
68
|
+
# @return [Bundle]
|
|
69
|
+
def self.bundle(source)
|
|
70
|
+
return source if source.is_a?(Bundle)
|
|
71
|
+
|
|
72
|
+
tree = validate(source)
|
|
73
|
+
Bundle.new(roots: tree["roots"], models: tree["models"],
|
|
74
|
+
value_types: tree["value_types"] || {},
|
|
75
|
+
capabilities: tree["capabilities"],
|
|
76
|
+
provenance: tree["provenance"])
|
|
77
|
+
rescue RejectedError => e
|
|
78
|
+
raise e.as_import
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.validate(source)
|
|
82
|
+
hash = case source
|
|
83
|
+
when Bundle then source.to_h
|
|
84
|
+
when ::String then parse(source)
|
|
85
|
+
else source
|
|
86
|
+
end
|
|
87
|
+
DocumentValidator.validate!(hash, kind: :bundle)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Rejects an object that repeats a key. The json gem's own
|
|
91
|
+
# behaviour differs by version (older ones keep the last value
|
|
92
|
+
# silently, some object_class paths skip the check), so this walks
|
|
93
|
+
# the text itself: strings are tokenised with escapes, keys are
|
|
94
|
+
# compared after decoding. Malformed text (an unterminated string,
|
|
95
|
+
# nesting past the cap) stops the walk at once and is left to
|
|
96
|
+
# JSON.parse to refuse, which keeps the scan linear.
|
|
97
|
+
def self.reject_duplicate_keys!(text)
|
|
98
|
+
scanner = StringScanner.new(text)
|
|
99
|
+
stack = []
|
|
100
|
+
until scanner.eos?
|
|
101
|
+
if scanner.skip(/\s+/) then next
|
|
102
|
+
elsif scanner.check(%r{/}) then reject_comment!
|
|
103
|
+
elsif scanner.check(/"/)
|
|
104
|
+
token = scan_string(scanner) or return
|
|
105
|
+
note_string(stack, token)
|
|
106
|
+
elsif scanner.skip(/\{/)
|
|
107
|
+
return if stack.size >= DocumentValidator::MAX_DEPTH
|
|
108
|
+
|
|
109
|
+
stack.push({ keys: {}, expect_key: true })
|
|
110
|
+
elsif scanner.skip(/\[/)
|
|
111
|
+
return if stack.size >= DocumentValidator::MAX_DEPTH
|
|
112
|
+
|
|
113
|
+
stack.push(nil)
|
|
114
|
+
elsif scanner.skip(/[}\]]/) then stack.pop
|
|
115
|
+
elsif scanner.skip(/,/) then stack.last[:expect_key] = true if stack.last
|
|
116
|
+
elsif (bare = scanner.scan(/[^\s,:\[\]{}"]+|:/))
|
|
117
|
+
reject_comment! if bare.include?("/")
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# The next string token, quotes included, or nil when it is
|
|
123
|
+
# unterminated (a lone trailing backslash or the end of the text).
|
|
124
|
+
def self.scan_string(scanner)
|
|
125
|
+
token = scanner.scan('"')
|
|
126
|
+
loop do
|
|
127
|
+
run = scanner.scan(STRING_RUN)
|
|
128
|
+
token << run
|
|
129
|
+
return token << scanner.scan('"') if scanner.check(/"/)
|
|
130
|
+
return nil if run.empty?
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# JSON has no comments. Older json gems accept them and newer ones
|
|
135
|
+
# do not, and a comment can hide a quote from this scanner, so they
|
|
136
|
+
# are refused outright and behaviour is the same on every json.
|
|
137
|
+
def self.reject_comment!
|
|
138
|
+
raise RejectedError.new(:invalid_document, nil, "not valid JSON: comments are not allowed",
|
|
139
|
+
import: true)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def self.note_string(stack, token)
|
|
143
|
+
frame = stack.last
|
|
144
|
+
return unless frame && frame[:expect_key]
|
|
145
|
+
|
|
146
|
+
frame[:expect_key] = false
|
|
147
|
+
key = JSON.parse(token)
|
|
148
|
+
if frame[:keys].key?(key)
|
|
149
|
+
raise RejectedError.new(:invalid_document, "bundle", "duplicate key #{RejectedError.show(key)}", import: true)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
frame[:keys][key] = true
|
|
153
|
+
rescue JSON::ParserError
|
|
154
|
+
nil
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def self.parse(text)
|
|
158
|
+
text = utf8_text(text)
|
|
159
|
+
reject_duplicate_keys!(text)
|
|
160
|
+
JSON.parse(text, max_nesting: DocumentValidator::MAX_DEPTH)
|
|
161
|
+
rescue JSON::ParserError => e
|
|
162
|
+
raise RejectedError.new(:invalid_document, "bundle", "not valid JSON: #{e.message.lines.first.to_s.strip}",
|
|
163
|
+
import: true)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def self.utf8_text(text)
|
|
167
|
+
text = text.dup.force_encoding(Encoding::UTF_8) if text.encoding == Encoding::BINARY
|
|
168
|
+
return text if text.encoding.ascii_compatible? && text.valid_encoding?
|
|
169
|
+
|
|
170
|
+
raise RejectedError.new(:invalid_document, nil, "not valid JSON: text is not valid UTF-8",
|
|
171
|
+
import: true)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
private_class_method :validate, :parse, :install_and_compile,
|
|
175
|
+
:reject_duplicate_keys!, :note_string, :scan_string, :check_namespace!,
|
|
176
|
+
:reject_comment!, :utf8_text
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|