featurevisor 3.1.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.
@@ -35,6 +35,8 @@ module Featurevisor
35
35
  @sticky_features = options[:sticky_features] || options[:stickyFeatures] || {}
36
36
  @sticky_variables = options[:sticky_variables] || options[:stickyVariables] || {}
37
37
  @closed = false
38
+ @evaluation_generation = 0
39
+ @datafile_mutex = Mutex.new
38
40
  @module_diagnostic_subscriptions = []
39
41
 
40
42
  # datafile
@@ -47,6 +49,7 @@ module Featurevisor
47
49
  modules: options[:modules] || [],
48
50
  report_diagnostic: method(:report_diagnostic),
49
51
  module_api_factory: method(:create_module_api),
52
+ on_change: method(:invalidate_evaluation_memo),
50
53
  clear_module_diagnostic_subscriptions: method(:clear_module_diagnostic_subscriptions)
51
54
  )
52
55
 
@@ -68,6 +71,8 @@ module Featurevisor
68
71
  end
69
72
 
70
73
  def handle_evaluation_diagnostic(level, message, details = nil)
74
+ return unless diagnostic_interested?(level)
75
+
71
76
  details = (details || {}).dup
72
77
  code = details.delete(:code) || details.delete("code") || details[:reason] || details["reason"] || message
73
78
  code = "deprecated_feature" if message == "feature is deprecated"
@@ -97,8 +102,9 @@ module Featurevisor
97
102
  # Set the datafile
98
103
  # @param datafile [Hash, String] Datafile content or JSON string
99
104
  # @param replace [Boolean] Whether to replace instead of merge
105
+ # @return [Boolean] Whether the datafile was accepted, even if unchanged
100
106
  def set_datafile(datafile, replace = false)
101
- return if @closed
107
+ return false if @closed
102
108
 
103
109
  begin
104
110
  parsed_datafile = if datafile.is_a?(String)
@@ -114,22 +120,45 @@ module Featurevisor
114
120
  (!parsed_datafile.key?(:variables) || parsed_datafile[:variables].is_a?(Hash))
115
121
  raise ArgumentError, "Invalid datafile"
116
122
  end
117
- next_datafile = replace ? parsed_datafile : merge_datafiles(@datafile.get_datafile, parsed_datafile)
118
- new_datafile = InstanceEvaluationDataProvider.new(
119
- datafile: next_datafile,
120
- diagnostics: @diagnostics
121
- )
123
+ normalization_errors = []
124
+ if parsed_datafile[:schemaVersion] == "2"
125
+ parsed_datafile, normalization_errors = Normalize.datafile(parsed_datafile)
126
+ end
127
+ # Serialize merge and publication, leaving application callbacks outside.
128
+ details = @datafile_mutex.synchronize do
129
+ return false if @closed
130
+ next_datafile = replace ? parsed_datafile : merge_datafiles(@datafile.get_datafile, parsed_datafile)
131
+ new_datafile = InstanceEvaluationDataProvider.new(
132
+ datafile: next_datafile,
133
+ diagnostics: @diagnostics
134
+ )
135
+ new_datafile.retain_normalized(@datafile, parsed_datafile) unless replace
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
145
+ @datafile = new_datafile
146
+ @evaluation_generation += 1
147
+ event
148
+ end
122
149
 
123
- details = Featurevisor::Events.get_params_for_datafile_set_event(@datafile, new_datafile, replace)
124
- @datafile = new_datafile
150
+ normalization_errors.each { |diagnostic| report_diagnostic(diagnostic) }
125
151
 
126
- report_diagnostic(
127
- level: "info",
128
- code: "datafile_set",
129
- message: "Datafile set",
130
- details: details
131
- )
132
- @emitter.trigger("datafile_set", details)
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
161
+ true
133
162
  rescue => e
134
163
  report_diagnostic(
135
164
  level: "error",
@@ -137,6 +166,7 @@ module Featurevisor
137
166
  message: "Could not parse datafile",
138
167
  original_error: e
139
168
  )
169
+ false
140
170
  end
141
171
  end
142
172
 
@@ -156,6 +186,7 @@ module Featurevisor
156
186
  end
157
187
 
158
188
  params = Featurevisor::Events.get_params_for_sticky_features_set_event(previous_sticky_features, @sticky_features, replace)
189
+ @evaluation_generation += 1
159
190
 
160
191
  report_diagnostic(
161
192
  level: "info",
@@ -169,6 +200,7 @@ module Featurevisor
169
200
  def set_sticky_variables(sticky, replace = false)
170
201
  previous = @sticky_variables || {}
171
202
  @sticky_variables = replace ? sticky : { **@sticky_variables, **sticky }
203
+ @evaluation_generation += 1
172
204
  params = Featurevisor::Events.get_params_for_sticky_variables_set_event(previous, @sticky_variables, replace)
173
205
  report_diagnostic(level: "info", code: "sticky_variables_set", message: "Sticky variables set", details: params)
174
206
  @emitter.trigger("sticky_variables_set", params)
@@ -214,53 +246,65 @@ module Featurevisor
214
246
  @modules_manager.add(mod)
215
247
  end
216
248
 
249
+ def invalidate_evaluation_memo
250
+ @evaluation_generation += 1
251
+ end
252
+ private :invalidate_evaluation_memo
253
+
217
254
  def remove_module(name)
218
255
  @modules_manager.remove(name)
219
256
  end
220
257
 
221
258
  # Subscribe to an event
222
259
  # @param event_name [String] Event name
223
- # @param callback [Proc] Callback function
260
+ # @param callback [Proc] Callback function, or a block
224
261
  # @return [Proc] Unsubscribe function
225
- def on(event_name, callback)
262
+ def on(event_name, callback = nil, &block)
226
263
  return -> {} if @closed
227
264
 
265
+ # A block or a callable, as a child takes them.
266
+ callback = block if block_given?
228
267
  @emitter.on(event_name, callback)
229
268
  end
230
269
 
231
270
  # Close the instance
232
271
  def close
233
- return if @closed
234
-
235
- @closed = true
272
+ @datafile_mutex.synchronize do
273
+ return if @closed
274
+ @closed = true
275
+ end
236
276
  @modules_manager.close_all
237
277
  @module_diagnostic_subscriptions = []
238
278
  @emitter.clear_all
279
+ @on_diagnostic = nil
280
+ @datafile.clear_caches
239
281
  end
240
282
 
241
283
  # Set context
242
284
  # @param context [Hash] Context to set
243
285
  # @param replace [Boolean] Whether to replace existing context
244
286
  def set_context(context, replace = false)
287
+ previous = @context
245
288
  if replace
246
289
  @context = context
247
290
  else
248
291
  @context = { **@context, **context }
249
292
  end
293
+ @evaluation_generation += 1
250
294
 
251
- @emitter.trigger("context_set", {
252
- context: @context,
253
- replaced: replace
254
- })
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)
255
302
 
256
303
  report_diagnostic(
257
304
  level: "debug",
258
305
  code: "context_set",
259
306
  message: replace ? "Context replaced" : "Context updated",
260
- details: {
261
- context: @context,
262
- replaced: replace
263
- }
307
+ details: details
264
308
  )
265
309
  end
266
310
 
@@ -315,6 +359,7 @@ module Featurevisor
315
359
  evaluation = evaluate_flag(feature_key, context, options)
316
360
  evaluation[:enabled] == true
317
361
  rescue => e
362
+ raise if e.is_a?(RequirementError) && options[:__featurevisor_prerequisite]
318
363
  report_diagnostic(level: "error", code: "evaluation_error", message: "isEnabled failed", originalError: e, details: { featureKey: feature_key })
319
364
  false
320
365
  end
@@ -351,6 +396,7 @@ module Featurevisor
351
396
  nil
352
397
  end
353
398
  rescue => e
399
+ raise if e.is_a?(RequirementError) && options[:__featurevisor_prerequisite]
354
400
  report_diagnostic(level: "error", code: "evaluation_error", message: "getVariation failed", originalError: e, details: { featureKey: feature_key })
355
401
  nil
356
402
  end
@@ -389,7 +435,7 @@ module Featurevisor
389
435
 
390
436
  if evaluation.key?(:variable_value)
391
437
  variable_type = evaluation.dig(:variable_schema, :type) || evaluation.dig(:variable, :type)
392
- if variable_type == "json" &&
438
+ if variable_type == "json" && !evaluation[:__featurevisor_decoded_json] && !evaluation.dig(:variable, :compose) &&
393
439
  evaluation[:variable_value].is_a?(String)
394
440
  JSON.parse(evaluation[:variable_value], symbolize_names: true)
395
441
  else
@@ -399,6 +445,8 @@ module Featurevisor
399
445
  nil
400
446
  end
401
447
  rescue => e
448
+ getter_options = variable_key_or_context.is_a?(String) ? options : context_or_options
449
+ raise if e.is_a?(RequirementError) && getter_options[:__featurevisor_prerequisite]
402
450
  report_diagnostic(level: "error", code: "evaluation_error", message: "getVariable failed", originalError: e, details: { variableKey: variable_key_or_context || feature_or_variable_key })
403
451
  nil
404
452
  end
@@ -437,15 +485,15 @@ module Featurevisor
437
485
  get_value_by_type(variable_value, "integer")
438
486
  end
439
487
 
440
- # Get variable as double
488
+ # Get variable as a number, whole or fractional
441
489
  # @param feature_key [String] Feature key
442
490
  # @param variable_key [String] Variable key
443
491
  # @param context [Hash] Context
444
492
  # @param options [Hash] Override options
445
493
  # @return [Float, nil] Float value or nil
446
- def get_variable_double(*args)
494
+ def get_variable_number(*args)
447
495
  variable_value = get_variable(*args)
448
- get_value_by_type(variable_value, "double")
496
+ get_value_by_type(variable_value, "number")
449
497
  end
450
498
 
451
499
  # Get variable as array
@@ -538,14 +586,24 @@ module Featurevisor
538
586
  evaluation_options = {
539
587
  type: "variable",
540
588
  variable_key: variable_key.to_s,
541
- context: get_context(context)
589
+ context: options[:__featurevisor_effective_context] ? context : get_context(context)
542
590
  }
591
+ session = options[:__featurevisor_session] || EvaluationSession.new
592
+ evaluation_options[:dependency_of] = session.dependency_of if options[:__featurevisor_prerequisite]
593
+ evaluation_options[:dependency_purpose] = options[:dependency_purpose] || "condition" if options[:__featurevisor_prerequisite]
543
594
  evaluation_options[:default_variable_value] = options[:default_variable_value] if options.key?(:default_variable_value)
544
595
  begin
545
- evaluation_options = @modules_manager.run_before_evaluation_modules(evaluation_options)
596
+ # Only prerequisite entries fingerprint effective dependency inputs.
597
+ session_options = options[:__featurevisor_prerequisite] ? global_variable_session_options(evaluation_options, options) : evaluation_options
598
+ return session.run(session_options,
599
+ prerequisite: options[:__featurevisor_prerequisite]) do
600
+ if @modules_manager.before_evaluation?
601
+ evaluation_options = @modules_manager.run_before_evaluation_modules(evaluation_options)
602
+ end
603
+ session.update_current(evaluation_options)
546
604
  resolved_key = evaluation_options[:variable_key]
547
605
  variable = @datafile.get_global_variable(resolved_key)
548
- sticky = options[:__featurevisor_child_sticky_variables] || @sticky_variables
606
+ sticky = options[:__featurevisor_sticky_variables_source]&.call || options[:__featurevisor_child_sticky_variables] || @sticky_variables
549
607
  evaluation = { type: "variable", variable_key: resolved_key, reason: Featurevisor::EvaluationReason::VARIABLE_NOT_FOUND }
550
608
 
551
609
  if sticky.key?(resolved_key) || sticky.key?(resolved_key.to_sym)
@@ -553,28 +611,49 @@ module Featurevisor
553
611
  evaluation.merge!(reason: Featurevisor::EvaluationReason::STICKY, variable: variable,
554
612
  variable_value: sticky_value)
555
613
  elsif variable
556
- unless required_features_are_matched(variable[:requiredFeatures], evaluation_options[:context], options)
557
- value_key = variable[:useDefaultWhenDisabled] ? :defaultValue : :disabledValue
558
- evaluation.merge!(reason: Featurevisor::EvaluationReason::REQUIRED_FEATURES_UNMET,
559
- variable: variable)
614
+ dependencies = get_evaluation_dependencies(evaluation_options[:context], options.merge(__featurevisor_effective_context: true, __featurevisor_session: session))
615
+ if variable.key?(:conditions) && !UnifiedConditions.matches?(variable[:conditions], dependencies)
616
+ value_key = !variable[:compose] && variable[:useDefaultWhenDisabled] ? :defaultValue : :disabledValue
617
+ evaluation.merge!(reason: Featurevisor::EvaluationReason::CONDITIONS_UNMET,
618
+ conditions: variable[:conditions], variable: variable)
560
619
  evaluation[:variable_value] = variable[value_key] if variable.key?(value_key)
620
+ evaluation[:variable_value] = nil if variable[:compose] && !variable.key?(value_key)
621
+ if variable[:compose].is_a?(Array) && evaluation[:variable_value].is_a?(Hash)
622
+ evaluation[:variable_value] = evaluation[:variable_value].dup
623
+ end
561
624
  else
562
- (variable[:overrides] || []).each_with_index do |override, index|
563
- next unless required_features_are_matched(override[:requiredFeatures], evaluation_options[:context], options)
564
- conditions_match = !override[:conditions] || @datafile.all_conditions_are_matched(
565
- @datafile.parse_conditions_if_stringified(override[:conditions]), evaluation_options[:context]
566
- )
567
- segments_match = !override[:segments] || @datafile.all_segments_are_matched(
568
- @datafile.parse_segments_if_stringified(override[:segments]), evaluation_options[:context]
569
- )
570
- next unless conditions_match && segments_match
571
-
572
- evaluation.merge!(reason: Featurevisor::EvaluationReason::VARIABLE_OVERRIDE_RULE,
573
- variable: variable,
574
- variable_override_index: index, variable_override_key: override[:key],
575
- variable_override_path: override[:keyPath])
576
- evaluation[:variable_value] = override[:value] if override.key?(:value)
577
- break
625
+ if variable[:compose]
626
+ evaluation.merge!(reason: Featurevisor::EvaluationReason::VARIABLE_COMPOSED,
627
+ variable: variable, variable_value: compose_value(variable[:compose], dependencies))
628
+ end
629
+ program = variable[:overrideProgram]
630
+ overrides = program || (variable[:compose] ? [] : variable[:overrides]) || []
631
+ accumulated_value = variable[:compose] ? evaluation[:variable_value] : variable[:defaultValue]
632
+ applied_paths = []
633
+ index = 0
634
+ while index < overrides.length
635
+ override = overrides[index]
636
+ matched = !override.key?(:conditions) || UnifiedConditions.matches?(override[:conditions], dependencies)
637
+ if matched
638
+ if program
639
+ accumulated_value = Mutations.apply(accumulated_value, override[:operations])
640
+ if variable[:compose].is_a?(Array) && override[:operations].any? { |operation| operation[:op] == "set" && operation[:path].empty? }
641
+ accumulated_value = accumulated_value.dup
642
+ end
643
+ applied_paths << (override[:keyPath] || [override[:key]])
644
+ end
645
+ evaluation.merge!(reason: Featurevisor::EvaluationReason::VARIABLE_OVERRIDE_RULE,
646
+ variable: variable, variable_override_index: index,
647
+ variable_override_key: override[:key], variable_override_path: override[:keyPath])
648
+ evaluation[:variable_value] = program ? accumulated_value : override[:value]
649
+ evaluation[:variable_override_paths] = applied_paths.dup if program
650
+ break unless program
651
+ end
652
+ next_index = program ? override[matched ? :onMatch : :onMiss] : index + 1
653
+ if program && (!next_index.is_a?(Integer) || next_index <= index || next_index > overrides.length)
654
+ raise ArgumentError, "Invalid variable override program jump"
655
+ end
656
+ index = next_index
578
657
  end
579
658
  if evaluation[:reason] == Featurevisor::EvaluationReason::VARIABLE_NOT_FOUND
580
659
  evaluation.merge!(reason: Featurevisor::EvaluationReason::VARIABLE_DEFAULT,
@@ -582,43 +661,137 @@ module Featurevisor
582
661
  evaluation[:variable_value] = variable[:defaultValue] if variable.key?(:defaultValue)
583
662
  end
584
663
  end
585
- report_diagnostic(level: "warn", code: "variable_deprecated", message: "Variable \"#{resolved_key}\" is deprecated",
664
+ report_diagnostic(level: "warn", code: "deprecated_variable", message: "Variable \"#{resolved_key}\" is deprecated",
586
665
  details: { variableKey: resolved_key, evaluation: evaluation }) if variable[:deprecated]
587
666
  end
588
667
 
589
668
  if !evaluation.key?(:variable_value) && evaluation_options.key?(:default_variable_value)
590
669
  evaluation[:variable_value] = evaluation_options[:default_variable_value]
591
670
  end
592
- evaluation = @modules_manager.run_global_after_modules(evaluation, evaluation_options)
671
+ if @modules_manager.after_evaluation?
672
+ evaluation = @modules_manager.run_after_evaluation_modules(evaluation, evaluation_options)
673
+ end
593
674
  report_diagnostic(level: "debug", code: evaluation[:reason], message: "Global variable evaluated", details: evaluation)
594
675
  evaluation
676
+ end
595
677
  rescue => e
678
+ raise if e.is_a?(RequirementError) && options[:__featurevisor_prerequisite]
596
679
  evaluation = { type: "variable", variable_key: evaluation_options[:variable_key],
597
680
  reason: Featurevisor::EvaluationReason::ERROR, error: e }
681
+ session.remember(global_variable_session_options(evaluation_options, options).merge(__featurevisor_prerequisite: true), evaluation) if options[:__featurevisor_prerequisite]
598
682
  report_diagnostic(level: "error", code: "evaluation_error", message: "Global variable evaluation failed",
599
683
  originalError: e, details: evaluation)
600
684
  evaluation
601
685
  end
602
686
  end
603
687
 
604
- def required_features_are_matched(requirements, context, options)
605
- return true if requirements.nil?
688
+ def global_variable_session_options(evaluation_options, options)
689
+ evaluation_options.merge(
690
+ sticky: options[:__featurevisor_sticky_features_source]&.call || options[:__featurevisor_child_sticky_features] || @sticky_features,
691
+ sticky_variables: options[:__featurevisor_sticky_variables_source]&.call || options[:__featurevisor_child_sticky_variables] || @sticky_variables,
692
+ datafile: @datafile,
693
+ __featurevisor_generation: -> { @evaluation_generation },
694
+ __featurevisor_child_generation: options[:__featurevisor_child_generation],
695
+ __featurevisor_project_variable: options[:__featurevisor_project_variable]
696
+ )
697
+ end
606
698
 
607
- items = requirements.is_a?(Array) ? requirements : [requirements]
608
- clean_options = options.reject { |key, _| %i[default_variation_value default_variable_value].include?(key) }
609
- items.all? do |required|
610
- if required.is_a?(String)
611
- key = required
612
- expected_enabled = true
613
- expected_variation = nil
614
- else
615
- key = required[:feature]
616
- expected_enabled = required.key?(:enabled) ? required[:enabled] : true
617
- expected_variation = required[:variation]
699
+ def project_variable_dependency(evaluation)
700
+ return evaluation unless evaluation.key?(:variable_value)
701
+ type = evaluation.dig(:variable_schema, :type) || evaluation.dig(:variable, :type)
702
+ return evaluation unless type == "json" && !evaluation.dig(:variable, :compose) && evaluation[:variable_value].is_a?(String)
703
+
704
+ evaluation.merge(variable_value: JSON.parse(evaluation[:variable_value], symbolize_names: true), __featurevisor_decoded_json: true)
705
+ rescue JSON::ParserError => error
706
+ report_diagnostic(level: "error", code: "evaluation_error", message: "getVariable failed", originalError: error,
707
+ details: { featureKey: evaluation[:feature_key], variableKey: evaluation[:variable_key] })
708
+ evaluation.merge(variable_value: nil, __featurevisor_decoded_json: true)
709
+ end
710
+
711
+ def evaluate_required_variable(required, dependencies)
712
+ options = {
713
+ __featurevisor_sticky_features_source: dependencies[:__featurevisor_sticky_features_source],
714
+ __featurevisor_sticky_variables_source: dependencies[:__featurevisor_sticky_variables_source],
715
+ __featurevisor_child_generation: dependencies[:__featurevisor_child_generation],
716
+ __featurevisor_session: dependencies[:__featurevisor_session],
717
+ __featurevisor_prerequisite: true,
718
+ __featurevisor_effective_context: true,
719
+ dependency_purpose: "condition",
720
+ __featurevisor_project_variable: method(:project_variable_dependency)
721
+ }
722
+ if required[:feature]
723
+ get_variable(required[:feature], required[:variable], dependencies[:context], options)
724
+ else
725
+ get_variable(required[:variable], dependencies[:context], options)
726
+ end
727
+ end
728
+
729
+ def composition_source(reference, dependencies)
730
+ context = dependencies[:context]
731
+ options = {
732
+ __featurevisor_session: dependencies[:__featurevisor_session],
733
+ __featurevisor_prerequisite: true,
734
+ __featurevisor_effective_context: true,
735
+ __featurevisor_sticky_features_source: dependencies[:__featurevisor_sticky_features_source],
736
+ __featurevisor_sticky_variables_source: dependencies[:__featurevisor_sticky_variables_source],
737
+ __featurevisor_child_generation: dependencies[:__featurevisor_child_generation],
738
+ dependency_purpose: "composition",
739
+ __featurevisor_project_variable: method(:project_variable_dependency)
740
+ }
741
+ if reference.key?(:variable)
742
+ reference.key?(:feature) ? get_variable(reference[:feature], reference[:variable], context, options) : get_variable(reference[:variable], context, options)
743
+ elsif reference[:variation]
744
+ get_variation(reference[:feature], context, options)
745
+ else
746
+ is_enabled(reference[:feature], context, options)
747
+ end
748
+ end
749
+
750
+ def compose_value(compose, dependencies)
751
+ return composition_source({ variable: compose }, dependencies) if compose.is_a?(String)
752
+ raise RequirementError, "Invalid composition" unless compose.is_a?(Array) && !compose.empty?
753
+ compose.each_with_object({}) do |reference, result|
754
+ key = reference[:key]
755
+ if !key.is_a?(String) || key.empty? || %w[__proto__ prototype constructor].include?(key)
756
+ raise RequirementError, "Invalid composition property"
757
+ end
758
+ result[key.to_sym] = composition_source(reference, dependencies)
759
+ end
760
+ end
761
+
762
+ def variable_condition_matches?(required, dependencies)
763
+ value = evaluate_required_variable(required, dependencies)
764
+ if required.key?(:path)
765
+ raise ArgumentError, "Variable requirement path must be compiled" unless required[:path].is_a?(Array)
766
+ required[:path].each do |part|
767
+ raise ArgumentError, "Unsafe variable requirement path" if %w[__proto__ prototype constructor].include?(part)
768
+ index = Mutations.send(:array_index, part)
769
+ if part.is_a?(Numeric) && index.nil?
770
+ raise ArgumentError, "Invalid variable requirement path"
771
+ end
772
+ value = if !index.nil?
773
+ value.is_a?(Array) && index < value.length ? value[index] : Conditions.missing_value
774
+ elsif part.is_a?(String)
775
+ if value.is_a?(Hash)
776
+ key = value.key?(part.to_sym) ? part.to_sym : part
777
+ value.key?(key) ? value[key] : Conditions.missing_value
778
+ elsif value.is_a?(Array) && part == "length"
779
+ value.length
780
+ elsif value.is_a?(Array) && part.match?(/\A(?:0|[1-9]\d*)\z/) && part.to_i < value.length
781
+ value[part.to_i]
782
+ else
783
+ Conditions.missing_value
784
+ end
785
+ else
786
+ raise ArgumentError, "Invalid variable requirement path"
787
+ end
618
788
  end
619
- next false unless is_enabled(key, context, clean_options) == expected_enabled
620
- expected_variation.nil? || get_variation(key, context, clean_options) == expected_variation
621
789
  end
790
+ Conditions.value_is_matched(value, required, ->(pattern, flags) { @datafile.get_regex(pattern, flags) })
791
+ rescue RequirementError
792
+ raise
793
+ rescue => error
794
+ raise RequirementError, error.message
622
795
  end
623
796
 
624
797
  # Get evaluation dependencies
@@ -626,14 +799,29 @@ module Featurevisor
626
799
  # @param options [Hash] Override options
627
800
  # @return [Hash] Evaluation dependencies
628
801
  def get_evaluation_dependencies(context, options = {})
802
+ sticky_source = options[:__featurevisor_sticky_features_source] ||
803
+ -> { options[:__featurevisor_child_sticky_features] || @sticky_features }
804
+ variable_source = options[:__featurevisor_sticky_variables_source] ||
805
+ -> { options[:__featurevisor_child_sticky_variables] || @sticky_variables }
629
806
  {
630
- context: get_context(context),
807
+ context: options[:__featurevisor_effective_context] ? context : get_context(context),
631
808
  diagnostics: @diagnostics,
632
809
  modules_manager: @modules_manager,
633
810
  datafile: @datafile,
634
- sticky: options[:__featurevisor_child_sticky_features] || @sticky_features,
635
- sticky_variables: options[:__featurevisor_child_sticky_variables] || @sticky_variables,
811
+ __featurevisor_instance: self,
812
+ sticky: sticky_source.call,
813
+ sticky_variables: variable_source.call,
814
+ __featurevisor_project_variable: options[:__featurevisor_project_variable],
815
+ __featurevisor_session: options[:__featurevisor_session],
816
+ __featurevisor_prerequisite: options[:__featurevisor_prerequisite],
817
+ __featurevisor_generation: -> { @evaluation_generation },
818
+ __featurevisor_child_generation: options[:__featurevisor_child_generation],
819
+ __featurevisor_sticky_features_source: sticky_source,
820
+ __featurevisor_sticky_variables_source: variable_source,
821
+ __featurevisor_datafile_source: -> { @datafile },
636
822
  }.tap do |dependencies|
823
+ dependencies[:dependency_purpose] = options[:dependency_purpose] if options[:dependency_purpose]
824
+ dependencies[:dependency_of] = options[:__featurevisor_session].dependency_of if options[:__featurevisor_prerequisite] && options[:__featurevisor_session]
637
825
  dependencies[:default_variation_value] = options[:default_variation_value] if options.key?(:default_variation_value)
638
826
  dependencies[:default_variable_value] = options[:default_variable_value] if options.key?(:default_variable_value)
639
827
  end
@@ -652,7 +840,7 @@ module Featurevisor
652
840
  when "integer"
653
841
  return value if value.is_a?(Integer)
654
842
  value.is_a?(Float) && value.finite? && value == value.to_i ? value.to_i : nil
655
- when "double"
843
+ when "number"
656
844
  value.is_a?(Numeric) && value.finite? ? value.to_f : nil
657
845
  when "boolean"
658
846
  value == true || value == false ? value : nil
@@ -688,7 +876,16 @@ module Featurevisor
688
876
  **(previous[:variables] || {}),
689
877
  **(incoming[:variables] || {})
690
878
  }
691
- }.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
692
889
  end
693
890
 
694
891
  def create_module_api(mod)
@@ -696,6 +893,8 @@ module Featurevisor
696
893
  {
697
894
  get_revision: -> { instance.get_revision },
698
895
  on_diagnostic: lambda do |handler, options = {}|
896
+ next -> {} if @closed
897
+
699
898
  subscription = {
700
899
  id: SecureRandom.uuid,
701
900
  module_id: mod.id,
@@ -705,7 +904,24 @@ module Featurevisor
705
904
  @module_diagnostic_subscriptions << subscription
706
905
  -> { @module_diagnostic_subscriptions.reject! { |item| item[:id] == subscription[:id] } }
707
906
  end,
708
- 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
709
925
  }
710
926
  end
711
927
 
@@ -714,6 +930,8 @@ module Featurevisor
714
930
  end
715
931
 
716
932
  def report_diagnostic(diagnostic, source_module = nil)
933
+ return unless diagnostic_interested?((diagnostic || {})[:level] || "info", source_module)
934
+
717
935
  diagnostic = (diagnostic || {}).dup
718
936
  diagnostic[:level] ||= "info"
719
937
  diagnostic[:module] = source_module.name if source_module && source_module.name
@@ -772,6 +990,26 @@ module Featurevisor
772
990
  subscriber_index >= diagnostic_index
773
991
  end
774
992
 
993
+ # Check live subscriptions before copying and formatting evaluation details.
994
+ # Errors also reach the error event, independently of diagnostic levels.
995
+ def diagnostic_interested?(level, source_module = nil)
996
+ return true if level == "error"
997
+
998
+ # The default console reporter and explicit handlers have historically
999
+ # handled unrecognized levels differently. Preserve both policies.
1000
+ instance_interested = if @on_diagnostic
1001
+ should_report_diagnostic?(level, @diagnostics.level)
1002
+ else
1003
+ LOG_LEVELS.index(level).to_i <= LOG_LEVELS.index(@diagnostics.level).to_i
1004
+ end
1005
+ instance_interested ||
1006
+ @module_diagnostic_subscriptions.any? do |subscription|
1007
+ (!source_module || subscription[:module_id] != source_module.id) &&
1008
+ should_report_diagnostic?(level, subscription[:level])
1009
+ end
1010
+ end
1011
+ private :diagnostic_interested?
1012
+
775
1013
  def camelize_diagnostic_value(value)
776
1014
  case value
777
1015
  when Hash