featurevisor 4.0.0 → 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.
- checksums.yaml +4 -4
- data/README.md +625 -561
- data/bin/cli.rb +1 -1
- data/bin/commands/test.rb +120 -4
- data/lib/featurevisor/child_instance.rb +7 -1
- data/lib/featurevisor/conditions.rb +55 -13
- data/lib/featurevisor/evaluate.rb +4 -4
- data/lib/featurevisor/evaluation_data_provider.rb +20 -3
- data/lib/featurevisor/evaluation_session.rb +2 -0
- data/lib/featurevisor/events.rb +72 -0
- data/lib/featurevisor/instance.rb +61 -22
- data/lib/featurevisor/modules.rb +50 -5
- data/lib/featurevisor/package_conditions.rb +213 -0
- data/lib/featurevisor/unified_conditions.rb +2 -1
- data/lib/featurevisor/version.rb +1 -1
- data/lib/featurevisor.rb +18 -0
- metadata +2 -1
data/bin/cli.rb
CHANGED
|
@@ -56,7 +56,7 @@ module FeaturevisorCLI
|
|
|
56
56
|
options.key_pattern = v
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
-
opts.on("--entity-type=TYPE", "--entityType=TYPE", %w[feature segment variable scenario], "Filter tests by entity type") do |v|
|
|
59
|
+
opts.on("--entity-type=TYPE", "--entityType=TYPE", %w[feature segment variable scenario schema], "Filter tests by entity type") do |v|
|
|
60
60
|
options.entity_type = v
|
|
61
61
|
end
|
|
62
62
|
|
data/bin/commands/test.rb
CHANGED
|
@@ -214,14 +214,24 @@ module FeaturevisorCLI
|
|
|
214
214
|
end
|
|
215
215
|
|
|
216
216
|
def get_tests(set = @active_set)
|
|
217
|
+
# Schema specs come too, validated with the featurevisor-schemas gem.
|
|
217
218
|
arguments = ["list", "--tests", "--execution-protocol", "--json"]
|
|
219
|
+
arguments << "--with-schemas" unless @schema_specs_rejected
|
|
218
220
|
arguments << "--keyPattern=#{@options.key_pattern}" if @options.key_pattern && !@options.key_pattern.empty?
|
|
219
221
|
if @options.assertion_pattern && !@options.assertion_pattern.empty?
|
|
220
222
|
arguments << "--assertionPattern=#{@options.assertion_pattern}"
|
|
221
223
|
end
|
|
222
224
|
arguments << "--entityType=#{@options.entity_type}" if @options.entity_type && !@options.entity_type.empty?
|
|
223
225
|
arguments << "--case=#{@options.case_id}" if @options.case_id
|
|
224
|
-
tests_output =
|
|
226
|
+
tests_output = begin
|
|
227
|
+
execute_command(node_command(*arguments, *set_arguments(set)))
|
|
228
|
+
rescue InfrastructureError => e
|
|
229
|
+
# A CLI from before schema specs rejects the option, and has none to list.
|
|
230
|
+
raise unless arguments.include?("--with-schemas") && e.message.include?("Unknown argument") && e.message.include?("with-schemas")
|
|
231
|
+
|
|
232
|
+
@schema_specs_rejected = true
|
|
233
|
+
execute_command(node_command(*(arguments - ["--with-schemas"]), *set_arguments(set)))
|
|
234
|
+
end
|
|
225
235
|
|
|
226
236
|
begin
|
|
227
237
|
payload = JSON.parse(tests_output, symbolize_names: true)
|
|
@@ -302,7 +312,7 @@ module FeaturevisorCLI
|
|
|
302
312
|
report = empty_report
|
|
303
313
|
|
|
304
314
|
tests.each do |test|
|
|
305
|
-
discriminators = %i[feature variable segment scenario].select { |key| test.key?(key) }
|
|
315
|
+
discriminators = %i[feature variable segment scenario schema].select { |key| test.key?(key) }
|
|
306
316
|
type = discriminators.first
|
|
307
317
|
invalid_discriminator = discriminators.length != 1
|
|
308
318
|
assertions = test[:assertions] || []
|
|
@@ -311,7 +321,7 @@ module FeaturevisorCLI
|
|
|
311
321
|
elsif type == :scenario && assertions.empty?
|
|
312
322
|
assertions = [{ description: "malformed scenario spec", __missing_assertions: true }]
|
|
313
323
|
end
|
|
314
|
-
if !invalid_discriminator && %i[feature variable scenario].include?(type) && !@options.targets.empty?
|
|
324
|
+
if !invalid_discriminator && %i[feature variable scenario schema].include?(type) && !@options.targets.empty?
|
|
315
325
|
assertions = assertions.select { |assertion| assertion[:target].nil? || @options.targets.include?(assertion[:target]) }
|
|
316
326
|
next if assertions.empty?
|
|
317
327
|
end
|
|
@@ -354,10 +364,11 @@ module FeaturevisorCLI
|
|
|
354
364
|
|
|
355
365
|
def execute_assertion(test, type, assertion, datafiles_by_key, segments_by_key, level, set)
|
|
356
366
|
if assertion[:__invalid_discriminator]
|
|
357
|
-
return { has_error: true, errors: " ✘ test spec requires exactly one of feature, variable, segment or
|
|
367
|
+
return { has_error: true, errors: " ✘ test spec requires exactly one of feature, variable, segment, scenario or schema\n", duration: 0 }
|
|
358
368
|
end
|
|
359
369
|
|
|
360
370
|
datafile = resolve_datafile_for_assertion(assertion, datafiles_by_key)
|
|
371
|
+
return run_test_schema(test[:schema], assertion, datafile) if type == :schema
|
|
361
372
|
if type == :segment
|
|
362
373
|
if !datafile && ((assertion[:environment].is_a?(String) && !assertion[:environment].empty?) || assertion[:target])
|
|
363
374
|
return { has_error: true, errors: " ✘ no datafile found for assertion target/environment combination\n", duration: 0 }
|
|
@@ -417,6 +428,110 @@ module FeaturevisorCLI
|
|
|
417
428
|
end
|
|
418
429
|
end
|
|
419
430
|
|
|
431
|
+
# A schema spec's assertion: validates its value with the featurevisor-schemas gem
|
|
432
|
+
# against the datafile its environment and Target build, under its context, as the
|
|
433
|
+
# JavaScript CLI does, so a test proves what applications see.
|
|
434
|
+
def run_test_schema(schema, assertion, datafile)
|
|
435
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
436
|
+
errors = []
|
|
437
|
+
record = ->(message, expected, actual) { errors << { type: "schema", message: message, expected: expected, actual: actual } }
|
|
438
|
+
environment = assertion[:environment]
|
|
439
|
+
target = assertion[:target]
|
|
440
|
+
if !datafile
|
|
441
|
+
message = if environment.is_a?(String) && !environment.empty?
|
|
442
|
+
%(datafile not found for environment "#{environment}"#{target ? %( and target "#{target}") : ""})
|
|
443
|
+
elsif target
|
|
444
|
+
%(datafile not found for target "#{target}")
|
|
445
|
+
else
|
|
446
|
+
"No datafile without an environment. When the project configures environments, add environment to the assertion."
|
|
447
|
+
end
|
|
448
|
+
record.call(message, "datafile", nil)
|
|
449
|
+
elsif !(datafile[:schemas].is_a?(Hash) && datafile[:schemas].key?(schema.to_sym))
|
|
450
|
+
record.call(%(Target "#{target || "undefined"}" does not deliver schema "#{schema}". Tag the schema for it, or list it in its includeSchemas.), schema, nil)
|
|
451
|
+
elsif !schemas_gem?
|
|
452
|
+
record.call("Schema specs need the featurevisor-schemas gem. Add it beside featurevisor.", "featurevisor-schemas", nil)
|
|
453
|
+
else
|
|
454
|
+
issues = validate_schema(schema, assertion, datafile)
|
|
455
|
+
valid = issues.empty?
|
|
456
|
+
if [true, false].include?(assertion[:expectedToBeValid]) && assertion[:expectedToBeValid] != valid
|
|
457
|
+
message = if valid
|
|
458
|
+
"Expected the value to be invalid, and it is valid."
|
|
459
|
+
else
|
|
460
|
+
count = issues.length == 1 ? "an issue" : "#{issues.length} issues"
|
|
461
|
+
"Expected the value to be valid, and it has #{count}:\n" + issues.map { |issue| " #{JSON.generate(issue)}" }.join("\n")
|
|
462
|
+
end
|
|
463
|
+
record.call(message, assertion[:expectedToBeValid], valid)
|
|
464
|
+
end
|
|
465
|
+
if assertion[:expectedIssues].is_a?(Array)
|
|
466
|
+
# Each expected issue pairs with one issue found, and every issue found must be expected.
|
|
467
|
+
left = issues.dup
|
|
468
|
+
missing = []
|
|
469
|
+
assertion[:expectedIssues].each do |expected|
|
|
470
|
+
index = left.index { |actual| issue_matches?(expected, actual) }
|
|
471
|
+
index ? left.delete_at(index) : missing << expected
|
|
472
|
+
end
|
|
473
|
+
unless missing.empty? && left.empty?
|
|
474
|
+
lines = missing.map { |issue| " missing: #{JSON.generate(issue)}" } +
|
|
475
|
+
left.map { |issue| " unexpected: #{JSON.generate(issue)}" }
|
|
476
|
+
record.call("The issues differ from expectedIssues:\n" + lines.join("\n"), assertion[:expectedIssues], issues)
|
|
477
|
+
end
|
|
478
|
+
end
|
|
479
|
+
end
|
|
480
|
+
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
|
|
481
|
+
{ has_error: !errors.empty?, errors: errors.empty? ? "" : errors, duration: duration }
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# Whether the featurevisor-schemas gem loads, which the featurevisor gem does not depend on.
|
|
485
|
+
def schemas_gem?
|
|
486
|
+
return @schemas_gem unless @schemas_gem.nil?
|
|
487
|
+
|
|
488
|
+
@schemas_gem = begin
|
|
489
|
+
require "featurevisor-schemas"
|
|
490
|
+
true
|
|
491
|
+
rescue LoadError
|
|
492
|
+
false
|
|
493
|
+
end
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
# The issues a value has, as a test result shows them, without the schema key every issue repeats.
|
|
497
|
+
def validate_schema(schema, assertion, datafile)
|
|
498
|
+
instance = Featurevisor.create_featurevisor(datafile: datafile, context: assertion[:context] || {}, log_level: "fatal")
|
|
499
|
+
schemas = Featurevisor::Schemas.create(instance)
|
|
500
|
+
schemas.validate(schema, assertion[:value]).issues.map do |issue|
|
|
501
|
+
described = { path: issue.path, code: issue.code }
|
|
502
|
+
described[:mutation] = issue.mutation unless issue.mutation.nil?
|
|
503
|
+
described.merge(message: issue.message, params: issue.params)
|
|
504
|
+
end
|
|
505
|
+
ensure
|
|
506
|
+
schemas&.close
|
|
507
|
+
instance&.close
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
# Whether an issue has every field an expected issue gives, params by JSON equality.
|
|
511
|
+
def issue_matches?(expected, actual)
|
|
512
|
+
return false unless expected.is_a?(Hash)
|
|
513
|
+
return false if expected.key?(:path) && JSON.generate(expected[:path]) != JSON.generate(actual[:path])
|
|
514
|
+
return false if %i[code mutation message].any? { |field| expected.key?(field) && expected[field] != actual[field] }
|
|
515
|
+
|
|
516
|
+
params = actual[:params] || {}
|
|
517
|
+
(expected[:params] || {}).all? do |name, value|
|
|
518
|
+
found = params.key?(name) ? params[name] : params[name.to_s]
|
|
519
|
+
(params.key?(name) || params.key?(name.to_s)) && json_equal?(value, found)
|
|
520
|
+
end
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
def json_equal?(left, right)
|
|
524
|
+
normalize = lambda do |value|
|
|
525
|
+
case value
|
|
526
|
+
when Float then value == value.to_i ? value.to_i : value
|
|
527
|
+
when Hash then value.to_h { |key, item| [key.to_s, normalize.call(item)] }
|
|
528
|
+
when Array then value.map { |item| normalize.call(item) }
|
|
529
|
+
else value
|
|
530
|
+
end
|
|
531
|
+
end
|
|
532
|
+
normalize.call(left) == normalize.call(right)
|
|
533
|
+
end
|
|
534
|
+
|
|
420
535
|
def run_test_variable(assertion, variable_key, instance, datafile, diagnostics, segments = {})
|
|
421
536
|
root_context = parse_context(assertion[:context])
|
|
422
537
|
matcher = segment_matcher(instance, segments)
|
|
@@ -1262,6 +1377,7 @@ module FeaturevisorCLI
|
|
|
1262
1377
|
[]
|
|
1263
1378
|
end
|
|
1264
1379
|
error[:path] = path + suffix unless suffix.empty?
|
|
1380
|
+
error[:path] = path if error[:type] == "schema"
|
|
1265
1381
|
error[:message] ||= "expected #{format_test_value(error[:expected])}, received #{format_test_value(error[:actual])}"
|
|
1266
1382
|
error
|
|
1267
1383
|
end
|
|
@@ -46,9 +46,15 @@ module Featurevisor
|
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
def set_context(context, replace = false)
|
|
49
|
+
previous = @context
|
|
49
50
|
@context = replace ? context : { **@context, **context }
|
|
50
51
|
@evaluation_generation += 1
|
|
51
|
-
|
|
52
|
+
details = { context: @context, replaced: replace }
|
|
53
|
+
# Only a listener reads the changed keys, so only then are they worked out.
|
|
54
|
+
if @emitter.listeners["context_set"]&.any?
|
|
55
|
+
details[:keys] = Events.get_changed_context_keys(previous, @context, replace ? nil : context)
|
|
56
|
+
end
|
|
57
|
+
@emitter.trigger("context_set", details)
|
|
52
58
|
end
|
|
53
59
|
|
|
54
60
|
def get_context(context = nil)
|
|
@@ -47,9 +47,14 @@ module Featurevisor
|
|
|
47
47
|
end
|
|
48
48
|
private_class_method :get_value_with_presence
|
|
49
49
|
|
|
50
|
+
# A number as JavaScript reads it, a double, so integers above 2^53 compare as JavaScript compares them.
|
|
51
|
+
def self.binary64(number)
|
|
52
|
+
number.to_f
|
|
53
|
+
end
|
|
54
|
+
|
|
50
55
|
def self.strict_equal?(left, right)
|
|
51
56
|
return true if left.nil? && right.nil?
|
|
52
|
-
return left == right if left.is_a?(Numeric) && right.is_a?(Numeric)
|
|
57
|
+
return binary64(left) == binary64(right) if left.is_a?(Numeric) && right.is_a?(Numeric)
|
|
53
58
|
return left == right if left.is_a?(String) && right.is_a?(String)
|
|
54
59
|
return left == right if (left == true || left == false) && (right == true || right == false)
|
|
55
60
|
|
|
@@ -164,17 +169,18 @@ module Featurevisor
|
|
|
164
169
|
when "greaterThan", "greaterThanOrEquals", "lessThan", "lessThanOrEquals"
|
|
165
170
|
# numeric operations
|
|
166
171
|
if context_value_from_path.is_a?(Numeric) && value.is_a?(Numeric)
|
|
167
|
-
value_in_context = context_value_from_path
|
|
172
|
+
value_in_context = binary64(context_value_from_path)
|
|
173
|
+
compared = binary64(value)
|
|
168
174
|
|
|
169
175
|
case operator
|
|
170
176
|
when "greaterThan"
|
|
171
|
-
value_in_context >
|
|
177
|
+
value_in_context > compared
|
|
172
178
|
when "greaterThanOrEquals"
|
|
173
|
-
value_in_context >=
|
|
179
|
+
value_in_context >= compared
|
|
174
180
|
when "lessThan"
|
|
175
|
-
value_in_context <
|
|
181
|
+
value_in_context < compared
|
|
176
182
|
when "lessThanOrEquals"
|
|
177
|
-
value_in_context <=
|
|
183
|
+
value_in_context <= compared
|
|
178
184
|
end
|
|
179
185
|
else
|
|
180
186
|
false
|
|
@@ -235,12 +241,36 @@ module Featurevisor
|
|
|
235
241
|
end
|
|
236
242
|
private_class_method :portable_date
|
|
237
243
|
|
|
238
|
-
|
|
244
|
+
# A portable timestamp, as before, after and a schema's format "date-time" read
|
|
245
|
+
# it, to the millisecond, or nil: years 0001 to 9999, offsets up to 14 hours,
|
|
246
|
+
# and fractions up to nine digits.
|
|
247
|
+
def self.portable_timestamp(value)
|
|
248
|
+
portable_date(value)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# JavaScript's whitespace and line terminators, for \s.
|
|
252
|
+
PORTABLE_SPACES = '\t\n\x0B\f\r \u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF'
|
|
253
|
+
# Everything else, for \S inside a class.
|
|
254
|
+
PORTABLE_NOT_SPACES = '\x00-\x08\x0E-\x1F\x21-\x7F\u0080-\u009F\u00A1-\u167F\u1681-\u1FFF\u200B-\u2027' \
|
|
255
|
+
'\u202A-\u202E\u2030-\u205E\u2060-\u2FFF\u3001-\uFEFE\uFF00-\u{10FFFF}'
|
|
256
|
+
|
|
257
|
+
# A portable pattern in Onigmo's syntax, meaning what it means in JavaScript
|
|
258
|
+
# with the same flags: \d and \w are ASCII, \s is JavaScript's whitespace, a
|
|
259
|
+
# dot refuses only JavaScript's line terminators, and without m, ^ and $
|
|
260
|
+
# match at the start and end of the value only, where Onigmo's match at every
|
|
261
|
+
# line.
|
|
262
|
+
def self.normalize_portable_regex(pattern, flags = '')
|
|
263
|
+
multiline = flags.to_s.include?('m')
|
|
264
|
+
dot_all = flags.to_s.include?('s')
|
|
239
265
|
inside = {
|
|
240
|
-
'd' => '0-9', 'D' => '
|
|
241
|
-
'w' => 'A-Za-z0-9_', 'W' => '
|
|
266
|
+
'd' => '0-9', 'D' => '\u{0}-\u{2F}\u{3A}-\u{10FFFF}',
|
|
267
|
+
'w' => 'A-Za-z0-9_', 'W' => '\u{0}-\u{2F}\u{3A}-\u{40}\u{5B}-\u{5E}\u{60}\u{7B}-\u{10FFFF}',
|
|
268
|
+
's' => PORTABLE_SPACES, 'S' => PORTABLE_NOT_SPACES
|
|
269
|
+
}
|
|
270
|
+
outside = {
|
|
271
|
+
'd' => '[0-9]', 'D' => '[^0-9]', 'w' => '(?-i:[A-Za-z0-9_])', 'W' => '(?-i:[^A-Za-z0-9_])',
|
|
272
|
+
's' => "[#{PORTABLE_SPACES}]", 'S' => "[^#{PORTABLE_SPACES}]"
|
|
242
273
|
}
|
|
243
|
-
outside = { 'd' => '[0-9]', 'D' => '[^0-9]', 'w' => '(?-i:[A-Za-z0-9_])', 'W' => '(?-i:[^A-Za-z0-9_])' }
|
|
244
274
|
output = +''
|
|
245
275
|
in_class = false
|
|
246
276
|
index = 0
|
|
@@ -252,9 +282,21 @@ module Featurevisor
|
|
|
252
282
|
index += 2
|
|
253
283
|
next
|
|
254
284
|
end
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
285
|
+
if in_class
|
|
286
|
+
in_class = false if char == ']'
|
|
287
|
+
output << char
|
|
288
|
+
elsif char == '['
|
|
289
|
+
in_class = true
|
|
290
|
+
output << char
|
|
291
|
+
elsif char == '^' && !multiline
|
|
292
|
+
output << '\A'
|
|
293
|
+
elsif char == '$' && !multiline
|
|
294
|
+
output << '\z'
|
|
295
|
+
elsif char == '.' && !dot_all
|
|
296
|
+
output << '[^\n\r\u2028\u2029]'
|
|
297
|
+
else
|
|
298
|
+
output << char
|
|
299
|
+
end
|
|
258
300
|
index += 1
|
|
259
301
|
end
|
|
260
302
|
output
|
|
@@ -58,7 +58,7 @@ module Featurevisor
|
|
|
58
58
|
# The usual read has no modules. Keep its evaluation options intact so
|
|
59
59
|
# it does not allocate two filtered hashes and a merged hash.
|
|
60
60
|
result_options = options
|
|
61
|
-
if modules_manager
|
|
61
|
+
if modules_manager&.before_evaluation?
|
|
62
62
|
private_options = options.select { |key, _| private_evaluation_option?(key) }
|
|
63
63
|
public_options = options.reject { |key, _| private_evaluation_option?(key) }
|
|
64
64
|
result_options = modules_manager.run_before_evaluation_modules(public_options)
|
|
@@ -96,7 +96,7 @@ module Featurevisor
|
|
|
96
96
|
# Run after modules
|
|
97
97
|
# A callback during evaluation may add a module. In that case its
|
|
98
98
|
# after hook still receives only public options.
|
|
99
|
-
if modules_manager
|
|
99
|
+
if modules_manager&.after_evaluation?
|
|
100
100
|
evaluation = modules_manager.run_after_evaluation_modules(
|
|
101
101
|
evaluation, result_options.reject { |key, _| private_evaluation_option?(key) }
|
|
102
102
|
)
|
|
@@ -423,7 +423,7 @@ module Featurevisor
|
|
|
423
423
|
context: context,
|
|
424
424
|
bucket_by: feature[:bucketBy],
|
|
425
425
|
bucket_key: bucket_key
|
|
426
|
-
}) if modules_manager
|
|
426
|
+
}) if modules_manager&.bucket_key?
|
|
427
427
|
|
|
428
428
|
# bucketValue
|
|
429
429
|
bucket_value = Featurevisor::Bucketer.get_bucketed_number(bucket_key)
|
|
@@ -434,7 +434,7 @@ module Featurevisor
|
|
|
434
434
|
bucket_key: bucket_key,
|
|
435
435
|
context: context,
|
|
436
436
|
bucket_value: bucket_value
|
|
437
|
-
}) if modules_manager
|
|
437
|
+
}) if modules_manager&.bucket_value?
|
|
438
438
|
|
|
439
439
|
matched_traffic = nil
|
|
440
440
|
matched_allocation = nil
|
|
@@ -5,13 +5,17 @@ require "json"
|
|
|
5
5
|
module Featurevisor
|
|
6
6
|
# Private datafile and matching adapter used by the evaluator.
|
|
7
7
|
class InstanceEvaluationDataProvider
|
|
8
|
-
attr_reader :schema_version, :revision, :featurevisor_version, :segments, :features, :variables, :diagnostics, :regex_cache, :normalized
|
|
8
|
+
attr_reader :schema_version, :revision, :featurevisor_version, :segments, :features, :variables, :diagnostics, :regex_cache, :normalized,
|
|
9
|
+
:package_conditions
|
|
9
10
|
attr_accessor :dependency_index
|
|
10
11
|
|
|
11
12
|
# Initialize a new evaluation data provider.
|
|
12
13
|
# @param options [Hash] Options hash containing datafile and diagnostics
|
|
13
14
|
# @option options [Hash] :datafile Datafile content
|
|
14
15
|
# @option options [DiagnosticReporter] :diagnostics Diagnostic reporter
|
|
16
|
+
# The datafile's own keys. Every other top level key is a section or carried as it is.
|
|
17
|
+
DATAFILE_KEYS = %i[schemaVersion revision featurevisorVersion segments features variables].freeze
|
|
18
|
+
|
|
15
19
|
def initialize(options)
|
|
16
20
|
datafile = options[:datafile]
|
|
17
21
|
@diagnostics = options[:diagnostics]
|
|
@@ -22,10 +26,15 @@ module Featurevisor
|
|
|
22
26
|
@segments = (datafile[:segments] || {}).transform_keys(&:to_sym)
|
|
23
27
|
@features = (datafile[:features] || {}).transform_keys(&:to_sym)
|
|
24
28
|
@variables = (datafile[:variables] || {}).transform_keys(&:to_sym)
|
|
29
|
+
# Every other top level key, such as a schemas section, kept as the datafile has it.
|
|
30
|
+
@sections = datafile.reject { |key, _| DATAFILE_KEYS.include?(key) }
|
|
25
31
|
@normalized = { feature: {}, segment: {}, variable: {} }
|
|
26
32
|
@normalization_slots = { feature: {}, segment: {}, variable: {} }
|
|
27
33
|
@normalization_mutex = Mutex.new
|
|
28
34
|
@regex_cache = {}
|
|
35
|
+
# A package's conditions as Core writes them, JSON text, parsed once per text
|
|
36
|
+
# for this datafile.
|
|
37
|
+
@package_conditions = {}
|
|
29
38
|
@conditions_cache = {}
|
|
30
39
|
@segments_cache = {}
|
|
31
40
|
@attribute_conditions_cache = {}
|
|
@@ -52,7 +61,15 @@ module Featurevisor
|
|
|
52
61
|
segments: @segments,
|
|
53
62
|
features: @features,
|
|
54
63
|
variables: @variables
|
|
55
|
-
}.compact
|
|
64
|
+
}.compact.merge(@sections)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
attr_reader :sections
|
|
68
|
+
|
|
69
|
+
# A section by name, such as "schemas": a top level key besides the SDK's own whose value is an object.
|
|
70
|
+
def get_section(name)
|
|
71
|
+
section = @sections[name.to_sym]
|
|
72
|
+
section.is_a?(Hash) ? section : nil
|
|
56
73
|
end
|
|
57
74
|
|
|
58
75
|
# Get a segment by key
|
|
@@ -178,7 +195,7 @@ module Featurevisor
|
|
|
178
195
|
invalid_flags = flags.delete("gimsuy")
|
|
179
196
|
raise ArgumentError, "invalid regular expression flags: #{invalid_flags}" unless invalid_flags.empty?
|
|
180
197
|
|
|
181
|
-
regex = Regexp.new(Conditions.normalize_portable_regex(regex_string), ruby_flags)
|
|
198
|
+
regex = Regexp.new(Conditions.normalize_portable_regex(regex_string, flags), ruby_flags)
|
|
182
199
|
@regex_cache[cache_key] = regex
|
|
183
200
|
@regex_cache[cache_key]
|
|
184
201
|
end
|
|
@@ -121,6 +121,8 @@ module Featurevisor
|
|
|
121
121
|
identity[:type] = options[:type] unless options[:type].nil?
|
|
122
122
|
identity[:feature_key] = options[:feature_key] unless options[:feature_key].nil?
|
|
123
123
|
identity[:variable_key] = options[:variable_key] unless options[:variable_key].nil?
|
|
124
|
+
# A package's entry, such as a schema, whose conditions read features and variables.
|
|
125
|
+
identity[:key] = options[:__featurevisor_entry_key] unless options[:__featurevisor_entry_key].nil?
|
|
124
126
|
identity
|
|
125
127
|
end
|
|
126
128
|
|
data/lib/featurevisor/events.rb
CHANGED
|
@@ -49,6 +49,7 @@ module Featurevisor
|
|
|
49
49
|
revisionChanged: previous_revision != new_revision,
|
|
50
50
|
features: new_feature_keys,
|
|
51
51
|
variables: new_variable_keys,
|
|
52
|
+
segments: new_reader.get_segment_keys,
|
|
52
53
|
replaced: replace
|
|
53
54
|
}
|
|
54
55
|
end
|
|
@@ -124,9 +125,80 @@ module Featurevisor
|
|
|
124
125
|
revisionChanged: previous_revision != new_revision,
|
|
125
126
|
features: all_affected_features,
|
|
126
127
|
variables: all_affected_variables,
|
|
128
|
+
# The dependency changes add features and variables only, so these are the changed definitions.
|
|
129
|
+
segments: changed_segments,
|
|
127
130
|
replaced: replace
|
|
128
131
|
}
|
|
129
132
|
end
|
|
130
133
|
|
|
134
|
+
# How deep two context values are compared. A value nested deeper, such as
|
|
135
|
+
# one that contains itself, counts as changed.
|
|
136
|
+
ATTRIBUTE_VALUE_DEPTH = 64
|
|
137
|
+
|
|
138
|
+
# The top level keys whose values differ between two contexts, compared by
|
|
139
|
+
# value. A merge can change only the keys it carries, given as incoming, and
|
|
140
|
+
# a replace any key of either context. nil is a value, so a key set to nil
|
|
141
|
+
# where it was absent has changed.
|
|
142
|
+
def self.get_changed_context_keys(previous, next_context, incoming = nil)
|
|
143
|
+
if previous.equal?(next_context) || previous.equal?(incoming)
|
|
144
|
+
# The stored context given back may have been changed in place, so every
|
|
145
|
+
# key in it may have changed.
|
|
146
|
+
return previous.keys + next_context.keys.reject { |key| previous.key?(key) }
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
if incoming
|
|
150
|
+
return incoming.keys.select do |key|
|
|
151
|
+
!previous.key?(key) || !same_attribute_value?(previous[key], next_context[key], 0)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
previous.keys.select do |key|
|
|
156
|
+
!next_context.key?(key) || !same_attribute_value?(previous[key], next_context[key], 0)
|
|
157
|
+
end + next_context.keys.reject { |key| previous.key?(key) }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Whether an attribute keeps its value. Numbers compare by numeric value,
|
|
161
|
+
# hashes key by key and arrays item by item. A hash or array given again as
|
|
162
|
+
# the same object counts as changed, since it may have been changed in place.
|
|
163
|
+
def self.same_attribute_value?(before, after, depth)
|
|
164
|
+
return !(before.is_a?(Hash) || before.is_a?(Array)) if before.equal?(after)
|
|
165
|
+
return false if depth > ATTRIBUTE_VALUE_DEPTH
|
|
166
|
+
|
|
167
|
+
case before
|
|
168
|
+
when Hash
|
|
169
|
+
after.is_a?(Hash) && before.size == after.size &&
|
|
170
|
+
before.all? { |key, value| after.key?(key) && same_attribute_value?(value, after[key], depth + 1) }
|
|
171
|
+
when Array
|
|
172
|
+
after.is_a?(Array) && before.size == after.size &&
|
|
173
|
+
before.each_index.all? { |index| same_attribute_value?(before[index], after[index], depth + 1) }
|
|
174
|
+
when Numeric
|
|
175
|
+
after.is_a?(Numeric) && before == after
|
|
176
|
+
else
|
|
177
|
+
before.instance_of?(after.class) && before == after
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# The changed entries of every section in either datafile, by hash: added,
|
|
183
|
+
# changed or, on replace, removed. The first datafile reports every entry.
|
|
184
|
+
def self.get_changed_sections(previous_reader, new_reader)
|
|
185
|
+
names = (previous_reader.sections.select { |_, value| value.is_a?(Hash) }.keys +
|
|
186
|
+
new_reader.sections.select { |_, value| value.is_a?(Hash) }.keys).uniq
|
|
187
|
+
names.each_with_object({}) do |name, sections|
|
|
188
|
+
before = previous_reader.get_section(name) || {}
|
|
189
|
+
after = new_reader.get_section(name) || {}
|
|
190
|
+
sections[name.to_s] = if before.empty?
|
|
191
|
+
after.keys.map(&:to_s)
|
|
192
|
+
else
|
|
193
|
+
changed = before.keys.select do |key|
|
|
194
|
+
entry = before[key]
|
|
195
|
+
next_entry = after[key]
|
|
196
|
+
!after.key?(key) || !entry.is_a?(Hash) || !next_entry.is_a?(Hash) ||
|
|
197
|
+
entry[:hash].to_s.empty? || next_entry[:hash].to_s.empty? || entry[:hash] != next_entry[:hash]
|
|
198
|
+
end
|
|
199
|
+
(changed + (after.keys - before.keys)).map(&:to_s)
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
131
203
|
end
|
|
132
204
|
end
|
|
@@ -133,7 +133,15 @@ module Featurevisor
|
|
|
133
133
|
diagnostics: @diagnostics
|
|
134
134
|
)
|
|
135
135
|
new_datafile.retain_normalized(@datafile, parsed_datafile) unless replace
|
|
136
|
-
|
|
136
|
+
# Only a datafile_set listener or an info diagnostic receiver reads the
|
|
137
|
+
# details, so only then are they and the dependency closure worked out.
|
|
138
|
+
# Without them the new provider keeps no index, and the next update
|
|
139
|
+
# that is read builds it again from the datafile it has.
|
|
140
|
+
event = nil
|
|
141
|
+
if @emitter.listeners["datafile_set"]&.any? || diagnostic_interested?("info", nil)
|
|
142
|
+
event = Featurevisor::Events.get_params_for_datafile_set_event(@datafile, new_datafile, replace, parsed_datafile)
|
|
143
|
+
event[:sections] = Featurevisor::Events.get_changed_sections(@datafile, new_datafile)
|
|
144
|
+
end
|
|
137
145
|
@datafile = new_datafile
|
|
138
146
|
@evaluation_generation += 1
|
|
139
147
|
event
|
|
@@ -141,13 +149,15 @@ module Featurevisor
|
|
|
141
149
|
|
|
142
150
|
normalization_errors.each { |diagnostic| report_diagnostic(diagnostic) }
|
|
143
151
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
152
|
+
if details
|
|
153
|
+
report_diagnostic(
|
|
154
|
+
level: "info",
|
|
155
|
+
code: "datafile_set",
|
|
156
|
+
message: "Datafile set",
|
|
157
|
+
details: details
|
|
158
|
+
)
|
|
159
|
+
@emitter.trigger("datafile_set", details)
|
|
160
|
+
end
|
|
151
161
|
true
|
|
152
162
|
rescue => e
|
|
153
163
|
report_diagnostic(
|
|
@@ -247,11 +257,13 @@ module Featurevisor
|
|
|
247
257
|
|
|
248
258
|
# Subscribe to an event
|
|
249
259
|
# @param event_name [String] Event name
|
|
250
|
-
# @param callback [Proc] Callback function
|
|
260
|
+
# @param callback [Proc] Callback function, or a block
|
|
251
261
|
# @return [Proc] Unsubscribe function
|
|
252
|
-
def on(event_name, callback)
|
|
262
|
+
def on(event_name, callback = nil, &block)
|
|
253
263
|
return -> {} if @closed
|
|
254
264
|
|
|
265
|
+
# A block or a callable, as a child takes them.
|
|
266
|
+
callback = block if block_given?
|
|
255
267
|
@emitter.on(event_name, callback)
|
|
256
268
|
end
|
|
257
269
|
|
|
@@ -272,6 +284,7 @@ module Featurevisor
|
|
|
272
284
|
# @param context [Hash] Context to set
|
|
273
285
|
# @param replace [Boolean] Whether to replace existing context
|
|
274
286
|
def set_context(context, replace = false)
|
|
287
|
+
previous = @context
|
|
275
288
|
if replace
|
|
276
289
|
@context = context
|
|
277
290
|
else
|
|
@@ -279,19 +292,19 @@ module Featurevisor
|
|
|
279
292
|
end
|
|
280
293
|
@evaluation_generation += 1
|
|
281
294
|
|
|
282
|
-
@
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
295
|
+
details = { context: @context, replaced: replace }
|
|
296
|
+
# Only a listener or a debug diagnostic receiver reads the changed keys, so
|
|
297
|
+
# only then are they worked out.
|
|
298
|
+
if @emitter.listeners["context_set"]&.any? || diagnostic_interested?("debug", nil)
|
|
299
|
+
details[:keys] = Events.get_changed_context_keys(previous, @context, replace ? nil : context)
|
|
300
|
+
end
|
|
301
|
+
@emitter.trigger("context_set", details)
|
|
286
302
|
|
|
287
303
|
report_diagnostic(
|
|
288
304
|
level: "debug",
|
|
289
305
|
code: "context_set",
|
|
290
306
|
message: replace ? "Context replaced" : "Context updated",
|
|
291
|
-
details:
|
|
292
|
-
context: @context,
|
|
293
|
-
replaced: replace
|
|
294
|
-
}
|
|
307
|
+
details: details
|
|
295
308
|
)
|
|
296
309
|
end
|
|
297
310
|
|
|
@@ -584,7 +597,7 @@ module Featurevisor
|
|
|
584
597
|
session_options = options[:__featurevisor_prerequisite] ? global_variable_session_options(evaluation_options, options) : evaluation_options
|
|
585
598
|
return session.run(session_options,
|
|
586
599
|
prerequisite: options[:__featurevisor_prerequisite]) do
|
|
587
|
-
|
|
600
|
+
if @modules_manager.before_evaluation?
|
|
588
601
|
evaluation_options = @modules_manager.run_before_evaluation_modules(evaluation_options)
|
|
589
602
|
end
|
|
590
603
|
session.update_current(evaluation_options)
|
|
@@ -655,7 +668,7 @@ module Featurevisor
|
|
|
655
668
|
if !evaluation.key?(:variable_value) && evaluation_options.key?(:default_variable_value)
|
|
656
669
|
evaluation[:variable_value] = evaluation_options[:default_variable_value]
|
|
657
670
|
end
|
|
658
|
-
|
|
671
|
+
if @modules_manager.after_evaluation?
|
|
659
672
|
evaluation = @modules_manager.run_after_evaluation_modules(evaluation, evaluation_options)
|
|
660
673
|
end
|
|
661
674
|
report_diagnostic(level: "debug", code: evaluation[:reason], message: "Global variable evaluated", details: evaluation)
|
|
@@ -863,7 +876,16 @@ module Featurevisor
|
|
|
863
876
|
**(previous[:variables] || {}),
|
|
864
877
|
**(incoming[:variables] || {})
|
|
865
878
|
}
|
|
866
|
-
}.compact
|
|
879
|
+
}.compact.tap do |merged|
|
|
880
|
+
# Sections merge by entry, as segments do. A top level value that is not an object is replaced whole.
|
|
881
|
+
[previous, incoming].each do |source|
|
|
882
|
+
source.each do |key, value|
|
|
883
|
+
next if InstanceEvaluationDataProvider::DATAFILE_KEYS.include?(key)
|
|
884
|
+
current = merged[key]
|
|
885
|
+
merged[key] = source.equal?(incoming) && value.is_a?(Hash) && current.is_a?(Hash) ? current.merge(value) : value
|
|
886
|
+
end
|
|
887
|
+
end
|
|
888
|
+
end
|
|
867
889
|
end
|
|
868
890
|
|
|
869
891
|
def create_module_api(mod)
|
|
@@ -882,7 +904,24 @@ module Featurevisor
|
|
|
882
904
|
@module_diagnostic_subscriptions << subscription
|
|
883
905
|
-> { @module_diagnostic_subscriptions.reject! { |item| item[:id] == subscription[:id] } }
|
|
884
906
|
end,
|
|
885
|
-
report_diagnostic: ->(diagnostic) { report_diagnostic(diagnostic, mod) }
|
|
907
|
+
report_diagnostic: ->(diagnostic) { report_diagnostic(diagnostic, mod) },
|
|
908
|
+
# An entry of a datafile section, such as ("schemas", "signUp"), as stored with symbol keys, so treat it as read only.
|
|
909
|
+
get_entry: ->(section, key) { @datafile.get_section(section)&.[](key.to_sym) },
|
|
910
|
+
# The keys of a datafile section's entries, or an empty list for a missing section.
|
|
911
|
+
get_entry_keys: ->(section) { (@datafile.get_section(section) || {}).keys.map(&:to_s) },
|
|
912
|
+
# Answers a package's conditions with the instance's engine. Readers answer
|
|
913
|
+
# leaves only the package knows, such as input, returning
|
|
914
|
+
# Featurevisor::Conditions.missing_value for no value.
|
|
915
|
+
match_conditions: lambda do |conditions, context = {}, options = {}|
|
|
916
|
+
PackageConditions.match(instance, conditions, context, options, mod)
|
|
917
|
+
end,
|
|
918
|
+
# Evaluates a package's own entry as a getter evaluates a feature: every
|
|
919
|
+
# module's before_evaluation runs first, with type and key naming the entry,
|
|
920
|
+
# and the block answers conditions with the context the modules leave, in
|
|
921
|
+
# one dependency session. The block's result is returned.
|
|
922
|
+
evaluate: lambda do |request, callback = nil, &block|
|
|
923
|
+
PackageConditions.evaluate(instance, request, mod, &(callback || block))
|
|
924
|
+
end
|
|
886
925
|
}
|
|
887
926
|
end
|
|
888
927
|
|