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/lib/featurevisor/modules.rb
CHANGED
|
@@ -23,6 +23,24 @@ module Featurevisor
|
|
|
23
23
|
@setup.call(api) if @setup
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
+
# Which evaluation phases the module takes part in. A module without hooks,
|
|
27
|
+
# such as a package's, takes part in none.
|
|
28
|
+
def before_evaluation?
|
|
29
|
+
!@before_evaluation.nil?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def after_evaluation?
|
|
33
|
+
!@after_evaluation.nil?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def bucket_key?
|
|
37
|
+
!@bucket_key.nil?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def bucket_value?
|
|
41
|
+
!@bucket_value.nil?
|
|
42
|
+
end
|
|
43
|
+
|
|
26
44
|
def call_before_evaluation(options)
|
|
27
45
|
return options unless @before_evaluation
|
|
28
46
|
|
|
@@ -56,7 +74,7 @@ module Featurevisor
|
|
|
56
74
|
attr_reader :modules
|
|
57
75
|
|
|
58
76
|
def initialize(options = {})
|
|
59
|
-
|
|
77
|
+
assign([])
|
|
60
78
|
@on_change = options[:on_change]
|
|
61
79
|
@report_diagnostic = options[:report_diagnostic]
|
|
62
80
|
@module_api_factory = options[:module_api_factory]
|
|
@@ -102,7 +120,7 @@ module Featurevisor
|
|
|
102
120
|
return nil
|
|
103
121
|
end
|
|
104
122
|
# Replace membership so an active phase keeps its starting list.
|
|
105
|
-
@modules
|
|
123
|
+
assign(@modules + [mod])
|
|
106
124
|
@on_change&.call
|
|
107
125
|
|
|
108
126
|
-> { remove(mod) }
|
|
@@ -110,11 +128,11 @@ module Featurevisor
|
|
|
110
128
|
|
|
111
129
|
def remove(name_or_module)
|
|
112
130
|
removed = []
|
|
113
|
-
@modules
|
|
131
|
+
assign(@modules.reject do |mod|
|
|
114
132
|
matches = name_or_module.equal?(mod) || mod.name == name_or_module
|
|
115
133
|
removed << mod if matches
|
|
116
134
|
matches
|
|
117
|
-
end
|
|
135
|
+
end)
|
|
118
136
|
@on_change&.call unless removed.empty?
|
|
119
137
|
|
|
120
138
|
removed.each do |mod|
|
|
@@ -127,6 +145,24 @@ module Featurevisor
|
|
|
127
145
|
@modules
|
|
128
146
|
end
|
|
129
147
|
|
|
148
|
+
# Whether any module takes part in a phase. A getter skips a phase no
|
|
149
|
+
# module takes part in, so a module without hooks costs evaluations nothing.
|
|
150
|
+
def before_evaluation?
|
|
151
|
+
@before_evaluation
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def after_evaluation?
|
|
155
|
+
@after_evaluation
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def bucket_key?
|
|
159
|
+
@bucket_key
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def bucket_value?
|
|
163
|
+
@bucket_value
|
|
164
|
+
end
|
|
165
|
+
|
|
130
166
|
def run_before_evaluation_modules(options)
|
|
131
167
|
@modules.reduce(options) do |result, mod|
|
|
132
168
|
mod.call_before_evaluation(result)
|
|
@@ -161,11 +197,20 @@ module Featurevisor
|
|
|
161
197
|
@clear_module_diagnostic_subscriptions.call(mod) if @clear_module_diagnostic_subscriptions
|
|
162
198
|
close_module(mod)
|
|
163
199
|
end
|
|
164
|
-
|
|
200
|
+
assign([])
|
|
165
201
|
end
|
|
166
202
|
|
|
167
203
|
private
|
|
168
204
|
|
|
205
|
+
# The list is replaced whole on every change, so the phases are known up front.
|
|
206
|
+
def assign(modules)
|
|
207
|
+
@modules = modules
|
|
208
|
+
@before_evaluation = modules.any?(&:before_evaluation?)
|
|
209
|
+
@after_evaluation = modules.any?(&:after_evaluation?)
|
|
210
|
+
@bucket_key = modules.any?(&:bucket_key?)
|
|
211
|
+
@bucket_value = modules.any?(&:bucket_value?)
|
|
212
|
+
end
|
|
213
|
+
|
|
169
214
|
def close_module(mod)
|
|
170
215
|
mod.call_close
|
|
171
216
|
rescue => e
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Featurevisor
|
|
6
|
+
# A package's conditions, such as a schema mutation's, as Core writes them.
|
|
7
|
+
# Groups, JSON text and leaves only the package knows, such as input, are
|
|
8
|
+
# answered here, and every other leaf by the instance's engine, in one
|
|
9
|
+
# dependency session whose feature and variable leaves read as dependencies of
|
|
10
|
+
# the package's entry.
|
|
11
|
+
module PackageConditions
|
|
12
|
+
OWN_LEAVES = %i[attribute variable feature].freeze
|
|
13
|
+
private_constant :OWN_LEAVES
|
|
14
|
+
|
|
15
|
+
def self.match(instance, conditions, context, options, mod)
|
|
16
|
+
options ||= {}
|
|
17
|
+
type = (options[:type] || options["type"] || mod&.name || "package").to_s
|
|
18
|
+
key = options[:key] || options["key"]
|
|
19
|
+
readers = (options[:readers] || options["readers"] || {}).transform_keys(&:to_s)
|
|
20
|
+
dependencies = instance.send(:get_evaluation_dependencies, context || {})
|
|
21
|
+
session = dependencies[:__featurevisor_session] ||= EvaluationSession.new
|
|
22
|
+
session.run({ type: type, __featurevisor_entry_key: key }) { walk(conditions, dependencies, readers) }
|
|
23
|
+
rescue StandardError, SystemStackError => error
|
|
24
|
+
instance.send(:report_diagnostic, {
|
|
25
|
+
level: "error",
|
|
26
|
+
code: "evaluation_error",
|
|
27
|
+
message: "matchConditions failed",
|
|
28
|
+
original_error: error,
|
|
29
|
+
details: { type: type, key: key }.compact
|
|
30
|
+
}, mod)
|
|
31
|
+
false
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# What a package evaluation's block reads with: the context after every
|
|
35
|
+
# module's before_evaluation, as a getter's conditions see it, and conditions
|
|
36
|
+
# answered with it in the evaluation's one dependency session.
|
|
37
|
+
class Scope
|
|
38
|
+
attr_reader :context
|
|
39
|
+
|
|
40
|
+
def initialize(context, match)
|
|
41
|
+
@context = context
|
|
42
|
+
@match = match
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Answers conditions as the module API's match_conditions does, with readers
|
|
46
|
+
# for leaves only the package knows, such as input.
|
|
47
|
+
def match_conditions(conditions, readers = {})
|
|
48
|
+
@match.call(conditions, readers)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.evaluate(instance, request, mod)
|
|
53
|
+
request ||= {}
|
|
54
|
+
type = (request[:type] || request["type"]).to_s
|
|
55
|
+
key = request[:key] || request["key"]
|
|
56
|
+
details = { type: type, key: key }.compact
|
|
57
|
+
dependencies = instance.send(:get_evaluation_dependencies, request[:context] || request["context"] || {})
|
|
58
|
+
session = dependencies[:__featurevisor_session] ||= EvaluationSession.new
|
|
59
|
+
# One session for every condition the package answers in this evaluation.
|
|
60
|
+
session.run({ type: type, __featurevisor_entry_key: key }) do
|
|
61
|
+
options = dependencies.merge(type: type, key: key)
|
|
62
|
+
evaluated = before_modules(instance, options, mod, details)
|
|
63
|
+
match = lambda do |conditions, readers|
|
|
64
|
+
walk(conditions, evaluated, (readers || {}).transform_keys(&:to_s))
|
|
65
|
+
rescue StandardError, SystemStackError => error
|
|
66
|
+
report(instance, error, details, mod, "matchConditions failed")
|
|
67
|
+
false
|
|
68
|
+
end
|
|
69
|
+
yield Scope.new(evaluated[:context], match)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Every module's before_evaluation in registration order, as a getter runs them,
|
|
74
|
+
# keeping the private options and the sticky sources they would otherwise lose.
|
|
75
|
+
def self.before_modules(instance, options, mod, details)
|
|
76
|
+
modules_manager = options[:modules_manager]
|
|
77
|
+
return options unless modules_manager&.before_evaluation?
|
|
78
|
+
|
|
79
|
+
private_options = options.select { |name, _| Evaluate.private_evaluation_option?(name) }
|
|
80
|
+
public_options = options.reject { |name, _| Evaluate.private_evaluation_option?(name) }
|
|
81
|
+
evaluated = modules_manager.run_before_evaluation_modules(public_options)
|
|
82
|
+
evaluated = evaluated.reject { |name, _| Evaluate.private_evaluation_option?(name) }.merge(private_options)
|
|
83
|
+
{ sticky: :__featurevisor_sticky_features_source,
|
|
84
|
+
sticky_variables: :__featurevisor_sticky_variables_source }.each do |field, source|
|
|
85
|
+
next if evaluated[field].equal?(options[field])
|
|
86
|
+
|
|
87
|
+
replacement = evaluated[field]
|
|
88
|
+
evaluated[source] = -> { replacement }
|
|
89
|
+
end
|
|
90
|
+
evaluated
|
|
91
|
+
rescue StandardError => error
|
|
92
|
+
report(instance, error, details, mod, "beforeEvaluation failed")
|
|
93
|
+
options
|
|
94
|
+
end
|
|
95
|
+
private_class_method :before_modules
|
|
96
|
+
|
|
97
|
+
def self.report(instance, error, details, mod, message)
|
|
98
|
+
instance.send(:report_diagnostic, {
|
|
99
|
+
level: "error",
|
|
100
|
+
code: "evaluation_error",
|
|
101
|
+
message: message,
|
|
102
|
+
original_error: error,
|
|
103
|
+
details: details
|
|
104
|
+
}, mod)
|
|
105
|
+
end
|
|
106
|
+
private_class_method :report
|
|
107
|
+
|
|
108
|
+
# JSON text parsed once per text for the current datafile. Text that does not
|
|
109
|
+
# parse is reported on every call, as JavaScript does, and never kept.
|
|
110
|
+
def self.parsed_text(conditions, text, dependencies)
|
|
111
|
+
cache = dependencies[:datafile].respond_to?(:package_conditions) ? dependencies[:datafile].package_conditions : nil
|
|
112
|
+
return cache[conditions] if cache&.key?(conditions)
|
|
113
|
+
|
|
114
|
+
# Frozen, since every later call shares it.
|
|
115
|
+
parsed = JSON.parse(text, symbolize_names: true, freeze: true)
|
|
116
|
+
if cache
|
|
117
|
+
# Text a package makes up at run time cannot grow the cache without bound.
|
|
118
|
+
cache.clear if cache.size >= 1000
|
|
119
|
+
cache[conditions] = parsed
|
|
120
|
+
end
|
|
121
|
+
parsed
|
|
122
|
+
rescue JSON::ParserError => error
|
|
123
|
+
dependencies[:__featurevisor_instance]&.send(:report_diagnostic, {
|
|
124
|
+
level: "error",
|
|
125
|
+
code: "conditions_parse_error",
|
|
126
|
+
message: "Error parsing conditions",
|
|
127
|
+
original_error: error,
|
|
128
|
+
details: { conditions: conditions }
|
|
129
|
+
})
|
|
130
|
+
nil
|
|
131
|
+
end
|
|
132
|
+
private_class_method :parsed_text
|
|
133
|
+
|
|
134
|
+
def self.walk(conditions, dependencies, readers)
|
|
135
|
+
case conditions
|
|
136
|
+
when String
|
|
137
|
+
return true if conditions == "*"
|
|
138
|
+
|
|
139
|
+
text = conditions.lstrip
|
|
140
|
+
# Conditions written as JSON text, as Core writes them by default. A bare segment key stays a key.
|
|
141
|
+
if text.start_with?("{", "[", "\"")
|
|
142
|
+
parsed = parsed_text(conditions, text, dependencies)
|
|
143
|
+
return false if parsed.nil?
|
|
144
|
+
|
|
145
|
+
return walk(parsed, dependencies, readers)
|
|
146
|
+
end
|
|
147
|
+
UnifiedConditions.matches?(conditions, dependencies)
|
|
148
|
+
when Array
|
|
149
|
+
!conditions.empty? && conditions.all? { |child| walk(child, dependencies, readers) }
|
|
150
|
+
when Hash
|
|
151
|
+
leaf = conditions.transform_keys(&:to_sym)
|
|
152
|
+
return UnifiedConditions.matches?(symbolize(leaf), dependencies) if OWN_LEAVES.any? { |name| leaf.key?(name) }
|
|
153
|
+
|
|
154
|
+
# A leaf only a package knows, such as input, is read through the package's reader.
|
|
155
|
+
kind = leaf.keys.find { |name| readers.key?(name.to_s) }
|
|
156
|
+
return reader_leaf(leaf, kind, readers, dependencies) if kind
|
|
157
|
+
|
|
158
|
+
%i[and or not].each do |group|
|
|
159
|
+
next unless leaf.key?(group)
|
|
160
|
+
|
|
161
|
+
children = leaf[group]
|
|
162
|
+
return false unless children.is_a?(Array) && !children.empty?
|
|
163
|
+
return children.any? { |child| walk(child, dependencies, readers) } if group == :or
|
|
164
|
+
|
|
165
|
+
result = children.all? { |child| walk(child, dependencies, readers) }
|
|
166
|
+
return group == :not ? !result : result
|
|
167
|
+
end
|
|
168
|
+
false
|
|
169
|
+
else
|
|
170
|
+
false
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
private_class_method :walk
|
|
174
|
+
|
|
175
|
+
def self.reader_leaf(leaf, kind, readers, dependencies)
|
|
176
|
+
operator = leaf[:operator]
|
|
177
|
+
return false unless operator.is_a?(String)
|
|
178
|
+
|
|
179
|
+
# value is always a value. valueFrom reads the other side, such as another input.
|
|
180
|
+
missing = Conditions.missing_value
|
|
181
|
+
compared = leaf.key?(:value) ? leaf[:value] : missing
|
|
182
|
+
if leaf.key?(:valueFrom)
|
|
183
|
+
source = leaf[:valueFrom]
|
|
184
|
+
other = source.is_a?(Hash) ? source.keys.find { |name| readers.key?(name.to_s) } : nil
|
|
185
|
+
# A source no reader answers matches nothing, and a pattern read from a value is one nobody reviewed, so it never runs.
|
|
186
|
+
return false if other.nil? || operator.end_with?("atches")
|
|
187
|
+
|
|
188
|
+
compared = readers[other.to_s].call(source[other].to_s)
|
|
189
|
+
end
|
|
190
|
+
read = readers[kind.to_s].call(leaf[kind].to_s)
|
|
191
|
+
if compared.equal?(missing)
|
|
192
|
+
# Without a value to compare, JavaScript compares with undefined, which only an absent read equals.
|
|
193
|
+
return read.equal?(missing) if operator == "equals"
|
|
194
|
+
return !read.equal?(missing) if operator == "notEquals"
|
|
195
|
+
|
|
196
|
+
compared = nil
|
|
197
|
+
end
|
|
198
|
+
condition = { operator: operator, value: compared }
|
|
199
|
+
condition[:regexFlags] = leaf[:regexFlags] if leaf[:regexFlags].is_a?(String)
|
|
200
|
+
Conditions.value_is_matched(read, condition, dependencies[:datafile].method(:get_regex))
|
|
201
|
+
end
|
|
202
|
+
private_class_method :reader_leaf
|
|
203
|
+
|
|
204
|
+
def self.symbolize(value)
|
|
205
|
+
case value
|
|
206
|
+
when Hash then value.each_with_object({}) { |(key, item), result| result[key.to_sym] = symbolize(item) }
|
|
207
|
+
when Array then value.map { |item| symbolize(item) }
|
|
208
|
+
else value
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
private_class_method :symbolize
|
|
212
|
+
end
|
|
213
|
+
end
|
|
@@ -36,7 +36,8 @@ module Featurevisor
|
|
|
36
36
|
end
|
|
37
37
|
end
|
|
38
38
|
if conditions.key?(:feature)
|
|
39
|
-
|
|
39
|
+
# A package's entry key stays with the package, never with the features its conditions read.
|
|
40
|
+
clean = options.reject { |key, _| %i[feature_key variable_key default_variable_value default_variation_value key __featurevisor_entry_key].include?(key) }
|
|
40
41
|
clean = clean.merge(__featurevisor_prerequisite: true, dependency_purpose: "condition")
|
|
41
42
|
flag = Evaluate.evaluate_with_modules(clean.merge(type: "flag", feature_key: conditions[:feature]))
|
|
42
43
|
enabled = conditions[:enabled].nil? ? true : conditions[:enabled]
|
data/lib/featurevisor/version.rb
CHANGED
data/lib/featurevisor.rb
CHANGED
|
@@ -16,10 +16,28 @@ require_relative "featurevisor/evaluate"
|
|
|
16
16
|
require_relative "featurevisor/instance"
|
|
17
17
|
require_relative "featurevisor/child_instance"
|
|
18
18
|
require_relative "featurevisor/events"
|
|
19
|
+
require_relative "featurevisor/package_conditions"
|
|
19
20
|
|
|
20
21
|
module Featurevisor
|
|
21
22
|
class Error < StandardError; end
|
|
22
23
|
|
|
24
|
+
# Applies compiled operations, such as a schema mutation's, to a copy of a
|
|
25
|
+
# value, which is left unchanged. Raises ArgumentError for an operation the
|
|
26
|
+
# interpreter refuses.
|
|
27
|
+
def self.apply_variable_operations(value, operations)
|
|
28
|
+
Mutations.apply(value, operations)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Whether the version operators can compare a value, as a schema's format "semver" asks.
|
|
32
|
+
def self.valid_version?(version)
|
|
33
|
+
return false unless version.is_a?(String) && !version.include?("\n")
|
|
34
|
+
|
|
35
|
+
compare_versions(version, version)
|
|
36
|
+
true
|
|
37
|
+
rescue StandardError
|
|
38
|
+
false
|
|
39
|
+
end
|
|
40
|
+
|
|
23
41
|
private_constant :InstanceEvaluationDataProvider
|
|
24
42
|
private_constant :DiagnosticReporter
|
|
25
43
|
private_constant :Mutations
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: featurevisor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.
|
|
4
|
+
version: 4.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Fahad Heylaal
|
|
@@ -92,6 +92,7 @@ files:
|
|
|
92
92
|
- lib/featurevisor/murmurhash.rb
|
|
93
93
|
- lib/featurevisor/mutations.rb
|
|
94
94
|
- lib/featurevisor/normalize.rb
|
|
95
|
+
- lib/featurevisor/package_conditions.rb
|
|
95
96
|
- lib/featurevisor/unified_conditions.rb
|
|
96
97
|
- lib/featurevisor/version.rb
|
|
97
98
|
homepage: https://featurevisor.com
|