Class: Hocon::Impl::SimpleConfig

Inherits:
Object
  • Object
show all
Includes:
ConfigMergeable
Defined in:
lib/hocon/impl/simple_config.rb

Constant Summary collapse

ConfigMissingError =
Hocon::ConfigError::ConfigMissingError
ConfigNotResolvedError =
Hocon::ConfigError::ConfigNotResolvedError
ConfigNullError =
Hocon::ConfigError::ConfigNullError
ConfigWrongTypeError =
Hocon::ConfigError::ConfigWrongTypeError
ConfigValueType =
Hocon::ConfigValueType
Path =
Hocon::Impl::Path
DefaultTransformer =
Hocon::Impl::DefaultTransformer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ SimpleConfig

Returns a new instance of SimpleConfig.



25
26
27
# File 'lib/hocon/impl/simple_config.rb', line 25

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



23
24
25
# File 'lib/hocon/impl/simple_config.rb', line 23

def object
  @object
end

Class Method Details

.find_key(me, key, expected, original_path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hocon/impl/simple_config.rb', line 51

def self.find_key(me, key, expected, original_path)
  v = me.peek_assuming_resolved(key, original_path)
  if v.nil?
    raise ConfigMissingError.new(nil, "No configuration setting found for key '#{original_path.render}'", nil)
  end

  if not expected.nil?
    v = DefaultTransformer.transform(v, expected)
  end

  if v.value_type == ConfigValueType::NULL
    raise ConfigNullError.new(v.origin,
                              (ConfigNullError.make_message(original_path.render,
                                                            (not expected.nil?) ? ConfigValueType.name(expected) : nil)),
                              nil)
  elsif (not expected.nil?) && v.value_type != expected
    raise ConfigWrongTypeError.new(v.origin,
                                   "#{original_path.render} has type #{ConfigValueType.name(v.value_type)} " +
                                       "rather than #{ConfigValueType.name(expected)}",
                                   nil)
  else
    return v
  end
end

.find_key_or_null(me, key, expected, original_path) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hocon/impl/simple_config.rb', line 110

def self.find_key_or_null(me, key, expected, original_path)
  v = me.peek_assuming_resolved(key, original_path)

  if v.nil?
    raise Hocon::ConfigError::ConfigMissingError.new(nil, original_path.render, nil)
  end

  if not expected.nil?
    v = Hocon::Impl::DefaultTransformer.transform(v, expected)
  end

  if (not expected.nil?) && (v.value_type != expected && v.value_type != Hocon::ConfigValueType::NULL)
    raise Hocon::ConfigError::ConfigWrongTypeError.with_expected_actual(v.origin,
                                                                        original_path.render,
                                                                        expected.name,
                                                                        Hocon::ConfigValueType.name(v.value_type))
  else
    return v
  end
end

.find_or_null(me, path, expected, original_path) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hocon/impl/simple_config.rb', line 131

def self.find_or_null(me, path, expected, original_path)
  begin
    key = path.first
    remainder = path.remainder

    if remainder.nil?
      return self.find_key_or_null(me, key, expected, original_path)
    else
      o = find_key(me,
                   key,
                   Hocon::ConfigValueType::OBJECT,
                   original_path.sub_path(0, original_path.length - remainder.length))

      if o.nil?
        raise "Missing key: #{key} on path: #{path}"
      end

      find_or_null(o, remainder, expected, original_path)
    end
  rescue Hocon::ConfigError::ConfigNotResolvedError
    raise Hocon::Impl::ConfigImpl::improved_not_resolved(path, e)
  end
end

Instance Method Details

#==(other) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/hocon/impl/simple_config.rb', line 98

def ==(other)
  if other.is_a? Hocon::Impl::SimpleConfig
    @object == other.object
  else
    false
  end
end

#at_key(key) ⇒ Object



310
311
312
# File 'lib/hocon/impl/simple_config.rb', line 310

def at_key(key)
  root.at_key(key)
end

#at_key_with_origin(origin, key) ⇒ Object

In java this is an overloaded version of atKey



315
316
317
# File 'lib/hocon/impl/simple_config.rb', line 315

def at_key_with_origin(origin, key)
  root.at_key_with_origin(origin, key)
end

#empty?Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/hocon/impl/simple_config.rb', line 306

def empty?
  @object.empty?
end

#find(me, path, expected, original_path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hocon/impl/simple_config.rb', line 76

def find(me, path, expected, original_path)
  key = path.first
  rest = path.remainder
  if rest.nil?
    self.class.find_key(me, key, expected, original_path)
  else
    o = self.class.find_key(me, key, ConfigValueType::OBJECT,
                 original_path.sub_path(0, original_path.length - rest.length))
    raise "Error: object o is nil" unless not o.nil?
    find(o, rest, expected, original_path)
  end
end

#find2(path_expression, expected) ⇒ Object



93
94
95
96
# File 'lib/hocon/impl/simple_config.rb', line 93

def find2(path_expression, expected)
  path = Path.new_path(path_expression)
  find3(path, expected, path)
end

#find3(path_expression, expected, original_path) ⇒ Object



89
90
91
# File 'lib/hocon/impl/simple_config.rb', line 89

def find3(path_expression, expected, original_path)
  find(@object, path_expression, expected, original_path)
end

#get_any_ref(path) ⇒ Object



198
199
200
201
# File 'lib/hocon/impl/simple_config.rb', line 198

def get_any_ref(path)
  v = find2(path, nil)
  v.unwrapped
end

#get_boolean(path) ⇒ Object



166
167
168
169
# File 'lib/hocon/impl/simple_config.rb', line 166

def get_boolean(path)
  v = find2(path, ConfigValueType::BOOLEAN)
  v.unwrapped
end

#get_boolean_list(path) ⇒ Object



231
232
233
# File 'lib/hocon/impl/simple_config.rb', line 231

def get_boolean_list(path)
  get_homogeneous_unwrapped_list(path, ConfigValueType::BOOLEAN)
end

#get_bytes(path) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/hocon/impl/simple_config.rb', line 203

def get_bytes(path)
  size = null
  begin
    size = get_long(path)
  rescue ConfigWrongTypeError => e
    v = find2(path, ConfigValueType::STRING)
    size = self.class.parse_bytes(v.unwrapped, v.origin, path)
  end
  size
end

#get_config(path) ⇒ Object



194
195
196
# File 'lib/hocon/impl/simple_config.rb', line 194

def get_config(path)
  get_object(path).to_config
end

#get_config_number(path_expression) ⇒ Object



171
172
173
174
175
# File 'lib/hocon/impl/simple_config.rb', line 171

def get_config_number(path_expression)
  path = Path.new_path(path_expression)
  v = find(@object, path, ConfigValueType::NUMBER, path)
  v.unwrapped
end

#get_double_list(path) ⇒ Object



248
249
250
251
252
253
254
255
# File 'lib/hocon/impl/simple_config.rb', line 248

def get_double_list(path)
  l = []
  numbers = get_number_list(path)
  numbers.each do |n|
    l << n.double_value
  end
  l
end

#get_homogeneous_unwrapped_list(path, expected) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/hocon/impl/simple_config.rb', line 214

def get_homogeneous_unwrapped_list(path, expected)
  l = []
  list = get_list(path)
  list.each do |cv|
    if !expected.nil?
      v = DefaultTransformer.transform(cv, expected)
    end
    if v.value_type != expected
      raise ConfigWrongTypeError.with_expected_actual(origin, path,
            "list of #{expected.name}",
            "list of #{v.value_type.name}")
    end
    l << v.unwrapped
  end
  l
end

#get_homogeneous_wrapped_list(path, expected) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/hocon/impl/simple_config.rb', line 265

def get_homogeneous_wrapped_list(path, expected)
  l = []
  list = get_list(path)
  list.each do |cv|
    if !expected.nil?
      v = DefaultTransformer.transform(cv, expected)
    end
    if v.value_type != expected
      raise ConfigWrongTypeError.with_expected_actual(origin, path,
                                                      "list of #{expected.name}",
                                                      "list of #{v.value_type.name}")
    end
    l << v
  end
  l
end

#get_int(path) ⇒ Object



177
178
179
# File 'lib/hocon/impl/simple_config.rb', line 177

def get_int(path)
  get_config_number(path)
end

#get_int_list(path) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/hocon/impl/simple_config.rb', line 239

def get_int_list(path)
  l = []
  numbers = get_homogeneous_wrapped_list(path, ConfigValueType::NUMBER)
  numbers.each do |v|
    l << v.int_value_range_checked(path)
  end
  l
end

#get_list(path) ⇒ Object



186
187
188
# File 'lib/hocon/impl/simple_config.rb', line 186

def get_list(path)
  find2(path, ConfigValueType::LIST)
end

#get_number_list(path) ⇒ Object



235
236
237
# File 'lib/hocon/impl/simple_config.rb', line 235

def get_number_list(path)
  get_homogeneous_unwrapped_list(path, ConfigValueType::NUMBER)
end

#get_object(path) ⇒ Object



190
191
192
# File 'lib/hocon/impl/simple_config.rb', line 190

def get_object(path)
  find2(path, ConfigValueType::OBJECT)
end

#get_object_list(path) ⇒ Object



261
262
263
# File 'lib/hocon/impl/simple_config.rb', line 261

def get_object_list(path)
  get_homogeneous_wrapped_list(path, ConfigValueType::OBJECT)
end

#get_string(path) ⇒ Object



181
182
183
184
# File 'lib/hocon/impl/simple_config.rb', line 181

def get_string(path)                                                                                                                                                                                                                                                                 
  v = find2(path, ConfigValueType::STRING)
  v.unwrapped
end

#get_string_list(path) ⇒ Object



257
258
259
# File 'lib/hocon/impl/simple_config.rb', line 257

def get_string_list(path)
  get_homogeneous_unwrapped_list(path, ConfigValueType::STRING)
end

#get_value(path) ⇒ Object



161
162
163
164
# File 'lib/hocon/impl/simple_config.rb', line 161

def get_value(path)
  parsed_path = Path.new_path(path)
  find(@object, parsed_path, nil, parsed_path)
end

#has_path?(path_expression) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
297
298
# File 'lib/hocon/impl/simple_config.rb', line 294

def has_path?(path_expression)
  peeked = has_path_peek(path_expression)

  (not peeked.nil?) && peeked.value_type != ConfigValueType::NULL
end

#has_path_or_null?(path) ⇒ Boolean

Returns:

  • (Boolean)


300
301
302
303
304
# File 'lib/hocon/impl/simple_config.rb', line 300

def has_path_or_null?(path)
  peeked = has_path_peek(path)

  not peeked.nil?
end

#has_path_peek(path_expression) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
# File 'lib/hocon/impl/simple_config.rb', line 282

def has_path_peek(path_expression)
  path = Path.new_path(path_expression)

  begin
    peeked = @object.peek_path(path)
  rescue Hocon::ConfigError::ConfigNotResolvedError
    raise Hocon::Impl::ConfigImpl.improved_not_resolved(path, e)
  end

  peeked
end

#hashObject



106
107
108
# File 'lib/hocon/impl/simple_config.rb', line 106

def hash
  41 * @object.hash
end

#is_null?(path_expression) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
159
# File 'lib/hocon/impl/simple_config.rb', line 155

def is_null?(path_expression)
  path = Path.new_path(path_expression)
  v = self.class.find_or_null(@object, path, nil, path)
  v.value_type == Hocon::ConfigValueType::NULL
end

#originObject



34
35
36
# File 'lib/hocon/impl/simple_config.rb', line 34

def origin
  @object.origin
end

#resolve(options = Hocon::ConfigResolveOptions.defaults) ⇒ Object



38
39
40
# File 'lib/hocon/impl/simple_config.rb', line 38

def resolve(options = Hocon::ConfigResolveOptions.defaults)
  resolve_with(self, options)
end

#resolve_with(source, options) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/hocon/impl/simple_config.rb', line 42

def resolve_with(source, options)
  resolved = Hocon::Impl::ResolveContext.resolve(@object, source.object, options)
  if resolved.eql?(@object)
    self
  else
    Hocon::Impl::SimpleConfig.new(resolved)
  end
end

#rootObject



30
31
32
# File 'lib/hocon/impl/simple_config.rb', line 30

def root
  @object
end

#to_fallback_valueObject



334
335
336
# File 'lib/hocon/impl/simple_config.rb', line 334

def to_fallback_value
  @object
end

#with_fallback(other) ⇒ Object



338
339
340
# File 'lib/hocon/impl/simple_config.rb', line 338

def with_fallback(other)
  @object.with_fallback(other).to_config
end

#with_only_path(path_expression) ⇒ Object



319
320
321
322
# File 'lib/hocon/impl/simple_config.rb', line 319

def with_only_path(path_expression)
  path = Path.new_path(path_expression)
  self.class.new(root.with_only_path(path))
end

#with_value(path_expression, v) ⇒ Object



329
330
331
332
# File 'lib/hocon/impl/simple_config.rb', line 329

def with_value(path_expression, v)
  path = Path.new_path(path_expression)
  self.class.new(root.with_value(path, v))
end

#without_path(path_expression) ⇒ Object



324
325
326
327
# File 'lib/hocon/impl/simple_config.rb', line 324

def without_path(path_expression)
  path = Path.new_path(path_expression)
  self.class.new(root.without_path(path))
end