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,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
module Lml
|
|
6
|
+
class Exporter
|
|
7
|
+
# The v1 document: a Lml::Document with the original attribute and
|
|
8
|
+
# mapping vocabulary (unresolved render policies, XML namespace
|
|
9
|
+
# strings, per-mapping ordered-content marker). Frozen — any change
|
|
10
|
+
# moves the v1 checksum.
|
|
11
|
+
class V1Projector < Exporter
|
|
12
|
+
def export
|
|
13
|
+
reject_non_serializable!
|
|
14
|
+
reject_custom_initialize!
|
|
15
|
+
reject_reserved_member!
|
|
16
|
+
Document.new(
|
|
17
|
+
model_name: model_name(@klass),
|
|
18
|
+
register: @register,
|
|
19
|
+
attributes: @klass.attributes(@register).map { |_, attr| project_attribute(attr) },
|
|
20
|
+
mappings: project_mappings,
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def project_attribute(attr)
|
|
27
|
+
{
|
|
28
|
+
"name" => attr.name.to_s,
|
|
29
|
+
"type" => type_name(attr),
|
|
30
|
+
"collection" => collection_field(attr),
|
|
31
|
+
"default" => static_default(attr),
|
|
32
|
+
"required" => true_or_nil(attr.options[:required]),
|
|
33
|
+
"values" => enum_field(attr),
|
|
34
|
+
"initialize_empty" => true_or_nil(attr.initialize_empty?),
|
|
35
|
+
"polymorphic_class" => class_path(attr.options[:polymorphic_class]),
|
|
36
|
+
"reference" => reference_field(attr),
|
|
37
|
+
}.compact
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def enum_field(attr)
|
|
41
|
+
values = attr.enum_values
|
|
42
|
+
return nil if values.nil? || values.empty?
|
|
43
|
+
|
|
44
|
+
values.map(&:to_s)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def namespace_fields(mapping)
|
|
48
|
+
fields = { "namespace_uri" => mapping.namespace_uri,
|
|
49
|
+
"namespace_prefix" => mapping.namespace_prefix }
|
|
50
|
+
fields["ordered_content"] = ORDERED_CONTENT if mapping.ordered? || mapping.mixed_content?
|
|
51
|
+
fields
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def content_rule_extras(rule)
|
|
55
|
+
{ "render_nil" => render_nil_field(rule) }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def element_rule_extras(rule)
|
|
59
|
+
{ "namespace_uri" => xml_rule_namespace(rule),
|
|
60
|
+
"prefix" => rule.prefix&.to_s,
|
|
61
|
+
"render_nil" => render_nil_field(rule) }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def key_value_rule_extras(rule)
|
|
65
|
+
{ "render_nil" => render_nil_field(rule) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# render_empty / render_default / value_map when they depart
|
|
69
|
+
# from the defaults.
|
|
70
|
+
def render_fields(rule)
|
|
71
|
+
{
|
|
72
|
+
"render_empty" => render_policy_field(rule.render_empty),
|
|
73
|
+
"render_default" => true_or_nil(rule.render_default),
|
|
74
|
+
"value_map" => value_map_field(rule),
|
|
75
|
+
}
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def render_nil_field(rule)
|
|
79
|
+
render_policy_field(rule.render_nil)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def value_map_field(rule)
|
|
83
|
+
defaults = rule.default_value_map
|
|
84
|
+
actual = { from: rule.value_map(:from), to: rule.value_map(:to) }
|
|
85
|
+
return nil if actual == defaults
|
|
86
|
+
|
|
87
|
+
actual.to_h do |dir, map|
|
|
88
|
+
[dir.to_s, map.to_h { |key, value| [key.to_s, value.to_s] }]
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def xml_rule_namespace(rule)
|
|
93
|
+
namespace = rule.namespace
|
|
94
|
+
return nil unless namespace.is_a?(String) || namespace.is_a?(Symbol)
|
|
95
|
+
|
|
96
|
+
namespace.to_s
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
module Lml
|
|
6
|
+
class Exporter
|
|
7
|
+
# The portable v2 model document (lutaml-model#1033 contract in
|
|
8
|
+
# 01-DOCUMENT-CONTRACT): tagged types, typed defaults, resolved
|
|
9
|
+
# render policies, namespace modes, discriminators and the
|
|
10
|
+
# ordered-content descriptor. Everything with no portable slot is
|
|
11
|
+
# refused by name instead of silently dropped.
|
|
12
|
+
class V2Projector < Exporter
|
|
13
|
+
def project
|
|
14
|
+
reject_non_serializable!
|
|
15
|
+
reject_anonymous!
|
|
16
|
+
reject_custom_initialize!
|
|
17
|
+
reject_reserved_member!
|
|
18
|
+
mappings = project_mappings
|
|
19
|
+
choices = project_choices
|
|
20
|
+
{
|
|
21
|
+
"lml" => 2,
|
|
22
|
+
"model" => model_name(@klass),
|
|
23
|
+
"register" => portable_register,
|
|
24
|
+
"attributes" => @klass.attributes(@register).map { |_, attr| project_attribute(attr) },
|
|
25
|
+
"mappings" => mappings,
|
|
26
|
+
"ordered_content" => ordered_content_descriptor,
|
|
27
|
+
"choices" => choices,
|
|
28
|
+
"value_types" => @declared_value_types&.empty? == false ? @declared_value_types : nil,
|
|
29
|
+
}.compact
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def project_attribute(attr)
|
|
35
|
+
reject_unportable_options!(attr)
|
|
36
|
+
{
|
|
37
|
+
"name" => attr.name.to_s,
|
|
38
|
+
"type" => portable_type(attr),
|
|
39
|
+
"required" => attr.options[:required] ? true : false,
|
|
40
|
+
"collection" => portable_collection(attr),
|
|
41
|
+
"default" => portable_default(attr),
|
|
42
|
+
"initialize_empty" => attr.initialize_empty? == true,
|
|
43
|
+
"values" => portable_enum(attr),
|
|
44
|
+
"reference" => reference_field(attr),
|
|
45
|
+
}.compact
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def reject_unportable_options!(attr)
|
|
49
|
+
attr.options.each do |key, value|
|
|
50
|
+
next if PORTABLE_ATTRIBUTE_OPTIONS.include?(key)
|
|
51
|
+
next if value.nil? || value.equal?(false) || empty_option?(value)
|
|
52
|
+
next if declarative_option?(key, value)
|
|
53
|
+
|
|
54
|
+
reason = REFERENCE_OPTIONS.include?(key) ? :unsupported_type : :unsupported_mapping
|
|
55
|
+
raise RejectedError.new(reason, @klass,
|
|
56
|
+
"attribute #{attr.name}: option #{key} has no portable form")
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def empty_option?(value)
|
|
61
|
+
value.respond_to?(:empty?) && value.empty?
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Model choices travel as explicit, deferred validation metadata;
|
|
65
|
+
# sequence choices travel in the XML compositor contract.
|
|
66
|
+
def declarative_option?(key, value)
|
|
67
|
+
if key == :choice
|
|
68
|
+
return @model_choice_ids.key?(value) ||
|
|
69
|
+
(value.is_a?(Lutaml::Model::Choice) && value.format.to_s == "xml")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
key == :polymorphic_class
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def project_choices
|
|
76
|
+
@model_choice_ids = {}.compare_by_identity
|
|
77
|
+
groups = @klass.choice_attributes.map { |choice| project_model_choice(choice, []) }
|
|
78
|
+
return if groups.empty?
|
|
79
|
+
|
|
80
|
+
{ "version" => 1, "validation" => "explicit", "groups" => groups }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def project_model_choice(choice, ancestors)
|
|
84
|
+
unless choice.format.nil? || choice.format.to_s == "xml"
|
|
85
|
+
raise RejectedError.new(:unsupported_mapping, @klass,
|
|
86
|
+
"model choice format must be XML or unspecified")
|
|
87
|
+
end
|
|
88
|
+
if ancestors.any? { |ancestor| ancestor.equal?(choice) }
|
|
89
|
+
raise RejectedError.new(:unsupported_mapping, @klass, "cyclic model choice")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
min = choice.min
|
|
93
|
+
max = choice.max
|
|
94
|
+
unbounded = max.respond_to?(:infinite?) && max.infinite? == 1
|
|
95
|
+
unless min.is_a?(::Integer) && min >= 0 &&
|
|
96
|
+
(unbounded || (max.is_a?(::Integer) && max >= 0))
|
|
97
|
+
raise RejectedError.new(:unsupported_type, @klass,
|
|
98
|
+
"model choice bounds must be nonnegative integers or an unbounded maximum")
|
|
99
|
+
end
|
|
100
|
+
@model_choice_ids[choice] = true
|
|
101
|
+
members = choice.attributes.map do |member|
|
|
102
|
+
if member.is_a?(Lutaml::Model::Choice)
|
|
103
|
+
{ "choice" => project_model_choice(member, ancestors + [choice]) }
|
|
104
|
+
else
|
|
105
|
+
current = @klass.attributes(@register)[member.name]
|
|
106
|
+
unless current && portable_collection(current) == portable_collection(member)
|
|
107
|
+
raise RejectedError.new(:unsupported_mapping, @klass,
|
|
108
|
+
"model choice member #{member.name} differs from its declared attribute")
|
|
109
|
+
end
|
|
110
|
+
{ "attribute" => member.name.to_s }
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
{ "min" => min, "max" => unbounded ? nil : max,
|
|
114
|
+
"format" => choice.format&.to_s, "members" => members }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def portable_type(attr)
|
|
118
|
+
resolved = resolve_type(attr)
|
|
119
|
+
if (declaration = @value_declarations&.fetch(resolved))
|
|
120
|
+
id = "#{resolved.name}@#{portable_register}"
|
|
121
|
+
(@declared_value_types ||= {})[id] = declaration
|
|
122
|
+
return { "kind" => "value", "id" => id }
|
|
123
|
+
end
|
|
124
|
+
name = type_name(attr)
|
|
125
|
+
return { "kind" => "model", "id" => model_id(resolved) } if resolved && name == model_name(resolved)
|
|
126
|
+
|
|
127
|
+
{ "kind" => "builtin", "name" => name }
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def portable_collection(attr)
|
|
131
|
+
return custom_collection(attr) if attr.options[:collection].is_a?(Class)
|
|
132
|
+
|
|
133
|
+
field = collection_field(attr)
|
|
134
|
+
return false if field.nil?
|
|
135
|
+
return { "min" => 0, "max" => nil } if field == true
|
|
136
|
+
|
|
137
|
+
{ "min" => field["min"], "max" => field["max"] }
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A custom collection class travels by name (an importer supplies
|
|
141
|
+
# the trusted binding); an unnameable one has no portable form.
|
|
142
|
+
def custom_collection(attr)
|
|
143
|
+
name = attr.options[:collection].name
|
|
144
|
+
if name.nil? || name.empty?
|
|
145
|
+
raise RejectedError.new(:unsupported_mapping, @klass,
|
|
146
|
+
"attribute #{attr.name}: an anonymous collection class has no portable name")
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
{ "min" => 0, "max" => nil, "class" => name }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def portable_default(attr)
|
|
153
|
+
return { "present" => false } if attr.default_kind == :none
|
|
154
|
+
|
|
155
|
+
{ "present" => true, "value" => portable_value(attr, static_default(attr), "default") }
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def portable_enum(attr)
|
|
159
|
+
values = attr.enum_values
|
|
160
|
+
return nil if values.nil? || values.empty?
|
|
161
|
+
|
|
162
|
+
values.to_a.map { |value| portable_value(attr, value, "values") }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# A default or enum value as the JSON scalar (or array/object of
|
|
166
|
+
# scalars) the declared type can express; anything else is refused
|
|
167
|
+
# at export rather than failing the bundle later.
|
|
168
|
+
def portable_value(attr, value, what)
|
|
169
|
+
kind = portable_kind(attr)
|
|
170
|
+
if kind == "float" && what == "default" && (value.nil? || value.is_a?(::Array))
|
|
171
|
+
# The importer reads a float default as one canonical string:
|
|
172
|
+
# no null and no collection of floats.
|
|
173
|
+
refuse_value(attr, value, what)
|
|
174
|
+
end
|
|
175
|
+
if attr.collection? && what == "default" && value.is_a?(::Array)
|
|
176
|
+
return value.map { |item| portable_scalar(attr, kind, item, what) }
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
portable_scalar(attr, kind, value, what)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def portable_kind(attr)
|
|
183
|
+
resolved = resolve_type(attr)
|
|
184
|
+
declaration = @value_declarations&.fetch(resolved)
|
|
185
|
+
if declaration
|
|
186
|
+
declaration.fetch("base")
|
|
187
|
+
else
|
|
188
|
+
(resolved.nil? ? nil : type_name(attr))
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def portable_scalar(attr, kind, value, what)
|
|
193
|
+
json = json_value(attr, value, what)
|
|
194
|
+
return json if json.nil?
|
|
195
|
+
|
|
196
|
+
unless scalar_matches?(kind, json)
|
|
197
|
+
raise RejectedError.new(:non_serializable_default, @klass,
|
|
198
|
+
"attribute #{attr.name}: #{what} #{RejectedError.show(value)} is not a #{kind}")
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
kind == "float" ? float_wire(attr, json, what) : portable_exact(attr, json, what)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# The importer refuses a JSON float anywhere but a float-typed value
|
|
205
|
+
# and an integer outside +-(2**53 - 1); refuse both here instead of
|
|
206
|
+
# exporting a bundle that cannot be imported.
|
|
207
|
+
def portable_exact(attr, json, what)
|
|
208
|
+
case json
|
|
209
|
+
when ::Float then refuse_value(attr, json, what)
|
|
210
|
+
when ::Integer
|
|
211
|
+
refuse_value(attr, json, what) if json.abs > DocumentValidator::MAX_SAFE_INTEGER
|
|
212
|
+
when ::Array then json.each { |item| portable_exact(attr, item, what) }
|
|
213
|
+
when ::Hash then json.each_value { |item| portable_exact(attr, item, what) }
|
|
214
|
+
end
|
|
215
|
+
json
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def scalar_matches?(kind, json)
|
|
219
|
+
return json.is_a?(::Numeric) if kind == "float"
|
|
220
|
+
|
|
221
|
+
DocumentValidator.default_shape?(kind, json)
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def float_wire(attr, json, what)
|
|
225
|
+
refuse_value(attr, json, what) unless json.to_f.finite?
|
|
226
|
+
|
|
227
|
+
DocumentValidator.float_wire(json)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def json_value(attr, value, what)
|
|
231
|
+
case value
|
|
232
|
+
when nil, true, false, ::Integer then value
|
|
233
|
+
when ::String then portable_string(attr, value, what)
|
|
234
|
+
when ::Symbol then portable_string(attr, value.to_s, what)
|
|
235
|
+
when ::Float
|
|
236
|
+
return value if value.finite?
|
|
237
|
+
|
|
238
|
+
refuse_value(attr, value, what)
|
|
239
|
+
when ::Date, ::Time then value.iso8601
|
|
240
|
+
when ::BigDecimal then value.to_s("F")
|
|
241
|
+
when ::Rational then value.to_s
|
|
242
|
+
when ::Array then value.map { |item| json_value(attr, item, what) }
|
|
243
|
+
when ::Hash then value.to_h { |k, v| [portable_string(attr, k.to_s, what), json_value(attr, v, what)] }
|
|
244
|
+
else refuse_value(attr, value, what)
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def portable_string(attr, string, what)
|
|
249
|
+
return string if DocumentValidator.portable_string?(string)
|
|
250
|
+
|
|
251
|
+
raise RejectedError.new(:non_serializable_default, @klass,
|
|
252
|
+
"attribute #{attr.name}: #{what} #{RejectedError.show(string)} is not valid UTF-8")
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def refuse_value(attr, value, what)
|
|
256
|
+
raise RejectedError.new(:non_serializable_default, @klass,
|
|
257
|
+
"attribute #{attr.name}: #{what} #{RejectedError.show(value)} has no JSON form")
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# The exporter emits no bundle value_types, so a custom value
|
|
261
|
+
# type could never pass the gate; refuse it by name.
|
|
262
|
+
def reject_custom_value_type!(resolved, attr)
|
|
263
|
+
return unless resolved.is_a?(Class) && resolved <= Lutaml::Model::Type::Value && resolved.name
|
|
264
|
+
|
|
265
|
+
raise RejectedError.new(:unsupported_type, @klass,
|
|
266
|
+
"attribute #{attr.name}: custom value type #{resolved.name} has no portable form")
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# Derived formats are dropped when identical to their json/yaml
|
|
270
|
+
# twin, else refused.
|
|
271
|
+
def finish_mappings!(projected)
|
|
272
|
+
DERIVED_FORMATS.each { |format| drop_derived_format!(projected, format) }
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def drop_derived_format!(projected, format)
|
|
276
|
+
return unless projected.key?(format)
|
|
277
|
+
|
|
278
|
+
twin = projected["json"] || projected["yaml"]
|
|
279
|
+
unless twin && projected[format] == twin
|
|
280
|
+
raise RejectedError.new(:unsupported_mapping, @klass,
|
|
281
|
+
"format #{format} has no portable form")
|
|
282
|
+
end
|
|
283
|
+
projected.delete(format)
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def project_xml_mapping(mapping)
|
|
287
|
+
previous = @xml_mapping
|
|
288
|
+
@xml_mapping = mapping
|
|
289
|
+
super
|
|
290
|
+
ensure
|
|
291
|
+
@xml_mapping = previous
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def namespace_fields(mapping)
|
|
295
|
+
{ "namespace" => mapping_namespace(mapping) }
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def mapping_namespace(mapping)
|
|
299
|
+
return { "mode" => "inherit" } if mapping.namespace_uri.nil?
|
|
300
|
+
|
|
301
|
+
{ "mode" => "uri", "uri" => mapping.namespace_uri,
|
|
302
|
+
"prefix" => mapping.namespace_prefix }.compact
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# map_all_attributes: {kind:"any_attribute", to:<hash attribute>}.
|
|
306
|
+
# The optional `matching:` filter is a Ruby Regexp with no
|
|
307
|
+
# portable form, so it is refused by name instead of dropped.
|
|
308
|
+
def any_attribute_rule(mapping)
|
|
309
|
+
rule = mapping.respond_to?(:all_attributes_rule) ? mapping.all_attributes_rule : nil
|
|
310
|
+
return nil if rule.nil?
|
|
311
|
+
|
|
312
|
+
if rule.matching
|
|
313
|
+
raise RejectedError.new(:unsupported_mapping, @klass,
|
|
314
|
+
"map_all_attributes matching: #{RejectedError.show(rule.matching)} has no portable form")
|
|
315
|
+
end
|
|
316
|
+
{ "kind" => "any_attribute", "to" => rule.to.to_s }
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# Executable rule parts have no portable form; refuse without
|
|
320
|
+
# calling them.
|
|
321
|
+
def reject_executable_rule!(rule)
|
|
322
|
+
if rule.custom_methods.to_h.any?
|
|
323
|
+
raise RejectedError.new(:unsupported_mapping, @klass,
|
|
324
|
+
"rule #{Array(rule.name).first}: custom with: methods are executable")
|
|
325
|
+
end
|
|
326
|
+
return if rule.transform.to_h.empty?
|
|
327
|
+
|
|
328
|
+
raise RejectedError.new(:unsupported_mapping, @klass,
|
|
329
|
+
"rule #{Array(rule.name).first}: transform is executable")
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
def content_rule_extras(_rule)
|
|
333
|
+
{}
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def element_rule_extras(rule)
|
|
337
|
+
{ "discriminator" => discriminator_field(rule) }
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def key_value_rule_extras(rule)
|
|
341
|
+
{ "root_mappings" => rule_root_mappings_field(rule),
|
|
342
|
+
"discriminator" => discriminator_field(rule) }
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
def discriminator_field(rule)
|
|
346
|
+
poly = rule.polymorphic
|
|
347
|
+
return nil unless poly.is_a?(::Hash) && poly[:attribute]
|
|
348
|
+
|
|
349
|
+
cases = (poly[:class_map] || {}).to_h do |wire, target|
|
|
350
|
+
[wire.to_s, model_id(target)]
|
|
351
|
+
end
|
|
352
|
+
{ "attribute" => poly[:attribute].to_s, "cases" => cases }
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# One resolved "render" object.
|
|
356
|
+
def render_fields(rule)
|
|
357
|
+
{ "render" => resolved_render(rule),
|
|
358
|
+
"namespace" => (rule_namespace(rule) if rule.respond_to?(:namespace_param) && rule.name) }
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
def rule_namespace(rule)
|
|
362
|
+
if rule.namespace_param.nil? || rule.namespace_param == :inherit
|
|
363
|
+
if rule.attribute && @xml_mapping&.namespace_class
|
|
364
|
+
attribute = @klass.attributes(@register)[rule.to]
|
|
365
|
+
info = rule.resolve_namespace(
|
|
366
|
+
attr: attribute, register: @register,
|
|
367
|
+
parent_ns_uri: @xml_mapping.namespace_uri,
|
|
368
|
+
parent_ns_class: @xml_mapping.namespace_class,
|
|
369
|
+
form_default: @xml_mapping.namespace_class.attribute_form_default || :unqualified
|
|
370
|
+
)
|
|
371
|
+
return { "mode" => "inherit" } if info[:uri].nil?
|
|
372
|
+
|
|
373
|
+
return { "mode" => "uri", "uri" => info[:uri], "prefix" => info[:prefix] }.compact
|
|
374
|
+
end
|
|
375
|
+
return { "mode" => "inherit" }
|
|
376
|
+
end
|
|
377
|
+
return { "mode" => "unqualified" } if rule.namespace_param == :blank || rule.namespace.nil?
|
|
378
|
+
|
|
379
|
+
{ "mode" => "uri", "uri" => rule.namespace, "prefix" => rule.prefix&.to_s }.compact
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
def resolved_render(rule)
|
|
383
|
+
to = rule.value_map(:to)
|
|
384
|
+
{
|
|
385
|
+
"nil" => RENDER_NAMES.fetch(to[:nil]),
|
|
386
|
+
"empty" => RENDER_NAMES.fetch(to[:empty]),
|
|
387
|
+
"default" => rule.render_default ? true : false,
|
|
388
|
+
}
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def rule_root_mappings_field(rule)
|
|
392
|
+
root_mappings = rule.respond_to?(:root_mappings) ? rule.root_mappings : nil
|
|
393
|
+
return nil unless root_mappings.is_a?(::Hash) && root_mappings.any?
|
|
394
|
+
|
|
395
|
+
root_mappings.to_h { |key, value| [key.to_s, canonicalize_value(value)] }
|
|
396
|
+
end
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
end
|
|
401
|
+
end
|