Module: LaunchDarkly::Impl::FileData Private

Defined in:
lib/ldclient-rb/impl/file_data/document.rb,
lib/ldclient-rb/impl/file_data/merge.rb,
lib/ldclient-rb/impl/file_data/poller.rb,
lib/ldclient-rb/impl/file_data/watcher.rb,
lib/ldclient-rb/impl/file_data/reloader.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Shared file reading, parsing, and merging code for the components that load flag and segment data from local files.

Since:

  • 5.5.0

Defined Under Namespace

Modules: DuplicateKeysHandling Classes: Document, DocumentSummary, FileSummary, MergeError, MergeResult, Poller, ReadError, Reloader, Watcher

Constant Summary collapse

YAML_FILE_NAME_ENDINGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The endings of the names of the files that are read as YAML, compared without regard to case. Every other file is read as JSON.

Since:

  • 5.5.0

[".yaml", ".yml"].freeze

Class Method Summary collapse

Class Method Details

.absolute_paths(paths) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts each path to an absolute path.

Parameters:

  • paths (Array<String>, String)

Returns:

  • (Array<String>)

Since:

  • 5.5.0



262
263
264
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 262

def self.absolute_paths(paths)
  Array(paths).map { |p| File.absolute_path(p.to_s) }
end

.blank?(content) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if the content is empty or only white space.

Parameters:

  • content (String)

Returns:

  • (Boolean)

Since:

  • 5.5.0



175
176
177
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 175

def self.blank?(content)
  content.each_byte.all? { |byte| WHITE_SPACE_BYTES.include?(byte) }
end

.check_encoding(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises an error if the content is not valid UTF-8.

Parameters:

  • content (String)

Raises:

  • (ArgumentError) —

    if the content is not valid UTF-8

Since:

  • 5.5.0



160
161
162
163
164
165
166
167
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 160

def self.check_encoding(content)
  return if content.valid_encoding?

  if content.b.start_with?(*UTF16_AND_UTF32_BOMS)
    raise ArgumentError, "the file is encoded in UTF-16 or UTF-32; data files must be UTF-8"
  end
  raise ArgumentError, "the file is not valid UTF-8"
end

.format_of(path) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the format of a data file, which its name decides: a name that ends in ".yaml" or ".yml", in any case, is YAML, and any other name is JSON.

Parameters:

  • path (String)

Returns:

  • (Symbol) —

    :json or :yaml

Since:

  • 5.5.0



150
151
152
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 150

def self.format_of(path)
  path.downcase.end_with?(*YAML_FILE_NAME_ENDINGS) ? :yaml : :json
end

.make_flag_with_value(key, value) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Expands a flag key to value entry into a full flag definition that returns the given value for every context. The flag is on, has the value as its only variation, and serves that variation as its fallthrough, so an evaluation reports the FALLTHROUGH reason kind.

Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Hash)

Since:

  • 5.5.0



246
247
248
249
250
251
252
253
254
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 246

def self.make_flag_with_value(key, value)
  {
    key: key,
    on: true,
    version: 1,
    fallthrough: { variation: 0 },
    variations: [value],
  }
end

.merge(documents, duplicate_keys_handling: DuplicateKeysHandling::FAIL, logger: nil) ⇒ MergeResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Combines the items of the given documents into one set of flags and one set of segments. Flag key to value entries expand into full flag definitions. Entries are deserialized into the SDK's data model classes, which validate them. The documents are processed in order, and the configured duplicate keys handling applies when the same key appears more than once.

The key under which an entry appears in the document is the item's key for every purpose: the item is stored under it, duplicates are detected by it, and the item's own "key" member is set to it, so that evaluations report the flag by that key. A missing "version" defaults to 1.

Parameters:

  • documents (Array<Document>)
  • duplicate_keys_handling (Symbol) (defaults to: DuplicateKeysHandling::FAIL) —

    one of the DuplicateKeysHandling values

  • logger (Logger, nil) (defaults to: nil) —

    receives data model validation messages

Returns:

Raises:

  • (MergeError) —

    if the documents cannot be combined

Since:

  • 5.5.0



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ldclient-rb/impl/file_data/merge.rb', line 88

def self.merge(documents, duplicate_keys_handling: DuplicateKeysHandling::FAIL, logger: nil)
  flags = {}
  segments = {}
  summaries = []

  documents.each do |document|
    summary = DocumentSummary.new(0, 0)

    document.flags.each do |key, data|
      data = prepare_entry("flag", key, data)
      item = deserialize(DataStore::FEATURES, "flag", key, data, logger)
      summary.flags += 1 if insert(flags, "flag", key, item, duplicate_keys_handling)
    end

    document.flag_values.each do |key, value|
      data = make_flag_with_value(key.to_s, value)
      item = Model.deserialize(DataStore::FEATURES, data, logger)
      summary.flags += 1 if insert(flags, "flag", key, item, duplicate_keys_handling)
    end

    document.segments.each do |key, data|
      data = prepare_entry("segment", key, data)
      item = deserialize(DataStore::SEGMENTS, "segment", key, data, logger)
      summary.segments += 1 if insert(segments, "segment", key, item, duplicate_keys_handling)
    end

    summaries << summary
  end

  MergeResult.new(flags, segments, summaries)
end

.parse_file(path, content) ⇒ Document

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses raw content that was read from the given path.

Parameters:

  • path (String)
  • content (String)

Returns:

Raises:

  • (ReadError) —

    if the content cannot be parsed

Since:

  • 5.5.0



212
213
214
215
216
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 212

def self.parse_file(path, content)
  Document.parse(content, format: format_of(path))
rescue StandardError => e
  raise ReadError.new(path, "error parsing file: #{e.message}")
end

.read_file(path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads the raw content of one file, as UTF-8 and without a UTF-8 byte order mark, whatever the process's default encoding. The bytes are not checked here: LaunchDarkly::Impl::FileData::Document.parse rejects content that is not valid UTF-8, so that such a file fails to load like any other content that cannot be parsed.

Parameters:

  • path (String)

Returns:

  • (String)

Raises:

Since:

  • 5.5.0



194
195
196
197
198
199
200
201
202
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 194

def self.read_file(path)
  content = File.binread(path)
  content = content.byteslice(UTF8_BOM.bytesize..) if content.start_with?(UTF8_BOM)
  content.force_encoding(Encoding::UTF_8)
rescue Errno::ENOENT => e
  raise ReadError.new(path, "unable to read file: #{e.message}", missing: true)
rescue SystemCallError, IOError => e
  raise ReadError.new(path, "unable to read file: #{e.message}")
end

.symbolize_keys(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Recursively converts hash keys to symbols. The SDK expects all data model objects to have symbol keys.

Parameters:

  • value (Object)

Returns:

  • (Object)

Since:

  • 5.5.0



225
226
227
228
229
230
231
232
233
234
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 225

def self.symbolize_keys(value)
  case value
  when Hash
    value.to_h { |k, v| [k.to_s.to_sym, symbolize_keys(v)] }
  when Array
    value.map { |v| symbolize_keys(v) }
  else
    value
  end
end