lutaml-model 0.8.195 → 0.8.197

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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/docs/_references/plan-schema-spec.adoc +308 -0
  3. data/lib/compat/opal/lutaml_model_boot.rb +2 -0
  4. data/lib/lutaml/lml/format/adapter/standard_adapter.rb +10 -3
  5. data/lib/lutaml/lml/models/top_element_attribute.rb +7 -2
  6. data/lib/lutaml/model/serialize/annotations.rb +74 -0
  7. data/lib/lutaml/model/serialize/builder.rb +2 -0
  8. data/lib/lutaml/model/serialize/initialization.rb +20 -1
  9. data/lib/lutaml/model/serialize/tracking_default_hash.rb +64 -0
  10. data/lib/lutaml/model/serialize.rb +57 -0
  11. data/lib/lutaml/model/version.rb +1 -1
  12. data/lib/lutaml/xml/adapter_element.rb +5 -1
  13. data/lib/lutaml/xml/mapping.rb +15 -0
  14. data/lib/lutaml/xml/schema/xsd/attribute.rb +3 -0
  15. data/lib/lutaml/xml/schema/xsd/element.rb +3 -0
  16. data/lib/lutaml/xml/schema/xsd/max_scale.rb +25 -0
  17. data/lib/lutaml/xml/schema/xsd/min_scale.rb +25 -0
  18. data/lib/lutaml/xml/schema/xsd/restriction_simple_type.rb +9 -0
  19. data/lib/lutaml/xml/schema/xsd/vc_conditional_attributes.rb +62 -0
  20. data/lib/lutaml/xml/schema/xsd.rb +6 -2
  21. data/lib/lutaml/xml/schema/xsd_schema.rb +2 -0
  22. data/spec/lml/lutaml/lml/format_adapter_spec.rb +24 -0
  23. data/spec/lml/lutaml/lml/json_transfer_spec.rb +188 -0
  24. data/spec/lml/lutaml/lml/property_laws_spec.rb +3 -38
  25. data/spec/lml/support/lml_property_support.rb +168 -0
  26. data/spec/lutaml/model/annotations_spec.rb +97 -0
  27. data/spec/lutaml/model/mutable_default_spec.rb +110 -0
  28. data/spec/lutaml/xml/adapter_element_verbatim_spec.rb +44 -0
  29. data/spec/lutaml/xml/plan_mixed_content_nested_namespace_spec.rb +2 -2
  30. data/spec/lutaml/xml/schema/xsd_scale_vc_spec.rb +59 -0
  31. metadata +14 -2
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.195"
5
+ VERSION = "0.8.197"
6
6
  end
7
7
  end
@@ -88,7 +88,11 @@ module Lutaml
88
88
  end
89
89
 
90
90
  def to_xml(_builder = nil)
91
- @moxml_node.to_xml(declaration: false, expand_empty: false)
91
+ # Verbatim raw capture (#223): the markup must come back byte-for-byte,
92
+ # so serialization opts out of the context's default indent — a
93
+ # formatted emission would compound onto the source's own
94
+ # whitespace-only text nodes and corrupt the captured subtree.
95
+ @moxml_node.to_xml(declaration: false, expand_empty: false, indent: 0)
92
96
  end
93
97
 
94
98
  def build_xml(builder = nil)
@@ -896,6 +896,21 @@ module Lutaml
896
896
  @all_attributes_rule = AllAttributesMapping.new(to, matching)
897
897
  end
898
898
 
899
+ # Declare the six XML Schema Versioning conditional-inclusion
900
+ # attributes on this mapping (XSD 1.1 §4.2, lutaml-model#1281).
901
+ # The model carries the vc_* attributes and the *_from/*_to
902
+ # handlers via VcConditionalAttributes; this only wires the
903
+ # spellings, so any component model opts in with one line.
904
+ def map_vc_attributes
905
+ vc = Lutaml::Xml::Schema::Xsd::VcConditionalAttributes
906
+ vc::VC_ATTRIBUTES.each do |attr|
907
+ spelling = vc::VC_SPELLINGS[attr]
908
+ map_attribute spelling, to: attr,
909
+ with: { from: :"#{attr}_from",
910
+ to: :"#{attr}_to" }
911
+ end
912
+ end
913
+
899
914
  def sequence(&block)
900
915
  @element_sequence << ::Lutaml::Model::Sequence.new(self,
901
916
  format: :xml).tap do |s|
@@ -5,6 +5,8 @@ module Lutaml
5
5
  module Schema
6
6
  module Xsd
7
7
  class Attribute < Base
8
+ include VcConditionalAttributes
9
+
8
10
  attribute :id, :string
9
11
  attribute :use, :string, values: %w[required prohibited optional], default: -> {
10
12
  "optional"
@@ -30,6 +32,7 @@ module Lutaml
30
32
  map_attribute :type, to: :type
31
33
  map_attribute :form, to: :form
32
34
  map_attribute :fixed, to: :fixed
35
+ map_vc_attributes
33
36
  map_attribute :default, to: :default
34
37
  map_element :annotation, to: :annotation
35
38
  map_element :simpleType, to: :simple_type
@@ -5,6 +5,8 @@ module Lutaml
5
5
  module Schema
6
6
  module Xsd
7
7
  class Element < Base
8
+ include VcConditionalAttributes
9
+
8
10
  attribute :id, :string
9
11
  attribute :ref, :string
10
12
  attribute :name, :string
@@ -49,6 +51,7 @@ module Lutaml
49
51
  map_attribute :substitutionGroup, to: :substitution_group
50
52
  map_element :complexType, to: :complex_type
51
53
  map_element :simpleType, to: :simple_type
54
+ map_vc_attributes
52
55
  map_element :annotation, to: :annotation
53
56
  map_element :keyref, to: :keyref
54
57
  map_element :unique, to: :unique
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Xml
5
+ module Schema
6
+ module Xsd
7
+ class MaxScale < Base
8
+ attribute :fixed, :string
9
+ attribute :value, :integer
10
+
11
+ xml do
12
+ element "maxScale"
13
+ mixed_content
14
+ namespace Lutaml::Xml::Schema::XsdNamespace
15
+
16
+ map_attribute :value, to: :value
17
+ map_attribute :fixed, to: :fixed
18
+ end
19
+
20
+ Lutaml::Xml::Schema::Xsd.register_model(self, :max_scale)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Xml
5
+ module Schema
6
+ module Xsd
7
+ class MinScale < Base
8
+ attribute :fixed, :string
9
+ attribute :value, :integer
10
+
11
+ xml do
12
+ element "minScale"
13
+ mixed_content
14
+ namespace Lutaml::Xml::Schema::XsdNamespace
15
+
16
+ map_attribute :value, to: :value
17
+ map_attribute :fixed, to: :fixed
18
+ end
19
+
20
+ Lutaml::Xml::Schema::Xsd.register_model(self, :min_scale)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -5,6 +5,8 @@ module Lutaml
5
5
  module Schema
6
6
  module Xsd
7
7
  class RestrictionSimpleType < Base
8
+ include VcConditionalAttributes
9
+
8
10
  attribute :id, :string
9
11
  attribute :base, :string
10
12
  attribute :annotation, :annotation
@@ -21,6 +23,10 @@ module Lutaml
21
23
  initialize_empty: true
22
24
  attribute :total_digits, :total_digits, collection: true,
23
25
  initialize_empty: true
26
+ attribute :min_scale, :min_scale, collection: true,
27
+ initialize_empty: true
28
+ attribute :max_scale, :max_scale, collection: true,
29
+ initialize_empty: true
24
30
  attribute :max_exclusive, :max_exclusive, collection: true,
25
31
  initialize_empty: true
26
32
  attribute :min_exclusive, :min_exclusive, collection: true,
@@ -50,6 +56,9 @@ module Lutaml
50
56
  map_element :simple_type, to: :simple_type
51
57
  map_element :enumeration, to: :enumeration
52
58
  map_element :totalDigits, to: :total_digits
59
+ map_vc_attributes
60
+ map_element :minScale, to: :min_scale
61
+ map_element :maxScale, to: :max_scale
53
62
  map_element :maxExclusive, to: :max_exclusive
54
63
  map_element :minExclusive, to: :min_exclusive
55
64
  map_element :maxInclusive, to: :max_inclusive
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Xml
5
+ module Schema
6
+ module Xsd
7
+ # The component-level conditional-inclusion attributes of the
8
+ # XML Schema Versioning namespace (XSD 1.1 §4.2, issue #1281).
9
+ #
10
+ # The schema ROOT carries vc:minVersion/vc:maxVersion (mapped
11
+ # on Schema since #1222); every component declaration can carry
12
+ # all six spellings. Including this module declares the six
13
+ # attributes; the model's `xml do` block adds the
14
+ # map_vc_attributes lines.
15
+ module VcConditionalAttributes
16
+ VC_ATTRIBUTES = %i[
17
+ vc_type_available
18
+ vc_type_unavailable
19
+ vc_facet_available
20
+ vc_facet_unavailable
21
+ vc_min_version
22
+ vc_max_version
23
+ ].freeze
24
+
25
+ VC_SPELLINGS = {
26
+ vc_type_available: "typeAvailable",
27
+ vc_type_unavailable: "typeUnavailable",
28
+ vc_facet_available: "facetAvailable",
29
+ vc_facet_unavailable: "facetUnavailable",
30
+ vc_min_version: "minVersion",
31
+ vc_max_version: "maxVersion",
32
+ }.freeze
33
+
34
+ def self.included(base)
35
+ VC_ATTRIBUTES.each do |name|
36
+ base.attribute name, :string
37
+ end
38
+ end
39
+
40
+ VC_ATTRIBUTES.each do |name|
41
+ spelling = VC_SPELLINGS[name]
42
+
43
+ define_method(:"#{name}_from") do |model, value|
44
+ model.public_send(:"#{name}=", value)
45
+ end
46
+
47
+ define_method(:"#{name}_to") do |model, parent, _wrapper|
48
+ value = model.public_send(name)
49
+ return if value.nil?
50
+
51
+ parent.add_attribute(
52
+ ::Lutaml::Xml::DataModel::XmlAttribute.new(
53
+ spelling, value, Lutaml::Xml::Schema::VcNamespace
54
+ ),
55
+ )
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -33,8 +33,8 @@ module Lutaml
33
33
  AttributeGroup Choice ComplexContent ComplexType Documentation
34
34
  Element Enumeration ExtensionComplexContent ExtensionSimpleContent
35
35
  Field FractionDigits Glob Group Import Include Key Keyref Length List
36
- MaxExclusive MaxInclusive MaxLength MinExclusive MinInclusive
37
- MinLength Notation OpenContent Pattern Redefine
36
+ MaxExclusive MaxInclusive MaxLength MaxScale MinExclusive MinInclusive
37
+ MinLength MinScale Notation OpenContent Pattern Redefine
38
38
  RestrictionComplexContent RestrictionSimpleContent
39
39
  RestrictionSimpleType Schema SchemaPath Selector Sequence SimpleContent
40
40
  SimpleType TotalDigits Union Unique WhiteSpace
@@ -42,6 +42,8 @@ module Lutaml
42
42
  ].freeze
43
43
 
44
44
  # Autoload all XSD model files (lazy loading)
45
+ autoload :VcConditionalAttributes,
46
+ "#{__dir__}/xsd/vc_conditional_attributes"
45
47
  autoload :VERSION, "#{__dir__}/xsd/version"
46
48
  autoload :Error, "#{__dir__}/xsd/errors"
47
49
  autoload :SchemaValidator, "#{__dir__}/xsd/schema_validator"
@@ -95,6 +97,8 @@ module Lutaml
95
97
  autoload :MinExclusive, "#{__dir__}/xsd/min_exclusive"
96
98
  autoload :MinInclusive, "#{__dir__}/xsd/min_inclusive"
97
99
  autoload :MinLength, "#{__dir__}/xsd/min_length"
100
+ autoload :MinScale, "#{__dir__}/xsd/min_scale"
101
+ autoload :MaxScale, "#{__dir__}/xsd/max_scale"
98
102
  autoload :Notation, "#{__dir__}/xsd/notation"
99
103
  autoload :Pattern, "#{__dir__}/xsd/pattern"
100
104
  autoload :Redefine, "#{__dir__}/xsd/redefine"
@@ -693,6 +693,8 @@ skip_unmapped: false)
693
693
  emit_facet(xml, :length, facets[:length])
694
694
  emit_facet(xml, :minLength, facets[:min_length])
695
695
  emit_facet(xml, :maxLength, facets[:max_length])
696
+ emit_facet(xml, :minScale, facets[:min_scale])
697
+ emit_facet(xml, :maxScale, facets[:max_scale])
696
698
  Array(facets[:enumeration]).each do |value|
697
699
  emit_facet(xml, :enumeration, lexical_value(type, value))
698
700
  end
@@ -269,4 +269,28 @@ RSpec.describe "LML format adapter" do
269
269
  expect(model_class.new).to respond_to(:to_lml)
270
270
  end
271
271
  end
272
+
273
+ describe "reference cargo after a JSON reload" do
274
+ let(:adapter) { Lutaml::Lml::Format::Adapter::StandardAdapter }
275
+
276
+ it "emits string-keyed reference cargo as ref:(...)" do
277
+ lml = adapter.new({ "glaze" => { "reference" => "Glazes.blue_matte" } }).to_lml
278
+
279
+ expect(lml).to include("glaze = ref:(Glazes.blue_matte)")
280
+ expect(lml).not_to match(/instance \{\s*reference/)
281
+ end
282
+
283
+ it "emits string-keyed reference items in a list as ref:(...)" do
284
+ lml = adapter.new({ "glazes" => [{ "reference" => "Glazes.a" }] }).to_lml
285
+
286
+ expect(lml).to include("ref:(Glazes.a)")
287
+ expect(lml).not_to match(/instance \{\s*reference/)
288
+ end
289
+
290
+ it "emits symbol-keyed reference cargo the same way" do
291
+ lml = adapter.new({ glaze: { reference: "Glazes.blue_matte" } }).to_lml
292
+
293
+ expect(lml).to include("glaze = ref:(Glazes.blue_matte)")
294
+ end
295
+ end
272
296
  end
@@ -0,0 +1,188 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "json"
5
+ require_relative "../../support/lml_property_support"
6
+
7
+ # JSON transfer of LML documents (lutaml-lml#100, slice 1): document, class,
8
+ # enum, attribute, package, mapping-block and instance nodes. Interleaving
9
+ # order, the XML namespace and the XML/YAML bindings are open questions and
10
+ # are not asserted here.
11
+ RSpec.describe "LML JSON transfer" do
12
+ include LmlDocumentGenerator
13
+
14
+ # Defaults the model materialises on load; a rendering may omit them.
15
+ let(:defaults) do
16
+ { "visibility" => "public", "literal" => false, "is_derived" => false,
17
+ "is_static" => false, "is_read_only" => false, "is_abstract" => false,
18
+ "keyword" => "enumeration", "mixed_content" => false }
19
+ end
20
+
21
+ def parse(source)
22
+ Lutaml::Lml.parse_document(source)
23
+ end
24
+
25
+ def graph(document)
26
+ strip(document.to_hash)
27
+ end
28
+
29
+ # Only model defaults (and nil, which a rendering omits) are dropped;
30
+ # literal payloads such as `value => []` or `{}` are compared as is.
31
+ def strip(node)
32
+ case node
33
+ when Hash
34
+ node.each_with_object({}) do |(key, value), result|
35
+ next if defaults[key] == value || value.nil?
36
+
37
+ # JSON has no symbols: a parsed map literal's symbol keys read back as strings.
38
+ result[key.to_s] = strip(value)
39
+ end
40
+ when Array then node.map { |item| strip(item) }
41
+ else node
42
+ end
43
+ end
44
+
45
+ def transfer(document)
46
+ Lutaml::Lml::Document.from_json(document.to_json)
47
+ end
48
+
49
+ describe "a declared model" do
50
+ let(:source) do
51
+ <<~LML
52
+ lml "1.0"
53
+
54
+ models Shop {
55
+ class Item < Base {
56
+ definition "a thing for sale"
57
+ attribute price, Float { cardinality 0..n }
58
+ attribute kind, String { values { "a", "b" } }
59
+ attribute sku, String { pattern /a[0-9]+/ }
60
+ attribute note, String { default: "none" }
61
+ }
62
+
63
+ enum Grade {
64
+ definition "quality"
65
+ value "good" {
66
+ definition "member"
67
+ }
68
+ value ok
69
+ }
70
+
71
+ package Stock {
72
+ class Bin { attribute x, String }
73
+ enum Size { value "s" }
74
+ }
75
+ }
76
+ LML
77
+ end
78
+ let(:document) { parse(source) }
79
+ let(:json) { JSON.parse(document.to_json) }
80
+
81
+ it "renders every node kind as JSON keys" do
82
+ names = json.values_at("classes", "enums", "packages").flatten.map { |n| n["name"] }
83
+
84
+ expect(names).to include("Item", "Grade", "Stock")
85
+ expect(json["packages"].first.values_at("classes", "enums").flatten.map { |n| n["name"] })
86
+ .to contain_exactly("Bin", "Size")
87
+ end
88
+
89
+ it "keeps class parent, definition and attribute facets" do
90
+ item = json["classes"].find { |c| c["name"] == "Item" }
91
+ facets = item["attributes"].to_h { |a| [a["name"], a] }
92
+
93
+ expect(item).to include("parent_class" => "Base", "definition" => "a thing for sale")
94
+ expect(facets["price"]["cardinality"]).to include("min" => "0", "max" => "n")
95
+ expect(facets["kind"]["value_set"]).to eq(%w[a b])
96
+ expect(facets["sku"]["pattern"]).to include("a[0-9]+")
97
+ expect(facets["note"]["properties"]).to include(a_hash_including("name" => "default", "value" => "none"))
98
+ end
99
+
100
+ it "parses back to the identical document graph" do
101
+ expect(graph(transfer(document))).to eq(graph(document))
102
+ end
103
+
104
+ it "reaches a fixed point after one transfer" do
105
+ once = transfer(document).to_json
106
+
107
+ expect(Lutaml::Lml::Document.from_json(once).to_json).to eq(once)
108
+ end
109
+ end
110
+
111
+ describe "the RS 3001 corpus" do
112
+ corpus = Dir[File.expand_path("../../fixtures/rs3001/*.lml", __dir__)]
113
+ .map { |path| File.basename(path, ".lml") }.sort
114
+
115
+ corpus.each do |name|
116
+ it "round-trips #{name}" do
117
+ path = File.expand_path("../../fixtures/rs3001/#{name}.lml", __dir__)
118
+ document = parse(File.read(path))
119
+
120
+ expect(graph(transfer(document))).to eq(graph(document))
121
+ end
122
+ end
123
+ end
124
+
125
+ describe "empty literal values" do
126
+ let(:source) do
127
+ "instances Data {\n Row \"r\" { list = [] }\n}\n"
128
+ end
129
+
130
+ def list(document)
131
+ document.instances.instances.first.attributes.find { |a| a.name == "list" }
132
+ end
133
+
134
+ it "keeps an empty list value through a JSON round trip" do
135
+ document = parse(source)
136
+ reloaded = transfer(document)
137
+
138
+ expect(list(document).to_hash).to include("value" => [])
139
+ expect(list(reloaded).to_hash).to include("value" => [])
140
+ expect(strip(list(reloaded).to_hash)).to eq(strip(list(document).to_hash))
141
+ end
142
+
143
+ it "does not strip a literal empty value when comparing graphs" do
144
+ with_empty = { "attributes" => [{ "name" => "a", "value" => [] }] }
145
+ without = { "attributes" => [{ "name" => "a" }] }
146
+
147
+ expect(strip(with_empty)).not_to eq(strip(without))
148
+ expect(strip(with_empty)).to eq(with_empty)
149
+ end
150
+ end
151
+
152
+ describe "references" do
153
+ let(:source) do
154
+ "instances Glazes {\n GlazeFormula \"blue_matte\" { a = 1 }\n " \
155
+ "CeramicTile \"t\" { glaze = ref:(Glazes.blue_matte) }\n}\n"
156
+ end
157
+
158
+ def glaze(document)
159
+ document.instances.instances.last.attributes.first
160
+ end
161
+
162
+ it "keeps the typed reference after a JSON reload" do
163
+ reloaded = transfer(parse(source))
164
+
165
+ expect(glaze(reloaded).reference&.path).to eq("Glazes.blue_matte")
166
+ end
167
+
168
+ it "gives the same typed reference before and after" do
169
+ document = parse(source)
170
+
171
+ expect(glaze(transfer(document)).reference&.path).to eq(glaze(document).reference&.path)
172
+ end
173
+ end
174
+
175
+ describe "generated documents" do
176
+ it "round-trips whole documents through JSON" do
177
+ warn "LML property seed: #{PropertyLawsSupport::SEED}"
178
+ property = property_of { Tuple.new(Array.new(10) { range(0, 24) }) }
179
+
180
+ property.check(PropertyLawsSupport::COUNT) do |params|
181
+ source = generated_document(params)
182
+ document = parse(source)
183
+
184
+ expect(graph(transfer(document))).to eq(graph(document)), "source:\n#{source}"
185
+ end
186
+ end
187
+ end
188
+ end
@@ -8,46 +8,11 @@ require "rantly"
8
8
  require "rantly/rspec_extensions"
9
9
  require "rantly/shrinks"
10
10
  require "lutaml/lml/format"
11
-
12
- # Constants shared by the generative laws (TODO.next/sota/properties.md).
13
- # Seeded and reproducible: the seed is printed before every property run,
14
- # and Rantly shrinks the generated tuple on failure. PR runs use a
15
- # reduced count and depth; the nightly workflow raises both through
16
- # LML_PROPERTY_COUNT / LML_PROPERTY_DEPTH and supplies a fresh seed.
17
- module PropertyLawsSupport
18
- SEED = Integer(ENV.fetch("LML_PROPERTY_SEED", "20261020"))
19
- COUNT = Integer(ENV.fetch("LML_PROPERTY_COUNT", "40"))
20
- DEPTH = Integer(ENV.fetch("LML_PROPERTY_DEPTH", "2"))
21
-
22
- TYPES = {
23
- string: :string, integer: :integer, float: :float, boolean: :boolean
24
- }.freeze
25
-
26
- # Values that a bare-word emission would read back as something other
27
- # than a string (or not read at all).
28
- EDGE_STRINGS = [
29
- "", "null", "true", "false", "01", "1.50", "2dCi9o", "0x10",
30
- "with space", "a-b", 'quote"x', "back\\slash", " padded ",
31
- "line\nbreak", "tab\there", "üñï", "🎨", "12", "-3"
32
- ].freeze
33
-
34
- STRING_GEN = proc do
35
- case choose(:print, :ascii, :edge)
36
- when :print then string(:print)
37
- when :ascii then string(:ascii)
38
- else choose(*EDGE_STRINGS)
39
- end
40
- end
41
-
42
- # The document-model comparison defined with TODO.canon/5.
43
- DEFAULT_VALUES = {
44
- "visibility" => "public", "literal" => false, "is_derived" => false,
45
- "is_static" => false, "is_read_only" => false, "is_abstract" => false,
46
- "direction" => "in"
47
- }.freeze
48
- end
11
+ require_relative "../../support/lml_property_support"
49
12
 
50
13
  RSpec.describe "LML round-trip properties" do
14
+ include LmlDocumentGenerator
15
+
51
16
  def report_seed
52
17
  warn "LML property seed: #{PropertyLawsSupport::SEED}"
53
18
  end