latticerb 0.4.0-x86_64-linux → 0.4.1-x86_64-linux

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: 193f28d0d9c14bd4922f02ae5f265b9f44c1c855c3f06da665e1be8ad1f774e1
4
- data.tar.gz: 614785ce3dbb079a3d1dfc3c9180ae5a7587898a0818b3074782d82dce06693d
3
+ metadata.gz: 9bb44a510a9758fa575d36496f9e2e0d3bb239b21da78e04bbb1ffeec59b3eef
4
+ data.tar.gz: 6ff8d7907de934358168ae307d449143bf96f526a572b55f9aed63837c51e08d
5
5
  SHA512:
6
- metadata.gz: dc362d0e5d6fb4631b9015ec2a07cadd200d22609ad2881ec8cb88df51e9578babb22e44899a7570c39d9081e714e5a60ebb5c978750c6ac0a4a9b0308a9dee6
7
- data.tar.gz: 8040390e30a9ae8a40ef42f1d857c7a3b5e589dafa23a5b95d19e5ee8433eb3f6400114f19c68b53dbb17f47414e2a0d38be532e68ecb24d42eea88cfa181574
6
+ metadata.gz: 2051bc9cd978209f0b26bcc2ff94f338817f7303f6d34ab734066fb0ed9f36ef7aa6dda69f73f818d6d4fc269bba64fbd9a4bbea3bfd639ab895087abf016883
7
+ data.tar.gz: f7da8521b3f9053efd3e39adbe226974bfeb125656743951c247c4497135d0b4ea2a948b38feb1ba2027cfb14f4651356d7e0e6c64987c8c342026b089733eba
data/README.md CHANGED
@@ -108,7 +108,7 @@ Every value in a schema (the model name, parameter names and values, and constra
108
108
  - Unquoted `yes`, `no`, `on`, `off`, `true` and `false` (lowercase, Capitalized or UPPERCASE) become `true` / `false`, and an empty value, `~` or `null` becomes `null`. Quote them (`"yes"`) to keep the word.
109
109
  - JSON integers are written in plain decimal form (`-0` becomes `0`).
110
110
 
111
- `strength` must be a whole number, and `weights` decimal numbers such as `0.7` or `1e-3`. Anchors, aliases and `<<` merge keys work; a key written twice, more than one YAML document, or a tag other than YAML's standard ones is an error.
111
+ `strength` must be a whole number, and `weights` decimal numbers such as `0.7` or `1e-3`. Anchors, aliases and `<<` merge keys work, up to 100,000 values once aliases are expanded; a key written twice, more than one YAML document, or a tag other than YAML's standard ones is an error.
112
112
 
113
113
  Changed in 0.3.0: earlier versions converted unquoted numbers and dates, so `0x1F` became `31`, `1.10` became `1.1`, and some values (`.inf`, timestamps) came out differently in the Ruby and Python implementations.
114
114
 
Binary file
Binary file
Binary file
Binary file
@@ -88,6 +88,9 @@ module Lattice
88
88
  errors = []
89
89
  loaders.each do |loader|
90
90
  return loader == :json ? SchemaText.load_json(text) : SchemaText.load_yaml(text)
91
+ rescue SchemaText::TooLarge => e
92
+ # Not a syntax error, so the other loader's complaint would only mislead.
93
+ raise ModelIOError, "Unable to parse model from `#{source_name}`: #{e.message}"
91
94
  rescue StandardError => e
92
95
  errors << e.message
93
96
  end
@@ -21,6 +21,18 @@ module Lattice
21
21
 
22
22
  class SyntaxError < StandardError; end
23
23
 
24
+ # Raised when YAML aliases would expand the schema past MAX_VALUES values
25
+ # (the "billion laughs" pattern). Every value counts once per place it
26
+ # appears after expansion, so the limit holds however the aliases nest.
27
+ class TooLarge < StandardError; end
28
+
29
+ MAX_VALUES = 100_000
30
+ TOO_LARGE = "schema expands to more than #{MAX_VALUES} values (check YAML aliases)".freeze
31
+
32
+ # Anchors seen so far, as anchor => [value, size] once built (nil while
33
+ # still being built), and the running count of expanded values.
34
+ Expansion = Struct.new(:anchors, :count)
35
+
24
36
  TRUE_WORDS = %w[yes Yes YES true True TRUE on On ON].freeze
25
37
  FALSE_WORDS = %w[no No NO false False FALSE off Off OFF].freeze
26
38
  NULL_WORDS = ["", "~", "null", "Null", "NULL"].freeze
@@ -57,7 +69,7 @@ module Lattice
57
69
  raise SyntaxError, "expected a single YAML document, found #{documents.length}" if documents.length > 1
58
70
  return nil if documents.empty?
59
71
 
60
- yaml_value(documents.first.root, {})
72
+ yaml_value(documents.first.root, Expansion.new({}, 0))
61
73
  end
62
74
 
63
75
  private
@@ -71,31 +83,39 @@ module Lattice
71
83
  end
72
84
  end
73
85
 
74
- # `anchors` maps each anchor to its value once built; an anchor still
75
- # being built (nil value) means an alias points back into itself.
76
- def yaml_value(node, anchors)
77
- return yaml_alias(node, anchors) if node.is_a?(Psych::Nodes::Alias)
86
+ # An anchor still being built (nil in `anchors`) means an alias points
87
+ # back into itself.
88
+ def yaml_value(node, expansion)
89
+ return yaml_alias(node, expansion) if node.is_a?(Psych::Nodes::Alias)
78
90
 
79
- anchors[node.anchor] = nil if node.anchor
91
+ start = expansion.count
92
+ add_values(expansion, 1)
93
+ expansion.anchors[node.anchor] = nil if node.anchor
80
94
  value =
81
95
  case node
82
96
  when Psych::Nodes::Scalar then yaml_scalar(node)
83
97
  when Psych::Nodes::Sequence
84
98
  check_tag(node.tag, SEQUENCE_TAGS)
85
- node.children.map { |child| yaml_value(child, anchors) }
86
- when Psych::Nodes::Mapping then yaml_mapping(node, anchors)
99
+ node.children.map { |child| yaml_value(child, expansion) }
100
+ when Psych::Nodes::Mapping then yaml_mapping(node, expansion)
87
101
  end
88
- anchors[node.anchor] = [value] if node.anchor
102
+ expansion.anchors[node.anchor] = [value, expansion.count - start] if node.anchor
89
103
  value
90
104
  end
91
105
 
92
- def yaml_alias(node, anchors)
93
- built = anchors.fetch(node.anchor) { raise SyntaxError, "unknown alias `*#{node.anchor}`" }
106
+ def yaml_alias(node, expansion)
107
+ built = expansion.anchors.fetch(node.anchor) { raise SyntaxError, "unknown alias `*#{node.anchor}`" }
94
108
  raise SyntaxError, "recursive alias" if built.nil?
95
109
 
110
+ add_values(expansion, built.last)
96
111
  built.first
97
112
  end
98
113
 
114
+ def add_values(expansion, values)
115
+ expansion.count += values
116
+ raise TooLarge, TOO_LARGE if expansion.count > MAX_VALUES
117
+ end
118
+
99
119
  def yaml_scalar(node)
100
120
  check_tag(node.tag, [nil, "!"] + SCALAR_TAGS)
101
121
  return node.value unless node.style == Psych::Nodes::Scalar::PLAIN && !STR_TAGS.include?(node.tag)
@@ -108,20 +128,20 @@ module Lattice
108
128
 
109
129
  # Keys from `<<` merges come first, earlier sources winning; the
110
130
  # mapping's own keys then override them. A key written twice is an error.
111
- def yaml_mapping(node, anchors)
131
+ def yaml_mapping(node, expansion)
112
132
  check_tag(node.tag, MAPPING_TAGS)
113
133
  merged = {}
114
134
  own = {}
115
135
  seen_merge = false
116
136
  node.children.each_slice(2) do |key_node, value_node|
117
- key = yaml_value(key_node, anchors)
137
+ key = yaml_value(key_node, expansion)
118
138
  raise SyntaxError, "mapping keys must be scalars" if key.is_a?(Enumerable)
119
139
 
120
140
  if merge_key?(key_node)
121
141
  raise SyntaxError, "duplicate key `<<`" if seen_merge
122
142
 
123
143
  seen_merge = true
124
- merge_sources(yaml_value(value_node, anchors)).each do |source|
144
+ merge_sources(yaml_value(value_node, expansion)).each do |source|
125
145
  source.each { |merged_key, merged_value| merged[merged_key] = merged_value unless merged.key?(merged_key) }
126
146
  end
127
147
  next
@@ -129,7 +149,7 @@ module Lattice
129
149
 
130
150
  raise SyntaxError, "duplicate key `#{Lattice.normalize_scalar(key)}`" if own.key?(key)
131
151
 
132
- own[key] = yaml_value(value_node, anchors)
152
+ own[key] = yaml_value(value_node, expansion)
133
153
  end
134
154
  merged.merge(own)
135
155
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lattice
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: latticerb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: x86_64-linux
6
6
  authors:
7
7
  - Tyler Klose
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-01 00:00:00.000000000 Z
11
+ date: 2026-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: csv