2020-07-28 23:09:34 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Feature
|
|
|
|
class Definition
|
|
|
|
include ::Feature::Shared
|
|
|
|
|
|
|
|
attr_reader :path
|
|
|
|
attr_reader :attributes
|
|
|
|
|
|
|
|
PARAMS.each do |param|
|
|
|
|
define_method(param) do
|
|
|
|
attributes[param]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
TYPES.each do |type, _|
|
|
|
|
define_method("#{type}?") do
|
|
|
|
attributes[:type].to_sym == type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def initialize(path, opts = {})
|
|
|
|
@path = path
|
|
|
|
@attributes = {}
|
|
|
|
|
|
|
|
# assign nil, for all unknown opts
|
|
|
|
PARAMS.each do |param|
|
|
|
|
@attributes[param] = opts[param]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
name.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate!
|
|
|
|
unless name.present?
|
|
|
|
raise Feature::InvalidFeatureFlagError, "Feature flag is missing name"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless path.present?
|
|
|
|
raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' is missing path"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless type.present?
|
|
|
|
raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' is missing type. Ensure to update #{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless Definition::TYPES.include?(type.to_sym)
|
|
|
|
raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' type '#{type}' is invalid. Ensure to update #{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless File.basename(path, ".yml") == name
|
|
|
|
raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' has an invalid path: '#{path}'. Ensure to update #{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless File.basename(File.dirname(path)) == type
|
|
|
|
raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' has an invalid type: '#{path}'. Ensure to update #{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if default_enabled.nil?
|
|
|
|
raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' is missing default_enabled. Ensure to update #{path}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_usage!(type_in_code:, default_enabled_in_code:)
|
|
|
|
unless Array(type).include?(type_in_code.to_s)
|
|
|
|
# Raise exception in test and dev
|
|
|
|
raise Feature::InvalidFeatureFlagError, "The `type:` of `#{key}` is not equal to config: " \
|
|
|
|
"#{type_in_code} vs #{type}. Ensure to use valid type in #{path} or ensure that you use " \
|
|
|
|
"a valid syntax: #{TYPES.dig(type, :example)}"
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
unless default_enabled_in_code == :yaml || default_enabled == default_enabled_in_code
|
2020-07-28 23:09:34 +05:30
|
|
|
# Raise exception in test and dev
|
|
|
|
raise Feature::InvalidFeatureFlagError, "The `default_enabled:` of `#{key}` is not equal to config: " \
|
|
|
|
"#{default_enabled_in_code} vs #{default_enabled}. Ensure to update #{path}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def paths
|
|
|
|
@paths ||= [Rails.root.join('config', 'feature_flags', '**', '*.yml')]
|
|
|
|
end
|
|
|
|
|
|
|
|
def definitions
|
2021-01-03 14:25:43 +05:30
|
|
|
# We lazily load all definitions
|
|
|
|
# The hot reloading might request a feature flag
|
|
|
|
# before we can properly call `load_all!`
|
|
|
|
@definitions ||= load_all!
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def get(key)
|
|
|
|
definitions[key.to_sym]
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def reload!
|
|
|
|
@definitions = load_all!
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def has_definition?(key)
|
|
|
|
definitions.has_key?(key.to_sym)
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def valid_usage!(key, type:, default_enabled:)
|
2021-02-22 17:27:13 +05:30
|
|
|
if definition = get(key)
|
2020-07-28 23:09:34 +05:30
|
|
|
definition.valid_usage!(type_in_code: type, default_enabled_in_code: default_enabled)
|
|
|
|
elsif type_definition = self::TYPES[type]
|
|
|
|
raise InvalidFeatureFlagError, "Missing feature definition for `#{key}`" unless type_definition[:optional]
|
|
|
|
else
|
|
|
|
raise InvalidFeatureFlagError, "Unknown feature flag type used: `#{type}`"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def default_enabled?(key)
|
|
|
|
if definition = get(key)
|
|
|
|
definition.default_enabled
|
|
|
|
else
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(
|
|
|
|
InvalidFeatureFlagError.new("The feature flag YAML definition for '#{key}' does not exist"))
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def register_hot_reloader!
|
|
|
|
# Reload feature flags on change of this file or any `.yml`
|
|
|
|
file_watcher = Rails.configuration.file_watcher.new(reload_files, reload_directories) do
|
2021-01-03 14:25:43 +05:30
|
|
|
Feature::Definition.reload!
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
Rails.application.reloaders << file_watcher
|
|
|
|
Rails.application.reloader.to_run { file_watcher.execute_if_updated }
|
|
|
|
|
|
|
|
file_watcher
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
private
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def load_all!
|
|
|
|
paths.each_with_object({}) do |glob_path, definitions|
|
|
|
|
load_all_from_path!(definitions, glob_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def load_from_file(path)
|
|
|
|
definition = File.read(path)
|
|
|
|
definition = YAML.safe_load(definition)
|
|
|
|
definition.deep_symbolize_keys!
|
|
|
|
|
|
|
|
self.new(path, definition).tap(&:validate!)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2020-07-28 23:09:34 +05:30
|
|
|
raise Feature::InvalidFeatureFlagError, "Invalid definition for `#{path}`: #{e.message}"
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def load_all_from_path!(definitions, glob_path)
|
2020-07-28 23:09:34 +05:30
|
|
|
Dir.glob(glob_path).each do |path|
|
|
|
|
definition = load_from_file(path)
|
|
|
|
|
|
|
|
if previous = definitions[definition.key]
|
|
|
|
raise InvalidFeatureFlagError, "Feature flag '#{definition.key}' is already defined in '#{previous.path}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
definitions[definition.key] = definition
|
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def reload_files
|
2021-01-03 14:25:43 +05:30
|
|
|
[]
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def reload_directories
|
|
|
|
paths.each_with_object({}) do |path, result|
|
|
|
|
path = File.dirname(path)
|
|
|
|
Dir.glob(path).each do |matching_dir|
|
|
|
|
result[matching_dir] = 'yml'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Feature::Definition.prepend_mod_with('Feature::Definition')
|