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
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
require "date"
|
|
5
|
+
require "time"
|
|
6
|
+
|
|
3
7
|
module Lutaml
|
|
4
8
|
module Model
|
|
5
9
|
module Lml
|
|
@@ -11,15 +15,74 @@ module Lutaml
|
|
|
11
15
|
# Only public introspection is used: `Klass.attributes(register)`,
|
|
12
16
|
# `Klass.mappings_for(format, register)`, `Klass.mappings` (the
|
|
13
17
|
# declared-format keys), `Lutaml::Model::Type`'s builtin registry.
|
|
18
|
+
#
|
|
19
|
+
# This class holds what both document versions share (type
|
|
20
|
+
# resolution, reject checks, the mapping walk); the version-specific
|
|
21
|
+
# parts live in Exporter::V1Projector (the v1 Document) and
|
|
22
|
+
# Exporter::V2Projector (the portable v2 Hash), which fill the
|
|
23
|
+
# extension hooks below.
|
|
14
24
|
class Exporter
|
|
25
|
+
autoload :V1Projector, "#{__dir__}/exporter/v1_projector"
|
|
26
|
+
autoload :V2Projector, "#{__dir__}/exporter/v2_projector"
|
|
27
|
+
|
|
15
28
|
SERIALIZABLE_BASE = Lutaml::Model::Serialize
|
|
29
|
+
ORDERED_CONTENT_MEMBER = "__lml_content"
|
|
30
|
+
ORDERED_CONTENT = { "member" => ORDERED_CONTENT_MEMBER, "version" => 1 }.freeze
|
|
31
|
+
PORTABLE_REGISTER = /\A[A-Za-z0-9_.:-]+\z/
|
|
32
|
+
RENDER_NAMES = { omitted: "omit", nil: "as_nil", empty: "as_empty",
|
|
33
|
+
blank: "as_blank" }.freeze
|
|
34
|
+
# key_value/rdf DSL blocks fan out to these resolved formats that
|
|
35
|
+
# are not wire formats of the v2 contract (in-memory Hash,
|
|
36
|
+
# line-delimited json/yaml, and the :lml format the LML adapter
|
|
37
|
+
# registers as key-value). Portable output omits one only when
|
|
38
|
+
# it is identical to the json/yaml projection, else refuses it.
|
|
39
|
+
DERIVED_FORMATS = %w[hash jsonl yamls lml].freeze
|
|
40
|
+
# Attribute options the portable document carries (the rest of
|
|
41
|
+
# the option space is behaviour — transforms, patterns, facets,
|
|
42
|
+
# polymorphism, choices, references — with no slot in the v2
|
|
43
|
+
# contract, so a model using one is refused by name instead of
|
|
44
|
+
# exporting a plain attribute that silently loses it).
|
|
45
|
+
PORTABLE_ATTRIBUTE_OPTIONS = %i[default collection values initialize_empty
|
|
46
|
+
required desc].freeze
|
|
47
|
+
REFERENCE_OPTIONS = %i[ref_model_class ref_key_attribute].freeze
|
|
48
|
+
DEFAULT_NAMES = ->(klass) { klass.is_a?(::String) ? klass : klass.name }
|
|
49
|
+
|
|
50
|
+
def self.export(klass, register: nil, names: nil)
|
|
51
|
+
V1Projector.new(klass, register: register, names: names).export
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Portable-v2 model document (lutaml-model#1033 contract in
|
|
55
|
+
# 01-DOCUMENT-CONTRACT): tagged types, typed defaults, resolved
|
|
56
|
+
# render policies, namespace modes and the ordered-content
|
|
57
|
+
# descriptor. `export` keeps the v1 document unchanged.
|
|
58
|
+
def self.project(klass, register: nil, names: nil, value_declarations: nil)
|
|
59
|
+
V2Projector.new(klass, register: register, names: names, value_declarations: value_declarations).project
|
|
60
|
+
end
|
|
16
61
|
|
|
17
|
-
|
|
18
|
-
|
|
62
|
+
# The projector the graph exporter and the bundle gates call:
|
|
63
|
+
# (klass, register string) -> v2 document Hash. "default" is the
|
|
64
|
+
# absence of a register.
|
|
65
|
+
def self.graph_projector(names: nil, value_declarations: nil)
|
|
66
|
+
lambda do |klass, register|
|
|
67
|
+
project(klass, register: register.to_s == GraphExporter::DEFAULT_REGISTER ? nil : register.to_sym,
|
|
68
|
+
names: names, value_declarations: value_declarations)
|
|
69
|
+
end
|
|
19
70
|
end
|
|
20
71
|
|
|
21
|
-
|
|
72
|
+
# @param names [#call, nil] klass (or a class-name String, as a
|
|
73
|
+
# discriminator case may carry) -> the name the document records
|
|
74
|
+
# for that class (default: the class's own name). Lets a caller
|
|
75
|
+
# whose classes carry other names than the declarations they
|
|
76
|
+
# stand for (a materialized bundle) export in the original
|
|
77
|
+
# identifier space.
|
|
78
|
+
def initialize(klass, register: nil, names: nil, value_declarations: nil)
|
|
79
|
+
unless value_declarations.nil? || value_declarations.is_a?(ValueDeclarations)
|
|
80
|
+
raise ArgumentError, "value_declarations must be a ValueDeclarations registry"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
@value_declarations = value_declarations
|
|
22
84
|
@klass = klass
|
|
85
|
+
@names = names || DEFAULT_NAMES
|
|
23
86
|
# A nil caller register resolves through the model's own
|
|
24
87
|
# declared default register (lutaml_default_register) — the
|
|
25
88
|
# same precedence mappings_for applies internally, needed
|
|
@@ -29,18 +92,75 @@ module Lutaml
|
|
|
29
92
|
(klass.lutaml_default_register if klass.respond_to?(:lutaml_default_register))
|
|
30
93
|
end
|
|
31
94
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
95
|
+
private
|
|
96
|
+
|
|
97
|
+
# ---- extension hooks (version-specific) -------------------------
|
|
98
|
+
|
|
99
|
+
def namespace_fields(_mapping) = raise(NotImplementedError)
|
|
100
|
+
def content_rule_extras(_rule) = raise(NotImplementedError)
|
|
101
|
+
def element_rule_extras(_rule) = raise(NotImplementedError)
|
|
102
|
+
def key_value_rule_extras(_rule) = raise(NotImplementedError)
|
|
103
|
+
def render_fields(_rule) = raise(NotImplementedError)
|
|
104
|
+
def any_attribute_rule(_mapping) = nil
|
|
105
|
+
def finish_mappings!(_projected) = nil
|
|
106
|
+
def reject_executable_rule!(_rule) = nil
|
|
107
|
+
def reject_custom_value_type!(_resolved, _attr) = nil
|
|
108
|
+
|
|
109
|
+
# ---- naming ------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
def model_name(klass)
|
|
112
|
+
@names.call(klass)
|
|
41
113
|
end
|
|
42
114
|
|
|
43
|
-
|
|
115
|
+
def portable_register
|
|
116
|
+
name = (@register || "default").to_s
|
|
117
|
+
return name if name.match?(PORTABLE_REGISTER)
|
|
118
|
+
|
|
119
|
+
raise RejectedError.new(:invalid_document, @klass,
|
|
120
|
+
"register #{RejectedError.show(name)} is not a portable name")
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def model_id(klass)
|
|
124
|
+
"#{model_name(klass)}@#{portable_register}"
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def class_path(value)
|
|
128
|
+
return nil if value.nil?
|
|
129
|
+
return model_name(value) if value.is_a?(Class)
|
|
130
|
+
|
|
131
|
+
value.to_s
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# ---- reject checks ----------------------------------------------
|
|
135
|
+
|
|
136
|
+
# The portable document addresses a model by its class path; an
|
|
137
|
+
# anonymous class has none.
|
|
138
|
+
def reject_anonymous!
|
|
139
|
+
return unless @klass.respond_to?(:name) && (@klass.name.nil? || @klass.name.empty?)
|
|
140
|
+
|
|
141
|
+
raise RejectedError.new(:invalid_document, @klass,
|
|
142
|
+
"anonymous class has no portable model name")
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def reject_reserved_member!
|
|
146
|
+
return unless @klass.attributes(@register).key?(ORDERED_CONTENT_MEMBER.to_sym)
|
|
147
|
+
|
|
148
|
+
raise RejectedError.new(:reserved_member, @klass,
|
|
149
|
+
"#{ORDERED_CONTENT_MEMBER} is reserved for ordered content")
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def ordered_content_descriptor
|
|
153
|
+
return nil unless ordered_models?
|
|
154
|
+
|
|
155
|
+
ORDERED_CONTENT
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def ordered_models?
|
|
159
|
+
@klass.mappings.keys.any? do |format|
|
|
160
|
+
mapping = @klass.mappings_for(format, @register)
|
|
161
|
+
mapping.respond_to?(:ordered?) && (mapping.ordered? || mapping.mixed_content?)
|
|
162
|
+
end
|
|
163
|
+
end
|
|
44
164
|
|
|
45
165
|
def reject_non_serializable!
|
|
46
166
|
return if @klass.is_a?(Class) && @klass.include?(SERIALIZABLE_BASE)
|
|
@@ -70,39 +190,10 @@ module Lutaml
|
|
|
70
190
|
"initialize is owned by #{owner}; the body stays hand-written")
|
|
71
191
|
end
|
|
72
192
|
|
|
73
|
-
|
|
74
|
-
{
|
|
75
|
-
"name" => attr.name.to_s,
|
|
76
|
-
"type" => type_name(attr),
|
|
77
|
-
"collection" => collection_field(attr),
|
|
78
|
-
"default" => static_default(attr),
|
|
79
|
-
"required" => true_or_nil(attr.options[:required]),
|
|
80
|
-
"values" => enum_field(attr),
|
|
81
|
-
"initialize_empty" => true_or_nil(attr.initialize_empty?),
|
|
82
|
-
"polymorphic_class" => class_path(attr.options[:polymorphic_class]),
|
|
83
|
-
"reference" => reference_field(attr),
|
|
84
|
-
}.compact
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def enum_field(attr)
|
|
88
|
-
values = attr.enum_values
|
|
89
|
-
return nil if values.nil? || values.empty?
|
|
90
|
-
|
|
91
|
-
values.map(&:to_s)
|
|
92
|
-
end
|
|
193
|
+
# ---- type resolution --------------------------------------------
|
|
93
194
|
|
|
94
195
|
def type_name(attr)
|
|
95
|
-
resolved =
|
|
96
|
-
begin
|
|
97
|
-
attr.type(@register)
|
|
98
|
-
rescue UnknownTypeError
|
|
99
|
-
# Deferred model references: consumers that declare
|
|
100
|
-
# `attribute :x, "Ns::Model"` by string path resolve
|
|
101
|
-
# through autoload, which the type resolver refuses to
|
|
102
|
-
# materialize mid-finalization. Exporting happens
|
|
103
|
-
# post-definition — materialize here.
|
|
104
|
-
const_get_or_nil(attr.unresolved_type.to_s)
|
|
105
|
-
end
|
|
196
|
+
resolved = resolve_type(attr)
|
|
106
197
|
if resolved.nil?
|
|
107
198
|
raise RejectedError.new(:unknown_type, @klass,
|
|
108
199
|
"attribute #{attr.name} has an unresolved type")
|
|
@@ -111,6 +202,17 @@ module Lutaml
|
|
|
111
202
|
builtin_name(resolved) || model_name_for(resolved, attr)
|
|
112
203
|
end
|
|
113
204
|
|
|
205
|
+
def resolve_type(attr)
|
|
206
|
+
attr.type(@register)
|
|
207
|
+
rescue UnknownTypeError
|
|
208
|
+
# Deferred model references: consumers that declare
|
|
209
|
+
# `attribute :x, "Ns::Model"` by string path resolve
|
|
210
|
+
# through autoload, which the type resolver refuses to
|
|
211
|
+
# materialize mid-finalization. Exporting happens
|
|
212
|
+
# post-definition — materialize here.
|
|
213
|
+
const_get_or_nil(attr.unresolved_type.to_s)
|
|
214
|
+
end
|
|
215
|
+
|
|
114
216
|
def const_get_or_nil(path)
|
|
115
217
|
Object.const_get(path)
|
|
116
218
|
rescue NameError
|
|
@@ -130,13 +232,16 @@ module Lutaml
|
|
|
130
232
|
end
|
|
131
233
|
|
|
132
234
|
def model_name_for(resolved, attr)
|
|
133
|
-
|
|
235
|
+
reject_custom_value_type!(resolved, attr)
|
|
236
|
+
return model_name(resolved) if resolved.is_a?(Class) &&
|
|
134
237
|
resolved.include?(SERIALIZABLE_BASE) && resolved.name
|
|
135
238
|
|
|
136
239
|
raise RejectedError.new(:unknown_type, @klass,
|
|
137
240
|
"attribute #{attr.name}: #{resolved} has no LML name")
|
|
138
241
|
end
|
|
139
242
|
|
|
243
|
+
# ---- attribute field helpers -------------------------------------
|
|
244
|
+
|
|
140
245
|
def collection_field(attr)
|
|
141
246
|
return nil unless attr.collection?
|
|
142
247
|
|
|
@@ -157,7 +262,7 @@ module Lutaml
|
|
|
157
262
|
if default.is_a?(Float) && !default.finite?
|
|
158
263
|
# JSON's canonical form cannot carry Infinity/NaN.
|
|
159
264
|
raise RejectedError.new(:non_serializable_default, @klass,
|
|
160
|
-
"attribute #{attr.name}: default #{default
|
|
265
|
+
"attribute #{attr.name}: default #{RejectedError.show(default)}")
|
|
161
266
|
end
|
|
162
267
|
default
|
|
163
268
|
when :delegate
|
|
@@ -176,25 +281,20 @@ module Lutaml
|
|
|
176
281
|
{ "model" => class_path(ref) }.compact
|
|
177
282
|
end
|
|
178
283
|
|
|
179
|
-
def class_path(value)
|
|
180
|
-
return nil if value.nil?
|
|
181
|
-
return value.name if value.is_a?(Class)
|
|
182
|
-
|
|
183
|
-
value.to_s
|
|
184
|
-
end
|
|
185
|
-
|
|
186
284
|
def true_or_nil(value)
|
|
187
285
|
value ? true : nil
|
|
188
286
|
end
|
|
189
287
|
|
|
288
|
+
# ---- mappings -----------------------------------------------------
|
|
289
|
+
|
|
190
290
|
def project_mappings
|
|
191
291
|
# Export per RESOLVED format, not per DSL block: the
|
|
192
292
|
# key_value/rdf DSL fan one block out across the registered
|
|
193
293
|
# formats, and each resolved format carries its own
|
|
194
294
|
# projection.
|
|
195
|
-
@klass.mappings.
|
|
196
|
-
|
|
197
|
-
|
|
295
|
+
projected = @klass.mappings.keys.to_h { |format| [format.to_s, project_format(format)] }
|
|
296
|
+
finish_mappings!(projected)
|
|
297
|
+
projected
|
|
198
298
|
end
|
|
199
299
|
|
|
200
300
|
def project_format(format)
|
|
@@ -208,25 +308,72 @@ module Lutaml
|
|
|
208
308
|
|
|
209
309
|
def project_xml_mapping(mapping)
|
|
210
310
|
reject_imperative_element_order!(mapping)
|
|
211
|
-
sequence = mapping.element_sequence
|
|
212
|
-
sequence = nil if sequence.is_a?(Array) && sequence.empty?
|
|
213
311
|
{
|
|
214
312
|
"root" => root_field(mapping),
|
|
215
313
|
"ordered" => true_or_nil(mapping.ordered?),
|
|
216
314
|
"mixed_content" => true_or_nil(mapping.mixed_content?),
|
|
217
|
-
"sequence" =>
|
|
218
|
-
"
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
315
|
+
"sequence" => sequence_field(mapping),
|
|
316
|
+
"rules" => project_xml_rules(mapping),
|
|
317
|
+
}.merge(namespace_fields(mapping)).compact
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# The explicit rules in declared order, then the catch-all
|
|
321
|
+
# attribute rule (Ruby emits that remainder after the mapped
|
|
322
|
+
# attributes, so it is the last rule).
|
|
323
|
+
def project_xml_rules(mapping)
|
|
324
|
+
rules = mapping.mappings.map { |rule| project_xml_rule(rule) }
|
|
325
|
+
any = any_attribute_rule(mapping)
|
|
326
|
+
rules << any if any
|
|
327
|
+
rules
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# Both v1 and v2 project every sequence compositor as
|
|
331
|
+
# {kind:"sequence", members:[...]}: member element rules in
|
|
332
|
+
# declared order, with a nested choice as
|
|
333
|
+
# {kind:"choice",min,max,members:[...]} at the position of its
|
|
334
|
+
# first member. (v1 formerly emitted Sequence#to_s, which embeds
|
|
335
|
+
# an object address and made exports differ between runs.)
|
|
336
|
+
def sequence_field(mapping)
|
|
337
|
+
sequences = mapping.element_sequence
|
|
338
|
+
return nil unless sequences.is_a?(Array) && sequences.any?
|
|
339
|
+
|
|
340
|
+
sequences.map { |sequence| project_sequence(sequence) }
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def project_sequence(sequence)
|
|
344
|
+
members = []
|
|
345
|
+
placed = []
|
|
346
|
+
sequence.attributes.each do |rule|
|
|
347
|
+
choice = sequence.compositors.find { |c| c.attributes.any? { |a| a.name == rule.to } }
|
|
348
|
+
if choice.nil?
|
|
349
|
+
members << project_xml_rule(rule)
|
|
350
|
+
elsif !placed.include?(choice)
|
|
351
|
+
placed << choice
|
|
352
|
+
members << project_choice(choice, sequence)
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
{ "kind" => "sequence", "members" => members }
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def project_choice(choice, sequence)
|
|
359
|
+
names = choice.attributes.map(&:name)
|
|
360
|
+
rules = sequence.attributes.select { |rule| names.include?(rule.to) }
|
|
361
|
+
{ "kind" => "choice", "min" => choice.min, "max" => choice.max,
|
|
362
|
+
"members" => rules.map { |rule| project_xml_rule(rule) } }
|
|
222
363
|
end
|
|
223
364
|
|
|
365
|
+
# Declarative ordered/mixed content exports (per-instance order is
|
|
366
|
+
# data, carried by the ordered-content descriptor). Only order
|
|
367
|
+
# driven by executable callbacks has no declarative form.
|
|
224
368
|
def reject_imperative_element_order!(mapping)
|
|
225
|
-
|
|
226
|
-
|
|
369
|
+
return unless mapping.ordered? || mapping.mixed_content?
|
|
370
|
+
|
|
371
|
+
callback = mapping.mappings.find { |rule| rule.custom_methods.to_h.any? }
|
|
372
|
+
callback ||= mapping.all_listeners.first if mapping.respond_to?(:all_listeners)
|
|
373
|
+
return unless callback
|
|
227
374
|
|
|
228
375
|
raise RejectedError.new(:imperative_element_order, @klass,
|
|
229
|
-
"ordered content
|
|
376
|
+
"ordered content is driven by an executable callback")
|
|
230
377
|
end
|
|
231
378
|
|
|
232
379
|
def root_field(mapping)
|
|
@@ -245,38 +392,27 @@ module Lutaml
|
|
|
245
392
|
"kind" => "content",
|
|
246
393
|
"to" => rule.to&.to_s,
|
|
247
394
|
"cdata" => true_or_nil(rule.cdata),
|
|
248
|
-
|
|
249
|
-
}.compact
|
|
395
|
+
}.merge(content_rule_extras(rule)).merge(render_fields(rule)).compact
|
|
250
396
|
end
|
|
251
397
|
|
|
398
|
+
reject_executable_rule!(rule)
|
|
252
399
|
{
|
|
253
400
|
"kind" => rule.attribute ? "attribute" : "element",
|
|
254
401
|
"name" => Array(rule.name).map(&:to_s),
|
|
255
402
|
"to" => rule.to&.to_s,
|
|
256
|
-
"namespace_uri" => xml_rule_namespace(rule),
|
|
257
|
-
"prefix" => rule.prefix&.to_s,
|
|
258
403
|
"cdata" => true_or_nil(rule.cdata),
|
|
259
|
-
"render_nil" => render_nil_field(rule),
|
|
260
404
|
"polymorphic_map" => polymorphic_map_field(rule),
|
|
261
|
-
}.compact
|
|
405
|
+
}.merge(element_rule_extras(rule)).merge(render_fields(rule)).compact
|
|
262
406
|
end
|
|
263
407
|
|
|
264
|
-
def
|
|
265
|
-
namespace = rule.namespace
|
|
266
|
-
return nil unless namespace.is_a?(String) || namespace.is_a?(Symbol)
|
|
267
|
-
|
|
268
|
-
namespace.to_s
|
|
269
|
-
end
|
|
270
|
-
|
|
271
|
-
def render_nil_field(rule)
|
|
272
|
-
value = rule.render_nil
|
|
408
|
+
def render_policy_field(value)
|
|
273
409
|
return nil unless value && value != false
|
|
274
410
|
|
|
275
411
|
value == true ? true : value.to_s
|
|
276
412
|
end
|
|
277
413
|
|
|
278
414
|
def polymorphic_map_field(rule)
|
|
279
|
-
return nil unless rule.polymorphic_map.is_a?(Hash) && rule.polymorphic_map.any?
|
|
415
|
+
return nil unless rule.polymorphic_map.is_a?(::Hash) && rule.polymorphic_map.any?
|
|
280
416
|
|
|
281
417
|
rule.polymorphic_map.to_h { |key, value| [class_path(key), value.to_s] }
|
|
282
418
|
end
|
|
@@ -293,30 +429,30 @@ module Lutaml
|
|
|
293
429
|
return nil unless mapping.respond_to?(:root_mappings)
|
|
294
430
|
|
|
295
431
|
root_mappings = mapping.root_mappings
|
|
296
|
-
return nil unless root_mappings.is_a?(Hash) && root_mappings.any?
|
|
432
|
+
return nil unless root_mappings.is_a?(::Hash) && root_mappings.any?
|
|
297
433
|
|
|
298
434
|
root_mappings.to_h { |key, value| [key.to_s, value.to_s] }
|
|
299
435
|
end
|
|
300
436
|
|
|
301
437
|
def project_key_value_rule(rule)
|
|
438
|
+
reject_executable_rule!(rule)
|
|
302
439
|
{
|
|
303
440
|
"key" => Array(rule.name).map(&:to_s),
|
|
304
441
|
"to" => rule.to&.to_s,
|
|
305
|
-
"render_nil" => render_nil_field(rule),
|
|
306
442
|
"child_mappings" => child_mappings_field(rule),
|
|
307
|
-
}.compact
|
|
443
|
+
}.merge(key_value_rule_extras(rule)).merge(render_fields(rule)).compact
|
|
308
444
|
end
|
|
309
445
|
|
|
310
446
|
def child_mappings_field(rule)
|
|
311
447
|
child_mappings = rule.child_mappings
|
|
312
|
-
return nil unless child_mappings.is_a?(Hash) && child_mappings.any?
|
|
448
|
+
return nil unless child_mappings.is_a?(::Hash) && child_mappings.any?
|
|
313
449
|
|
|
314
450
|
child_mappings.to_h { |key, value| [key.to_s, canonicalize_value(value)] }
|
|
315
451
|
end
|
|
316
452
|
|
|
317
453
|
def canonicalize_value(value)
|
|
318
454
|
case value
|
|
319
|
-
when Hash then value.to_h { |key, item| [key.to_s, canonicalize_value(item)] }
|
|
455
|
+
when ::Hash then value.to_h { |key, item| [key.to_s, canonicalize_value(item)] }
|
|
320
456
|
when Array then value.map { |item| canonicalize_value(item) }
|
|
321
457
|
when Symbol then value.to_s
|
|
322
458
|
else value
|