Class: Prefab::LocalConfigParser

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

Class Method Summary collapse

Class Method Details

.complex_string(file, key, value_hash) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/prefab/local_config_parser.rb', line 113

def complex_string(file, key, value_hash)
  value = PrefabProto::ConfigValue.new(
    string: value_hash["value"],
    confidential: value_hash["confidential"],
    decrypt_with: value_hash["decrypt_with"],
  )

  row = PrefabProto::ConfigRow.new(
    values: [
      PrefabProto::ConditionalValue.new(
        value: value
      )
    ]
  )

  {
    source: file,
    config: PrefabProto::Config.new(
      config_type: :CONFIG,
      key: key,
      rows: [row]
    )
  }
end

.feature_flag_config(file, key, value) ⇒ Object

Raises:



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
# File 'lib/prefab/local_config_parser.rb', line 58

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

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

  row = PrefabProto::ConfigRow.new(
    values: [
      PrefabProto::ConditionalValue.new(
        criteria: [criterion].compact,
        value: variant
      )
    ]
  )

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

  {
    source: file,
    match: key,
    config: PrefabProto::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
35
36
37
38
# 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)
    elsif value['type'] == 'provided'
      config[key] = provided_config(file, key, value)
    elsif value['decrypt_with'] || value['confidential']
      config[key] = complex_string(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: PrefabProto::Config.new(
        config_type: :CONFIG,
        key: key,
        rows: [
          PrefabProto::ConfigRow.new(values: [
            PrefabProto::ConditionalValue.new(value: value_from(key, value))
          ])
        ]
      )
    }
  end

  config
end

.parse_criterion(criterion) ⇒ Object



138
139
140
141
142
# File 'lib/prefab/local_config_parser.rb', line 138

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

.parse_value_to_match(values) ⇒ Object



144
145
146
147
148
# File 'lib/prefab/local_config_parser.rb', line 144

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

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

.provided_config(file, key, value_hash) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/prefab/local_config_parser.rb', line 86

def provided_config(file, key, value_hash)
  value = PrefabProto::ConfigValue.new(provided: PrefabProto::Provided.new(
    source: :ENV_VAR,
    lookup: value_hash["lookup"],
  ),
                                       confidential: value_hash["confidential"],
  )

  row = PrefabProto::ConfigRow.new(
    values: [
      PrefabProto::ConditionalValue.new(
        value: value
      )
    ]
  )

  {
    source: file,
    match: value.provided.lookup,
    config: PrefabProto::Config.new(
      config_type: :CONFIG,
      key: key,
      rows: [row]
    )
  }
end

.value_from(key, raw) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/prefab/local_config_parser.rb', line 40

def value_from(key, raw)
  case raw
  when String
    if key.to_s.start_with? Prefab::LoggerClient::BASE_KEY
      prefab_log_level_resolve = PrefabProto::LogLevel.resolve(raw.upcase.to_sym) || PrefabProto::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