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.
- checksums.yaml +4 -4
- data/README.md +729 -547
- data/bin/cli.rb +84 -17
- data/bin/commands/assess_distribution.rb +10 -52
- data/bin/commands/benchmark.rb +10 -48
- data/bin/commands/node_cli.rb +20 -0
- data/bin/commands/runtime_project.rb +63 -0
- data/bin/commands/test.rb +846 -255
- data/bin/commands.rb +1 -0
- data/bin/segment_tests.rb +89 -0
- data/bin/test_checks.rb +767 -0
- data/lib/featurevisor/child_instance.rb +22 -3
- data/lib/featurevisor/compare_versions.rb +18 -36
- data/lib/featurevisor/conditions.rb +138 -14
- data/lib/featurevisor/dependencies.rb +150 -0
- data/lib/featurevisor/emitter.rb +4 -0
- data/lib/featurevisor/evaluate.rb +66 -117
- data/lib/featurevisor/evaluation_data_provider.rb +118 -52
- data/lib/featurevisor/evaluation_session.rb +193 -0
- data/lib/featurevisor/events.rb +101 -90
- data/lib/featurevisor/instance.rb +316 -78
- data/lib/featurevisor/modules.rb +50 -45
- data/lib/featurevisor/mutations.rb +213 -0
- data/lib/featurevisor/normalize.rb +105 -0
- data/lib/featurevisor/package_conditions.rb +213 -0
- data/lib/featurevisor/unified_conditions.rb +80 -0
- data/lib/featurevisor/version.rb +1 -1
- data/lib/featurevisor.rb +25 -0
- metadata +11 -1
|
@@ -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
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Featurevisor
|
|
4
|
+
module UnifiedConditions
|
|
5
|
+
def self.matches?(conditions, options)
|
|
6
|
+
match(conditions, options)
|
|
7
|
+
rescue Normalize::ParseFailure
|
|
8
|
+
false
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.match(conditions, options)
|
|
12
|
+
raise Normalize::ParseFailure if conditions.is_a?(Normalize::InvalidConditions)
|
|
13
|
+
return true if conditions == "*"
|
|
14
|
+
if conditions.is_a?(String)
|
|
15
|
+
datafile = options[:__featurevisor_datafile_source]&.call || options[:datafile]
|
|
16
|
+
segment = datafile.get_segment(conditions)
|
|
17
|
+
return false unless segment
|
|
18
|
+
raise Normalize::ParseFailure if segment[:conditions].is_a?(Normalize::InvalidConditions)
|
|
19
|
+
if datafile.attribute_only_conditions?(segment[:conditions])
|
|
20
|
+
return datafile.all_conditions_are_matched(segment[:conditions], options[:context])
|
|
21
|
+
end
|
|
22
|
+
session = options[:__featurevisor_session] ||= EvaluationSession.new
|
|
23
|
+
return session.segment(conditions) { match(segment[:conditions], options) }
|
|
24
|
+
end
|
|
25
|
+
if conditions.is_a?(Array)
|
|
26
|
+
return !conditions.empty? && conditions.all? { |child| match(child, options) }
|
|
27
|
+
end
|
|
28
|
+
return false unless conditions.is_a?(Hash)
|
|
29
|
+
if conditions.key?(:variable)
|
|
30
|
+
begin
|
|
31
|
+
return options[:__featurevisor_instance].send(:variable_condition_matches?, conditions, options)
|
|
32
|
+
rescue RequirementError
|
|
33
|
+
raise
|
|
34
|
+
rescue => error
|
|
35
|
+
raise RequirementError, error.message
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
if conditions.key?(:feature)
|
|
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) }
|
|
41
|
+
clean = clean.merge(__featurevisor_prerequisite: true, dependency_purpose: "condition")
|
|
42
|
+
flag = Evaluate.evaluate_with_modules(clean.merge(type: "flag", feature_key: conditions[:feature]))
|
|
43
|
+
enabled = conditions[:enabled].nil? ? true : conditions[:enabled]
|
|
44
|
+
return false unless (flag[:enabled] == true) == enabled
|
|
45
|
+
return true unless conditions.key?(:variation)
|
|
46
|
+
variation = Evaluate.evaluate_with_modules(clean.merge(type: "variation", feature_key: conditions[:feature]))
|
|
47
|
+
value = variation.key?(:variation_value) ? variation[:variation_value] : variation.dig(:variation, :value)
|
|
48
|
+
return value == conditions[:variation]
|
|
49
|
+
end
|
|
50
|
+
if conditions.key?(:attribute)
|
|
51
|
+
return options[:datafile].all_conditions_are_matched(conditions, options[:context])
|
|
52
|
+
end
|
|
53
|
+
%i[and or not].each do |group|
|
|
54
|
+
next unless conditions[group].is_a?(Array)
|
|
55
|
+
children = conditions[group]
|
|
56
|
+
return false if children.empty?
|
|
57
|
+
return children.any? { |child| match(child, options) } if group == :or
|
|
58
|
+
result = children.all? { |child| match(child, options) }
|
|
59
|
+
return group == :not ? !result : result
|
|
60
|
+
end
|
|
61
|
+
false
|
|
62
|
+
end
|
|
63
|
+
private_class_method :match
|
|
64
|
+
|
|
65
|
+
def self.attribute_only?(conditions)
|
|
66
|
+
case conditions
|
|
67
|
+
when Array
|
|
68
|
+
conditions.all? { |child| attribute_only?(child) }
|
|
69
|
+
when Hash
|
|
70
|
+
return false if conditions.is_a?(Normalize::InvalidConditions) || conditions.key?(:feature) || conditions.key?(:variable)
|
|
71
|
+
return true if conditions.key?(:attribute)
|
|
72
|
+
group = %i[and or not].find { |name| conditions.key?(name) }
|
|
73
|
+
group.nil? || attribute_only?(conditions[group])
|
|
74
|
+
else
|
|
75
|
+
false
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
private_class_method :attribute_only?
|
|
79
|
+
end
|
|
80
|
+
end
|
data/lib/featurevisor/version.rb
CHANGED
data/lib/featurevisor.rb
CHANGED
|
@@ -4,17 +4,42 @@ require_relative "featurevisor/compare_versions"
|
|
|
4
4
|
require_relative "featurevisor/diagnostics"
|
|
5
5
|
require_relative "featurevisor/emitter"
|
|
6
6
|
require_relative "featurevisor/conditions"
|
|
7
|
+
require_relative "featurevisor/normalize"
|
|
8
|
+
require_relative "featurevisor/mutations"
|
|
9
|
+
require_relative "featurevisor/dependencies"
|
|
7
10
|
require_relative "featurevisor/evaluation_data_provider"
|
|
8
11
|
require_relative "featurevisor/bucketer"
|
|
9
12
|
require_relative "featurevisor/modules"
|
|
13
|
+
require_relative "featurevisor/evaluation_session"
|
|
14
|
+
require_relative "featurevisor/unified_conditions"
|
|
10
15
|
require_relative "featurevisor/evaluate"
|
|
11
16
|
require_relative "featurevisor/instance"
|
|
12
17
|
require_relative "featurevisor/child_instance"
|
|
13
18
|
require_relative "featurevisor/events"
|
|
19
|
+
require_relative "featurevisor/package_conditions"
|
|
14
20
|
|
|
15
21
|
module Featurevisor
|
|
16
22
|
class Error < StandardError; end
|
|
17
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
|
+
|
|
18
41
|
private_constant :InstanceEvaluationDataProvider
|
|
19
42
|
private_constant :DiagnosticReporter
|
|
43
|
+
private_constant :Mutations
|
|
44
|
+
private_constant :Dependencies
|
|
20
45
|
end
|
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
|
+
version: 4.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Fahad Heylaal
|
|
@@ -69,21 +69,31 @@ files:
|
|
|
69
69
|
- bin/commands.rb
|
|
70
70
|
- bin/commands/assess_distribution.rb
|
|
71
71
|
- bin/commands/benchmark.rb
|
|
72
|
+
- bin/commands/node_cli.rb
|
|
73
|
+
- bin/commands/runtime_project.rb
|
|
72
74
|
- bin/commands/test.rb
|
|
73
75
|
- bin/featurevisor
|
|
76
|
+
- bin/segment_tests.rb
|
|
77
|
+
- bin/test_checks.rb
|
|
74
78
|
- lib/featurevisor.rb
|
|
75
79
|
- lib/featurevisor/bucketer.rb
|
|
76
80
|
- lib/featurevisor/child_instance.rb
|
|
77
81
|
- lib/featurevisor/compare_versions.rb
|
|
78
82
|
- lib/featurevisor/conditions.rb
|
|
83
|
+
- lib/featurevisor/dependencies.rb
|
|
79
84
|
- lib/featurevisor/diagnostics.rb
|
|
80
85
|
- lib/featurevisor/emitter.rb
|
|
81
86
|
- lib/featurevisor/evaluate.rb
|
|
82
87
|
- lib/featurevisor/evaluation_data_provider.rb
|
|
88
|
+
- lib/featurevisor/evaluation_session.rb
|
|
83
89
|
- lib/featurevisor/events.rb
|
|
84
90
|
- lib/featurevisor/instance.rb
|
|
85
91
|
- lib/featurevisor/modules.rb
|
|
86
92
|
- lib/featurevisor/murmurhash.rb
|
|
93
|
+
- lib/featurevisor/mutations.rb
|
|
94
|
+
- lib/featurevisor/normalize.rb
|
|
95
|
+
- lib/featurevisor/package_conditions.rb
|
|
96
|
+
- lib/featurevisor/unified_conditions.rb
|
|
87
97
|
- lib/featurevisor/version.rb
|
|
88
98
|
homepage: https://featurevisor.com
|
|
89
99
|
licenses:
|