Class: Hocon::Impl::DefaultTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/hocon/impl/default_transformer.rb

Constant Summary collapse

ConfigValueType =
Hocon::ConfigValueType

Class Method Summary collapse

Class Method Details

.transform(value, requested) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hocon/impl/default_transformer.rb', line 8

def self.transform(value, requested)
  if value.value == ConfigValueType::STRING
    s = value.unwrapped
    case requested
      when NUMBER
        begin
          v = Integer(s)
          return ConfigInt.new(value.origin, v, s)
        rescue ArgumentError
          # try Float
        end
        begin
          v = Float(s)
          return ConfigFloat.new(value.origin, v, s)
        rescue ArgumentError
          # oh well.
        end
      when NULL
        if s == "null"
          return ConfigNull.new(value.origin)
        end
      when BOOLEAN
        if s == "true" || s == "yes" || s == "on"
          return ConfigBoolean.new(value.origin, true)
        elsif s == "false" || s == "no" || s == "off"
          return ConfigBoolean.new(value.origin, false)
        end
      when LIST
        # can't go STRING to LIST automatically
      when OBJECT
        # can't go STRING to OBJECT automatically
      when STRING
        # no-op STRING to STRING
    end
  elsif requested == ConfigValueType::STRING
    # if we converted null to string here, then you wouldn't properly
    # get a missing-value error if you tried to get a null value
    # as a string.
    case value.value_type
      # NUMBER case removed since you can't fallthrough in a ruby case statement
      when BOOLEAN
        return ConfigString.new(value.origin, value.transform_to_string)
      when NULL
        # want to be sure this throws instead of returning "null" as a
        # string
      when OBJECT
        # no OBJECT to STRING automatically
      when LIST
        # no LIST to STRING automatically
      when STRING
        # no-op STRING to STRING
    end
  elsif requested == ConfigValueType::LIST && value.value_type == ConfigValueType::OBJECT
    # attempt to convert an array-like (numeric indices) object to a
    # list. This would be used with .properties syntax for example:
    # -Dfoo.0=bar -Dfoo.1=baz
    # To ensure we still throw type errors for objects treated
    # as lists in most cases, we'll refuse to convert if the object
    # does not contain any numeric keys. This means we don't allow
    # empty objects here though :-/
    o = value
    values = Hash.new
    values
    o.keys.each do |key|
      begin
        i = Integer(key, 10)
        if i < 0
          next
        end
        values[key] = i
      rescue ArgumentError
        next
      end
    end
    if not values.empty?
      entry_list = values.to_a
      # sort by numeric index
      entry_list.sort! {|a,b| b[0] <=> a[0]}
      # drop the indices (we allow gaps in the indices, for better or
      # worse)
      list = Array.new
      entry_list.each do |entry|
        list.push(entry[1])
      end
      return SimpleConfigList.new(value.origin, list)
    end
  end
  value
end