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,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Value
6
+ InCodeDefaultValue = Data.define(:value, :kind)
7
+
8
+ class InCodeDefaultValue
9
+ def self.from(default_value)
10
+ kind = kind_of(default_value) || raise(
11
+ ConfigDirectorValidationError,
12
+ "Invalid in-code default value of class #{default_value.class}. The in-code default value " \
13
+ "for a config must be true, false, a String, an Integer, a Float, a Hash or an Array."
14
+ )
15
+ new(value: default_value, kind: kind)
16
+ end
17
+
18
+ def self.kind_of(default_value)
19
+ case default_value
20
+ when true, false then :boolean
21
+ when String then :string
22
+ when Integer then :integer
23
+ when Float then :float
24
+ when Hash then :json_object
25
+ when Array then :json_array
26
+ end
27
+ end
28
+
29
+ private_class_method :kind_of
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module ConfigDirector
6
+ module Internal
7
+ module Value
8
+ module JsonContents
9
+ MAXIMUM_NESTING = 100
10
+ WHAT_JSON_CONTENTS_CAN_HOLD = "JSON contents can hold true and false, Integers, finite Floats, Strings, " \
11
+ "nil, Arrays and Hashes with String or Symbol keys."
12
+
13
+ private_constant :MAXIMUM_NESTING, :WHAT_JSON_CONTENTS_CAN_HOLD
14
+
15
+ def self.json_text_of(json_value, config_key)
16
+ JSON.generate(contents_at(JsonPosition.at_the_top_of(config_key), json_value))
17
+ end
18
+
19
+ def self.contents_at(position, contents)
20
+ case contents
21
+ when nil, true, false, Integer then contents
22
+ when String then utf8_text_at(position, contents)
23
+ when Float then finite_float_at(position, contents)
24
+ when Array, Hash then container_contents_at(position, contents)
25
+ else raise unservable_contents_at(position, "an object of class #{contents.class} cannot be encoded as JSON")
26
+ end
27
+ end
28
+
29
+ def self.utf8_text_at(position, text)
30
+ Utf8Text.of(text) || raise(position.unservable_because("the String is not valid UTF-8 text."))
31
+ end
32
+
33
+ def self.finite_float_at(position, float)
34
+ raise unservable_contents_at(position, "#{float} is not a finite number") unless float.finite?
35
+
36
+ float
37
+ end
38
+
39
+ def self.container_contents_at(position, container)
40
+ if position.nesting > MAXIMUM_NESTING
41
+ raise UnservableValue,
42
+ "Invalid value for '#{position.config_key}': nesting of #{position.nesting} is too deep."
43
+ end
44
+
45
+ case container
46
+ when Array
47
+ container.each_with_index.map { |element, index| contents_at(position.of_element(index), element) }
48
+ else members_at(position, container)
49
+ end
50
+ end
51
+
52
+ def self.members_at(position, json_object)
53
+ given_keys = json_object.keys
54
+ key_texts = given_keys.map { |given_key| key_text_at(position, given_key) }
55
+ key_given_twice = HashKeys.description_of_a_key_given_twice_in(given_keys, key_texts)
56
+ raise position.unservable_because("the key #{key_given_twice}.") unless key_given_twice.nil?
57
+
58
+ key_texts.zip(json_object.values).to_h do |key_text, member|
59
+ [key_text, contents_at(position.of_member(key_text), member)]
60
+ end
61
+ end
62
+
63
+ def self.key_text_at(position, given_key)
64
+ key_name = HashKeys.name_of(given_key)
65
+ if key_name.nil?
66
+ raise unservable_contents_at(position, "a key of class #{given_key.class} cannot be encoded as JSON")
67
+ end
68
+
69
+ Utf8Text.of(key_name) || raise(position.unservable_because("a key is not valid UTF-8 text."))
70
+ end
71
+
72
+ def self.unservable_contents_at(position, problem)
73
+ position.unservable_because("#{problem}. #{WHAT_JSON_CONTENTS_CAN_HOLD}")
74
+ end
75
+
76
+ private_class_method :contents_at, :utf8_text_at, :finite_float_at, :container_contents_at, :members_at,
77
+ :key_text_at, :unservable_contents_at
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Value
6
+ JsonPosition = Data.define(:config_key, :path, :nesting)
7
+
8
+ class JsonPosition
9
+ def self.at_the_top_of(config_key)
10
+ new(config_key: config_key, path: "", nesting: 1)
11
+ end
12
+
13
+ def of_member(key_text)
14
+ with(path: path.empty? ? key_text : "#{path}.#{key_text}", nesting: nesting + 1)
15
+ end
16
+
17
+ def of_element(index)
18
+ with(path: "#{path}[#{index}]", nesting: nesting + 1)
19
+ end
20
+
21
+ def unservable_because(problem)
22
+ UnservableValue.new("Invalid value for '#{config_key}'#{" at '#{path}'" unless path.empty?}: #{problem}")
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Value
6
+ module NativeValues
7
+ def self.reject_values_of_another_class(values)
8
+ return if values.is_a?(Hash)
9
+
10
+ raise ConfigDirectorValidationError, "Invalid 'values' option of class #{values.class}. It must be a Hash."
11
+ end
12
+
13
+ def self.config_keys_of(values)
14
+ given_keys = values.keys
15
+ config_keys = given_keys.map { |given_key| config_key_named_by(given_key) }
16
+ key_given_twice = HashKeys.description_of_a_key_given_twice_in(given_keys, config_keys)
17
+ raise UnservableValue, "The config key #{key_given_twice}." unless key_given_twice.nil?
18
+
19
+ config_keys
20
+ end
21
+
22
+ def self.name_of_a_config_key(given_key)
23
+ HashKeys.name_of(given_key) || raise(
24
+ UnservableValue,
25
+ "Invalid config key of class #{given_key.class}. A config key must be a String or a Symbol."
26
+ )
27
+ end
28
+
29
+ def self.config_key_named_by(given_key)
30
+ config_key = Utf8Text.of(name_of_a_config_key(given_key))
31
+ raise UnservableValue, "Invalid config key. The key of a value is not valid UTF-8 text." if config_key.nil?
32
+ raise UnservableValue, "Invalid config key. The key of a value cannot be empty." if config_key.strip.empty?
33
+
34
+ config_key
35
+ end
36
+
37
+ private_class_method :config_key_named_by
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module ConfigDirector
6
+ module Internal
7
+ module Value
8
+ module ParsedJson
9
+ MOST_DIGITS_OF_A_NUMBER = 16_384
10
+ MORE_DIGITS_THAN_A_NUMBER_HAS = ("0" * (MOST_DIGITS_OF_A_NUMBER + 1)).freeze
11
+ QUOTE = '"'
12
+ QUOTE_OR_SLASH = %r{["/]}
13
+ QUOTE_OR_BACKSLASH = /["\\]/
14
+ END_OF_A_COMMENT_BY_ITS_START = { "/*" => "*/", "//" => "\n" }.freeze
15
+
16
+ def self.of(json_text)
17
+ if number_with_too_many_digits_in?(json_text.b)
18
+ raise JSON::ParserError, "it holds a number of more than 16,384 digits"
19
+ end
20
+
21
+ parsed_without_repeating_the_text_in_an_error(json_text)
22
+ end
23
+
24
+ def self.parsed_without_repeating_the_text_in_an_error(json_text)
25
+ JSON.parse(json_text, allow_duplicate_key: true)
26
+ rescue JSON::ParserError
27
+ raise JSON::ParserError, "it is not valid JSON"
28
+ end
29
+
30
+ def self.number_with_too_many_digits_in?(json_bytes)
31
+ run_of_too_many_digits_in?(json_bytes) &&
32
+ run_of_too_many_digits_in?(bytes_outside_the_strings_and_comments_of(json_bytes))
33
+ end
34
+
35
+ def self.run_of_too_many_digits_in?(bytes)
36
+ bytes.tr("0-9", "0").include?(MORE_DIGITS_THAN_A_NUMBER_HAS)
37
+ end
38
+
39
+ def self.bytes_outside_the_strings_and_comments_of(json_bytes)
40
+ bytes_outside = String.new(encoding: Encoding::BINARY)
41
+ position = 0
42
+ while (quote_or_slash = json_bytes.byteindex(QUOTE_OR_SLASH, position))
43
+ bytes_outside << json_bytes.byteslice(position, quote_or_slash - position).to_s
44
+ position = position_after_what_opens_at(quote_or_slash, json_bytes)
45
+ end
46
+
47
+ bytes_outside << json_bytes.byteslice(position..).to_s
48
+ end
49
+
50
+ def self.position_after_what_opens_at(quote_or_slash, json_bytes)
51
+ return position_after_the_string_opened_at(quote_or_slash, json_bytes) if json_bytes[quote_or_slash] == QUOTE
52
+
53
+ end_of_the_comment = END_OF_A_COMMENT_BY_ITS_START[json_bytes.byteslice(quote_or_slash, 2)]
54
+ return quote_or_slash + 1 if end_of_the_comment.nil?
55
+
56
+ comment_end = json_bytes.byteindex(end_of_the_comment, quote_or_slash + 2)
57
+ comment_end.nil? ? json_bytes.bytesize : comment_end + end_of_the_comment.bytesize
58
+ end
59
+
60
+ def self.position_after_the_string_opened_at(opening_quote, json_bytes)
61
+ position = opening_quote + 1
62
+ while (quote_or_backslash = json_bytes.byteindex(QUOTE_OR_BACKSLASH, position))
63
+ return quote_or_backslash + 1 if json_bytes[quote_or_backslash] == QUOTE
64
+
65
+ position = quote_or_backslash + 2
66
+ end
67
+
68
+ json_bytes.bytesize
69
+ end
70
+
71
+ private_constant :MORE_DIGITS_THAN_A_NUMBER_HAS, :QUOTE, :QUOTE_OR_SLASH, :QUOTE_OR_BACKSLASH,
72
+ :END_OF_A_COMMENT_BY_ITS_START
73
+ private_class_method :parsed_without_repeating_the_text_in_an_error, :number_with_too_many_digits_in?,
74
+ :run_of_too_many_digits_in?, :bytes_outside_the_strings_and_comments_of,
75
+ :position_after_what_opens_at, :position_after_the_string_opened_at
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Value
6
+ module PlainDecimalText
7
+ DIGITS_UP_TO_THE_LAST_NONZERO_ONE = /\A[0-9]*[1-9]/
8
+
9
+ private_constant :DIGITS_UP_TO_THE_LAST_NONZERO_ONE
10
+
11
+ def self.of(finite_float)
12
+ mantissa, exponent = finite_float.to_s.split("e")
13
+ sign = finite_float.to_s.start_with?("-") ? "-" : ""
14
+ whole_digits, fraction_digits = mantissa.to_s.delete_prefix("-").split(".")
15
+ integer_part, fraction_part = parts_around_the_point(
16
+ "#{whole_digits}#{fraction_digits}", whole_digits.to_s.length + exponent.to_i
17
+ )
18
+ significant_fraction = fraction_part[DIGITS_UP_TO_THE_LAST_NONZERO_ONE]
19
+
20
+ significant_fraction.nil? ? "#{sign}#{integer_part}" : "#{sign}#{integer_part}.#{significant_fraction}"
21
+ end
22
+
23
+ def self.parts_around_the_point(digits, digits_before_the_point)
24
+ return ["0", "#{"0" * -digits_before_the_point}#{digits}"] unless digits_before_the_point.positive?
25
+
26
+ [
27
+ digits[0, digits_before_the_point].to_s.ljust(digits_before_the_point, "0"),
28
+ digits[digits_before_the_point..].to_s
29
+ ]
30
+ end
31
+
32
+ private_class_method :parts_around_the_point
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module ConfigDirector
6
+ module Internal
7
+ module Value
8
+ module ServedText
9
+ CONFIG_TYPES_NOT_READABLE_AS_A_STRING = %w[boolean integer float].freeze
10
+ WHOLE_NUMBER_DIGITS = /\A[+-]?[0-9]+\z/
11
+
12
+ def self.typed_as(in_code_default_value_kind, served_text, config_type)
13
+ return "value-missing" if served_text.nil? || served_text.empty?
14
+
15
+ TYPED_BY_IN_CODE_DEFAULT_VALUE_KIND.fetch(in_code_default_value_kind).call(served_text, config_type)
16
+ end
17
+
18
+ def self.boolean_in(served_text)
19
+ case served_text.downcase
20
+ when "true" then TypedValue.new(value: true)
21
+ when "false" then TypedValue.new(value: false)
22
+ else "invalid-boolean"
23
+ end
24
+ end
25
+
26
+ def self.string_of(served_text, config_type)
27
+ return "type-mismatch" if CONFIG_TYPES_NOT_READABLE_AS_A_STRING.include?(config_type)
28
+
29
+ TypedValue.new(value: served_text)
30
+ end
31
+
32
+ def self.whole_number_in(served_text)
33
+ return TypedValue.new(value: Integer(served_text, 10)) if digits_of_a_whole_number?(served_text)
34
+
35
+ finite_number = Evaluation::NumberComparison.finite_number_in(served_text)
36
+ return "invalid-number" if finite_number.nil? || finite_number != finite_number.floor
37
+
38
+ TypedValue.new(value: finite_number.to_i)
39
+ end
40
+
41
+ def self.digits_of_a_whole_number?(served_text)
42
+ served_text.count("0-9") <= ParsedJson::MOST_DIGITS_OF_A_NUMBER && served_text.match?(WHOLE_NUMBER_DIGITS)
43
+ end
44
+
45
+ def self.finite_number_in(served_text)
46
+ finite_number = Evaluation::NumberComparison.finite_number_in(served_text)
47
+
48
+ finite_number.nil? ? "invalid-number" : TypedValue.new(value: finite_number)
49
+ end
50
+
51
+ def self.json_in(served_text, json_class)
52
+ parsed = ParsedJson.of(served_text)
53
+
54
+ parsed.is_a?(json_class) ? TypedValue.new(value: parsed) : "invalid-json"
55
+ rescue JSON::ParserError
56
+ "invalid-json"
57
+ end
58
+
59
+ private_class_method :boolean_in, :string_of, :whole_number_in, :digits_of_a_whole_number?, :finite_number_in,
60
+ :json_in
61
+
62
+ TYPED_BY_IN_CODE_DEFAULT_VALUE_KIND = {
63
+ boolean: ->(served_text, _config_type) { boolean_in(served_text) },
64
+ string: ->(served_text, config_type) { string_of(served_text, config_type) },
65
+ integer: ->(served_text, _config_type) { whole_number_in(served_text) },
66
+ float: ->(served_text, _config_type) { finite_number_in(served_text) },
67
+ json_object: ->(served_text, _config_type) { json_in(served_text, Hash) },
68
+ json_array: ->(served_text, _config_type) { json_in(served_text, Array) }
69
+ }.freeze
70
+
71
+ private_constant :CONFIG_TYPES_NOT_READABLE_AS_A_STRING, :WHOLE_NUMBER_DIGITS,
72
+ :TYPED_BY_IN_CODE_DEFAULT_VALUE_KIND
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Value
6
+ TypedValue = Data.define(:value)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Value
6
+ class UnservableValue < ConfigDirectorError; end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Internal
5
+ module Value
6
+ module Utf8Text
7
+ def self.of(text)
8
+ utf8_text = case text.encoding
9
+ when Encoding::BINARY then String.new(text, encoding: Encoding::UTF_8)
10
+ else text.encode(Encoding::UTF_8)
11
+ end
12
+ utf8_text.valid_encoding? ? utf8_text.freeze : nil
13
+ rescue EncodingError
14
+ nil
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ # The SDK's own code, which applications do not call. Anything in it can change in any release.
5
+ #
6
+ # @api private
7
+ module Internal
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ Metadata = Data.define(:app_name, :app_version)
5
+
6
+ # Metadata about your application. Give it to {ConfigDirector.create_client} so that targeting rules can
7
+ # use your application's name and version. Metadata cannot be changed once built, compares by value, and
8
+ # +with+ builds a copy with other values.
9
+ #
10
+ # @!attribute [r] app_name
11
+ # @return [String, nil] the name of your application
12
+ # @!attribute [r] app_version
13
+ # @return [String, nil] the version of your application
14
+ class Metadata
15
+ # @param app_name [String, #to_s, nil] the name of your application. A value that is not a String, such as a
16
+ # Symbol, is read as its text.
17
+ # @param app_version [String, #to_s, nil] the version of your application. A value that is not a String,
18
+ # such as the build number +2+, is read as its text, +"2"+.
19
+ def initialize(app_name: nil, app_version: nil)
20
+ super(
21
+ app_name: Internal::UnchangeableCopy.of(app_name&.to_s),
22
+ app_version: Internal::UnchangeableCopy.of(app_version&.to_s)
23
+ )
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ # One handler registered with {ConfigDirectorClient#on} or one watch registered with
5
+ # {ConfigDirectorClient#watch}, returned by the method that registered it.
6
+ #
7
+ # @example
8
+ # subscription = client.watch("new-checkout", false) { |enabled| puts enabled }
9
+ # subscription.close
10
+ class Subscription
11
+ # @api private
12
+ def initialize(&removal)
13
+ @removal = removal
14
+ @closed = false
15
+ end
16
+
17
+ # Removes the handler or watch this subscription registered, and only that one, even when the same
18
+ # handler or callback was registered again. It is safe to call more than once.
19
+ #
20
+ # @return [void]
21
+ def close
22
+ return if @closed
23
+
24
+ @closed = true
25
+ @removal.call
26
+ end
27
+
28
+ # @return [Boolean] whether {#close} has been called on this subscription. It stays +false+ when the
29
+ # handler or watch was removed another way, such as by {ConfigDirectorClient#close}.
30
+ def closed?
31
+ @closed
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ TelemetryOptions = Data.define(:event_queue_limit, :flush_interval)
5
+
6
+ # Telemetry tuning. These settings rarely need to change. An application that evaluates a large number of
7
+ # configs each second can use them to trade memory for how often telemetry is sent. Telemetry options
8
+ # cannot be changed once built, compare by value, and +with+ builds a copy with other values.
9
+ #
10
+ # The client reports each {ConfigDirectorClient#get_value}, {ConfigDirectorClient#evaluate} and watch
11
+ # delivery, and each context that has an +id+ and is not anonymous.
12
+ #
13
+ # @!attribute [r] event_queue_limit
14
+ # @return [Integer] how many telemetry events the client holds between two reports before it drops the
15
+ # oldest: evaluations up to 70% of it and contexts up to 30%
16
+ # @!attribute [r] flush_interval
17
+ # @return [Integer, Float] how often the client sends its telemetry, in seconds
18
+ class TelemetryOptions
19
+ # @param event_queue_limit [Integer] how many telemetry events the client holds between two reports
20
+ # before it drops the oldest: evaluations up to 70% of it and contexts up to 30%. A number from 100 to
21
+ # 100,000. Defaults to 5,000.
22
+ # @param flush_interval [Integer, Float] how often the client sends its telemetry, in seconds. Defaults to 30
23
+ # seconds.
24
+ def initialize(event_queue_limit: 5_000, flush_interval: 30)
25
+ super
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigDirector
4
+ module Testing
5
+ # The SDK's real client over an in-memory connection that the test controls. Create one with
6
+ # {Testing.create_test_client}.
7
+ #
8
+ # The client under {#client} is the SDK's production client: reading values, watches, events and readiness
9
+ # behave as they do against ConfigDirector. Only the connection is replaced, so no request is made, no thread
10
+ # is started and nothing is sent.
11
+ #
12
+ # While the client is connected, every change this test client makes reaches it before the call returns: the
13
+ # +configs_updated+ handlers and the watches run on the calling thread, inside the call, so an assertion can
14
+ # follow it directly. A change made from inside a handler or a watch reaches the client right after the change
15
+ # being handled. The client is connected from an +init+ that serves the values, or a
16
+ # {#complete_initialization}, until its +close+ or an +init+ that is held or fails. A change made while it is
17
+ # not connected is stored, and reaches it with the next +init+ that serves the values.
18
+ class TestClient
19
+ # @return [ConfigDirectorClient] the SDK client to hand to the code under test. It starts not ready, like a
20
+ # production client, and its +init+ serves the stored values at once unless initialization is held or set
21
+ # to fail. Each later +init+ serves the stored values again, as a full update. Close it with +close+ when
22
+ # the test ends; the test client has no +close+ of its own.
23
+ attr_reader :client
24
+
25
+ # @return [Array<TrackedEvent>] every event the code under test tracked through {#client}, in the order it was
26
+ # tracked, as the SDK would report it: an event whose name does not have 1 to 150 characters is not
27
+ # recorded, a value or details the SDK would leave out are +nil+ in the record, and a context whose kind is
28
+ # invalid is not recorded with its event. Each of these logs a warning every time, since a test client sends
29
+ # no reports. The limit of 1,000 events per report does not apply, and nothing is recorded once the client
30
+ # is closed. It is the same Array on every call and grows as events are tracked, so a test can empty it with
31
+ # +clear+.
32
+ attr_reader :tracked_events
33
+
34
+ # @api private
35
+ def initialize(client, connection, tracked_events)
36
+ @client = client
37
+ @connection = connection
38
+ @tracked_events = tracked_events
39
+ end
40
+
41
+ # Stores a value under a config key and, while the client is connected, delivers an update that carries only
42
+ # that key, so reads, the key's watches and the +configs_updated+ handlers see it. The update names the key
43
+ # even when the value is the one it already has.
44
+ #
45
+ # The config type follows from the class of the value: +true+ or +false+ is a boolean config, an Integer an
46
+ # integer config, a Float a float config, a String a string config, and a Hash or an Array a JSON config,
47
+ # which can hold +nil+. Every context receives the same value, as a match (+"found-match"+) with no value
48
+ # ID and no variation, from a config whose ID is +test-config:+ followed by the key.
49
+ #
50
+ # @param config_key [String, Symbol] the config key. A Symbol is read as its name.
51
+ # @param value [Boolean, Integer, Float, String, Hash, Array] the value to serve
52
+ # @return [void]
53
+ # @raise [ConfigDirectorValidationError] when the key is blank, of another class or not UTF-8 text, or the
54
+ # value is one the SDK cannot serve, such as +nil+, a Symbol or a Float that is not finite. Nothing changes
55
+ # then.
56
+ def set_value(config_key, value)
57
+ @connection.set_value(config_key, value)
58
+ end
59
+
60
+ # Removes the value of a config key and, while the client is connected, delivers a full update of the values
61
+ # left: reads of the key return their in-code default value with +"config-state-missing"+, the key's
62
+ # watches receive that in-code default value, and the +configs_updated+ handlers find the key in
63
+ # +removed_keys+ and every key left in +keys+. A key that holds no value removes nothing, and the full
64
+ # update is still delivered.
65
+ #
66
+ # @param config_key [String, Symbol] the config key. A Symbol is read as its name.
67
+ # @return [void]
68
+ # @raise [ConfigDirectorValidationError] when the key is neither a String nor a Symbol
69
+ def remove_value(config_key)
70
+ @connection.remove_value(config_key)
71
+ end
72
+
73
+ # Replaces every stored value, cancels a {#hold_initialization} or {#fail_initialization} that no +init+
74
+ # has used yet, and, while the client is connected, delivers the new values as a full update. Use it to
75
+ # reset a test client shared between tests.
76
+ #
77
+ # @param values [Hash{String, Symbol => Boolean, Integer, Float, String, Hash, Array}] the values to serve
78
+ # from now on, keyed by config key, as {#set_value} takes them
79
+ # @return [void]
80
+ # @raise [ConfigDirectorValidationError] when +values+ is not a Hash, or as {#set_value} raises. Nothing
81
+ # changes then.
82
+ def replace_values(values)
83
+ @connection.replace_values(values)
84
+ end
85
+
86
+ # Makes the next +init+ wait until another thread calls {#complete_initialization} or
87
+ # {#fail_initialization}, or until the client's timeout. At the timeout +init+ logs the warning a production
88
+ # client logs when no values arrive in time and returns: a client that was not ready stays not ready, so
89
+ # reads return their in-code default value with +"client-not-ready"+, and one that was ready keeps serving
90
+ # the values it had, without the warning. A +close+ or an +init+ called meanwhile, from another thread or
91
+ # from a handler that {#complete_initialization} or {#fail_initialization} runs, returns once that held
92
+ # +init+ returns.
93
+ #
94
+ # @return [void]
95
+ def hold_initialization
96
+ @connection.hold_initialization
97
+ end
98
+
99
+ # Delivers the stored values to an +init+ that {#hold_initialization} held, on the calling thread: the
100
+ # +configs_updated+ handlers receive every config key, then the watches run, then the +client_ready+
101
+ # handlers if the client was not ready. The client is ready when this returns, and the held +init+
102
+ # returns once those handlers and watches have run. Called before +init+ while a hold is set, it cancels
103
+ # the hold instead. Otherwise it does nothing.
104
+ #
105
+ # @return [void]
106
+ def complete_initialization
107
+ @connection.complete_initialization
108
+ end
109
+
110
+ # Fails an +init+ that {#hold_initialization} held, on the calling thread, or makes the next +init+ fail.
111
+ # An error is logged, the +connection_error+ handlers receive a {ConfigDirectorConnectionError} with the
112
+ # status 401, and the failed +init+ returns once they have run. A client that was not ready stays not ready,
113
+ # and one that was ready keeps serving the values it had. The +init+ after a failed one serves the stored
114
+ # values.
115
+ #
116
+ # @return [void]
117
+ def fail_initialization
118
+ @connection.fail_initialization
119
+ end
120
+ end
121
+ end
122
+ end