configdirector-server-sdk 0.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.
Files changed (162) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +2 -0
  3. data/CHANGELOG.md +254 -0
  4. data/LICENSE +21 -0
  5. data/README.md +230 -0
  6. data/lib/configdirector/client_hooks.rb +46 -0
  7. data/lib/configdirector/config_director_client.rb +322 -0
  8. data/lib/configdirector/config_evaluated_event.rb +10 -0
  9. data/lib/configdirector/config_evaluation.rb +51 -0
  10. data/lib/configdirector/config_state.rb +39 -0
  11. data/lib/configdirector/configs_updated_event.rb +23 -0
  12. data/lib/configdirector/connection_error_event.rb +11 -0
  13. data/lib/configdirector/connection_options.rb +60 -0
  14. data/lib/configdirector/context.rb +53 -0
  15. data/lib/configdirector/errors.rb +29 -0
  16. data/lib/configdirector/internal/client/client_connection.rb +119 -0
  17. data/lib/configdirector/internal/client/client_factory.rb +104 -0
  18. data/lib/configdirector/internal/client/client_options_validation.rb +53 -0
  19. data/lib/configdirector/internal/client/config_read.rb +31 -0
  20. data/lib/configdirector/internal/client/config_reader.rb +55 -0
  21. data/lib/configdirector/internal/client/connection_intake.rb +59 -0
  22. data/lib/configdirector/internal/client/connection_options_validation.rb +94 -0
  23. data/lib/configdirector/internal/client/context_admission.rb +48 -0
  24. data/lib/configdirector/internal/client/context_attributes.rb +78 -0
  25. data/lib/configdirector/internal/client/context_kind.rb +24 -0
  26. data/lib/configdirector/internal/client/evaluated_read.rb +9 -0
  27. data/lib/configdirector/internal/client/event_handlers.rb +114 -0
  28. data/lib/configdirector/internal/client/event_tracking.rb +39 -0
  29. data/lib/configdirector/internal/client/live_client_connections.rb +58 -0
  30. data/lib/configdirector/internal/client/number_of_seconds.rb +21 -0
  31. data/lib/configdirector/internal/client/registrations.rb +43 -0
  32. data/lib/configdirector/internal/client/restart_in_forked_processes.rb +25 -0
  33. data/lib/configdirector/internal/client/served_configs.rb +83 -0
  34. data/lib/configdirector/internal/client/serving_state.rb +102 -0
  35. data/lib/configdirector/internal/client/watches.rb +51 -0
  36. data/lib/configdirector/internal/client/what_follows_a_fork.rb +24 -0
  37. data/lib/configdirector/internal/connection/condition_parser.rb +74 -0
  38. data/lib/configdirector/internal/connection/config_definition.rb +37 -0
  39. data/lib/configdirector/internal/connection/http_session.rb +15 -0
  40. data/lib/configdirector/internal/connection/in_memory_connection.rb +134 -0
  41. data/lib/configdirector/internal/connection/json_post_request.rb +24 -0
  42. data/lib/configdirector/internal/connection/not_a_server_sdk_payload.rb +9 -0
  43. data/lib/configdirector/internal/connection/offline_mode_connection.rb +96 -0
  44. data/lib/configdirector/internal/connection/offline_values_file.rb +49 -0
  45. data/lib/configdirector/internal/connection/payloads_delivered_in_turn.rb +46 -0
  46. data/lib/configdirector/internal/connection/polling_interval.rb +21 -0
  47. data/lib/configdirector/internal/connection/request_deadline.rb +45 -0
  48. data/lib/configdirector/internal/connection/retry_after_header.rb +24 -0
  49. data/lib/configdirector/internal/connection/server_sdk_payload.rb +37 -0
  50. data/lib/configdirector/internal/connection/server_sdk_payload_parser.rb +89 -0
  51. data/lib/configdirector/internal/connection/targeting_rules_parser.rb +70 -0
  52. data/lib/configdirector/internal/connection/test_client_initializations.rb +111 -0
  53. data/lib/configdirector/internal/connection/test_client_values.rb +53 -0
  54. data/lib/configdirector/internal/connection/unreadable_definition.rb +9 -0
  55. data/lib/configdirector/internal/connection/wire_fields.rb +59 -0
  56. data/lib/configdirector/internal/evaluation/array_comparison.rb +25 -0
  57. data/lib/configdirector/internal/evaluation/assigned_percentage.rb +21 -0
  58. data/lib/configdirector/internal/evaluation/attribute_condition.rb +48 -0
  59. data/lib/configdirector/internal/evaluation/config_evaluator.rb +29 -0
  60. data/lib/configdirector/internal/evaluation/datetime_comparison.rb +93 -0
  61. data/lib/configdirector/internal/evaluation/evaluation_context.rb +17 -0
  62. data/lib/configdirector/internal/evaluation/number_comparison.rb +119 -0
  63. data/lib/configdirector/internal/evaluation/rapidhash.rb +124 -0
  64. data/lib/configdirector/internal/evaluation/rollout_share.rb +9 -0
  65. data/lib/configdirector/internal/evaluation/rule.rb +70 -0
  66. data/lib/configdirector/internal/evaluation/scalar_text.rb +70 -0
  67. data/lib/configdirector/internal/evaluation/segment.rb +17 -0
  68. data/lib/configdirector/internal/evaluation/segment_condition.rb +26 -0
  69. data/lib/configdirector/internal/evaluation/semver_comparison.rb +49 -0
  70. data/lib/configdirector/internal/evaluation/served_value.rb +9 -0
  71. data/lib/configdirector/internal/evaluation/share.rb +9 -0
  72. data/lib/configdirector/internal/evaluation/targeting_rules.rb +18 -0
  73. data/lib/configdirector/internal/evaluation/text_comparison.rb +32 -0
  74. data/lib/configdirector/internal/evaluation/trait_pointer.rb +42 -0
  75. data/lib/configdirector/internal/evaluation.rb +21 -0
  76. data/lib/configdirector/internal/identity/sdk_identity.rb +39 -0
  77. data/lib/configdirector/internal/identity/wrapper.rb +48 -0
  78. data/lib/configdirector/internal/logging/client_log.rb +37 -0
  79. data/lib/configdirector/internal/logging/default_logger.rb +34 -0
  80. data/lib/configdirector/internal/polling/poll_answer.rb +111 -0
  81. data/lib/configdirector/internal/polling/poll_request.rb +106 -0
  82. data/lib/configdirector/internal/polling/poll_timing.rb +25 -0
  83. data/lib/configdirector/internal/polling/polling_endpoint.rb +36 -0
  84. data/lib/configdirector/internal/polling/polling_mode_connection.rb +53 -0
  85. data/lib/configdirector/internal/polling/polling_session.rb +99 -0
  86. data/lib/configdirector/internal/polling/polling_transport.rb +74 -0
  87. data/lib/configdirector/internal/polling/refusal_detail.rb +36 -0
  88. data/lib/configdirector/internal/server_sent_events/event_stream_limit_exceeded.rb +9 -0
  89. data/lib/configdirector/internal/server_sent_events/event_stream_parser.rb +95 -0
  90. data/lib/configdirector/internal/server_sent_events/server_sent_event.rb +9 -0
  91. data/lib/configdirector/internal/stoppable_thread.rb +69 -0
  92. data/lib/configdirector/internal/streaming/heartbeats.rb +51 -0
  93. data/lib/configdirector/internal/streaming/reconnecting_stream.rb +82 -0
  94. data/lib/configdirector/internal/streaming/reconnection_delay.rb +29 -0
  95. data/lib/configdirector/internal/streaming/stream_connection.rb +86 -0
  96. data/lib/configdirector/internal/streaming/stream_end.rb +69 -0
  97. data/lib/configdirector/internal/streaming/stream_endpoint.rb +43 -0
  98. data/lib/configdirector/internal/streaming/stream_timing.rb +33 -0
  99. data/lib/configdirector/internal/streaming/streaming_mode_connection.rb +60 -0
  100. data/lib/configdirector/internal/streaming/streaming_transport.rb +42 -0
  101. data/lib/configdirector/internal/telemetry/aggregated_wire_entries.rb +26 -0
  102. data/lib/configdirector/internal/telemetry/collected_telemetry.rb +98 -0
  103. data/lib/configdirector/internal/telemetry/compact_json_text.rb +64 -0
  104. data/lib/configdirector/internal/telemetry/discarded_telemetry.rb +31 -0
  105. data/lib/configdirector/internal/telemetry/event_reporter.rb +47 -0
  106. data/lib/configdirector/internal/telemetry/invalid_context_submissions.rb +76 -0
  107. data/lib/configdirector/internal/telemetry/report_answer.rb +31 -0
  108. data/lib/configdirector/internal/telemetry/report_schedule.rb +66 -0
  109. data/lib/configdirector/internal/telemetry/report_timing.rb +20 -0
  110. data/lib/configdirector/internal/telemetry/reported_context.rb +28 -0
  111. data/lib/configdirector/internal/telemetry/reported_details.rb +71 -0
  112. data/lib/configdirector/internal/telemetry/reported_evaluation.rb +82 -0
  113. data/lib/configdirector/internal/telemetry/reported_telemetry.rb +127 -0
  114. data/lib/configdirector/internal/telemetry/reported_text.rb +13 -0
  115. data/lib/configdirector/internal/telemetry/reported_time.rb +13 -0
  116. data/lib/configdirector/internal/telemetry/reported_value.rb +35 -0
  117. data/lib/configdirector/internal/telemetry/telemetry_endpoint.rb +54 -0
  118. data/lib/configdirector/internal/telemetry/telemetry_report.rb +50 -0
  119. data/lib/configdirector/internal/telemetry/test_client_telemetry.rb +37 -0
  120. data/lib/configdirector/internal/telemetry/tracked_event_admission.rb +72 -0
  121. data/lib/configdirector/internal/telemetry/tracked_event_entry.rb +27 -0
  122. data/lib/configdirector/internal/telemetry/tracked_event_queue.rb +47 -0
  123. data/lib/configdirector/internal/telemetry/tracked_event_warning.rb +28 -0
  124. data/lib/configdirector/internal/telemetry/value_id.rb +23 -0
  125. data/lib/configdirector/internal/telemetry/written_json.rb +9 -0
  126. data/lib/configdirector/internal/unchangeable_copy.rb +38 -0
  127. data/lib/configdirector/internal/value/encoded_native_value.rb +45 -0
  128. data/lib/configdirector/internal/value/hash_keys.rb +34 -0
  129. data/lib/configdirector/internal/value/in_code_default_value.rb +33 -0
  130. data/lib/configdirector/internal/value/json_contents.rb +81 -0
  131. data/lib/configdirector/internal/value/json_position.rb +27 -0
  132. data/lib/configdirector/internal/value/native_values.rb +41 -0
  133. data/lib/configdirector/internal/value/parsed_json.rb +79 -0
  134. data/lib/configdirector/internal/value/plain_decimal_text.rb +36 -0
  135. data/lib/configdirector/internal/value/served_text.rb +76 -0
  136. data/lib/configdirector/internal/value/typed_value.rb +9 -0
  137. data/lib/configdirector/internal/value/unservable_value.rb +9 -0
  138. data/lib/configdirector/internal/value/utf8_text.rb +19 -0
  139. data/lib/configdirector/internal.rb +9 -0
  140. data/lib/configdirector/metadata.rb +26 -0
  141. data/lib/configdirector/subscription.rb +34 -0
  142. data/lib/configdirector/telemetry_options.rb +28 -0
  143. data/lib/configdirector/testing/test_client.rb +122 -0
  144. data/lib/configdirector/testing/tracked_event.rb +46 -0
  145. data/lib/configdirector/testing.rb +44 -0
  146. data/lib/configdirector/variation.rb +21 -0
  147. data/lib/configdirector/version.rb +7 -0
  148. data/lib/configdirector-server-sdk.rb +3 -0
  149. data/lib/configdirector.rb +164 -0
  150. data/sig/configdirector/internal/client.rbs +518 -0
  151. data/sig/configdirector/internal/connection.rbs +389 -0
  152. data/sig/configdirector/internal/evaluation.rbs +364 -0
  153. data/sig/configdirector/internal/identity.rbs +51 -0
  154. data/sig/configdirector/internal/logging.rbs +33 -0
  155. data/sig/configdirector/internal/polling.rbs +240 -0
  156. data/sig/configdirector/internal/server_sent_events.rbs +51 -0
  157. data/sig/configdirector/internal/streaming.rbs +266 -0
  158. data/sig/configdirector/internal/telemetry.rbs +556 -0
  159. data/sig/configdirector/internal/value.rbs +159 -0
  160. data/sig/configdirector/testing.rbs +70 -0
  161. data/sig/configdirector.rbs +465 -0
  162. metadata +231 -0
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Connection
6
+ class TestClientInitializations
7
+ def initialize(lock, timeout_seconds)
8
+ @lock = lock
9
+ @timeout_seconds = timeout_seconds
10
+ @settled = ConditionVariable.new
11
+ @state = :not_connected
12
+ @next_initialization = :serves_the_values
13
+ @held_until_monotonic_seconds = 0.0
14
+ @outcome_of_the_held_initialization = :awaited
15
+ end
16
+
17
+ def connected?
18
+ @state == :connected
19
+ end
20
+
21
+ def hold_the_next_initialization
22
+ @next_initialization = :is_held
23
+ nil
24
+ end
25
+
26
+ def serve_the_values_at_the_next_initialization
27
+ @next_initialization = :serves_the_values
28
+ end
29
+
30
+ def begin_the_next_initialization
31
+ next_initialization = @next_initialization
32
+ @next_initialization = :serves_the_values
33
+ case next_initialization
34
+ when :serves_the_values then @state = :connected
35
+ when :fails then @state = :not_connected
36
+ when :is_held then hold
37
+ end
38
+
39
+ next_initialization
40
+ end
41
+
42
+ def complete_initialization
43
+ case @state
44
+ when :holding_an_initialization
45
+ @state = :connected
46
+ :held_initialization_completed
47
+ when :not_connected, :connected
48
+ cancel_a_hold_of_the_next_initialization
49
+ :no_initialization_held
50
+ end
51
+ end
52
+
53
+ def fail_initialization
54
+ case @state
55
+ when :holding_an_initialization
56
+ @state = :not_connected
57
+ :held_initialization_failed
58
+ when :not_connected, :connected
59
+ @next_initialization = :fails
60
+ :next_initialization_fails
61
+ end
62
+ end
63
+
64
+ def settling_the_held_initialization(outcome)
65
+ yield
66
+ ensure
67
+ @lock.synchronize do
68
+ @outcome_of_the_held_initialization = outcome
69
+ @settled.broadcast
70
+ end
71
+ end
72
+
73
+ def outcome_of_the_held_initialization
74
+ loop do
75
+ case @outcome_of_the_held_initialization
76
+ when :completed then return :completed
77
+ when :failed then return :failed
78
+ when :awaited
79
+ remaining_seconds = @held_until_monotonic_seconds - Process.clock_gettime(Process::CLOCK_MONOTONIC)
80
+ return outcome_at_the_timeout unless remaining_seconds.positive?
81
+
82
+ @settled.wait(@lock, remaining_seconds)
83
+ end
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def hold
90
+ @state = :holding_an_initialization
91
+ @held_until_monotonic_seconds = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout_seconds
92
+ @outcome_of_the_held_initialization = :awaited
93
+ end
94
+
95
+ def outcome_at_the_timeout
96
+ case @state
97
+ when :holding_an_initialization
98
+ @state = :not_connected
99
+ :ended_at_the_timeout
100
+ when :connected then :completed
101
+ when :not_connected then :failed
102
+ end
103
+ end
104
+
105
+ def cancel_a_hold_of_the_next_initialization
106
+ @next_initialization = :serves_the_values if @next_initialization == :is_held
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Connection
6
+ TestClientValues = Data.define(:configs)
7
+
8
+ class TestClientValues
9
+ CONFIG_ID_PREFIX = "test-config:"
10
+
11
+ private_constant :CONFIG_ID_PREFIX
12
+
13
+ def self.serving(values)
14
+ Value::NativeValues.reject_values_of_another_class(values)
15
+ new(
16
+ configs: rejecting_what_cannot_be_served do
17
+ ConfigDefinition.serving_each_value_to_every_context(values, CONFIG_ID_PREFIX)
18
+ end
19
+ )
20
+ end
21
+
22
+ def self.rejecting_what_cannot_be_served
23
+ yield
24
+ rescue Value::UnservableValue => e
25
+ raise ConfigDirectorValidationError, e.message
26
+ end
27
+
28
+ def merged_with(values)
29
+ with(configs: configs.merge(values.configs))
30
+ end
31
+
32
+ def without_the_value_of(given_key)
33
+ key_name = TestClientValues.rejecting_what_cannot_be_served do
34
+ Value::NativeValues.name_of_a_config_key(given_key)
35
+ end
36
+
37
+ case Value::Utf8Text.of(key_name)
38
+ in String => config_key then with(configs: configs.except(config_key))
39
+ in nil then self
40
+ end
41
+ end
42
+
43
+ def full_payload
44
+ ServerSdkPayload.of_definitions(configs)
45
+ end
46
+
47
+ def delta_payload
48
+ ServerSdkPayload.delta_of_definitions(configs)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Connection
6
+ class UnreadableDefinition < ConfigDirectorError; end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Connection
6
+ module WireFields
7
+ def self.object_or_nil_in(wire_object, field)
8
+ object = wire_object.is_a?(Hash) ? wire_object[field] : nil
9
+
10
+ object.is_a?(Hash) ? object : nil
11
+ end
12
+
13
+ def self.list_or_nil_in(wire_object, field)
14
+ list = wire_object.is_a?(Hash) ? wire_object[field] : nil
15
+
16
+ list.is_a?(Array) ? list : nil
17
+ end
18
+
19
+ def self.list_in(wire_object, field, owner)
20
+ list = wire_object[field]
21
+ return [] if list.nil?
22
+ raise UnreadableDefinition, "#{owner} has '#{field}' that is not a list" unless list.is_a?(Array)
23
+
24
+ list
25
+ end
26
+
27
+ def self.objects_listed_in(wire_object, field, owner, listed)
28
+ list = list_in(wire_object, field, owner)
29
+ raise UnreadableDefinition, "#{listed} is not an object" unless list.all?(Hash)
30
+
31
+ list
32
+ end
33
+
34
+ def self.text_required_in(wire_object, field, owner)
35
+ text = wire_object[field]
36
+ raise UnreadableDefinition, "#{owner} has no #{field}" unless text.is_a?(String) && !text.empty?
37
+
38
+ text
39
+ end
40
+
41
+ def self.text_or_nil_in(wire_object, field)
42
+ text = wire_object[field]
43
+
44
+ text.is_a?(String) ? text : nil
45
+ end
46
+
47
+ def self.text_or_empty_in(wire_object, field)
48
+ text_or_nil_in(wire_object, field) || ""
49
+ end
50
+
51
+ def self.whole_number_or_nil_in(wire_object, field)
52
+ whole_number = wire_object[field]
53
+
54
+ whole_number.is_a?(Integer) ? whole_number : nil
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Evaluation
6
+ module ArrayComparison
7
+ def self.matches?(value, operator, targets)
8
+ contains_any_target = value.is_a?(Array) && contains_any?(value, targets)
9
+
10
+ case operator
11
+ when "contains any of" then contains_any_target
12
+ when "does not contain any of" then !contains_any_target
13
+ else false
14
+ end
15
+ end
16
+
17
+ def self.contains_any?(elements, targets)
18
+ elements.filter_map { |element| ScalarText.of(element) }.intersect?(targets)
19
+ end
20
+
21
+ private_class_method :contains_any?
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Evaluation
6
+ module AssignedPercentage
7
+ SEED = 0x397832987
8
+ REACHABLE_TENTHS_OF_A_PERCENT = 1000
9
+
10
+ def self.of(config_id, identifier, context_kind)
11
+ hash_input = "#{identifier}-#{config_id}"
12
+ hash_input = "#{hash_input}:#{context_kind}" unless context_kind == EvaluationContext::USER_CONTEXT_KIND
13
+
14
+ (Rapidhash.of(hash_input, SEED) % REACHABLE_TENTHS_OF_A_PERCENT) / 10.0
15
+ end
16
+
17
+ private_constant :SEED, :REACHABLE_TENTHS_OF_A_PERCENT
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Evaluation
6
+ AttributeCondition = Data.define(:attribute, :trait, :operator, :target_type, :target_values)
7
+
8
+ class AttributeCondition
9
+ def holds_for?(context, _segments)
10
+ case attribute
11
+ when "identifier" then value_matches?(context.identifier)
12
+ when "kind" then value_matches?(context.effective_kind)
13
+ when "name" then value_matches?(context.name)
14
+ when "appName" then value_matches?(context.app_name)
15
+ when "appVersion" then value_matches?(context.app_version)
16
+ when "traits" then value_matches?(trait_of(context))
17
+ else false
18
+ end
19
+ end
20
+
21
+ def uses_any_segment_of?(_segment_ids)
22
+ false
23
+ end
24
+
25
+ private
26
+
27
+ def trait_of(context)
28
+ pointer = trait
29
+ pointer.nil? ? nil : TraitPointer.value_at(pointer, context.traits)
30
+ end
31
+
32
+ def value_matches?(value)
33
+ lowercase_operator = operator.downcase
34
+ text = ScalarText.of(value) || ""
35
+
36
+ case target_type
37
+ when "text" then TextComparison.matches?(text, lowercase_operator, target_values)
38
+ when "number" then NumberComparison.matches?(value, lowercase_operator, target_values)
39
+ when "semver" then SemverComparison.matches?(text, lowercase_operator, target_values)
40
+ when "datetime" then DatetimeComparison.matches?(text, lowercase_operator, target_values)
41
+ when "array" then ArrayComparison.matches?(value, lowercase_operator, target_values)
42
+ else false
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Evaluation
6
+ module ConfigEvaluator
7
+ def self.evaluate(config_id, targeting_rules, context, segments)
8
+ a_rollout_was_skipped = false
9
+ in_evaluation_order(targeting_rules.rules).each do |rule|
10
+ served = rule.serve(config_id, context, segments)
11
+ case served
12
+ when ServedValue then return served
13
+ when :rollout_skipped_no_identifier then a_rollout_was_skipped = true
14
+ when :not_matched then nil
15
+ end
16
+ end
17
+
18
+ targeting_rules.default_targeting_rule_value(a_rollout_was_skipped: a_rollout_was_skipped)
19
+ end
20
+
21
+ def self.in_evaluation_order(rules)
22
+ rules.sort_by.with_index { |rule, position| [rule.order || Float::INFINITY, position] }
23
+ end
24
+
25
+ private_class_method :in_evaluation_order
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Evaluation
6
+ module DatetimeComparison
7
+ ECMASCRIPT_DATE_TIME = /
8
+ \A(?<year>[+-][0-9]{6}|[0-9]{4})
9
+ (?:-(?<month>[0-9]{2})(?:-(?<day>[0-9]{2}))?)?
10
+ (?:T(?<hour>[0-9]{2}):(?<minute>[0-9]{2})(?::(?<second>[0-9]{2})(?:\.(?<fraction>[0-9]+))?)?
11
+ (?<offset>[Zz]|[+-][0-9]{2}:[0-9]{2})?)?\z
12
+ /x
13
+
14
+ DAYS_IN_MONTH_OF_A_COMMON_YEAR = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].freeze
15
+
16
+ def self.matches?(text, operator, targets)
17
+ instant = milliseconds_since_epoch_of(text)
18
+ target = targets.empty? ? nil : milliseconds_since_epoch_of(targets.first)
19
+ return false if instant.nil? || target.nil?
20
+
21
+ case operator
22
+ when "is before" then instant < target
23
+ when "is after" then instant > target
24
+ else false
25
+ end
26
+ end
27
+
28
+ def self.milliseconds_since_epoch_of(text)
29
+ match = ECMASCRIPT_DATE_TIME.match(text)
30
+ return nil if match.nil?
31
+
32
+ calendar_fields = possible_calendar_fields_of(match)
33
+ offset_minutes = offset_minutes_of(match[:offset])
34
+ return nil if calendar_fields.nil? || offset_minutes.nil?
35
+
36
+ milliseconds_read_as_utc(calendar_fields, match[:fraction]) - (offset_minutes * 60_000)
37
+ end
38
+
39
+ def self.possible_calendar_fields_of(match)
40
+ year = match[:year].to_i
41
+ month = match[:month]&.to_i || 1
42
+ day = match[:day]&.to_i || 1
43
+ hour = match[:hour].to_i
44
+ minute = match[:minute].to_i
45
+ second = match[:second].to_i
46
+ return nil unless possible_date?(year, month, day) && possible_time?(hour, minute, second)
47
+
48
+ [year, month, day, hour, minute, second]
49
+ end
50
+
51
+ def self.milliseconds_read_as_utc(calendar_fields, fraction)
52
+ year, month, day, hour, minute, second = calendar_fields
53
+ millisecond = fraction.to_s[0, 3].to_s.ljust(3, "0").to_i
54
+
55
+ (Time.utc(year, month, day, hour, minute, second).to_i * 1000) + millisecond
56
+ end
57
+
58
+ def self.offset_minutes_of(offset)
59
+ return 0 if offset.nil? || offset == "Z" || offset == "z"
60
+
61
+ hours = offset[1, 2].to_i
62
+ minutes = offset[4, 2].to_i
63
+ return nil unless possible_time?(hours, minutes, 0)
64
+
65
+ (offset.start_with?("-") ? -1 : 1) * ((hours * 60) + minutes)
66
+ end
67
+
68
+ def self.possible_date?(year, month, day)
69
+ month.between?(1, 12) && day.between?(1, days_in_month(year, month))
70
+ end
71
+
72
+ def self.possible_time?(hour, minute, second)
73
+ hour <= 23 && minute <= 59 && second <= 59
74
+ end
75
+
76
+ def self.days_in_month(year, month)
77
+ return 29 if month == 2 && leap_year?(year)
78
+
79
+ DAYS_IN_MONTH_OF_A_COMMON_YEAR.fetch(month - 1)
80
+ end
81
+
82
+ def self.leap_year?(year)
83
+ (year % 4).zero? && (!(year % 100).zero? || (year % 400).zero?)
84
+ end
85
+
86
+ private_constant :ECMASCRIPT_DATE_TIME, :DAYS_IN_MONTH_OF_A_COMMON_YEAR
87
+ private_class_method :milliseconds_since_epoch_of, :possible_calendar_fields_of, :milliseconds_read_as_utc,
88
+ :offset_minutes_of,
89
+ :possible_date?, :possible_time?, :days_in_month, :leap_year?
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Evaluation
6
+ EvaluationContext = Data.define(:identifier, :kind, :name, :traits, :app_name, :app_version)
7
+
8
+ class EvaluationContext
9
+ USER_CONTEXT_KIND = "user"
10
+
11
+ def effective_kind
12
+ kind || USER_CONTEXT_KIND
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Evaluation
6
+ module NumberComparison
7
+ NumberOperator = Data.define(:holds, :holds_for_an_unparseable_value)
8
+
9
+ EQUALS = NumberOperator.new(
10
+ holds: ->(number, target) { number == target }, holds_for_an_unparseable_value: false
11
+ )
12
+ DOES_NOT_EQUAL = NumberOperator.new(
13
+ holds: ->(number, target) { number != target }, holds_for_an_unparseable_value: true
14
+ )
15
+
16
+ OPERATORS = {
17
+ "=" => EQUALS,
18
+ "equals" => EQUALS,
19
+ "!=" => DOES_NOT_EQUAL,
20
+ "does not equal" => DOES_NOT_EQUAL,
21
+ "<" => NumberOperator.new(
22
+ holds: ->(number, target) { number < target }, holds_for_an_unparseable_value: false
23
+ ),
24
+ "<=" => NumberOperator.new(
25
+ holds: ->(number, target) { number <= target }, holds_for_an_unparseable_value: false
26
+ ),
27
+ ">" => NumberOperator.new(
28
+ holds: ->(number, target) { number > target }, holds_for_an_unparseable_value: false
29
+ ),
30
+ ">=" => NumberOperator.new(
31
+ holds: ->(number, target) { number >= target }, holds_for_an_unparseable_value: false
32
+ )
33
+ }.freeze
34
+
35
+ DECIMAL_LITERAL = /
36
+ \A(?<sign>[+-]?)(?<whole>[0-9]*)(?:\.(?<fraction>[0-9]*))?(?:[eE](?<exponent>[+-]?[0-9]+))?\z
37
+ /x
38
+
39
+ ROUNDS_TO_INFINITY_FROM = (1 << 1024) - (1 << 970)
40
+ ROUNDS_TO_ZERO_UP_TO = Rational(1, 1 << 1075)
41
+ LARGEST_FLOAT_DECIMAL_EXPONENT = 308
42
+ SMALLEST_FLOAT_DECIMAL_EXPONENT = -324
43
+
44
+ def self.matches?(value, operator, targets)
45
+ number_operator = OPERATORS[operator]
46
+ return false if number_operator.nil?
47
+
48
+ number = finite_number_of(value)
49
+ return number_operator.holds_for_an_unparseable_value if number.nil?
50
+
51
+ target = targets.empty? ? nil : finite_number_in(targets.first)
52
+ return false if target.nil?
53
+
54
+ number_operator.holds.call(number, target)
55
+ end
56
+
57
+ def self.finite_number_of(value)
58
+ case value
59
+ when Integer then value.abs < ROUNDS_TO_INFINITY_FROM ? value.to_f : nil
60
+ when Float then value.finite? ? value : nil
61
+ when String then finite_number_in(value)
62
+ end
63
+ end
64
+
65
+ def self.finite_number_in(text)
66
+ decimal_parts = decimal_parts_of(text)
67
+ return nil if decimal_parts.nil?
68
+
69
+ sign, significant_digits, power_of_ten = decimal_parts
70
+ return 0.0 if significant_digits.empty?
71
+
72
+ case rounding_of(significant_digits, power_of_ten)
73
+ when :overflows then nil
74
+ when :underflows then 0.0
75
+ when :fits then Float("#{sign}#{significant_digits}e#{power_of_ten}")
76
+ end
77
+ end
78
+
79
+ def self.decimal_parts_of(text)
80
+ literal = DECIMAL_LITERAL.match(text)
81
+ return nil if literal.nil?
82
+
83
+ digits = "#{literal[:whole]}#{literal[:fraction]}"
84
+ return nil if digits.empty?
85
+
86
+ [literal[:sign].to_s, digits.sub(/\A0+/, ""), literal[:exponent].to_i - literal[:fraction].to_s.length]
87
+ end
88
+
89
+ def self.rounding_of(significant_digits, power_of_ten)
90
+ case significant_digits.length - 1 + power_of_ten
91
+ when ((LARGEST_FLOAT_DECIMAL_EXPONENT + 1)..) then :overflows
92
+ when LARGEST_FLOAT_DECIMAL_EXPONENT then rounding_near_the_largest_float(significant_digits, power_of_ten)
93
+ when (...SMALLEST_FLOAT_DECIMAL_EXPONENT) then :underflows
94
+ when SMALLEST_FLOAT_DECIMAL_EXPONENT then rounding_near_the_smallest_float(significant_digits, power_of_ten)
95
+ else :fits
96
+ end
97
+ end
98
+
99
+ def self.rounding_near_the_largest_float(significant_digits, power_of_ten)
100
+ exact_value(significant_digits, power_of_ten) >= ROUNDS_TO_INFINITY_FROM ? :overflows : :fits
101
+ end
102
+
103
+ def self.rounding_near_the_smallest_float(significant_digits, power_of_ten)
104
+ exact_value(significant_digits, power_of_ten) <= ROUNDS_TO_ZERO_UP_TO ? :underflows : :fits
105
+ end
106
+
107
+ def self.exact_value(significant_digits, power_of_ten)
108
+ Rational(Integer(significant_digits, 10)) * (Rational(10)**power_of_ten)
109
+ end
110
+
111
+ private_constant :NumberOperator, :EQUALS, :DOES_NOT_EQUAL, :OPERATORS, :DECIMAL_LITERAL,
112
+ :ROUNDS_TO_INFINITY_FROM, :ROUNDS_TO_ZERO_UP_TO, :LARGEST_FLOAT_DECIMAL_EXPONENT,
113
+ :SMALLEST_FLOAT_DECIMAL_EXPONENT
114
+ private_class_method :finite_number_of, :decimal_parts_of, :rounding_of,
115
+ :rounding_near_the_largest_float, :rounding_near_the_smallest_float, :exact_value
116
+ end
117
+ end
118
+ end
119
+ end