standard_flags 0.2.2 → 0.3.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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +158 -0
  3. data/README.md +566 -35
  4. data/conformance/README.md +166 -0
  5. data/conformance/bucket_vectors.json +46 -0
  6. data/conformance/guards_and_errors.json +35 -0
  7. data/conformance/legacy_env.json +38 -0
  8. data/conformance/legacy_exact.json +90 -0
  9. data/conformance/operations/write_path.json +723 -0
  10. data/conformance/permanent_flags.json +38 -0
  11. data/conformance/resolution.json +68 -0
  12. data/conformance/runtime/client.json +1431 -0
  13. data/conformance/runtime/rules.json +1305 -0
  14. data/conformance/schema/fixture.schema.json +1340 -0
  15. data/conformance/versions/legacy_ranges.json +183 -0
  16. data/conformance/versions/version_ranges.json +503 -0
  17. data/conformance/wire/client_state.json +1905 -0
  18. data/conformance/wire/delivery.json +1541 -0
  19. data/lib/standard_flags/audience.rb +49 -0
  20. data/lib/standard_flags/client.rb +157 -0
  21. data/lib/standard_flags/config.rb +21 -0
  22. data/lib/standard_flags/context.rb +70 -17
  23. data/lib/standard_flags/controller.rb +92 -10
  24. data/lib/standard_flags/definition.rb +71 -8
  25. data/lib/standard_flags/delivery.rb +195 -0
  26. data/lib/standard_flags/errors.rb +33 -0
  27. data/lib/standard_flags/job.rb +12 -1
  28. data/lib/standard_flags/mcp/flags.rb +3 -3
  29. data/lib/standard_flags/mcp/gating.rb +151 -0
  30. data/lib/standard_flags/mcp.rb +64 -5
  31. data/lib/standard_flags/notifiers/logger.rb +4 -0
  32. data/lib/standard_flags/ofrep/controller.rb +211 -0
  33. data/lib/standard_flags/ofrep.rb +115 -0
  34. data/lib/standard_flags/open_feature/provider.rb +1 -1
  35. data/lib/standard_flags/operations/rules.rb +17 -4
  36. data/lib/standard_flags/operations/validation.rb +25 -4
  37. data/lib/standard_flags/operations.rb +0 -2
  38. data/lib/standard_flags/payload.rb +8 -3
  39. data/lib/standard_flags/registry.rb +157 -12
  40. data/lib/standard_flags/runtime/cache.rb +1 -0
  41. data/lib/standard_flags/runtime/resolver.rb +26 -17
  42. data/lib/standard_flags/runtime/values.rb +8 -1
  43. data/lib/standard_flags/runtime.rb +42 -1
  44. data/lib/standard_flags/semantic_version.rb +141 -0
  45. data/lib/standard_flags/subscribers.rb +3 -0
  46. data/lib/standard_flags/testing.rb +4 -2
  47. data/lib/standard_flags/tooling.rb +25 -3
  48. data/lib/standard_flags/version.rb +1 -1
  49. data/lib/standard_flags/version_range.rb +309 -0
  50. data/lib/standard_flags.rb +38 -4
  51. metadata +24 -1
@@ -0,0 +1,309 @@
1
+ module StandardFlags
2
+ # A range of client versions, in a syntax the Kotlin and TypeScript
3
+ # clients share: comparators (`>=`, `>`, `<=`, `<`, `=`) separated by
4
+ # spaces, all of which must hold, with optional `||` alternatives.
5
+ # Whitespace between an operator and its version is optional, and versions
6
+ # take the shorthands StandardFlags::Version reads. Whitespace means ASCII
7
+ # spaces and tabs only, and a range is at most 1024 characters.
8
+ #
9
+ # range = StandardFlags::VersionRange.parse(">=1.4.0 <2.0.0 || =0.9.5-beta.1")
10
+ # range.satisfied_by?("1.9.3") # => true
11
+ # range.to_s # => ">=1.4.0 <2.0.0 || =0.9.5-beta.1"
12
+ #
13
+ # Comparison is plain SemVer ordering, nothing more: `<2.0.0` admits
14
+ # `2.0.0-beta.1`, which sorts before `2.0.0`, and a shorthand is an exact
15
+ # version, not a wildcard (`>1.4` admits `1.4.1`; `<=1.4` excludes
16
+ # `1.4.5`). A version that doesn't parse satisfies no neutral range.
17
+ #
18
+ # Ranges stored by 0.2.x are RubyGems requirements (`">= 1.4.0"`,
19
+ # `"~> 1.4"`, `">= 1.4, < 2"`), and `parse` keeps reading the ones only
20
+ # RubyGems can read exactly as RubyGems does. Where one has a neutral
21
+ # equivalent it is translated:
22
+ # `~> 1.4` reads as `>=1.4.0 <2.0.0-0`, `~> 1.4.2` as `>=1.4.2 <1.5.0-0`,
23
+ # `!= 1.5` as `<1.5.0 || >1.5.0`. The `-0` is the lowest prerelease SemVer
24
+ # allows, so `<2.0.0-0` excludes `2.0.0-beta.1` too, as RubyGems' `~>`
25
+ # (which compares the release part only) does. A requirement the neutral
26
+ # syntax can't express (`>= 1.4.0.1`, or any prerelease operand such as
27
+ # `>= 1.4.0.beta`, which RubyGems orders differently) is still evaluated
28
+ # with Gem::Requirement, and so is any range RubyGems can read when the
29
+ # client version isn't SemVer (`1.4.0.1`, `1.4.0.beta`), as 0.2.2 did. A
30
+ # string valid in both syntaxes (`>= 1.4.0`, `>= 1.0.0-alpha`) is neutral:
31
+ # a SemVer client version is compared by SemVer. Only the neutral syntax
32
+ # is portable; the write path stores it when it can.
33
+ class VersionRange
34
+ class Invalid < StandardFlags::Error; end
35
+
36
+ COMPARATOR = /(>=|<=|>|<|=)[ \t]*([^ \t<>=|]+)/
37
+ ALTERNATIVE = /\A[ \t]*#{COMPARATOR}(?:[ \t]+#{COMPARATOR})*[ \t]*\z/
38
+ # Longest range read, and most alternatives a translated RubyGems
39
+ # requirement may expand to.
40
+ MAX_LENGTH = 1024
41
+ MAX_ALTERNATIVES = 64
42
+ CACHE_LIMIT = 1024
43
+ CACHE_LOCK = Mutex.new
44
+
45
+ # One `operator operand` test.
46
+ Comparator = Data.define(:operator, :operand) do
47
+ def satisfied_by?(value)
48
+ order = value <=> operand
49
+ case operator
50
+ when ">=" then order >= 0
51
+ when ">" then order.positive?
52
+ when "<=" then order <= 0
53
+ when "<" then order.negative?
54
+ else order.zero?
55
+ end
56
+ end
57
+
58
+ def to_s
59
+ "#{operator}#{operand}"
60
+ end
61
+ end
62
+
63
+ class << self
64
+ # The range for `text` in either syntax, or nil when it parses as
65
+ # neither. Parsed ranges are cached, so the read path parses each
66
+ # stored range once per process.
67
+ def parse(text)
68
+ cached(text) do
69
+ range = neutral(text)
70
+ requirement = requirement(text)
71
+ if range
72
+ requirement ? new(range.alternatives, requirement: requirement) : range
73
+ elsif requirement
74
+ new(translated(text), requirement: requirement, source: text.to_s)
75
+ end
76
+ end
77
+ end
78
+
79
+ def parse!(text)
80
+ parse(text) || raise(Invalid, "#{text.inspect} is not a #{noun}")
81
+ end
82
+
83
+ def valid?(text)
84
+ !parse(text).nil?
85
+ end
86
+
87
+ # The range for `text` in the neutral syntax only, or nil. It compares
88
+ # SemVer only, as clients do.
89
+ def neutral(text)
90
+ text = clean(text)
91
+ return nil if text.nil? || text.length > MAX_LENGTH
92
+
93
+ alternatives = text.split("||", -1).map { |alternative| comparators(alternative) }
94
+ new(alternatives) if alternatives.any? && alternatives.all?
95
+ end
96
+
97
+ # The neutral form of a RubyGems requirement (comma-joined), or nil
98
+ # when it isn't one, or is one the neutral syntax can't express.
99
+ def translate(text)
100
+ alternatives = requirement(text) && translated(text)
101
+ alternatives && new(alternatives).to_s
102
+ end
103
+
104
+ private
105
+
106
+ def noun
107
+ "version range"
108
+ end
109
+
110
+ def clean(text)
111
+ return nil unless text.is_a?(String) || text.is_a?(Symbol)
112
+
113
+ Version.clean(text)
114
+ end
115
+
116
+ # Each class keeps its own cache (nil answers included), emptied
117
+ # when it fills.
118
+ def cached(text)
119
+ key = [ text.class, text.to_s ]
120
+ CACHE_LOCK.synchronize do
121
+ @cache ||= {}
122
+ return @cache[key] if @cache.key?(key)
123
+ end
124
+ range = yield
125
+ CACHE_LOCK.synchronize do
126
+ @cache.clear if @cache.size >= CACHE_LIMIT
127
+ @cache[key] = range
128
+ end
129
+ end
130
+
131
+ def comparators(text)
132
+ return nil unless ALTERNATIVE.match?(text)
133
+
134
+ text.scan(COMPARATOR).map do |operator, raw|
135
+ value = operand(raw)
136
+ return nil if value.nil?
137
+
138
+ Comparator.new(operator: operator, operand: value)
139
+ end
140
+ end
141
+
142
+ def operand(text)
143
+ Version.parse(text)
144
+ end
145
+
146
+ def terms(text)
147
+ text = clean(text)
148
+ return nil if text.nil? || text.length > MAX_LENGTH
149
+
150
+ terms = text.split(",").map(&:strip)
151
+ terms.empty? ? [ ">= 0" ] : terms # RubyGems' default requirement
152
+ end
153
+
154
+ # The Gem::Requirement for a comma-joined RubyGems requirement, or nil.
155
+ def requirement(text)
156
+ list = terms(text)
157
+ list && Gem::Requirement.new(*list)
158
+ rescue Gem::Requirement::BadRequirementError, ArgumentError
159
+ nil
160
+ end
161
+
162
+ # The neutral alternatives a RubyGems requirement means, or nil when
163
+ # the neutral syntax can't hold it.
164
+ def translated(text)
165
+ options = terms(text).map { |term| legacy_term(term) }
166
+ return nil if options.any?(&:nil?)
167
+
168
+ combined = options.reduce([ [] ]) do |alternatives, choices|
169
+ alternatives.product(choices).map { |left, right| left + right }
170
+ end
171
+ return nil if combined.size > MAX_ALTERNATIVES
172
+
173
+ neutral(combined.map { |alternative| alternative.join(" ") }.join(" || "))&.alternatives
174
+ end
175
+
176
+ # The alternatives (each a list of comparator strings) one RubyGems
177
+ # term means, or nil when SemVer can't say the same: a prerelease
178
+ # operand, which RubyGems orders differently (it reads a client's
179
+ # `1.4.0-beta` as `1.4.0.pre.beta`), or more than three release
180
+ # segments.
181
+ def legacy_term(term)
182
+ match = Gem::Requirement::PATTERN.match(term)
183
+ return nil if Gem::Version.new(match[2]).prerelease?
184
+
185
+ operator = match[1] || "="
186
+ version = legacy_version(match[2])
187
+ return nil unless version
188
+
189
+ case operator
190
+ when "~>"
191
+ upper = legacy_version(Gem::Version.new(match[2]).bump.to_s)
192
+ upper && [ [ ">=#{version}", "<#{upper}-0" ] ]
193
+ when "!=" then [ [ "<#{version}" ], [ ">#{version}" ] ]
194
+ else [ [ "#{operator}#{version}" ] ]
195
+ end
196
+ end
197
+
198
+ # A RubyGems release version as SemVer: "1.4" → "1.4.0", "1.4.0.0" →
199
+ # "1.4.0". nil past three non-zero release segments, which SemVer
200
+ # can't hold.
201
+ def legacy_version(text)
202
+ release = Gem::Version.new(text).segments
203
+ release.pop while release.size > 3 && release.last.zero?
204
+ return nil if release.size > 3
205
+
206
+ Version.parse((release + ([ 0 ] * (3 - release.size))).join("."))&.to_s
207
+ end
208
+ end
209
+
210
+ # The neutral alternatives, or nil for a RubyGems requirement the
211
+ # neutral syntax can't express. The Gem::Requirement, when the range is
212
+ # also RubyGems syntax.
213
+ attr_reader :alternatives, :requirement
214
+
215
+ def initialize(alternatives, requirement: nil, source: nil)
216
+ @alternatives = alternatives&.map(&:freeze)&.freeze
217
+ @requirement = requirement
218
+ @source = source&.dup&.freeze
219
+ freeze
220
+ end
221
+
222
+ # Whether `value` (a Version, a String, or anything whose `to_s` is one)
223
+ # meets every comparator of at least one alternative. A SemVer value is
224
+ # compared by SemVer; any other value meets the range only through
225
+ # RubyGems, when the range is RubyGems syntax.
226
+ def satisfied_by?(value)
227
+ version = Version.parse(value)
228
+ return alternatives_met?(version) if version && alternatives
229
+
230
+ gem_version = gem_version(value)
231
+ !gem_version.nil? && !requirement.nil? && requirement.satisfied_by?(gem_version)
232
+ end
233
+
234
+ # Whether this was read as a RubyGems requirement rather than the
235
+ # neutral syntax.
236
+ def legacy?
237
+ !@source.nil?
238
+ end
239
+
240
+ # The canonical neutral form (">=1.4.0 <2.0.0 || =0.9.5"), or the stored
241
+ # text of a RubyGems requirement the neutral syntax can't express.
242
+ def to_s
243
+ return @source unless alternatives
244
+
245
+ alternatives.map { |comparators| comparators.join(" ") }.join(" || ")
246
+ end
247
+
248
+ def inspect
249
+ "#<#{self.class.name} #{self}>"
250
+ end
251
+
252
+ private
253
+
254
+ def alternatives_met?(version)
255
+ alternatives.any? { |comparators| comparators.all? { |comparator| comparator.satisfied_by?(version) } }
256
+ end
257
+
258
+ def gem_version(value)
259
+ text = Version.clean(value)
260
+ return nil if text.nil? || text.empty? || !Gem::Version.correct?(text)
261
+
262
+ Gem::Version.new(text)
263
+ end
264
+ end
265
+
266
+ # A range of client build numbers, in the same comparator syntax:
267
+ # `>=120 <200`, `=7 || >=12`. A build number is 1 to 18 ASCII digits (so
268
+ # it fits a signed 64-bit integer); leading zeros are allowed and ignored,
269
+ # signs are not. There is no older syntax to translate.
270
+ class BuildRange < VersionRange
271
+ NUMBER = /\A\d{1,18}\z/
272
+
273
+ class << self
274
+ def parse(text)
275
+ cached(text) { neutral(text) }
276
+ end
277
+
278
+ def translate(_text)
279
+ nil
280
+ end
281
+
282
+ # A build number from a non-negative Integer below 10**18 or a digit
283
+ # string, or nil.
284
+ def number(value)
285
+ case value
286
+ when Integer then value if value >= 0 && value < 10**18
287
+ when String, Symbol
288
+ text = Version.clean(value)
289
+ text.to_i if text && NUMBER.match?(text)
290
+ end
291
+ end
292
+
293
+ private
294
+
295
+ def noun
296
+ "build range"
297
+ end
298
+
299
+ def operand(text)
300
+ number(text)
301
+ end
302
+ end
303
+
304
+ def satisfied_by?(value)
305
+ build = self.class.number(value)
306
+ !build.nil? && alternatives.any? { |comparators| comparators.all? { |comparator| comparator.satisfied_by?(build) } }
307
+ end
308
+ end
309
+ end
@@ -9,8 +9,12 @@ require "standard_flags/events"
9
9
  require "standard_flags/reason"
10
10
  require "standard_flags/evaluation"
11
11
  require "standard_flags/mode"
12
+ require "standard_flags/audience"
12
13
  require "standard_flags/definition"
13
14
  require "standard_flags/registry"
15
+ require "standard_flags/semantic_version"
16
+ require "standard_flags/version_range"
17
+ require "standard_flags/client"
14
18
  require "standard_flags/context"
15
19
  require "standard_flags/memo"
16
20
  require "standard_flags/reporting"
@@ -25,6 +29,7 @@ require "standard_flags/reader"
25
29
  require "standard_flags/snapshot"
26
30
  require "standard_flags/sensor"
27
31
  require "standard_flags/payload"
32
+ require "standard_flags/delivery"
28
33
  require "standard_flags/config"
29
34
  require "standard_flags/notifiers/logger"
30
35
  require "standard_flags/notifiers/sentry"
@@ -45,6 +50,9 @@ module StandardFlags
45
50
  autoload :Job, "standard_flags/job"
46
51
  autoload :PurgeJob, "standard_flags/purge_job"
47
52
  autoload :HealthCheck, "standard_flags/health_check"
53
+ # The OFREP controller mixin (actionpack); StandardFlags::OFREP itself is
54
+ # plain Ruby.
55
+ autoload :OFREP, "standard_flags/ofrep"
48
56
 
49
57
  class << self
50
58
  # Declares the app's flags, normally in config/flags.rb. Replaces the
@@ -134,8 +142,32 @@ module StandardFlags
134
142
  Sensor.run(reader(for: binding.local_variable_get(:for)), key, report: report, &block)
135
143
  end
136
144
 
137
- def payload(audience, for: nil)
138
- Payload.build(registry, audience, Context.wrap(binding.local_variable_get(:for)))
145
+ # Raises StandardFlags::FeatureDisabled unless the flag is enabled (on,
146
+ # or a mode past its first value). StandardFlags::Controller renders it
147
+ # as a `feature_disabled` denial.
148
+ def require!(key, for: nil)
149
+ return true if reader(for: binding.local_variable_get(:for)).enabled?(key)
150
+
151
+ raise FeatureDisabled, key
152
+ end
153
+
154
+ # What `audience` is told, evaluated `for:` a target or context.
155
+ # `client:` (a Client or Hash) replaces the context's client.
156
+ #
157
+ # * `format: :flat` (the default): `{ wire_name => value }`, the Inertia
158
+ # prop (StandardFlags::Payload).
159
+ # * `format: :ofrep`: OFREP's bulk response body,
160
+ # `{ "flags" => [...], "metadata" => { "version" => etag } }`, with
161
+ # `min_client:` and tombstones' static wires applied
162
+ # (StandardFlags::Delivery), for bootstrap responses, device check-ins
163
+ # and MCP whoami.
164
+ def payload(audience, for: nil, client: nil, format: :flat)
165
+ context = Context.wrap(binding.local_variable_get(:for), client: client)
166
+ case format
167
+ when :flat then Payload.build(registry, audience, context)
168
+ when :ofrep then Delivery.build(audience, context, registry: registry).to_h
169
+ else raise ArgumentError, "format: must be :flat or :ofrep, not #{format.inspect}"
170
+ end
139
171
  end
140
172
 
141
173
  # Reads the runtime memberships of many units in one query and memoises
@@ -151,8 +183,10 @@ module StandardFlags
151
183
  Runtime.preload(unit, records_or_keys)
152
184
  end
153
185
 
154
- def payload_digest(audience, for: nil)
155
- Payload.digest(payload(audience, for: binding.local_variable_get(:for)))
186
+ # The digest of the flat payload (an ETag for it). The OFREP format
187
+ # carries its own, as `metadata.version`.
188
+ def payload_digest(audience, for: nil, client: nil)
189
+ Payload.digest(payload(audience, for: binding.local_variable_get(:for), client: client))
156
190
  end
157
191
 
158
192
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_flags
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rarebit One
@@ -90,6 +90,21 @@ files:
90
90
  - LICENSE
91
91
  - README.md
92
92
  - Rakefile
93
+ - conformance/README.md
94
+ - conformance/bucket_vectors.json
95
+ - conformance/guards_and_errors.json
96
+ - conformance/legacy_env.json
97
+ - conformance/legacy_exact.json
98
+ - conformance/operations/write_path.json
99
+ - conformance/permanent_flags.json
100
+ - conformance/resolution.json
101
+ - conformance/runtime/client.json
102
+ - conformance/runtime/rules.json
103
+ - conformance/schema/fixture.schema.json
104
+ - conformance/versions/legacy_ranges.json
105
+ - conformance/versions/version_ranges.json
106
+ - conformance/wire/client_state.json
107
+ - conformance/wire/delivery.json
93
108
  - lib/generators/standard_flags/admin/admin_generator.rb
94
109
  - lib/generators/standard_flags/admin/templates/controller.rb.tt
95
110
  - lib/generators/standard_flags/admin/templates/index.tsx.tt
@@ -101,12 +116,15 @@ files:
101
116
  - lib/generators/standard_flags/migrations/templates/create_standard_flags_tables.rb.tt
102
117
  - lib/standard_flags.rb
103
118
  - lib/standard_flags/actor.rb
119
+ - lib/standard_flags/audience.rb
104
120
  - lib/standard_flags/audit_trail.rb
105
121
  - lib/standard_flags/bucket.rb
122
+ - lib/standard_flags/client.rb
106
123
  - lib/standard_flags/config.rb
107
124
  - lib/standard_flags/context.rb
108
125
  - lib/standard_flags/controller.rb
109
126
  - lib/standard_flags/definition.rb
127
+ - lib/standard_flags/delivery.rb
110
128
  - lib/standard_flags/diagnostics.rb
111
129
  - lib/standard_flags/engine.rb
112
130
  - lib/standard_flags/env_pin.rb
@@ -120,6 +138,7 @@ files:
120
138
  - lib/standard_flags/mcp.rb
121
139
  - lib/standard_flags/mcp/cohorts.rb
122
140
  - lib/standard_flags/mcp/flags.rb
141
+ - lib/standard_flags/mcp/gating.rb
123
142
  - lib/standard_flags/mcp/members.rb
124
143
  - lib/standard_flags/mcp/writes.rb
125
144
  - lib/standard_flags/memo.rb
@@ -127,6 +146,8 @@ files:
127
146
  - lib/standard_flags/notifiers/logger.rb
128
147
  - lib/standard_flags/notifiers/metrics.rb
129
148
  - lib/standard_flags/notifiers/sentry.rb
149
+ - lib/standard_flags/ofrep.rb
150
+ - lib/standard_flags/ofrep/controller.rb
130
151
  - lib/standard_flags/open_feature.rb
131
152
  - lib/standard_flags/open_feature/provider.rb
132
153
  - lib/standard_flags/operations.rb
@@ -159,6 +180,7 @@ files:
159
180
  - lib/standard_flags/runtime/rule_snapshot.rb
160
181
  - lib/standard_flags/runtime/store.rb
161
182
  - lib/standard_flags/runtime/values.rb
183
+ - lib/standard_flags/semantic_version.rb
162
184
  - lib/standard_flags/sensor.rb
163
185
  - lib/standard_flags/snapshot.rb
164
186
  - lib/standard_flags/subscribers.rb
@@ -166,6 +188,7 @@ files:
166
188
  - lib/standard_flags/tooling.rb
167
189
  - lib/standard_flags/tooling/changes.rb
168
190
  - lib/standard_flags/version.rb
191
+ - lib/standard_flags/version_range.rb
169
192
  - lib/tasks/standard_flags.rake
170
193
  homepage: https://github.com/rarebit-one/standard_flags
171
194
  licenses: