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.
@@ -12,11 +12,9 @@ module Featurevisor
12
12
  @id = SecureRandom.uuid
13
13
  @name = options[:name]
14
14
  @setup = options[:setup]
15
- @before = options[:before]
16
15
  @before_evaluation = options[:before_evaluation] || options[:beforeEvaluation]
17
16
  @bucket_key = options[:bucket_key]
18
17
  @bucket_value = options[:bucket_value]
19
- @after = options[:after]
20
18
  @after_evaluation = options[:after_evaluation] || options[:afterEvaluation]
21
19
  @close = options[:close]
22
20
  end
@@ -25,10 +23,22 @@ module Featurevisor
25
23
  @setup.call(api) if @setup
26
24
  end
27
25
 
28
- def call_before(options)
29
- return options unless @before
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
30
39
 
31
- @before.call(options)
40
+ def bucket_value?
41
+ !@bucket_value.nil?
32
42
  end
33
43
 
34
44
  def call_before_evaluation(options)
@@ -49,12 +59,6 @@ module Featurevisor
49
59
  @bucket_value.call(options)
50
60
  end
51
61
 
52
- def call_after(evaluation, options)
53
- return evaluation unless @after
54
-
55
- @after.call(evaluation, options)
56
- end
57
-
58
62
  def call_after_evaluation(evaluation, options)
59
63
  return evaluation unless @after_evaluation
60
64
 
@@ -70,7 +74,8 @@ module Featurevisor
70
74
  attr_reader :modules
71
75
 
72
76
  def initialize(options = {})
73
- @modules = []
77
+ assign([])
78
+ @on_change = options[:on_change]
74
79
  @report_diagnostic = options[:report_diagnostic]
75
80
  @module_api_factory = options[:module_api_factory]
76
81
  @clear_module_diagnostic_subscriptions = options[:clear_module_diagnostic_subscriptions]
@@ -114,18 +119,21 @@ module Featurevisor
114
119
  close_module(mod)
115
120
  return nil
116
121
  end
117
- @modules << mod
122
+ # Replace membership so an active phase keeps its starting list.
123
+ assign(@modules + [mod])
124
+ @on_change&.call
118
125
 
119
126
  -> { remove(mod) }
120
127
  end
121
128
 
122
129
  def remove(name_or_module)
123
130
  removed = []
124
- @modules = @modules.reject do |mod|
131
+ assign(@modules.reject do |mod|
125
132
  matches = name_or_module.equal?(mod) || mod.name == name_or_module
126
133
  removed << mod if matches
127
134
  matches
128
- end
135
+ end)
136
+ @on_change&.call unless removed.empty?
129
137
 
130
138
  removed.each do |mod|
131
139
  @clear_module_diagnostic_subscriptions.call(mod) if @clear_module_diagnostic_subscriptions
@@ -137,46 +145,33 @@ module Featurevisor
137
145
  @modules
138
146
  end
139
147
 
140
- def run_before_modules(options)
141
- result = @modules.reduce(options) do |current, mod|
142
- mod.call_before(current)
143
- end
144
- @modules.reduce(result) do |current, mod|
145
- mod.call_before_evaluation(current)
146
- end
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
147
152
  end
148
153
 
149
- def run_before_evaluation_modules(options)
150
- @modules.reduce(options) do |result, mod|
151
- mod.call_before_evaluation(result)
152
- end
154
+ def after_evaluation?
155
+ @after_evaluation
153
156
  end
154
157
 
155
- def run_after_evaluation_modules(evaluation, options)
156
- @modules.reduce(evaluation) do |result, mod|
157
- mod.call_after_evaluation(result, options)
158
- end
158
+ def bucket_key?
159
+ @bucket_key
159
160
  end
160
161
 
161
- def run_after_modules(evaluation, options)
162
- result = @modules.reduce(evaluation) do |current, mod|
163
- mod.call_after_evaluation(current, options)
164
- end
165
- @modules.reduce(result) do |current, mod|
166
- mod.call_after(current, options)
167
- end
162
+ def bucket_value?
163
+ @bucket_value
168
164
  end
169
165
 
170
- # Deprecated feature-only callbacks are intentionally excluded here.
171
- def run_global_after_modules(evaluation, options)
172
- @modules.reduce(evaluation) do |result, mod|
173
- mod.call_after_evaluation(result, options)
166
+ def run_before_evaluation_modules(options)
167
+ @modules.reduce(options) do |result, mod|
168
+ mod.call_before_evaluation(result)
174
169
  end
175
170
  end
176
171
 
177
- def run_legacy_before_modules(options)
178
- @modules.reduce(options) do |result, mod|
179
- mod.call_before(result)
172
+ def run_after_evaluation_modules(evaluation, options)
173
+ @modules.reduce(evaluation) do |result, mod|
174
+ mod.call_after_evaluation(result, options)
180
175
  end
181
176
  end
182
177
 
@@ -197,15 +192,25 @@ module Featurevisor
197
192
  end
198
193
 
199
194
  def close_all
195
+ @on_change&.call unless @modules.empty?
200
196
  @modules.each do |mod|
201
197
  @clear_module_diagnostic_subscriptions.call(mod) if @clear_module_diagnostic_subscriptions
202
198
  close_module(mod)
203
199
  end
204
- @modules = []
200
+ assign([])
205
201
  end
206
202
 
207
203
  private
208
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
+
209
214
  def close_module(mod)
210
215
  mod.call_close
211
216
  rescue => e
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Featurevisor
4
+ module Mutations
5
+ DANGEROUS_KEYS = %w[__proto__ prototype constructor].freeze
6
+ OPERATIONS = %w[set append prepend appendUnique removeValue before after remove].freeze
7
+ MAX_SAFE_INTEGER = 9_007_199_254_740_991
8
+ private_constant :DANGEROUS_KEYS, :OPERATIONS, :MAX_SAFE_INTEGER
9
+
10
+ def self.apply(value, operations)
11
+ raise ArgumentError, "Invalid variable mutation operations" unless operations.is_a?(Array)
12
+
13
+ operations.reduce(value) do |current, operation|
14
+ validate(operation)
15
+ update_at_path(current, operation, 0)
16
+ end
17
+ end
18
+
19
+ def self.validate(operation)
20
+ unless operation.is_a?(Hash) && operation.key?(:op) && operation.key?(:path)
21
+ raise ArgumentError, "Invalid variable mutation operation"
22
+ end
23
+ op = operation[:op]
24
+ raise ArgumentError, "Invalid variable mutation operation" unless OPERATIONS.include?(op)
25
+ if op != "remove" && !operation.key?(:value)
26
+ raise ArgumentError, "Variable mutation operation requires a value"
27
+ end
28
+ if %w[appendUnique removeValue].include?(op)
29
+ value = operation[:value]
30
+ unless primitive?(value) && (!value.is_a?(Numeric) || !value.respond_to?(:finite?) || value.finite?)
31
+ raise ArgumentError, "Variable mutation operation requires a primitive value"
32
+ end
33
+ end
34
+ raise ArgumentError, "Invalid variable mutation path" unless operation[:path].is_a?(Array)
35
+
36
+ operation[:path].each do |part|
37
+ key = if part.is_a?(String)
38
+ part
39
+ elsif !array_index(part).nil?
40
+ nil
41
+ else
42
+ select = selector(part)
43
+ raise ArgumentError, "Invalid variable mutation path" unless select
44
+ select[:property]
45
+ end
46
+ raise ArgumentError, "Unsafe variable mutation key" if key && DANGEROUS_KEYS.include?(key)
47
+ end
48
+ end
49
+ private_class_method :validate
50
+
51
+ def self.primitive?(value)
52
+ value.nil? || value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false
53
+ end
54
+ private_class_method :primitive?
55
+
56
+ def self.same?(left, right)
57
+ return true if left.nil? && right.nil?
58
+ return left == right if left.is_a?(Numeric) && right.is_a?(Numeric)
59
+ return left == right if left.is_a?(String) && right.is_a?(String)
60
+ return left == right if [true, false].include?(left) && [true, false].include?(right)
61
+
62
+ left.equal?(right)
63
+ end
64
+ private_class_method :same?
65
+
66
+ def self.array_index(value)
67
+ return nil unless value.is_a?(Numeric)
68
+ return nil if value.respond_to?(:finite?) && !value.finite?
69
+ return nil unless value >= 0 && value <= MAX_SAFE_INTEGER && value.to_i == value
70
+
71
+ value.to_i
72
+ end
73
+ private_class_method :array_index
74
+
75
+ def self.selector(part)
76
+ return nil unless part.is_a?(Hash)
77
+ select = part[:select] || part["select"]
78
+ return nil unless select.is_a?(Hash)
79
+ property = select[:property] || select["property"]
80
+ values = select[:values] || select["values"]
81
+ return nil unless property.is_a?(String) && values.is_a?(Array) && values.all? { |value| primitive?(value) }
82
+
83
+ { property: property, values: values }
84
+ end
85
+ private_class_method :selector
86
+
87
+ def self.hash_key(hash, key)
88
+ return key if hash.key?(key)
89
+ symbol = key.to_sym
90
+ hash.key?(symbol) ? symbol : symbol
91
+ end
92
+ private_class_method :hash_key
93
+
94
+ def self.selected_index(array, select)
95
+ array.each_with_index do |candidate, index|
96
+ next unless candidate.is_a?(Hash)
97
+ key = hash_key(candidate, select[:property])
98
+ next unless candidate.key?(key)
99
+ value = candidate[key]
100
+ return index if primitive?(value) && select[:values].any? { |expected| same?(value, expected) }
101
+ end
102
+ -1
103
+ end
104
+ private_class_method :selected_index
105
+
106
+ def self.path_key(container, part)
107
+ if part.is_a?(String)
108
+ raise ArgumentError, "Variable mutation expected an object" unless container.is_a?(Hash)
109
+ return hash_key(container, part)
110
+ end
111
+ raise ArgumentError, "Variable mutation expected an array" unless container.is_a?(Array)
112
+
113
+ index = array_index(part)
114
+ return index unless index.nil?
115
+
116
+ select = selector(part)
117
+ raise ArgumentError, "Invalid variable mutation path" unless select
118
+
119
+ selected_index(container, select)
120
+ end
121
+ private_class_method :path_key
122
+
123
+ def self.exists?(container, key)
124
+ container.is_a?(Hash) ? container.key?(key) : key >= 0 && key < container.length
125
+ end
126
+ private_class_method :exists?
127
+
128
+ def self.replace(container, key, value)
129
+ result = container.dup
130
+ if result.is_a?(Array) && key == result.length
131
+ result << value
132
+ else
133
+ result[key] = value
134
+ end
135
+ result
136
+ end
137
+ private_class_method :replace
138
+
139
+ def self.update_array(array, op, item)
140
+ if op == "removeValue"
141
+ return array unless array.any? { |value| same?(value, item) }
142
+ return array.reject { |value| same?(value, item) }
143
+ end
144
+ return array if op == "appendUnique" && array.any? { |value| same?(value, item) }
145
+
146
+ op == "prepend" ? [item, *array] : [*array, item]
147
+ end
148
+ private_class_method :update_array
149
+
150
+ def self.update_final(container, part, operation)
151
+ return container if container.nil?
152
+ op = operation[:op]
153
+ if %w[before after].include?(op) && part.is_a?(String)
154
+ raise ArgumentError, "Variable mutation before/after requires an array element path"
155
+ end
156
+ key = path_key(container, part)
157
+ found = exists?(container, key)
158
+
159
+ if op == "remove"
160
+ return container unless found
161
+ result = container.dup
162
+ result.is_a?(Array) ? result.delete_at(key) : result.delete(key)
163
+ return result
164
+ end
165
+
166
+ item = operation[:value]
167
+ if op == "set"
168
+ return container if key.is_a?(Integer) && (key.negative? || key > container.length)
169
+ return container if found && same?(container[key], item)
170
+ return replace(container, key, item)
171
+ end
172
+ if %w[before after].include?(op)
173
+ return container unless found
174
+ result = container.dup
175
+ result.insert(key + (op == "after" ? 1 : 0), item)
176
+ return result
177
+ end
178
+
179
+ current = found ? container[key] : nil
180
+ return container if current.nil? && key.is_a?(Integer)
181
+ return container if current.nil? && op == "removeValue"
182
+ raise ArgumentError, "Variable mutation expected an array" if !current.nil? && !current.is_a?(Array)
183
+ array = current || []
184
+ result = update_array(array, op, item)
185
+ result.equal?(current) ? container : replace(container, key, result)
186
+ end
187
+ private_class_method :update_final
188
+
189
+ def self.update_at_path(value, operation, offset)
190
+ path = operation[:path]
191
+ if offset == path.length
192
+ return same?(value, operation[:value]) ? value : operation[:value] if operation[:op] == "set"
193
+ raise ArgumentError, "Cannot remove the root variable value" if operation[:op] == "remove"
194
+ if %w[before after].include?(operation[:op])
195
+ raise ArgumentError, "Variable mutation before/after requires an array element path"
196
+ end
197
+ return value if value.nil?
198
+ raise ArgumentError, "Variable mutation expected an array" unless value.is_a?(Array)
199
+ return update_array(value, operation[:op], operation[:value])
200
+ end
201
+
202
+ return update_final(value, path[offset], operation) if offset == path.length - 1
203
+ return value if value.nil?
204
+ key = path_key(value, path[offset])
205
+ return value unless exists?(value, key)
206
+ current = value[key]
207
+ return value if current.nil?
208
+ result = update_at_path(current, operation, offset + 1)
209
+ result.equal?(current) ? value : replace(value, key, result)
210
+ end
211
+ private_class_method :update_at_path
212
+ end
213
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Featurevisor
6
+ module Normalize
7
+ class InvalidConditions < Hash
8
+ def initialize
9
+ super
10
+ self[:or] = []
11
+ end
12
+ end
13
+
14
+ class ParseFailure < StandardError; end
15
+
16
+ def self.legacy(value, features = false)
17
+ return value if value.is_a?(InvalidConditions)
18
+ case value
19
+ when String
20
+ features ? { feature: value } : value
21
+ when Array
22
+ value.empty? ? "*" : value.map { |entry| legacy(entry, features) }
23
+ when Hash
24
+ group = %i[and or not].find { |name| value[name].is_a?(Array) }
25
+ return value unless group
26
+ return "*" if group == :and && value[group].empty?
27
+ { group => value[group].map { |entry| legacy(entry, features) } }
28
+ else
29
+ value
30
+ end
31
+ end
32
+
33
+ def self.entity(kind, key, source, legacy = false)
34
+ errors = []
35
+ parse = lambda do |value|
36
+ next value unless value.is_a?(String) && value.lstrip.match?(/\A[\{\[\"]/)
37
+ begin
38
+ JSON.parse(value, symbolize_names: true)
39
+ rescue JSON::ParserError => error
40
+ errors << { level: "error", code: "conditions_parse_error", message: "Error parsing conditions",
41
+ original_error: error,
42
+ details: { conditions: value, entityType: kind.to_s, entityKey: key.to_s } }
43
+ InvalidConditions.new
44
+ end
45
+ end
46
+ select = lambda do |entry, old = false|
47
+ result = entry.reject { |name, _| %i[segments requiredFeatures requiredVariables required].include?(name) }
48
+ parts = []
49
+ if entry.key?(:conditions)
50
+ value = parse.call(entry[:conditions])
51
+ parts << (old ? self.legacy(value) : value)
52
+ end
53
+ parts << self.legacy(parse.call(entry[:segments])) if entry.key?(:segments)
54
+ if entry.key?(:requiredFeatures)
55
+ features = entry[:requiredFeatures]
56
+ features = [features] unless features.is_a?(Array)
57
+ parts << features.map { |value| self.legacy(value, true) } unless features.empty?
58
+ end
59
+ if parts.any? { |part| part.is_a?(InvalidConditions) }
60
+ result[:conditions] = InvalidConditions.new
61
+ elsif parts.length == 1
62
+ result[:conditions] = parts.first
63
+ elsif !parts.empty?
64
+ result[:conditions] = parts.flat_map { |part| part.is_a?(Array) ? (part.empty? ? [{ and: [] }] : part) : [part] }
65
+ end
66
+ result
67
+ end
68
+ with_overrides = lambda do |entry|
69
+ next entry unless entry[:variableOverrides]
70
+ entry.merge(variableOverrides: entry[:variableOverrides].transform_values do |values|
71
+ values.map { |override| select.call(override, legacy) }
72
+ end)
73
+ end
74
+ if kind.to_s == "segment"
75
+ value = parse.call(source[:conditions])
76
+ result = source.merge(conditions: legacy ? self.legacy(value) : value)
77
+ else
78
+ result = select.call(source)
79
+ if kind.to_s == "feature"
80
+ result[:traffic] = (source[:traffic] || []).map { |entry| with_overrides.call(select.call(entry)) }
81
+ result[:force] = source[:force].map { |entry| select.call(entry, legacy) } if source[:force]
82
+ result[:variations] = source[:variations].map { |entry| with_overrides.call(entry) } if source[:variations]
83
+ else
84
+ %i[overrides overrideProgram].each do |name|
85
+ result[name] = source[name].map { |entry| select.call(entry, legacy && name == :overrides) } if source[name]
86
+ end
87
+ end
88
+ end
89
+ [result, errors]
90
+ end
91
+
92
+ def self.datafile(source)
93
+ errors = []
94
+ result = source.dup
95
+ { features: :feature, segments: :segment, variables: :variable }.each do |name, kind|
96
+ next unless source[name]
97
+ result[name] = source[name].each_with_object({}) do |(key, entry), values|
98
+ values[key], diagnostics = entity(kind, key, entry, true)
99
+ errors.concat(diagnostics)
100
+ end
101
+ end
102
+ [result, errors]
103
+ end
104
+ end
105
+ end