File: config_string.rb

package info (click to toggle)
ruby-hocon 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ruby: 7,903; makefile: 4
file content (85 lines) | stat: -rw-r--r-- 2,009 bytes parent folder | download
1
2
3
4
5
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
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# encoding: utf-8

require_relative '../../hocon/impl'
require_relative '../../hocon/impl/abstract_config_value'
require_relative '../../hocon/config_value_type'
require_relative '../../hocon/impl/config_impl_util'

class Hocon::Impl::ConfigString
  include Hocon::Impl::AbstractConfigValue

  ConfigImplUtil = Hocon::Impl::ConfigImplUtil

  attr_reader :value

  class Quoted < Hocon::Impl::ConfigString
    def initialize(origin, value)
      super(origin, value)
    end

    def new_copy(origin)
      self.class.new(origin, @value)
    end

    private

    # serialization all goes through SerializedConfigValue
    def write_replace
      Hocon::Impl::SerializedConfigValue.new(self)
    end
  end

  # this is sort of a hack; we want to preserve whether whitespace
  # was quoted until we process substitutions, so we can ignore
  # unquoted whitespace when concatenating lists or objects.
  # We dump this distinction when serializing and deserializing,
  # but that 's OK because it isn' t in equals/hashCode, and we
  # don 't allow serializing unresolved objects which is where
  # quoted-ness matters. If we later make ConfigOrigin point
  # to the original token range, we could use that to implement
  # wasQuoted()
  class Unquoted < Hocon::Impl::ConfigString
    def initialize(origin, value)
      super(origin, value)
    end

    def new_copy(origin)
      self.class.new(origin, @value)
    end

    def write_replace
      Hocon::Impl::SerializedConfigValue.new(self)
    end
  end

  def was_quoted?
    self.is_a?(Quoted)
  end

  def value_type
    Hocon::ConfigValueType::STRING
  end

  def unwrapped
    @value
  end

  def transform_to_string
    @value
  end

  def render_value_to_sb(sb, indent_size, at_root, options)
    if options.json?
      sb << ConfigImplUtil.render_json_string(@value)
    else
      sb << ConfigImplUtil.render_string_unquoted_if_possible(@value)
    end
  end

  private

  def initialize(origin, value)
    super(origin)
    @value = value
  end
end