Class: Hocon::Impl::SimpleConfigObject

Inherits:
AbstractConfigObject show all
Defined in:
lib/hocon/impl/simple_config_object.rb

Constant Summary collapse

ConfigBugOrBrokenError =
Hocon::ConfigError::ConfigBugOrBrokenError
ResolveStatus =
Hocon::Impl::ResolveStatus
SimpleConfigOrigin =
Hocon::Impl::SimpleConfigOrigin

Constants inherited from AbstractConfigObject

AbstractConfigObject::ConfigNotResolvedError

Constants inherited from AbstractConfigValue

AbstractConfigValue::ConfigImplUtil

Instance Attribute Summary collapse

Attributes inherited from AbstractConfigValue

#origin

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractConfigObject

#merge_origins, #new_copy, #peek_assuming_resolved, #peek_path, #peek_path_impl, #to_config, #to_fallback_value, #value_type

Methods inherited from AbstractConfigValue

#at_key, #at_path, #indent, #render, #render_to_sb, #require_not_ignoring_fallbacks, #resolve_status, #to_s, #with_fallback, #with_origin

Constructor Details

#initialize(origin, value, status = Hocon::Impl::ResolveStatus.from_values(value.values), ignores_fallbacks = false) ⇒ SimpleConfigObject

Returns a new instance of SimpleConfigObject.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hocon/impl/simple_config_object.rb', line 20

def initialize(origin, value,
                status = Hocon::Impl::ResolveStatus.from_values(value.values),
                ignores_fallbacks = false)
  super(origin)
  if value.nil?
    raise ConfigBugError, "creating config object with null map"
  end
  @value = value
  @resolved = (status == Hocon::Impl::ResolveStatus::RESOLVED)
  @ignores_fallbacks = ignores_fallbacks

  # Kind of an expensive debug check. Comment out?
  if status != Hocon::Impl::ResolveStatus.from_values(value.values)
    raise ConfigBugError, "Wrong resolved status on #{self}"
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



37
38
39
# File 'lib/hocon/impl/simple_config_object.rb', line 37

def value
  @value
end

Class Method Details

.empty_missing(base_origin) ⇒ Object



14
15
16
17
18
# File 'lib/hocon/impl/simple_config_object.rb', line 14

def self.empty_missing(base_origin)
  self.new(
      Hocon::Impl::SimpleConfigOrigin.new_simple("#{base_origin.description} (not found)"),
      {})
end

Instance Method Details

#attempt_peek_with_partial_resolve(key) ⇒ Object



184
185
186
# File 'lib/hocon/impl/simple_config_object.rb', line 184

def attempt_peek_with_partial_resolve(key)
  @value[key]
end

#empty?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/hocon/impl/simple_config_object.rb', line 180

def empty?
  @value.empty?
end

#ignores_fallbacks?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/hocon/impl/simple_config_object.rb', line 44

def ignores_fallbacks?
  @ignores_fallbacks
end

#key_setObject



176
177
178
# File 'lib/hocon/impl/simple_config_object.rb', line 176

def key_set
  Set.new(@value.keys)
end

#merged_with_object(abstract_fallback) ⇒ Object



52
53
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
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hocon/impl/simple_config_object.rb', line 52

def merged_with_object(abstract_fallback)
  require_not_ignoring_fallbacks

  unless abstract_fallback.is_a?(Hocon::Impl::SimpleConfigObject)
    raise ConfigBugError, "should not be reached (merging non-SimpleConfigObject)"
  end

  fallback = abstract_fallback
  changed = false
  all_resolved = true
  merged = {}
  all_keys = key_set.union(fallback.key_set)
  all_keys.each do |key|
    first = @value[key]
    second = fallback.value[key]
    kept =
        if first.nil?
          second
        elsif second.nil?
          first
        else
          first.with_fallback(second)
        end
    merged[key] = kept

    if first != kept
      changed = true
    end

    if kept.resolve_status == Hocon::Impl::ResolveStatus::UNRESOLVED
      all_resolved = false
    end
  end

  new_resolve_status = Hocon::Impl::ResolveStatus.from_boolean(all_resolved)
  new_ignores_fallbacks = fallback.ignores_fallbacks?

  if changed
    Hocon::Impl::SimpleConfigObject.new(merge_origins([self, fallback]),
                           merged, new_resolve_status,
                           new_ignores_fallbacks)
  elsif (new_resolve_status != resolve_status) || (new_ignores_fallbacks != ignores_fallbacks?)
    newCopy(new_resolve_status, origin, new_ignores_fallbacks)
  else
    self
  end
end

#new_copy_with_status(new_status, new_origin, new_ignores_fallbacks = nil) ⇒ Object



39
40
41
42
# File 'lib/hocon/impl/simple_config_object.rb', line 39

def new_copy_with_status(new_status, new_origin, new_ignores_fallbacks = nil)
  Hocon::Impl::SimpleConfigObject.new(new_origin,
            @value, new_status, new_ignores_fallbacks)
end

#render_value_to_sb(sb, indent_size, at_root, options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/hocon/impl/simple_config_object.rb', line 100

def render_value_to_sb(sb, indent_size, at_root, options)
  if empty?
    sb << "{}"
  else
    outer_braces = options.json? || !at_root

    inner_indent =
      if outer_braces
        sb << "{"
        if options.formatted?
          sb << "\n"
        end
        indent_size + 1
      else
        indent_size
      end

    separator_count = 0
    key_set.each do |k|
      v = @value[k]

      if options.origin_comments?
        indent(sb, inner_indent, options)
        sb << "# "
        sb << v.origin.description
        sb << "\n"
      end
      if options.comments?
        v.origin.comments.each do |comment|
          indent(sb, inner_indent, options)
          sb << "#"
          if !comment.start_with?(" ")
            sb << " "
          end
          sb << comment
          sb << "\n"
        end
      end
      indent(sb, inner_indent, options)
      v.render_to_sb(sb, inner_indent, false, k.to_s, options)

      if options.formatted?
        if options.json?
          sb << ","
          separator_count = 2
        else
          separator_count = 1
        end
        sb << "\n"
      else
        sb << ","
        separator_count = 1
      end
    end
    # chop last commas/newlines
    # couldn't figure out a better way to chop characters off of the end of
    # the StringIO.  This relies on making sure that, prior to returning the
    # final string, we take a substring that ends at sb.pos.
    sb.pos = sb.pos - separator_count

    if outer_braces
      if options.formatted?
        sb << "\n" # put a newline back
        if outer_braces
          indent(sb, indent_size, options)
        end
      end
      sb << "}"
    end
  end
  if at_root && options.formatted?
    sb << "\n"
  end
end

#unwrappedObject



48
49
50
# File 'lib/hocon/impl/simple_config_object.rb', line 48

def unwrapped
  @value.merge(@value) { |k,v| v.unwrapped }
end

#with_value(path, v) ⇒ Object



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

def with_value(path, v)
  key = path.first
  remainder = path.remainder

  if remainder.nil?
    return with_value_impl(key, v)
  else
    child = @value[key]
    if (not child.nil?) && child.is_a?(Hocon::Impl::AbstractConfigObject)
      return with_value_impl(key, child.with_value(remainder, v))
    else
      subtree = v.at_path(
          SimpleConfigOrigin.new_simple("with_value(#{remainder.render})"), remainder)
      with_value_impl(key, subtree.root)
    end
  end
end

#with_value_impl(key, v) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/hocon/impl/simple_config_object.rb', line 231

def with_value_impl(key, v)
  if v.nil?
    raise ConfigBugOrBrokenError.new("Trying to store null ConfigValue in a ConfigObject", nil)
  end

  new_map = Hash.new
  if @value.empty?
    new_map[key] = v
  else
    new_map = @value.clone
    new_map[key] = v
  end
  self.class.new(origin, new_map, ResolveStatus.from_values(new_map.values), @ignores_fallbacks)
end

#without_path(path) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/hocon/impl/simple_config_object.rb', line 188

def without_path(path)
  key = path.first
  remainder = path.remainder
  v = @value[key]

  if (not v.nil?) && (not remainder.nil?) && v.is_a?(Hocon::Impl::AbstractConfigObject)
    v = v.without_path(remainder)
    updated = @value.clone
    updated[key] = v
    return Hocon::Impl::SimpleConfigObject.new(origin, updated,
                                               ResolveStatus.from_values(updated.values), @ignores_fallbacks)
  elsif (not remainder.nil?) || v.nil?
    return self
  else
    smaller = Hash.new
    @value.each do |old_key, old_value|
      if not old_key == key
        smaller[old_key] = old_value
      end
    end
    return Hocon::Impl::SimpleConfigObject.new(origin, smaller,
                                               ResolveStatus.from_values(smaller.values), @ignores_fallbacks)
  end
end