lutaml-model 0.8.158 → 0.8.160

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74c783e3897939ae61d1cbd03782641f0ed8facb94ed8098588a7491504db44c
4
- data.tar.gz: c30df2f8628c15e232559c658ca820e1d4597b976a87d98936703c64bf99d5c9
3
+ metadata.gz: 2192a251ac90d3d81a496f34a1a359efafe9a61effac573c71d4af23fa1422d7
4
+ data.tar.gz: 460032d909391c961264243ea659804a9ffc98c6683a8b5d1e1c7799ade93485
5
5
  SHA512:
6
- metadata.gz: 2c48df71e7b19c791954371707b6ff671f2fd234f8de0be7ef4c5dbce1838b7ab349b669124793c15037f6bdd788a9260924e195cfc1d34ca779ecc9f07471ab
7
- data.tar.gz: f4069257888fb2b6c9fa5e9bd8913fd7bb2b0a369c5d65336f154902e96b5d5e3295c182adef140e78a0cb3ee008d8a8dceaefa0563fee71ee16a70bf3309c1d
6
+ metadata.gz: 788ffce88c22821c9fe1c2684fa241852b0a68550dc28cb6e3d07dda862851aac9e00b37700ec3a3c8cf532202691681ce52bd27a2ad0135b3fd1cb5decb1907
7
+ data.tar.gz: 7e10d6d2776f20771a4b1f26640668b56c97534b5b18fd1342adf1a22f52a190168b67a03a335067bc9f99e72bdb63a10a132ce2c053bc898bd0493f1aa4754b
@@ -18,10 +18,10 @@ on:
18
18
  types: [ release-passed ]
19
19
 
20
20
  jobs:
21
- # Most dependents are pure data/format gems: their suites invoke no
22
- # external tools, so skipping the tool install removes the Windows
23
- # Chocolatey flake surface (choco CDN 504s killed cells pre-rspec,
24
- # e.g. the ukiryu Windows leg) for the majority of the matrix.
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"
@@ -114,6 +114,7 @@ model_class: nil)
114
114
  # @param options [Hash] Serialization options
115
115
  # @return [Hash, nil] The serialized hash or nil if empty
116
116
  def serialize_nested_model(value, rule, options = {})
117
+ value = coerce_nested_for_rule(value, rule)
117
118
  validate_nested_model_type!(value, rule)
118
119
 
119
120
  # Determine the actual type for polymorphism support
@@ -152,6 +153,7 @@ model_class: nil)
152
153
  # For Serializable types, use to_#{format} method
153
154
  if rule.attribute_type.is_a?(Class) &&
154
155
  rule.attribute_type < Lutaml::Model::Serialize
156
+ value = coerce_nested_for_rule(value, rule)
155
157
  validate_serializable_type!(value, rule)
156
158
  return value.public_send(:"to_#{format}")
157
159
  end
@@ -245,6 +247,25 @@ model_class: nil)
245
247
 
246
248
  private
247
249
 
250
+ # Coerce a value the model accepted into the rule's declared
251
+ # nested class (#1089) before the validators run. Registered
252
+ # type substitutions pass through untouched — the validators
253
+ # accept them as-is.
254
+ #
255
+ # @param value [Object] The stored attribute value
256
+ # @param rule [CompiledRule] The compiled rule
257
+ # @return [Object] The coerced value, or the value unchanged
258
+ def coerce_nested_for_rule(value, rule)
259
+ expected = rule.attribute_type.model || rule.attribute_type
260
+ return value if value.is_a?(expected)
261
+
262
+ context = Lutaml::Model::GlobalContext.context(register_id)
263
+ return value if context.substitution_for(rule.attribute_type)
264
+ .any? { |s| s.to_type == value.class }
265
+
266
+ NestedValueCoercion.coerce(value, expected)
267
+ end
268
+
248
269
  # Validate that a nested model value matches the expected type.
249
270
  #
250
271
  # @param value [Object] The value to validate
@@ -15,6 +15,44 @@ module Lutaml
15
15
  #
16
16
  # This is a critical step toward symmetric OOP architecture
17
17
  # across all serialization formats.
18
+ # Coerces values the model layer accepted into a rule's declared
19
+ # nested type (lutaml-model#1089): constructors and writers accept
20
+ # raw hashes, sibling Serializable instances, and content scalars,
21
+ # but the KeyValue serializer demanded exact declared-class
22
+ # instances — accept-then-explode mid-render. Hashes construct
23
+ # directly; Serializable siblings round-trip through the framework
24
+ # hash; scalars wrap onto the declared type's content attribute
25
+ # when its XML mapping declares one (`map_content to: :x` — the
26
+ # same semantics the XML lane applies to text). Genuinely
27
+ # un-coercible values pass through unchanged so the caller's
28
+ # validation raises with its own message.
29
+ module NestedValueCoercion
30
+ @content_attributes = {}.compare_by_identity
31
+
32
+ # The attribute a declared type's XML mapping designates as its
33
+ # textual content. Class-level memo: the answer is a static
34
+ # per-type mapping fact. Keyed by type only; register-specific
35
+ # content mappings are not a supported combination.
36
+ def self.content_attribute_for(type)
37
+ @content_attributes[type] ||= begin
38
+ mapping = type.mappings_for(:xml)
39
+ mapping&.content_mapping&.to
40
+ end
41
+ end
42
+
43
+ def self.coerce(value, declared_class)
44
+ if value.is_a?(::Hash)
45
+ declared_class.new(value)
46
+ elsif value.is_a?(Lutaml::Model::Serialize)
47
+ declared_class.new(value.to_hash)
48
+ elsif (content_key = content_attribute_for(declared_class))
49
+ declared_class.new(content_key => value)
50
+ else
51
+ value
52
+ end
53
+ end
54
+ end
55
+
18
56
  class Transformation < Lutaml::Model::Transformation
19
57
  # Builtin Value types whose cast of an already-native value is the
20
58
  # identity — used by the serialize_value fast path.
@@ -1037,6 +1075,10 @@ child_mappings, options)
1037
1075
  # @param value [Object] The value
1038
1076
  # @param options [Hash] Options
1039
1077
  # @return [Object] The value (could be Hash, primitive, or KeyValueElement)
1078
+ def coerce_accepted_nested_value(value, declared_class)
1079
+ NestedValueCoercion.coerce(value, declared_class)
1080
+ end
1081
+
1040
1082
  # Child transformations memoized on this transformation instance:
1041
1083
  # the factory binds this instance's mapping/format/register, so
1042
1084
  # the memo is register-safe, and building one per serialized node
@@ -1083,15 +1125,21 @@ child_mappings, options)
1083
1125
  # Mapper class: value should be an instance of the mapped model
1084
1126
  # or a valid substituted type
1085
1127
  unless value.is_a?(rule.attribute_type.model) || uses_type_substitution
1086
- msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a '#{rule.attribute_type.model}'"
1087
- raise Lutaml::Model::IncorrectModelError, msg
1128
+ value = coerce_accepted_nested_value(value, rule.attribute_type.model)
1129
+ unless value.is_a?(rule.attribute_type.model)
1130
+ msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a '#{rule.attribute_type.model}'"
1131
+ raise Lutaml::Model::IncorrectModelError, msg
1132
+ end
1088
1133
  end
1089
1134
  else
1090
1135
  # Regular Serializable class: value should be a Serialize instance
1091
1136
  # or a valid substituted type
1092
1137
  unless value.is_a?(Lutaml::Model::Serialize) || uses_type_substitution
1093
- msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a kind of 'Lutaml::Model::Serialize'"
1094
- raise Lutaml::Model::IncorrectModelError, msg
1138
+ value = coerce_accepted_nested_value(value, rule.attribute_type)
1139
+ unless value.is_a?(Lutaml::Model::Serialize)
1140
+ msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a kind of 'Lutaml::Model::Serialize'"
1141
+ raise Lutaml::Model::IncorrectModelError, msg
1142
+ end
1095
1143
  end
1096
1144
  end
1097
1145
 
@@ -1191,8 +1239,11 @@ child_mappings, options)
1191
1239
  # When attribute_type is a Serializable class, value must be an instance of that class
1192
1240
  if plan[:nested]
1193
1241
  unless value.is_a?(rule.attribute_type)
1194
- msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a '#{rule.attribute_type}'"
1195
- raise Lutaml::Model::IncorrectModelError, msg
1242
+ value = coerce_accepted_nested_value(value, rule.attribute_type)
1243
+ unless value.is_a?(rule.attribute_type)
1244
+ msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a '#{rule.attribute_type}'"
1245
+ raise Lutaml::Model::IncorrectModelError, msg
1246
+ end
1196
1247
  end
1197
1248
 
1198
1249
  # Value is a valid Serialize instance - serialize it using its own to_#{format} method
@@ -478,9 +478,17 @@ module Lutaml
478
478
 
479
479
  def initialize(items = EMPTY_ITEMS,
480
480
  lutaml_register: Lutaml::Model::Config.default_register)
481
+ # Seed constructor-equivalent state in one compiled pass
482
+ # (#1059's walk-seed) and stand the generic walk down via the
483
+ # seed flag: 92% of a materialization corpus's constructor
484
+ # invocations are Collection wrappers whose generic super()
485
+ # walk only materialized defaults that the items mapping below
486
+ # overwrites again.
487
+ self.lutaml_constructor_seeded = true
488
+ init_walk_state(lutaml_register)
481
489
  super()
490
+ self.lutaml_constructor_seeded = nil
482
491
 
483
- @lutaml_register = lutaml_register
484
492
  items = [items].compact unless items.is_a?(Array)
485
493
 
486
494
  type = Lutaml::Model::GlobalContext.resolve_type(
@@ -57,21 +57,17 @@ module Lutaml
57
57
  # so thread it through when one is supplied. Plain Array collections and
58
58
  # register-less callers keep the prior construction path untouched.
59
59
  #
60
- # @param args [Array] Values to include in the collection
60
+ # The former *args splat + flatten ceremony allocated a splat Array
61
+ # plus a keyword-options hash per call (15k+ per corpus parse);
62
+ # every call site passes at most one already-flat Array, and
63
+ # Attribute#assert_flat_collection_items! guards nesting loudly.
64
+ #
65
+ # @param items [Array, nil] Values to include in the collection
61
66
  # @param register [Symbol, nil] Register for custom-collection type resolution
62
67
  # @return [Object] A new collection instance
63
- def build_collection(*args, register: nil)
64
- # args.flatten allocated a fresh Array even for the dominant
65
- # calls: no args, or one already-flat Array from cast_items
66
- # (71,949 flatten allocations per ISO-13849 parse).
67
- items = if args.empty?
68
- []
69
- elsif args.length == 1 && (first = args[0]).is_a?(::Array) &&
70
- first.none?(::Array)
71
- first
72
- else
73
- args.flatten
74
- end
68
+ def build_collection(items = nil, register: nil)
69
+ items = [] if items.nil?
70
+ items = [items] unless items.is_a?(::Array)
75
71
  unless register && initialize_accepts_register?
76
72
  return collection_class.new(items)
77
73
  end
@@ -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