lutaml-model 0.8.159 → 0.8.161
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/.github/workflows/dependent-tests.yml +6 -4
- data/lib/compat/opal/lutaml_model_boot.rb +5 -0
- data/lib/lutaml/model/lml/document.rb +109 -0
- data/lib/lutaml/model/lml/exporter.rb +328 -0
- data/lib/lutaml/model/lml/rejected_error.rb +32 -0
- data/lib/lutaml/model/lml/round_trip.rb +49 -0
- data/lib/lutaml/model/lml.rb +40 -0
- data/lib/lutaml/model/register.rb +6 -0
- data/lib/lutaml/model/transformation_registry.rb +7 -2
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/model.rb +1 -0
- data/lib/lutaml/xml/model_transform.rb +5 -0
- data/spec/lutaml/model/lml_exporter_spec.rb +150 -0
- metadata +7 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 40f892c8db0278f7a1dd5b88220134e6c42f0c14fa5371073ebd416be45f63d1
|
|
4
|
+
data.tar.gz: 00ddc4bdb2656c805708036ac712a82375add79b1b93286f08d24ff34d526944
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: caa94d3c42642ad8e70569b85170b452e45323fd7411cb61594d5b185e772ae387d7d799835ac0250f6efa26666caf6bbb92c60cd1edb9ecd6eadaf5fce29cf2
|
|
7
|
+
data.tar.gz: 36ab095bed9f7ef4b83679641f0ce47c98fe6b9517a3aa5e0a5ec0f8db7208a532e03e339ca9e8ba0dcd2759933c2232787080de69aa848031b6be2ce47ab897
|
|
@@ -18,10 +18,10 @@ on:
|
|
|
18
18
|
types: [ release-passed ]
|
|
19
19
|
|
|
20
20
|
jobs:
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
21
|
+
# The dependent lanes run Ruby 4.0 only: the matrix_ruby value is
|
|
22
|
+
# raw-interpolated into the reusable workflow's jq program, hence the
|
|
23
|
+
# JSON array literal (metanorma/ci#462 supplies the input). Today's
|
|
24
|
+
# default slice is 3.3-only.
|
|
25
25
|
rake:
|
|
26
26
|
uses: metanorma/ci/.github/workflows/dependent-rake.yml@main
|
|
27
27
|
with:
|
|
@@ -36,6 +36,7 @@ jobs:
|
|
|
36
36
|
# names alone.
|
|
37
37
|
command: set +e; echo "spec-files=$(find spec -name '*_spec.rb' 2>/dev/null | wc -l)"; bundle exec rspec > rspec-run.log 2>&1; code=$?; echo "rspec-exit=$code"; tail -25 rspec-run.log; grep -B2 -A14 "Failure/Error" rspec-run.log | head -160; exit $code
|
|
38
38
|
repos-json-file: .github/workflows/dependent-repos.json
|
|
39
|
+
matrix_ruby: '[{"version":"4.0","rubygems":"latest","bundler":"default","experimental":false}]'
|
|
39
40
|
after-setup-ruby: |
|
|
40
41
|
if [ -f frontend/package.json ]; then cd frontend && npm install && npm run build; fi
|
|
41
42
|
secrets:
|
|
@@ -51,6 +52,7 @@ jobs:
|
|
|
51
52
|
command: set +e; echo "spec-files=$(find spec -name '*_spec.rb' 2>/dev/null | wc -l)"; bundle exec rspec; code=$?; echo "rspec-exit=$code"; exit $code
|
|
52
53
|
repos-json-file: .github/workflows/dependent-repos-tools.json
|
|
53
54
|
setup-tools: 'inkscape,ghostscript,exiftool,ffmpeg,imagemagick,yq'
|
|
55
|
+
matrix_ruby: '[{"version":"4.0","rubygems":"latest","bundler":"default","experimental":false}]'
|
|
54
56
|
after-setup-ruby: |
|
|
55
57
|
if [ -f frontend/package.json ]; then cd frontend && npm install && npm run build; fi
|
|
56
58
|
secrets:
|
|
@@ -72,6 +72,7 @@ require "lutaml/model/json_adapter"
|
|
|
72
72
|
require "lutaml/model/jsonl"
|
|
73
73
|
require "lutaml/model/liquefiable"
|
|
74
74
|
require "lutaml/model/liquid"
|
|
75
|
+
require "lutaml/model/lml"
|
|
75
76
|
require "lutaml/model/mapping_hash"
|
|
76
77
|
require "lutaml/model/model_transformer"
|
|
77
78
|
require "lutaml/model/one_entry_cache"
|
|
@@ -289,6 +290,10 @@ require "lutaml/model/error/validation_error"
|
|
|
289
290
|
require "lutaml/model/error/validation_failed_error"
|
|
290
291
|
require "lutaml/model/liquid/indexed_access"
|
|
291
292
|
require "lutaml/model/liquid/mapping"
|
|
293
|
+
require "lutaml/model/lml/document"
|
|
294
|
+
require "lutaml/model/lml/exporter"
|
|
295
|
+
require "lutaml/model/lml/rejected_error"
|
|
296
|
+
require "lutaml/model/lml/round_trip"
|
|
292
297
|
require "lutaml/model/mapping/listener"
|
|
293
298
|
require "lutaml/model/mapping/mapping"
|
|
294
299
|
require "lutaml/model/mapping/mapping_rule"
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
module Lml
|
|
6
|
+
# The neutral LML document: a model class's declarations as a
|
|
7
|
+
# plain hash with a canonical render and a sha256 checksum. The
|
|
8
|
+
# hash layout is 1:1 with the declaration vocabulary so the
|
|
9
|
+
# round-trip gate (export → canonical render → checksum) is the
|
|
10
|
+
# design's spine.
|
|
11
|
+
class Document
|
|
12
|
+
LML_VERSION = 1
|
|
13
|
+
|
|
14
|
+
attr_reader :model_name, :attributes, :mappings, :register
|
|
15
|
+
|
|
16
|
+
def initialize(model_name:, attributes:, mappings:, register: nil)
|
|
17
|
+
@model_name = model_name
|
|
18
|
+
@attributes = attributes
|
|
19
|
+
@mappings = mappings
|
|
20
|
+
@register = register
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.from_hash(hash)
|
|
24
|
+
raise ArgumentError, "missing model name" unless hash["model"]
|
|
25
|
+
|
|
26
|
+
new(
|
|
27
|
+
model_name: hash["model"],
|
|
28
|
+
attributes: hash["attributes"] || [],
|
|
29
|
+
mappings: hash["mappings"] || {},
|
|
30
|
+
register: hash["register"],
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def to_h
|
|
35
|
+
{
|
|
36
|
+
"lml" => LML_VERSION,
|
|
37
|
+
"model" => model_name,
|
|
38
|
+
"register" => register,
|
|
39
|
+
"attributes" => attributes,
|
|
40
|
+
"mappings" => mappings,
|
|
41
|
+
}.compact
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def to_json(*)
|
|
45
|
+
require "json"
|
|
46
|
+
JSON.generate(to_h)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def to_yaml
|
|
50
|
+
require "yaml"
|
|
51
|
+
YAML.safe_dump(
|
|
52
|
+
deep_stringify_keys(to_h),
|
|
53
|
+
permitted_classes: [],
|
|
54
|
+
aliases: false,
|
|
55
|
+
sort_keys: false,
|
|
56
|
+
line_width: -1,
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# The canonical form: the document hash with sorted keys at
|
|
61
|
+
# every level, serialized deterministically. The checksum is
|
|
62
|
+
# pinned over this form — equal checksums mean equal
|
|
63
|
+
# declarations, independent of key order or format.
|
|
64
|
+
def canonical_json
|
|
65
|
+
require "json"
|
|
66
|
+
JSON.generate(canonicalize(to_h))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def checksum
|
|
70
|
+
require "digest"
|
|
71
|
+
"sha256:#{Digest::SHA256.hexdigest(canonical_json)}"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def ==(other)
|
|
75
|
+
other.is_a?(Document) && canonical_json == other.canonical_json
|
|
76
|
+
end
|
|
77
|
+
alias eql? ==
|
|
78
|
+
|
|
79
|
+
def hash
|
|
80
|
+
canonical_json.hash
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def canonicalize(node)
|
|
86
|
+
case node
|
|
87
|
+
when Hash
|
|
88
|
+
node.keys.sort_by(&:to_s).to_h { |key| [key.to_s, canonicalize(node[key])] }
|
|
89
|
+
when Array
|
|
90
|
+
node.map { |item| canonicalize(item) }
|
|
91
|
+
else
|
|
92
|
+
node
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def deep_stringify_keys(node)
|
|
97
|
+
case node
|
|
98
|
+
when Hash
|
|
99
|
+
node.to_h { |key, value| [key.to_s, deep_stringify_keys(value)] }
|
|
100
|
+
when Array
|
|
101
|
+
node.map { |item| deep_stringify_keys(item) }
|
|
102
|
+
else
|
|
103
|
+
node
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
module Lml
|
|
6
|
+
# Reads a model class's declaration surfaces — the Attribute
|
|
7
|
+
# registry and the TransformationRegistry-resolved per-format
|
|
8
|
+
# mappings — and projects them into the neutral Document
|
|
9
|
+
# (lutaml-model#1033 Phase 1).
|
|
10
|
+
#
|
|
11
|
+
# Only public introspection is used: `Klass.attributes(register)`,
|
|
12
|
+
# `Klass.mappings_for(format, register)`, `Klass.mappings` (the
|
|
13
|
+
# declared-format keys), `Lutaml::Model::Type`'s builtin registry.
|
|
14
|
+
class Exporter
|
|
15
|
+
SERIALIZABLE_BASE = Lutaml::Model::Serialize
|
|
16
|
+
|
|
17
|
+
def self.export(klass, register: nil)
|
|
18
|
+
new(klass, register: register).export
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(klass, register: nil)
|
|
22
|
+
@klass = klass
|
|
23
|
+
# A nil caller register resolves through the model's own
|
|
24
|
+
# declared default register (lutaml_default_register) — the
|
|
25
|
+
# same precedence mappings_for applies internally, needed
|
|
26
|
+
# explicitly here because attributes/type resolution takes
|
|
27
|
+
# the register literally.
|
|
28
|
+
@register = register ||
|
|
29
|
+
(klass.lutaml_default_register if klass.respond_to?(:lutaml_default_register))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def export
|
|
33
|
+
reject_non_serializable!
|
|
34
|
+
reject_custom_initialize!
|
|
35
|
+
Document.new(
|
|
36
|
+
model_name: @klass.name,
|
|
37
|
+
register: @register,
|
|
38
|
+
attributes: @klass.attributes(@register).map { |_, attr| project_attribute(attr) },
|
|
39
|
+
mappings: project_mappings,
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def reject_non_serializable!
|
|
46
|
+
return if @klass.is_a?(Class) && @klass.include?(SERIALIZABLE_BASE)
|
|
47
|
+
|
|
48
|
+
raise RejectedError.new(:not_serializable, @klass,
|
|
49
|
+
"the class does not include Lutaml::Model::Serializable")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# A custom `initialize` is constructor-contract: its body is not
|
|
53
|
+
# a declaration. The exported document records nothing about it;
|
|
54
|
+
# the model must be rejected so the schema stays honest.
|
|
55
|
+
#
|
|
56
|
+
# The framework prepends Serialize::Builder onto Serializable at
|
|
57
|
+
# boot, so every plain model's initialize is owned by Builder.
|
|
58
|
+
# The ownership check therefore walks the ancestry: the owner is
|
|
59
|
+
# custom only when it sits between the model and its
|
|
60
|
+
# Serializable boundary (the class itself or a module it
|
|
61
|
+
# included).
|
|
62
|
+
def reject_custom_initialize!
|
|
63
|
+
owner = @klass.instance_method(:initialize).owner
|
|
64
|
+
ancestors = @klass.ancestors
|
|
65
|
+
boundary = ancestors.index(Lutaml::Model::Serializable)
|
|
66
|
+
owner_index = owner && ancestors.index(owner)
|
|
67
|
+
return if boundary.nil? || owner_index.nil? || owner_index >= boundary
|
|
68
|
+
|
|
69
|
+
raise RejectedError.new(:custom_initialize, @klass,
|
|
70
|
+
"initialize is owned by #{owner}; the body stays hand-written")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def project_attribute(attr)
|
|
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
|
|
93
|
+
|
|
94
|
+
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
|
|
106
|
+
if resolved.nil?
|
|
107
|
+
raise RejectedError.new(:unknown_type, @klass,
|
|
108
|
+
"attribute #{attr.name} has an unresolved type")
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
builtin_name(resolved) || model_name_for(resolved, attr)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def const_get_or_nil(path)
|
|
115
|
+
Object.const_get(path)
|
|
116
|
+
rescue NameError
|
|
117
|
+
nil
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def builtin_name(resolved)
|
|
121
|
+
builtin_registry.find { |_, klass| klass == resolved }&.first&.to_s
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def builtin_registry
|
|
125
|
+
@builtin_registry ||= begin
|
|
126
|
+
registry = TypeRegistry.new
|
|
127
|
+
Type.register_builtin_types_in(registry)
|
|
128
|
+
registry.types
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def model_name_for(resolved, attr)
|
|
133
|
+
return resolved.name if resolved.is_a?(Class) &&
|
|
134
|
+
resolved.include?(SERIALIZABLE_BASE) && resolved.name
|
|
135
|
+
|
|
136
|
+
raise RejectedError.new(:unknown_type, @klass,
|
|
137
|
+
"attribute #{attr.name}: #{resolved} has no LML name")
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def collection_field(attr)
|
|
141
|
+
return nil unless attr.collection?
|
|
142
|
+
|
|
143
|
+
range = attr.resolved_collection
|
|
144
|
+
return true if range.nil?
|
|
145
|
+
|
|
146
|
+
max = range.max
|
|
147
|
+
max = nil if max.respond_to?(:infinite?) && max.infinite?
|
|
148
|
+
{ "min" => range.min, "max" => max }.compact
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def static_default(attr)
|
|
152
|
+
case attr.default_kind
|
|
153
|
+
when :none
|
|
154
|
+
nil
|
|
155
|
+
when :static
|
|
156
|
+
default = attr.options[:default]
|
|
157
|
+
if default.is_a?(Float) && !default.finite?
|
|
158
|
+
# JSON's canonical form cannot carry Infinity/NaN.
|
|
159
|
+
raise RejectedError.new(:non_serializable_default, @klass,
|
|
160
|
+
"attribute #{attr.name}: default #{default.inspect}")
|
|
161
|
+
end
|
|
162
|
+
default
|
|
163
|
+
when :delegate
|
|
164
|
+
raise RejectedError.new(:lambda_default, @klass,
|
|
165
|
+
"attribute #{attr.name}: delegated defaults are not declarative")
|
|
166
|
+
else # :proc
|
|
167
|
+
raise RejectedError.new(:lambda_default, @klass,
|
|
168
|
+
"attribute #{attr.name}: defaults must be constants in the schema")
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def reference_field(attr)
|
|
173
|
+
ref = attr.options[:ref_model_class]
|
|
174
|
+
return nil unless ref
|
|
175
|
+
|
|
176
|
+
{ "model" => class_path(ref) }.compact
|
|
177
|
+
end
|
|
178
|
+
|
|
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
|
+
def true_or_nil(value)
|
|
187
|
+
value ? true : nil
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def project_mappings
|
|
191
|
+
# Export per RESOLVED format, not per DSL block: the
|
|
192
|
+
# key_value/rdf DSL fan one block out across the registered
|
|
193
|
+
# formats, and each resolved format carries its own
|
|
194
|
+
# projection.
|
|
195
|
+
@klass.mappings.each_with_object({}) do |(format, _), out|
|
|
196
|
+
out[format.to_s] = project_format(format)
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def project_format(format)
|
|
201
|
+
mapping = @klass.mappings_for(format, @register)
|
|
202
|
+
if mapping.respond_to?(:root_element)
|
|
203
|
+
project_xml_mapping(mapping)
|
|
204
|
+
else
|
|
205
|
+
project_key_value_mapping(mapping)
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def project_xml_mapping(mapping)
|
|
210
|
+
reject_imperative_element_order!(mapping)
|
|
211
|
+
sequence = mapping.element_sequence
|
|
212
|
+
sequence = nil if sequence.is_a?(Array) && sequence.empty?
|
|
213
|
+
{
|
|
214
|
+
"root" => root_field(mapping),
|
|
215
|
+
"ordered" => true_or_nil(mapping.ordered?),
|
|
216
|
+
"mixed_content" => true_or_nil(mapping.mixed_content?),
|
|
217
|
+
"sequence" => sequence&.map(&:to_s),
|
|
218
|
+
"namespace_uri" => mapping.namespace_uri,
|
|
219
|
+
"namespace_prefix" => mapping.namespace_prefix,
|
|
220
|
+
"rules" => mapping.mappings.map { |rule| project_xml_rule(rule) },
|
|
221
|
+
}.compact
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def reject_imperative_element_order!(mapping)
|
|
225
|
+
sequence = mapping.element_sequence
|
|
226
|
+
return if !mapping.ordered? || (sequence.is_a?(Array) && !sequence.empty?)
|
|
227
|
+
|
|
228
|
+
raise RejectedError.new(:imperative_element_order, @klass,
|
|
229
|
+
"ordered content without a declared sequence is instance-driven")
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def root_field(mapping)
|
|
233
|
+
return nil if mapping.root_element.nil?
|
|
234
|
+
|
|
235
|
+
{
|
|
236
|
+
"element" => mapping.root_element,
|
|
237
|
+
"mixed" => true_or_nil(mapping.mixed_content?),
|
|
238
|
+
}.compact
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def project_xml_rule(rule)
|
|
242
|
+
if rule.name.nil?
|
|
243
|
+
# map_content: the content catch-all carries no name.
|
|
244
|
+
return {
|
|
245
|
+
"kind" => "content",
|
|
246
|
+
"to" => rule.to&.to_s,
|
|
247
|
+
"cdata" => true_or_nil(rule.cdata),
|
|
248
|
+
"render_nil" => render_nil_field(rule),
|
|
249
|
+
}.compact
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
{
|
|
253
|
+
"kind" => rule.attribute ? "attribute" : "element",
|
|
254
|
+
"name" => Array(rule.name).map(&:to_s),
|
|
255
|
+
"to" => rule.to&.to_s,
|
|
256
|
+
"namespace_uri" => xml_rule_namespace(rule),
|
|
257
|
+
"prefix" => rule.prefix&.to_s,
|
|
258
|
+
"cdata" => true_or_nil(rule.cdata),
|
|
259
|
+
"render_nil" => render_nil_field(rule),
|
|
260
|
+
"polymorphic_map" => polymorphic_map_field(rule),
|
|
261
|
+
}.compact
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def xml_rule_namespace(rule)
|
|
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
|
|
273
|
+
return nil unless value && value != false
|
|
274
|
+
|
|
275
|
+
value == true ? true : value.to_s
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def polymorphic_map_field(rule)
|
|
279
|
+
return nil unless rule.polymorphic_map.is_a?(Hash) && rule.polymorphic_map.any?
|
|
280
|
+
|
|
281
|
+
rule.polymorphic_map.to_h { |key, value| [class_path(key), value.to_s] }
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def project_key_value_mapping(mapping)
|
|
285
|
+
rules = mapping.mappings.map { |rule| project_key_value_rule(rule) }
|
|
286
|
+
{
|
|
287
|
+
"rules" => rules,
|
|
288
|
+
"root_mappings" => root_mappings_field(mapping),
|
|
289
|
+
}.compact
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def root_mappings_field(mapping)
|
|
293
|
+
return nil unless mapping.respond_to?(:root_mappings)
|
|
294
|
+
|
|
295
|
+
root_mappings = mapping.root_mappings
|
|
296
|
+
return nil unless root_mappings.is_a?(Hash) && root_mappings.any?
|
|
297
|
+
|
|
298
|
+
root_mappings.to_h { |key, value| [key.to_s, value.to_s] }
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def project_key_value_rule(rule)
|
|
302
|
+
{
|
|
303
|
+
"key" => Array(rule.name).map(&:to_s),
|
|
304
|
+
"to" => rule.to&.to_s,
|
|
305
|
+
"render_nil" => render_nil_field(rule),
|
|
306
|
+
"child_mappings" => child_mappings_field(rule),
|
|
307
|
+
}.compact
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def child_mappings_field(rule)
|
|
311
|
+
child_mappings = rule.child_mappings
|
|
312
|
+
return nil unless child_mappings.is_a?(Hash) && child_mappings.any?
|
|
313
|
+
|
|
314
|
+
child_mappings.to_h { |key, value| [key.to_s, canonicalize_value(value)] }
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def canonicalize_value(value)
|
|
318
|
+
case value
|
|
319
|
+
when Hash then value.to_h { |key, item| [key.to_s, canonicalize_value(item)] }
|
|
320
|
+
when Array then value.map { |item| canonicalize_value(item) }
|
|
321
|
+
when Symbol then value.to_s
|
|
322
|
+
else value
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
module Lml
|
|
6
|
+
# A named rejection from the exporter: the construct is
|
|
7
|
+
# constructor-contract, not schema, and has no declarative
|
|
8
|
+
# equivalent in the LML document (lutaml-model#1033 design
|
|
9
|
+
# decision #3).
|
|
10
|
+
class RejectedError < StandardError
|
|
11
|
+
attr_reader :reason, :klass, :detail
|
|
12
|
+
|
|
13
|
+
# +reason+ is one of:
|
|
14
|
+
# :lambda_default — a default given as a Proc
|
|
15
|
+
# :custom_initialize — the model overrides `initialize`
|
|
16
|
+
# :imperative_element_order — ordered content with no
|
|
17
|
+
# declarative sequence
|
|
18
|
+
# :unknown_type — a type with no registry name
|
|
19
|
+
# :not_serializable — the class does not include
|
|
20
|
+
# Serializable
|
|
21
|
+
def initialize(reason, klass, detail = nil)
|
|
22
|
+
@reason = reason
|
|
23
|
+
@klass = klass
|
|
24
|
+
@detail = detail
|
|
25
|
+
message = "cannot export #{klass} to LML: #{reason}"
|
|
26
|
+
message += " (#{detail})" if detail
|
|
27
|
+
super(message)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
module Lml
|
|
6
|
+
# The round-trip gate: the document must re-derive the model it
|
|
7
|
+
# came from. Phase 1's importer-side is a re-export comparison —
|
|
8
|
+
# the recorded model is re-read and re-projected, and the
|
|
9
|
+
# canonical forms must be identical (equal checksums). 1:1-ness
|
|
10
|
+
# is the design's spine; a drift here means the document lost a
|
|
11
|
+
# declaration on the way out.
|
|
12
|
+
class RoundTrip
|
|
13
|
+
def self.verify(document, register: nil)
|
|
14
|
+
new(document, register: register).verify
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(document, register: nil)
|
|
18
|
+
@document = document
|
|
19
|
+
@register = register
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def verify
|
|
23
|
+
klass = resolve_model!
|
|
24
|
+
reexported = Exporter.export(klass, register: @register)
|
|
25
|
+
return reexported if reexported.canonical_json == @document.canonical_json
|
|
26
|
+
|
|
27
|
+
raise RejectedError.new(:round_trip_mismatch, klass,
|
|
28
|
+
"the document does not re-derive the model " \
|
|
29
|
+
"(#{short(@document.checksum)} vs #{short(reexported.checksum)})")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def resolve_model!
|
|
35
|
+
name = @document.model_name
|
|
36
|
+
begin
|
|
37
|
+
Object.const_get(name)
|
|
38
|
+
rescue NameError
|
|
39
|
+
raise RejectedError.new(:unknown_type, name, "the recorded model is not loadable")
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def short(checksum)
|
|
44
|
+
checksum.to_s.slice(0, 20)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
module Lml
|
|
6
|
+
autoload :Document, "#{__dir__}/lml/document"
|
|
7
|
+
autoload :Exporter, "#{__dir__}/lml/exporter"
|
|
8
|
+
autoload :RejectedError, "#{__dir__}/lml/rejected_error"
|
|
9
|
+
autoload :RoundTrip, "#{__dir__}/lml/round_trip"
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# Export a model class's declarations to a neutral LML document
|
|
14
|
+
# (lutaml-model#1033 Phase 1).
|
|
15
|
+
#
|
|
16
|
+
# The document is the model's `attribute` + per-format mapping
|
|
17
|
+
# vocabulary, serialized with sorted keys and pinned by a sha256
|
|
18
|
+
# checksum over the canonical form — the same
|
|
19
|
+
# "declare once, verify everywhere" move PARG made for grammars.
|
|
20
|
+
#
|
|
21
|
+
# Constructor-contract constructs are rejected with named errors
|
|
22
|
+
# (RejectedError): lambda defaults, custom `initialize` bodies,
|
|
23
|
+
# imperative element-order mixes.
|
|
24
|
+
#
|
|
25
|
+
# @param klass [Class] a Serializable model class
|
|
26
|
+
# @param register [Symbol, nil] the register context
|
|
27
|
+
# @return [Lml::Document]
|
|
28
|
+
def export(klass, register: nil)
|
|
29
|
+
Exporter.export(klass, register: register)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The round-trip gate: re-export the model from the document's
|
|
33
|
+
# recorded model name and compare canonical forms — identical
|
|
34
|
+
# declarations, identical checksum.
|
|
35
|
+
def verify(document, register: nil)
|
|
36
|
+
RoundTrip.verify(document, register: register)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -48,9 +48,15 @@ module Lutaml
|
|
|
48
48
|
# Cache for resolve_for_child results.
|
|
49
49
|
# Key: [child_class.object_id, parent_register] → Value: resolved register Symbol or nil.
|
|
50
50
|
# Classes are singletons so object_id is stable for the process lifetime.
|
|
51
|
+
# Concurrent is required here (not at boot): requiring it while a
|
|
52
|
+
# require is still in flight makes concurrent-ruby's optional
|
|
53
|
+
# native-extension requires raise inside RubyGems' unresolved-tree
|
|
54
|
+
# resolution, which walks every installed gem on gem-heavy machines.
|
|
51
55
|
RESOLVE_CACHE = if Lutaml::Model.opal?
|
|
52
56
|
{}
|
|
53
57
|
else
|
|
58
|
+
Lutaml::Model::RuntimeCompatibility
|
|
59
|
+
.require_native("concurrent")
|
|
54
60
|
Concurrent::Map.new
|
|
55
61
|
end
|
|
56
62
|
|
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "runtime_compatibility"
|
|
4
4
|
|
|
5
|
-
Lutaml::Model::RuntimeCompatibility.require_native("concurrent")
|
|
6
|
-
|
|
7
5
|
module Lutaml
|
|
8
6
|
module Model
|
|
9
7
|
# Registry for managing transformation lifecycle and caching.
|
|
@@ -89,6 +87,12 @@ module Lutaml
|
|
|
89
87
|
end
|
|
90
88
|
|
|
91
89
|
def initialize
|
|
90
|
+
# Required lazily: requiring "concurrent" while a require is still
|
|
91
|
+
# in flight makes concurrent-ruby's optional native-extension
|
|
92
|
+
# requires raise inside RubyGems' unresolved-tree resolution,
|
|
93
|
+
# which walks every installed gem on gem-heavy machines and can
|
|
94
|
+
# add minutes to process boot
|
|
95
|
+
Lutaml::Model::RuntimeCompatibility.require_native("concurrent")
|
|
92
96
|
@transformations = {} # Cache for transformation instances (mutex-protected for cycle detection)
|
|
93
97
|
@mappings = if Lutaml::Model.opal?
|
|
94
98
|
{}
|
|
@@ -197,6 +201,7 @@ module Lutaml
|
|
|
197
201
|
# Inner cache level container matching the outer map's engine
|
|
198
202
|
# (Concurrent::Map under GVL-threaded MRI, plain Hash under Opal).
|
|
199
203
|
def new_inner_map
|
|
204
|
+
Lutaml::Model::RuntimeCompatibility.require_native("concurrent")
|
|
200
205
|
Lutaml::Model.opal? ? {} : ::Concurrent::Map.new
|
|
201
206
|
end
|
|
202
207
|
|
data/lib/lutaml/model/version.rb
CHANGED
data/lib/lutaml/model.rb
CHANGED
|
@@ -78,6 +78,7 @@ module Lutaml
|
|
|
78
78
|
autoload :JsonAdapter, "#{__dir__}/model/json_adapter"
|
|
79
79
|
autoload :SchemaLocation, "#{__dir__}/model/schema_location"
|
|
80
80
|
autoload :Validation, "#{__dir__}/model/validation"
|
|
81
|
+
autoload :Lml, "#{__dir__}/model/lml"
|
|
81
82
|
autoload :Choice, "#{__dir__}/model/choice"
|
|
82
83
|
autoload :Sequence, "#{__dir__}/model/sequence"
|
|
83
84
|
autoload :Organization, "#{__dir__}/model/organization"
|
|
@@ -20,9 +20,14 @@ module Lutaml
|
|
|
20
20
|
# The loop body used to re-derive these per parsed instance.
|
|
21
21
|
# Concurrent::Map under threaded MRI, plain Hash under Opal
|
|
22
22
|
# (Concurrent is unavailable there); class identity keys either.
|
|
23
|
+
# Concurrent is required here (not at boot) so its first load never
|
|
24
|
+
# happens inside RubyGems' unresolved-tree resolution (see
|
|
25
|
+
# TransformationRegistry#initialize).
|
|
23
26
|
RULE_RECORDS = if Lutaml::Model.opal?
|
|
24
27
|
{}
|
|
25
28
|
else
|
|
29
|
+
Lutaml::Model::RuntimeCompatibility
|
|
30
|
+
.require_native("concurrent")
|
|
26
31
|
Concurrent::Map.new # rubocop:disable Lint/HashCompareByIdentity
|
|
27
32
|
end
|
|
28
33
|
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model"
|
|
4
|
+
require "json"
|
|
5
|
+
require "yaml"
|
|
6
|
+
|
|
7
|
+
# lutaml-model#1033 Phase 1: the LML declaration exporter. Ruby model →
|
|
8
|
+
# neutral LML document (attributes + per-format mappings), checksummed
|
|
9
|
+
# over the canonical form, with the round-trip gate pinned from day one.
|
|
10
|
+
# Named fixture classes: the document addresses models by class path,
|
|
11
|
+
# so anonymous classes are unnameable (the exporter rejects them).
|
|
12
|
+
module LmlSpecFixtures # rubocop:disable Lint/ConstantDefinitionInBlock -- named fixtures, see below
|
|
13
|
+
class Dimension < Lutaml::Model::Serializable
|
|
14
|
+
attribute :si_name, :string
|
|
15
|
+
attribute :si_symbol, :string
|
|
16
|
+
|
|
17
|
+
xml do
|
|
18
|
+
root "dimension"
|
|
19
|
+
map_attribute "siName", to: :si_name
|
|
20
|
+
map_element "siSymbol", to: :si_symbol
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class Unit < Lutaml::Model::Serializable
|
|
25
|
+
attribute :id, :string
|
|
26
|
+
attribute :quantity_type, :string, values: %w[length mass time]
|
|
27
|
+
attribute :dimensions, Dimension, collection: 0..10
|
|
28
|
+
attribute :symbol, :string, default: "u"
|
|
29
|
+
|
|
30
|
+
xml do
|
|
31
|
+
root "unit"
|
|
32
|
+
map_attribute "id", to: :id
|
|
33
|
+
map_attribute "quantityType", to: :quantity_type
|
|
34
|
+
map_element "dimension", to: :dimensions
|
|
35
|
+
map_element "symbol", to: :symbol
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
json do
|
|
39
|
+
map "id", to: :id
|
|
40
|
+
map "quantityType", to: :quantity_type
|
|
41
|
+
map "dimensions", to: :dimensions
|
|
42
|
+
map "symbol", to: :symbol
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class OrderedMixed < Lutaml::Model::Serializable
|
|
47
|
+
attribute :text, :string
|
|
48
|
+
attribute :other, :string
|
|
49
|
+
|
|
50
|
+
xml do
|
|
51
|
+
root "mixed", ordered: true
|
|
52
|
+
map_element "text", to: :text
|
|
53
|
+
map_element "other", to: :other
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
RSpec.describe Lutaml::Model::Lml do
|
|
59
|
+
let(:unit_class) { LmlSpecFixtures::Unit }
|
|
60
|
+
|
|
61
|
+
def export(klass)
|
|
62
|
+
described_class.export(klass)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "projects attributes with types, collections, defaults and enums" do
|
|
66
|
+
document = export(unit_class)
|
|
67
|
+
dimensions = document.attributes.find { |attr| attr["name"] == "dimensions" }
|
|
68
|
+
symbol = document.attributes.find { |attr| attr["name"] == "symbol" }
|
|
69
|
+
quantity = document.attributes.find { |attr| attr["name"] == "quantity_type" }
|
|
70
|
+
|
|
71
|
+
expect(dimensions).to include(
|
|
72
|
+
"type" => "LmlSpecFixtures::Dimension",
|
|
73
|
+
"collection" => { "min" => 0, "max" => 10 },
|
|
74
|
+
)
|
|
75
|
+
expect(symbol).to include("default" => "u")
|
|
76
|
+
expect(quantity).to include("values" => %w[length mass time])
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "projects per-format mappings, not per-DSL blocks" do
|
|
80
|
+
document = export(unit_class)
|
|
81
|
+
expect(document.mappings.keys).to eq(["xml", "json"])
|
|
82
|
+
|
|
83
|
+
xml = document.mappings["xml"]
|
|
84
|
+
expect(xml["root"]).to include("element" => "unit")
|
|
85
|
+
expect(xml["rules"]).to include(
|
|
86
|
+
{ "kind" => "attribute", "name" => ["id"], "to" => "id" },
|
|
87
|
+
)
|
|
88
|
+
expect(document.mappings["json"]["rules"]).to include(
|
|
89
|
+
{ "key" => ["id"], "to" => "id" },
|
|
90
|
+
)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it "pins a checksum over the canonical form" do
|
|
94
|
+
document = export(unit_class)
|
|
95
|
+
expect(document.checksum).to start_with("sha256:")
|
|
96
|
+
expect(export(unit_class).checksum).to eq(document.checksum)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "is byte-stable across renders and key order" do
|
|
100
|
+
document = export(unit_class)
|
|
101
|
+
expect(document.to_yaml).to include("model:")
|
|
102
|
+
expect(JSON.parse(document.to_json)).to eq(JSON.parse(document.canonical_json))
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "round-trips: the document re-derives the model" do
|
|
106
|
+
document = export(unit_class)
|
|
107
|
+
verified = described_class.verify(
|
|
108
|
+
Lutaml::Model::Lml::Document.from_hash(document.to_h),
|
|
109
|
+
)
|
|
110
|
+
expect(verified.checksum).to eq(document.checksum)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "rejects lambda defaults" do
|
|
114
|
+
klass = Class.new(Lutaml::Model::Serializable) do
|
|
115
|
+
attribute :x, :string, default: -> { "d" }
|
|
116
|
+
end
|
|
117
|
+
expect { export(klass) }.to raise_error(
|
|
118
|
+
Lutaml::Model::Lml::RejectedError,
|
|
119
|
+
/lambda_default/,
|
|
120
|
+
)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "rejects a custom initialize" do
|
|
124
|
+
klass = Class.new(Lutaml::Model::Serializable) do
|
|
125
|
+
attribute :x, :string
|
|
126
|
+
|
|
127
|
+
def initialize(attrs = {})
|
|
128
|
+
super
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
expect { export(klass) }.to raise_error(
|
|
132
|
+
Lutaml::Model::Lml::RejectedError,
|
|
133
|
+
/custom_initialize/,
|
|
134
|
+
)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
it "rejects ordered content with no declared sequence" do
|
|
138
|
+
expect { export(LmlSpecFixtures::OrderedMixed) }.to raise_error(
|
|
139
|
+
Lutaml::Model::Lml::RejectedError,
|
|
140
|
+
/imperative_element_order/,
|
|
141
|
+
)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it "rejects a non-serializable class" do
|
|
145
|
+
expect { export(String) }.to raise_error(
|
|
146
|
+
Lutaml::Model::Lml::RejectedError,
|
|
147
|
+
/not_serializable/,
|
|
148
|
+
)
|
|
149
|
+
end
|
|
150
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lutaml-model
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.161
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
@@ -531,6 +531,11 @@ files:
|
|
|
531
531
|
- lib/lutaml/model/liquid.rb
|
|
532
532
|
- lib/lutaml/model/liquid/indexed_access.rb
|
|
533
533
|
- lib/lutaml/model/liquid/mapping.rb
|
|
534
|
+
- lib/lutaml/model/lml.rb
|
|
535
|
+
- lib/lutaml/model/lml/document.rb
|
|
536
|
+
- lib/lutaml/model/lml/exporter.rb
|
|
537
|
+
- lib/lutaml/model/lml/rejected_error.rb
|
|
538
|
+
- lib/lutaml/model/lml/round_trip.rb
|
|
534
539
|
- lib/lutaml/model/mapping/listener.rb
|
|
535
540
|
- lib/lutaml/model/mapping/mapping.rb
|
|
536
541
|
- lib/lutaml/model/mapping/mapping_rule.rb
|
|
@@ -1810,6 +1815,7 @@ files:
|
|
|
1810
1815
|
- spec/lutaml/model/liquefiable_spec.rb
|
|
1811
1816
|
- spec/lutaml/model/liquid/indexed_access_spec.rb
|
|
1812
1817
|
- spec/lutaml/model/liquid_compatibility_spec.rb
|
|
1818
|
+
- spec/lutaml/model/lml_exporter_spec.rb
|
|
1813
1819
|
- spec/lutaml/model/logger_spec.rb
|
|
1814
1820
|
- spec/lutaml/model/map_all_attributes_spec.rb
|
|
1815
1821
|
- spec/lutaml/model/map_all_spec.rb
|