featurevisor-schemas 4.1.0

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.
@@ -0,0 +1,292 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Featurevisor
6
+ module Schemas
7
+ # What a handle's members describe with an input: rules with every matching mutation applied, at every depth.
8
+ module Views
9
+ module_function
10
+
11
+ # Applies a node's mutations whose conditions match the value at it, in the schema's package evaluation.
12
+ def adjuster(match, resolver)
13
+ lambda do |node, value|
14
+ next node unless Engine.mutations?(node)
15
+
16
+ begin
17
+ Engine.mutated_rules(node, Engine.matching_mutations(node, value, match), resolver)
18
+ rescue StandardError
19
+ node
20
+ end
21
+ end
22
+ end
23
+
24
+ def map_children(node)
25
+ copy = node.dup
26
+ copy[:properties] = node[:properties].to_h { |key, child| [key, yield(child, key.to_s, nil)] } if node[:properties].is_a?(Hash)
27
+ copy[:items] = yield(node[:items], nil, "[]") if node[:items].is_a?(Hash)
28
+ copy[:additionalProperties] = yield(node[:additionalProperties], nil, "*") if node[:additionalProperties].is_a?(Hash)
29
+ copy[:oneOf] = node[:oneOf].map { |branch| yield(branch, nil, nil) } if node[:oneOf].is_a?(Array)
30
+ copy
31
+ end
32
+
33
+ # A node's subtree for a value, with the matching mutations applied at every
34
+ # node for the value there. References are inlined, so each use of a schema
35
+ # resolves for its own value. Items and an open object's values resolve for
36
+ # no value, since each is its own.
37
+ def resolve_tree(node, value, adjust, resolver, seen = [])
38
+ current = adjust.call(node, value)
39
+ return current unless current.is_a?(Hash)
40
+
41
+ key = current[:schema]
42
+ unless key.is_a?(String)
43
+ return map_children(current) do |child, name, many|
44
+ child_value = many ? nil : (name ? (value.is_a?(Hash) ? value[name] : nil) : value)
45
+ resolve_tree(child, child_value, adjust, resolver, seen)
46
+ end
47
+ end
48
+ target = seen.include?(key) ? nil : resolver.node(key)
49
+ return current unless target
50
+
51
+ inner = resolve_tree(target, value, adjust, resolver, seen + [key])
52
+ # The reference's own annotations win over the referenced schema's, as they do outside a view.
53
+ inner.merge(annotations: (inner[:annotations] || {}).merge(current[:annotations] || {}))
54
+ end
55
+
56
+ # A copy of values with a value at a path, so the fields around it read as they will.
57
+ def place(values, steps, value)
58
+ return value if steps.empty?
59
+
60
+ step = steps.first
61
+ rest = steps[1..]
62
+ case step.kind
63
+ when :key
64
+ copy = values.is_a?(Hash) ? values.dup : {}
65
+ copy[step.key] = place(copy[step.key], rest, value)
66
+ copy
67
+ when :index
68
+ list = values.is_a?(Array) ? values.dup : []
69
+ list[step.index] = place(list[step.index], rest, value)
70
+ list
71
+ else values
72
+ end
73
+ end
74
+
75
+ # Calls back with the input paths conditions read, in input and valueFrom, through every group.
76
+ def inputs_of(conditions, &found)
77
+ if conditions.is_a?(String)
78
+ text = conditions.strip
79
+ return unless text.start_with?("{", "[", "\"")
80
+
81
+ conditions = begin
82
+ JSON.parse(text, symbolize_names: true)
83
+ rescue JSON::ParserError
84
+ return
85
+ end
86
+ end
87
+ return conditions.each { |item| inputs_of(item, &found) } if conditions.is_a?(Array)
88
+ return unless conditions.is_a?(Hash)
89
+
90
+ leaf = conditions.transform_keys(&:to_sym)
91
+ found.call(leaf[:input]) if leaf[:input].is_a?(String)
92
+ compared = leaf[:valueFrom].is_a?(Hash) ? leaf[:valueFrom].transform_keys(&:to_sym)[:input] : nil
93
+ found.call(compared) if compared.is_a?(String)
94
+ %i[and or not].each { |group| inputs_of(leaf[group], &found) }
95
+ end
96
+
97
+ # The fields, relative to a node, that the conditions of its mutations and
98
+ # issues read, at any depth and through references, as field takes paths.
99
+ def reads_of(node, resolver)
100
+ reads = []
101
+ join = ->(at, path) { at.empty? ? path : "#{at}.#{path}" }
102
+ visit = lambda do |current, at, seen|
103
+ next unless current.is_a?(Hash)
104
+
105
+ [current[:mutations], current[:issues]].each do |list|
106
+ next unless list.is_a?(Array)
107
+
108
+ list.each do |entry|
109
+ inputs_of(entry.is_a?(Hash) ? entry[:conditions] : nil) do |path|
110
+ reads << join.call(at, path) unless reads.include?(join.call(at, path))
111
+ end
112
+ end
113
+ end
114
+ key = current[:schema]
115
+ visit.call(resolver.node(key), at, seen + [key]) if key.is_a?(String) && !seen.include?(key)
116
+ current[:properties].each { |name, child| visit.call(child, join.call(at, name.to_s), seen) } if current[:properties].is_a?(Hash)
117
+ visit.call(current[:items], "#{at}[]", seen) if current.key?(:items)
118
+ visit.call(current[:additionalProperties], join.call(at, "*"), seen) if current.key?(:additionalProperties)
119
+ current[:oneOf].each { |branch| visit.call(branch, at, seen) } if current[:oneOf].is_a?(Array)
120
+ end
121
+ visit.call(node, "", [])
122
+ reads
123
+ end
124
+ end
125
+
126
+ # What a handle derives from its node: constraints, choices, what a field renders as, field order and annotations.
127
+ module Derived
128
+ module_function
129
+
130
+ def non_null_branch(node, resolver)
131
+ return node unless node[:oneOf].is_a?(Array)
132
+
133
+ branches = node[:oneOf].filter_map { |branch| Engine.resolve_node(branch, resolver) }
134
+ .reject { |branch| branch.key?(:const) && branch[:const].nil? }
135
+ branches.length == 1 ? branches.first : nil
136
+ end
137
+
138
+ def object_node?(node)
139
+ node.is_a?(Hash) && (node[:type] == "object" || (!node.key?(:type) && node[:properties].is_a?(Hash)))
140
+ end
141
+
142
+ def takes_null?(node, resolver, depth = 0)
143
+ resolved = Engine.resolve_node(node, resolver)
144
+ return false if resolved.nil? || depth > 64
145
+ return resolved[:const].nil? if resolved.key?(:const)
146
+ return resolved[:enum].include?(nil) if resolved[:enum].is_a?(Array)
147
+
148
+ resolved[:oneOf].is_a?(Array) && resolved[:oneOf].any? { |branch| takes_null?(branch, resolver, depth + 1) }
149
+ end
150
+
151
+ def boolean_choice?(values)
152
+ !values.nil? && values.any? { |value| Values.boolean?(value) } && values.all? { |value| Values.boolean?(value) || value.nil? }
153
+ end
154
+
155
+ def empty_fails?(rules, resolver)
156
+ return false if rules[:type] == "boolean"
157
+ return Values.number?(rules[:minItems]) && rules[:minItems].positive? if rules[:type] == "array"
158
+
159
+ values = values_of(rules, resolver)
160
+ !boolean_choice?(values) || !values.include?(false)
161
+ end
162
+
163
+ def constraints(node, required, resolver)
164
+ resolved = Engine.resolve_node(node, resolver)
165
+ rules = resolved && non_null_branch(resolved, resolver)
166
+ is_required = required && !takes_null?(resolved, resolver) && (rules.nil? || empty_fails?(rules, resolver))
167
+ return Constraints.new(required: is_required) if rules.nil?
168
+
169
+ case rules[:type]
170
+ when "string"
171
+ Constraints.new(required: is_required, min_length: Values.number?(rules[:minLength]) ? rules[:minLength].to_i : nil,
172
+ max_length: Values.number?(rules[:maxLength]) ? rules[:maxLength].to_i : nil)
173
+ when "integer", "number"
174
+ Constraints.new(required: is_required, min: Values.number?(rules[:minimum]) ? rules[:minimum] : nil,
175
+ max: Values.number?(rules[:maximum]) ? rules[:maximum] : nil, step: rules[:type] == "integer" ? 1 : nil)
176
+ else
177
+ Constraints.new(required: is_required, multiple: rules[:type] == "array")
178
+ end
179
+ end
180
+
181
+ # What the block finds for each field of an object, by dot path, with nested objects walked too.
182
+ # Arrays stop here, since a row's handle has its own. The block gets the field's node and whether
183
+ # the object around it requires it, and a field it finds nothing for is left out.
184
+ def by_path(node, resolver)
185
+ result = {}
186
+ # The objects on the way to this one, so a schema that comes back to itself stops, while one
187
+ # used by two fields, such as an address, is walked for each.
188
+ way = []
189
+ collect = lambda do |current, prefix, depth|
190
+ next unless current[:properties].is_a?(Hash) && depth <= 64 && way.none? { |seen| seen.equal?(current) }
191
+
192
+ way.push(current)
193
+ required = current[:required].is_a?(Array) ? current[:required] : []
194
+ current[:properties].each do |key, child|
195
+ path = "#{prefix}#{key}"
196
+ found = yield(child, required.include?(key.to_s))
197
+ result[path] = found unless found.nil?
198
+ resolved = Engine.resolve_node(child, resolver)
199
+ collect.call(resolved, "#{path}.", depth + 1) if object_node?(resolved)
200
+ end
201
+ way.pop
202
+ end
203
+ resolved = Engine.resolve_node(node, resolver)
204
+ collect.call(resolved, "", 0) if object_node?(resolved)
205
+ result
206
+ end
207
+
208
+ def field_constraints(node, resolver)
209
+ by_path(node, resolver) { |child, required| constraints(child, required, resolver) }
210
+ end
211
+
212
+ # The nodes on the way are kept, so a loop stops while two branches naming one schema both count.
213
+ def choices(node, resolver, branch = false, depth = 0, way = [])
214
+ resolved = Engine.resolve_node(node, resolver)
215
+ return nil if resolved.nil? || depth > 64 || way.any? { |seen| seen.equal?(resolved) }
216
+
217
+ way += [resolved]
218
+ return resolved[:enum].map { |value| SchemaOption.build(value, Engine.option_entry(resolved, value)) } if resolved[:enum].is_a?(Array)
219
+ return [SchemaOption.build(resolved[:const], branch ? Engine.annotations_of(node, resolver) : nil)] if resolved.key?(:const)
220
+
221
+ if resolved[:oneOf].is_a?(Array)
222
+ # Only a list of choices gives its branches' annotations to the choices. Any other branch's are a field's.
223
+ listed = Engine.const_list?(resolved, resolver)
224
+ result = []
225
+ resolved[:oneOf].each do |each|
226
+ found = choices(each, resolver, listed, depth + 1, way)
227
+ return nil if found.nil?
228
+
229
+ found.each { |option| result << option unless result.any? { |other| Values.same?(other.value, option.value) } }
230
+ end
231
+ return result
232
+ end
233
+ return choices(resolved[:items], resolver, false, depth + 1, way) if resolved[:type] == "array" && resolved.key?(:items)
234
+
235
+ nil
236
+ end
237
+
238
+ def values_of(node, resolver)
239
+ choices(node, resolver)&.map(&:value)
240
+ end
241
+
242
+ def options(node, resolver)
243
+ choices(node, resolver) || []
244
+ end
245
+
246
+ def field_options(node, resolver)
247
+ by_path(node, resolver) { |child, _required| choices(child, resolver) }
248
+ end
249
+
250
+ # The keys of an object's fields, the ones its order lists first, then the rest in name order,
251
+ # by code point, as every SDK orders them.
252
+ def field_keys(node, resolver)
253
+ properties = Engine.resolve_node(node, resolver)&.[](:properties)
254
+ return [] unless properties.is_a?(Hash)
255
+
256
+ keys = properties.keys.map(&:to_s)
257
+ order = Engine.annotations_of(node, resolver)&.[](:order)
258
+ first = (order.is_a?(Array) ? order : []).select { |key| keys.include?(key) }.uniq
259
+ first + (keys - first).sort
260
+ end
261
+
262
+ def annotations(node, resolver)
263
+ Engine.annotations_of(node, resolver) || {}
264
+ end
265
+
266
+ # What a node's rules say a field renders as, or nil for a oneOf of shapes.
267
+ # The nodes on the way are kept, so a schema that comes back to itself, such as a list of such lists, stops.
268
+ def render_as(node, resolver, depth = 0, way = [])
269
+ resolved = Engine.resolve_node(node, resolver)
270
+ return nil if resolved.nil? || depth > 64 || way.any? { |seen| seen.equal?(resolved) }
271
+
272
+ way += [resolved]
273
+
274
+ if resolved[:enum].is_a?(Array) || resolved.key?(:const) || Engine.const_list?(resolved, resolver)
275
+ return boolean_choice?(values_of(resolved, resolver)) ? "boolean" : "select"
276
+ end
277
+ if resolved[:oneOf].is_a?(Array)
278
+ # A nullable field is what its other branch is.
279
+ rules = non_null_branch(resolved, resolver)
280
+ return rules && render_as(rules, resolver, depth + 1, way)
281
+ end
282
+ case resolved[:type]
283
+ when "string" then { "date-time" => "date-time", "semver" => "version" }.fetch(resolved[:format], "text")
284
+ when "array" then render_as(resolved[:items], resolver, depth + 1, way) == "select" ? "multiselect" : "list"
285
+ when "integer", "number" then "number"
286
+ when "boolean" then "boolean"
287
+ else object_node?(resolved) ? "object" : nil
288
+ end
289
+ end
290
+ end
291
+ end
292
+ end