Class: Prefab::LocalConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/prefab/local_config_parser.rb

Class Method Summary collapse

Class Method Details

.feature_flag_config(file, key, value) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/prefab/local_config_parser.rb', line 54

def feature_flag_config(file, key, value)
  criterion = (parse_criterion(value['criterion']) if value['criterion'])

  variant = Prefab::ConfigValue.new(value_from(key, value['value']))

  row = Prefab::ConfigRow.new(
    values: [
      Prefab::ConditionalValue.new(
        criteria: [criterion].compact,
        value: Prefab::ConfigValue.new(
          weighted_values: Prefab::WeightedValues.new(weighted_values: [
                                                        Prefab::WeightedValue.new(
                                                          weight: 1000,
                                                          value: variant
                                                        )
                                                      ])
        )
      )
    ]
  )

  raise Prefab::Error, "Feature flag config `#{key}` #{file} must have a `value`" unless value.key?('value')

  {
    source: file,
    match: key,
    config: Prefab::Config.new(
      config_type: :FEATURE_FLAG,
      key: key,
      allowable_values: [variant],
      rows: [row]
    )
  }
end

.parse(key, value, config, file) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/prefab/local_config_parser.rb', line 6

def parse(key, value, config, file)
  if value.instance_of?(Hash)
    if value['feature_flag']
      config[key] = feature_flag_config(file, key, value)
    else
      value.each do |nest_key, nest_value|
        nested_key = "#{key}.#{nest_key}"
        nested_key = key if nest_key == '_'
        parse(nested_key, nest_value, config, file)
      end
    end
  else
    config[key] = {
      source: file,
      match: 'default',
      config: Prefab::Config.new(
        config_type: :CONFIG,
        key: key,
        rows: [
          Prefab::ConfigRow.new(values: [
                                  Prefab::ConditionalValue.new(value: value_from(key, value))
                                ])
        ]
      )
    }
  end

  config
end

.parse_criterion(criterion) ⇒ Object



89
90
91
92
93
# File 'lib/prefab/local_config_parser.rb', line 89

def parse_criterion(criterion)
  Prefab::Criterion.new(operator: criterion['operator'],
                        property_name: parse_property(criterion),
                        value_to_match: parse_value_to_match(criterion['values']))
end

.parse_property(criterion) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/prefab/local_config_parser.rb', line 95

def parse_property(criterion)
  if criterion['operator'] == 'LOOKUP_KEY_IN'
    Prefab::CriteriaEvaluator::LOOKUP_KEY
  else
    criterion['property']
  end
end

.parse_value_to_match(values) ⇒ Object



103
104
105
106
107
# File 'lib/prefab/local_config_parser.rb', line 103

def parse_value_to_match(values)
  raise "Can't handle #{values}" unless values.instance_of?(Array)

  Prefab::ConfigValue.new(string_list: Prefab::StringList.new(values: values))
end

.value_from(key, raw) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/prefab/local_config_parser.rb', line 36

def value_from(key, raw)
  case raw
  when String
    if key.to_s.start_with? Prefab::LoggerClient::BASE_KEY
      prefab_log_level_resolve = Prefab::LogLevel.resolve(raw.upcase.to_sym) || Prefab::LogLevel::NOT_SET_LOG_LEVEL
      { log_level: prefab_log_level_resolve }
    else
      { string: raw }
    end
  when Integer
    { int: raw }
  when TrueClass, FalseClass
    { bool: raw }
  when Float
    { double: raw }
  end
end