hocon 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,12 @@
1
1
  require 'hocon/impl'
2
+ require 'hocon/impl/path_builder'
3
+ require 'hocon/config_error'
2
4
  require 'stringio'
3
5
 
4
6
  class Hocon::Impl::Path
7
+
8
+ ConfigBugOrBrokenError = Hocon::ConfigError::ConfigBugOrBrokenError
9
+
5
10
  # this doesn't have a very precise meaning, just to reduce
6
11
  # noise from quotes in the rendered path for average cases
7
12
  def self.has_funky_chars?(s)
@@ -34,6 +39,46 @@ class Hocon::Impl::Path
34
39
  end
35
40
  attr_reader :first, :remainder
36
41
 
42
+ def first
43
+ @first
44
+ end
45
+
46
+ def remainder
47
+ @remainder
48
+ end
49
+
50
+ def parent
51
+ if remainder.nil?
52
+ return nil
53
+ end
54
+
55
+ pb = Hocon::Impl::PathBuilder.new
56
+ p = self
57
+ while not p.remainder.nil?
58
+ pb.append_key(p.first)
59
+ p = p.remainder
60
+ end
61
+ pb.result
62
+ end
63
+
64
+ def last
65
+ p = self
66
+ while not p.remainder.nil?
67
+ p = p.remainder
68
+ end
69
+ p.first
70
+ end
71
+
72
+ def length
73
+ count = 1
74
+ p = remainder
75
+ while not p.nil? do
76
+ count += 1
77
+ p = p.remainder
78
+ end
79
+ return count
80
+ end
81
+
37
82
  #
38
83
  # toString() is a debugging-oriented version while this is an
39
84
  # error-message-oriented human-readable one.
@@ -56,4 +101,36 @@ class Hocon::Impl::Path
56
101
  @remainder.append_to_string_builder(sb)
57
102
  end
58
103
  end
104
+
105
+ def sub_path_to_end(remove_from_front)
106
+ count = remove_from_front
107
+ p = self
108
+ while (not p.nil?) && count > 0 do
109
+ count -= 1
110
+ p = p.remainder
111
+ end
112
+ p
113
+ end
114
+
115
+ def sub_path(first_index, last_index)
116
+ if last_index < first_index
117
+ raise ConfigBugOrBrokenError.new("bad call to sub_path", nil)
118
+ end
119
+ from = sub_path_to_end(first_index)
120
+ pb = Hocon::Impl::PathBuilder.new
121
+ count = last_index - first_index
122
+ while count > 0 do
123
+ count -= 1
124
+ pb.append_key(from.first)
125
+ from = from.remainder
126
+ if from.nil?
127
+ raise ConfigBugOrBrokenError.new("sub_path last_index out of range #{last_index}", nil)
128
+ end
129
+ end
130
+ pb.result
131
+ end
132
+
133
+ def self.new_path(path)
134
+ Hocon::Impl::Parser.parse_path(path)
135
+ end
59
136
  end
@@ -2,7 +2,6 @@ require 'hocon/impl'
2
2
  require 'hocon/impl/path'
3
3
 
4
4
  class Hocon::Impl::PathBuilder
5
- Path = Hocon::Impl::Path
6
5
 
7
6
  def initialize
8
7
  @keys = []
@@ -27,7 +26,7 @@ class Hocon::Impl::PathBuilder
27
26
  remainder = nil
28
27
  while !@keys.empty?
29
28
  key = @keys.pop
30
- remainder = Path.new(key, remainder)
29
+ remainder = Hocon::Impl::Path.new(key, remainder)
31
30
  end
32
31
  @result = remainder
33
32
  end
@@ -0,0 +1,83 @@
1
+ require 'hocon/impl'
2
+ require 'hocon/config_error'
3
+ require 'hocon/impl/config_impl'
4
+
5
+ class Hocon::Impl::PropertiesParser
6
+ def from_path_map(origin, path_map, converted_from_properties)
7
+ # First, build a list of paths that will have values, either string or
8
+ # object values.
9
+ scope_paths = Set.new
10
+ value_paths = Set.new
11
+ path_map.each_key do |path|
12
+ value_paths.add(path)
13
+
14
+ next_path = path.parent
15
+ while not next_path.nil? do
16
+ scope_paths.add(next_path)
17
+ next_path = next_path.parent
18
+ end
19
+ end
20
+
21
+ if converted_from_properties
22
+ # If any string values are also objects containing other values,
23
+ # drop those string values - objects "win".
24
+ value_paths = value_paths - scope_paths
25
+ else
26
+ # If we didn't start out as properties, then this is an error.
27
+ value_paths.each do |path|
28
+ if scope_paths.include?(path)
29
+ raise ConfigBugOrBrokenError.new("In the map, path '#{path.render}' occurs as both" +
30
+ " the parent object of a value and as a value. Because Map " +
31
+ "has no defined ordering, this is a broken situation.", nil)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Create maps for the object-valued values.
37
+ root = Hash.new
38
+ scopes = Hash.new
39
+
40
+ value_paths.each do |path|
41
+ parent_path = path.parent
42
+ parent = (not parent_path.nil?) ? scopes[parent_path] : root
43
+
44
+ last = path.last
45
+ raw_value = path_map.get(path)
46
+ if converted_from_properties
47
+ if raw_value.is_a?(String)
48
+ value = Hocon::Impl::ConfigString.new(origin, raw_value)
49
+ else
50
+ value = nil
51
+ end
52
+ else
53
+ value = Hocon::Impl::ConfigImpl.from_any_ref_impl(path_map[path], origin, Hocon::Impl::FromMapMode::KEYS_ARE_PATHS)
54
+ end
55
+ if not value.nil?
56
+ parent[last, value]
57
+ end
58
+ end
59
+
60
+ # Make a list of scope paths from longest to shortest, so children go
61
+ # before parents.
62
+ sorted_scope_paths = Array.new
63
+ sorted_scope_paths = sorted_scope_paths + scope_paths
64
+ sorted_scope_paths.sort! do |a,b|
65
+ b.length <=> a.length
66
+ end
67
+
68
+ # Create ConfigObject for each scope map, working from children to
69
+ # parents to avoid modifying any already-created ConfigObject. This is
70
+ # where we need the sorted list.
71
+ sorted_scope_paths.each do |scope_path|
72
+ scope = scopes[scope_path]
73
+
74
+ parent_path = scope_path.parent
75
+ parent = (not parent_path.nil?) ? scopes[parent_path] : root
76
+
77
+ o = Hocon::Impl::SimpleConfigObject.new(origin, scope, Hocon::Impl::ResolveStatus::RESOLVED, false)
78
+ parent[scope_path.last, o]
79
+ end
80
+
81
+ Hocon::Impl::SimpleConfigObject.new(origin, root, Hocon::Impl::ResolveStatus::RESOLVED, false)
82
+ end
83
+ end
@@ -1,6 +1,19 @@
1
1
  require 'hocon/impl'
2
+ require 'hocon/config_value_type'
3
+ require 'hocon/impl/path'
4
+ require 'hocon/impl/default_transformer'
5
+ require 'hocon/impl/config_impl'
2
6
 
3
7
  class Hocon::Impl::SimpleConfig
8
+
9
+ ConfigMissingError = Hocon::ConfigError::ConfigMissingError
10
+ ConfigNotResolvedError = Hocon::ConfigError::ConfigNotResolvedError
11
+ ConfigNullError = Hocon::ConfigError::ConfigNullError
12
+ ConfigWrongTypeError = Hocon::ConfigError::ConfigWrongTypeError
13
+ ConfigValueType = Hocon::ConfigValueType
14
+ Path = Hocon::Impl::Path
15
+ DefaultTransformer = Hocon::Impl::DefaultTransformer
16
+
4
17
  def initialize(object)
5
18
  @object = object
6
19
  end
@@ -8,4 +21,67 @@ class Hocon::Impl::SimpleConfig
8
21
  def root
9
22
  @object
10
23
  end
24
+
25
+ def find_key(me, key, expected, original_path)
26
+ v = me.peek_assuming_resolved(key, original_path)
27
+ if v.nil?
28
+ raise ConfigMissingError.new(nil, "No configuration setting found for key '#{original_path.render}'", nil)
29
+ end
30
+
31
+ if not expected.nil?
32
+ v = DefaultTransformer.transform(v, expected)
33
+ end
34
+
35
+ if v.value_type == ConfigValueType::NULL
36
+ raise ConfigNullError.new(v.origin,
37
+ (ConfigNullError.make_message(original_path.render,
38
+ (not expected.nil?) ? expected.name : nil)),
39
+ nil)
40
+ elsif (not expected.nil?) && v.value_type != expected
41
+ raise ConfigWrongTypeError.new(v.origin,
42
+ "#{original_path.render} has type #{v.value_type.name} " +
43
+ "rather than #{expected.name}",
44
+ nil)
45
+ else
46
+ return v
47
+ end
48
+ end
49
+
50
+ def find(me, path, expected, original_path)
51
+ key = path.first
52
+ rest = path.remainder
53
+ if rest.nil?
54
+ find_key(me, key, expected, original_path)
55
+ else
56
+ o = find_key(me, key, ConfigValueType::OBJECT,
57
+ original_path.sub_path(0, original_path.length - rest.length))
58
+ raise "Error: object o is nil" unless not o.nil?
59
+ find(o, rest, expected, original_path)
60
+ end
61
+ end
62
+
63
+ def get_value(path)
64
+ parsed_path = Path.new_path(path)
65
+ find(@object, parsed_path, nil, parsed_path)
66
+ end
67
+
68
+ def has_path(path_expression)
69
+ path = Path.new_path(path_expression)
70
+ begin
71
+ peeked = @object.peek_path(path)
72
+ rescue ConfigNotResolvedError => e
73
+ raise Hocon::Impl::ConfigImpl.improve_not_resolved(path, e)
74
+ end
75
+ (not peeked.nil?) && peeked.value_type != ConfigValueType::NULL
76
+ end
77
+
78
+ def without_path(path_expression)
79
+ path = Path.new_path(path_expression)
80
+ self.class.new(root.without_path(path))
81
+ end
82
+
83
+ def with_value(path_expression, v)
84
+ path = Path.new_path(path_expression)
85
+ self.class.new(root.with_value(path, v))
86
+ end
11
87
  end
@@ -1,6 +1,7 @@
1
1
  require 'hocon/impl'
2
2
  require 'hocon/impl/resolve_status'
3
3
  require 'hocon/config_value_type'
4
+ require 'hocon/impl/abstract_config_object'
4
5
 
5
6
  class Hocon::Impl::SimpleConfigList < Hocon::Impl::AbstractConfigValue
6
7
  ResolveStatus = Hocon::Impl::ResolveStatus
@@ -2,9 +2,15 @@ require 'hocon/impl'
2
2
  require 'hocon/impl/simple_config_origin'
3
3
  require 'hocon/impl/abstract_config_object'
4
4
  require 'hocon/impl/resolve_status'
5
+ require 'hocon/config_error'
5
6
  require 'set'
6
7
 
7
8
  class Hocon::Impl::SimpleConfigObject < Hocon::Impl::AbstractConfigObject
9
+
10
+ ConfigBugOrBrokenError = Hocon::ConfigError::ConfigBugOrBrokenError
11
+ ResolveStatus = Hocon::Impl::ResolveStatus
12
+ SimpleConfigOrigin = Hocon::Impl::SimpleConfigOrigin
13
+
8
14
  def self.empty_missing(base_origin)
9
15
  self.new(
10
16
  Hocon::Impl::SimpleConfigOrigin.new_simple("#{base_origin.description} (not found)"),
@@ -175,4 +181,65 @@ class Hocon::Impl::SimpleConfigObject < Hocon::Impl::AbstractConfigObject
175
181
  @value.empty?
176
182
  end
177
183
 
184
+ def attempt_peek_with_partial_resolve(key)
185
+ @value[key]
186
+ end
187
+
188
+ def without_path(path)
189
+ key = path.first
190
+ remainder = path.remainder
191
+ v = @value[key]
192
+
193
+ if (not v.nil?) && (not remainder.nil?) && v.is_a?(Hocon::Impl::AbstractConfigObject)
194
+ v = v.without_path(remainder)
195
+ updated = @value.clone
196
+ updated[key] = v
197
+ return Hocon::Impl::SimpleConfigObject.new(origin, updated,
198
+ ResolveStatus.from_values(updated.values), @ignores_fallbacks)
199
+ elsif (not remainder.nil?) || v.nil?
200
+ return self
201
+ else
202
+ smaller = Hash.new
203
+ @value.each do |old_key, old_value|
204
+ if not old_key == key
205
+ smaller[old_key] = old_value
206
+ end
207
+ end
208
+ return Hocon::Impl::SimpleConfigObject.new(origin, smaller,
209
+ ResolveStatus.from_values(smaller.values), @ignores_fallbacks)
210
+ end
211
+ end
212
+
213
+ def with_value(path, v)
214
+ key = path.first
215
+ remainder = path.remainder
216
+
217
+ if remainder.nil?
218
+ return with_value_impl(key, v)
219
+ else
220
+ child = @value[key]
221
+ if (not child.nil?) && child.is_a?(Hocon::Impl::AbstractConfigObject)
222
+ return with_value_impl(key, child.with_value(remainder, v))
223
+ else
224
+ subtree = v.at_path(
225
+ SimpleConfigOrigin.new_simple("with_value(#{remainder.render})"), remainder)
226
+ with_value_impl(key, subtree.root)
227
+ end
228
+ end
229
+ end
230
+
231
+ def with_value_impl(key, v)
232
+ if v.nil?
233
+ raise ConfigBugOrBrokenError.new("Trying to store null ConfigValue in a ConfigObject", nil)
234
+ end
235
+
236
+ new_map = Hash.new
237
+ if @value.empty?
238
+ new_map[key] = v
239
+ else
240
+ new_map = @value.clone
241
+ new_map[key] = v
242
+ end
243
+ self.class.new(origin, new_map, ResolveStatus.from_values(new_map.values), @ignores_fallbacks)
244
+ end
178
245
  end
@@ -2,6 +2,7 @@ require 'hocon/impl'
2
2
  require 'hocon/impl/config_impl_util'
3
3
  require 'hocon/impl/tokens'
4
4
  require 'stringio'
5
+ require 'forwardable'
5
6
 
6
7
  class Hocon::Impl::Tokenizer
7
8
  Tokens = Hocon::Impl::Tokens
@@ -10,6 +11,7 @@ class Hocon::Impl::Tokenizer
10
11
  end
11
12
 
12
13
  class TokenIterator
14
+ extend Forwardable
13
15
  class WhitespaceSaver
14
16
  def initialize
15
17
  @whitespace = StringIO.new
@@ -79,6 +81,8 @@ class Hocon::Impl::Tokenizer
79
81
  (c != "\n") and (Hocon::Impl::ConfigImplUtil.whitespace?(c))
80
82
  end
81
83
 
84
+ def_delegator :@tokens, :each
85
+
82
86
  def initialize(origin, input, allow_comments)
83
87
  @origin = origin
84
88
  @input = input
@@ -361,6 +365,10 @@ class Hocon::Impl::Tokenizer
361
365
  end
362
366
  t
363
367
  end
368
+
369
+ def empty?
370
+ @tokens.empty?
371
+ end
364
372
  end
365
373
 
366
374
 
@@ -3,6 +3,7 @@ require 'hocon/impl/token'
3
3
  require 'hocon/impl/token_type'
4
4
  require 'hocon/impl/config_number'
5
5
  require 'hocon/impl/config_string'
6
+ require 'hocon/impl/config_boolean'
6
7
 
7
8
  # FIXME the way the subclasses of Token are private with static isFoo and accessors is kind of ridiculous.
8
9
  class Hocon::Impl::Tokens
@@ -10,6 +11,7 @@ class Hocon::Impl::Tokens
10
11
  TokenType = Hocon::Impl::TokenType
11
12
  ConfigNumber = Hocon::Impl::ConfigNumber
12
13
  ConfigString = Hocon::Impl::ConfigString
14
+ ConfigBoolean = Hocon::Impl::ConfigBoolean
13
15
 
14
16
  START = Token.new_without_origin(TokenType::START, "start of file")
15
17
  EOF = Token.new_without_origin(TokenType::EOF, "end of file")
@@ -103,6 +105,10 @@ class Hocon::Impl::Tokens
103
105
  new_value(ConfigNumber.new_number(origin, value, original_text))
104
106
  end
105
107
 
108
+ def self.new_boolean(origin, value)
109
+ new_value(ConfigBoolean.new(origin, value))
110
+ end
111
+
106
112
  def self.comment?(t)
107
113
  t.is_a?(Comment)
108
114
  end
@@ -154,4 +160,4 @@ class Hocon::Impl::Tokens
154
160
  def self.problem?(t)
155
161
  t.is_a?(Problem)
156
162
  end
157
- end
163
+ end
metadata CHANGED
@@ -1,63 +1,102 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hocon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Chris Price
8
9
  - Wayne Warren
10
+ - Dai Akatsuka
11
+ - Preben Ingvaldsen
9
12
  autorequire:
10
13
  bindir: bin
11
14
  cert_chain: []
12
- date: 2014-07-23 00:00:00.000000000 Z
15
+ date: 2014-10-01 00:00:00.000000000 Z
13
16
  dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: bundler
19
+ requirement: !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: '1.5'
25
+ type: :development
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ type: :development
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
14
49
  - !ruby/object:Gem::Dependency
15
50
  name: rspec
16
51
  requirement: !ruby/object:Gem::Requirement
52
+ none: false
17
53
  requirements:
18
- - - "~>"
54
+ - - ~>
19
55
  - !ruby/object:Gem::Version
20
56
  version: '2.14'
21
57
  type: :development
22
58
  prerelease: false
23
59
  version_requirements: !ruby/object:Gem::Requirement
60
+ none: false
24
61
  requirements:
25
- - - "~>"
62
+ - - ~>
26
63
  - !ruby/object:Gem::Version
27
64
  version: '2.14'
28
- description: "== A port of the Java {Typesafe Config}[https://github.com/typesafehub/config]
29
- library to Ruby"
65
+ description: == A port of the Java {Typesafe Config}[https://github.com/typesafehub/config]
66
+ library to Ruby
30
67
  email: chris@puppetlabs.com
31
68
  executables: []
32
69
  extensions: []
33
70
  extra_rdoc_files: []
34
71
  files:
35
- - LICENSE
36
- - README.md
37
- - lib/hocon.rb
38
72
  - lib/hocon/config_error.rb
39
73
  - lib/hocon/config_factory.rb
40
74
  - lib/hocon/config_object.rb
41
75
  - lib/hocon/config_parse_options.rb
42
76
  - lib/hocon/config_render_options.rb
43
77
  - lib/hocon/config_syntax.rb
78
+ - lib/hocon/config_value_factory.rb
44
79
  - lib/hocon/config_value_type.rb
45
- - lib/hocon/impl.rb
46
80
  - lib/hocon/impl/abstract_config_object.rb
47
81
  - lib/hocon/impl/abstract_config_value.rb
82
+ - lib/hocon/impl/config_boolean.rb
48
83
  - lib/hocon/impl/config_concatenation.rb
49
84
  - lib/hocon/impl/config_float.rb
50
85
  - lib/hocon/impl/config_impl.rb
51
86
  - lib/hocon/impl/config_impl_util.rb
52
87
  - lib/hocon/impl/config_int.rb
88
+ - lib/hocon/impl/config_null.rb
53
89
  - lib/hocon/impl/config_number.rb
54
90
  - lib/hocon/impl/config_string.rb
91
+ - lib/hocon/impl/default_transformer.rb
92
+ - lib/hocon/impl/from_map_mode.rb
55
93
  - lib/hocon/impl/full_includer.rb
56
94
  - lib/hocon/impl/origin_type.rb
57
95
  - lib/hocon/impl/parseable.rb
58
96
  - lib/hocon/impl/parser.rb
59
97
  - lib/hocon/impl/path.rb
60
98
  - lib/hocon/impl/path_builder.rb
99
+ - lib/hocon/impl/properties_parser.rb
61
100
  - lib/hocon/impl/resolve_status.rb
62
101
  - lib/hocon/impl/simple_config.rb
63
102
  - lib/hocon/impl/simple_config_list.rb
@@ -70,28 +109,33 @@ files:
70
109
  - lib/hocon/impl/tokenizer.rb
71
110
  - lib/hocon/impl/tokens.rb
72
111
  - lib/hocon/impl/unmergeable.rb
112
+ - lib/hocon/impl.rb
113
+ - lib/hocon.rb
114
+ - LICENSE
115
+ - README.md
73
116
  homepage: https://github.com/cprice404/ruby-hocon
74
117
  licenses:
75
118
  - Apache License, v2
76
- metadata: {}
77
119
  post_install_message:
78
120
  rdoc_options: []
79
121
  require_paths:
80
122
  - lib
81
123
  required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
82
125
  requirements:
83
- - - ">="
126
+ - - ! '>='
84
127
  - !ruby/object:Gem::Version
85
128
  version: '0'
86
129
  required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
87
131
  requirements:
88
- - - ">="
132
+ - - ! '>='
89
133
  - !ruby/object:Gem::Version
90
134
  version: '0'
91
135
  requirements: []
92
136
  rubyforge_project:
93
- rubygems_version: 2.2.2
137
+ rubygems_version: 1.8.23.2
94
138
  signing_key:
95
- specification_version: 4
139
+ specification_version: 3
96
140
  summary: HOCON Config Library
97
141
  test_files: []