hocon 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,3 +23,11 @@ require 'hocon/config_factory'
23
23
  conf = Hocon::ConfigFactory.parse_file("myapp.conf")
24
24
  conf_map = conf.root.unwrapped
25
25
  ```
26
+
27
+ Testing
28
+ =======
29
+
30
+ ```sh
31
+ bundle install
32
+ bundle exec rake spec
33
+ ```
data/lib/hocon.rb CHANGED
@@ -1,6 +1,14 @@
1
- require 'hocon/config_factory'
2
-
3
1
  module Hocon
2
+ require 'hocon/config_factory'
3
+ require 'hocon/config_parse_options'
4
+ require 'hocon/config_error'
5
+ require 'hocon/config_object'
6
+ require 'hocon/config_parse_options'
7
+ require 'hocon/config_render_options'
8
+ require 'hocon/config_syntax'
9
+ require 'hocon/config_value_factory'
10
+ require 'hocon/config_value_type'
11
+
4
12
  def self.load(file)
5
13
  config = Hocon::ConfigFactory.parse_file(file)
6
14
  return config.root.unwrapped
@@ -1,15 +1,37 @@
1
- module Hocon
2
- class ConfigError < StandardError
3
- def initialize(origin, message, cause)
4
- super(message)
5
- @origin = origin
6
- @cause = cause
7
- end
1
+ require 'hocon'
2
+
3
+ class Hocon::ConfigError < StandardError
4
+ def initialize(origin, message, cause)
5
+ super(message)
6
+ @origin = origin
7
+ @cause = cause
8
+ end
8
9
 
9
- class ConfigParseError < Hocon::ConfigError
10
+ class ConfigMissingError < Hocon::ConfigError
11
+ end
12
+
13
+ class ConfigNullError < Hocon::ConfigError::ConfigMissingError
14
+ def self.make_message(path, expected)
15
+ if not expected.nil?
16
+ "Configuration key '#{path}' is set to nil but expected #{expected}"
17
+ else
18
+ "Configuration key '#{path}' is nil"
19
+ end
10
20
  end
21
+ end
11
22
 
12
- class ConfigWrongTypeError < Hocon::ConfigError
23
+ class ConfigParseError < Hocon::ConfigError
24
+ end
25
+
26
+ class ConfigWrongTypeError < Hocon::ConfigError
27
+ end
28
+
29
+ class ConfigBugOrBrokenError < Hocon::ConfigError
30
+ def initialize(message, cause)
31
+ super(nil, message, cause)
13
32
  end
14
33
  end
34
+
35
+ class ConfigNotResolvedError < Hocon::ConfigError::ConfigBugOrBrokenError
36
+ end
15
37
  end
@@ -1,14 +1,12 @@
1
- require 'hocon/config_parse_options'
1
+ require 'hocon'
2
2
  require 'hocon/impl/parseable'
3
3
 
4
- module Hocon
5
- class ConfigFactory
6
- def self.parse_file(file_path, options = Hocon::ConfigParseOptions.defaults)
7
- Hocon::Impl::Parseable.new_file(file_path, options).parse.to_config
8
- end
4
+ class Hocon::ConfigFactory
5
+ def self.parse_file(file_path, options = Hocon::ConfigParseOptions.defaults)
6
+ Hocon::Impl::Parseable.new_file(file_path, options).parse.to_config
7
+ end
9
8
 
10
- def self.parse_string(string, options = Hocon::ConfigParseOptions.defaults)
11
- Hocon::Impl::Parseable.new_string(string, options).parse.to_config
12
- end
9
+ def self.parse_string(string, options = Hocon::ConfigParseOptions.defaults)
10
+ Hocon::Impl::Parseable.new_string(string, options).parse.to_config
13
11
  end
14
12
  end
@@ -1,4 +1,4 @@
1
- module Hocon
2
- module ConfigObject
3
- end
4
- end
1
+ require 'hocon'
2
+
3
+ module Hocon::ConfigObject
4
+ end
@@ -1,53 +1,53 @@
1
- module Hocon
2
- class ConfigParseOptions
3
- attr_accessor :syntax, :origin_description, :allow_missing, :includer
1
+ require 'hocon'
4
2
 
5
- def self.defaults
6
- self.new(nil, nil, true, nil)
7
- end
3
+ class Hocon::ConfigParseOptions
4
+ attr_accessor :syntax, :origin_description, :allow_missing, :includer
8
5
 
9
- def initialize(syntax, origin_description, allow_missing, includer)
10
- @syntax = syntax
11
- @origin_description = origin_description
12
- @allow_missing = allow_missing
13
- @includer = includer
14
- end
6
+ def self.defaults
7
+ self.new(nil, nil, true, nil)
8
+ end
15
9
 
16
- def allow_missing?
17
- @allow_missing
18
- end
10
+ def initialize(syntax, origin_description, allow_missing, includer)
11
+ @syntax = syntax
12
+ @origin_description = origin_description
13
+ @allow_missing = allow_missing
14
+ @includer = includer
15
+ end
19
16
 
20
- def with_syntax(syntax)
21
- if @syntax == syntax
22
- self
23
- else
24
- Hocon::ConfigParseOptions.new(syntax,
25
- @origin_description,
26
- @allow_missing,
27
- @includer)
28
- end
29
- end
17
+ def allow_missing?
18
+ @allow_missing
19
+ end
30
20
 
31
- def with_includer(includer)
32
- if @includer == includer
33
- self
34
- else
35
- Hocon::ConfigParseOptions.new(@syntax,
36
- @origin_description,
37
- @allow_missing,
38
- includer)
39
- end
21
+ def with_syntax(syntax)
22
+ if @syntax == syntax
23
+ self
24
+ else
25
+ Hocon::ConfigParseOptions.new(syntax,
26
+ @origin_description,
27
+ @allow_missing,
28
+ @includer)
40
29
  end
30
+ end
41
31
 
42
- def append_includer(includer)
43
- if @includer == includer
44
- self
45
- elsif @includer
46
- with_includer(@includer.with_fallback(includer))
47
- else
48
- with_includer(includer)
49
- end
32
+ def with_includer(includer)
33
+ if @includer == includer
34
+ self
35
+ else
36
+ Hocon::ConfigParseOptions.new(@syntax,
37
+ @origin_description,
38
+ @allow_missing,
39
+ includer)
50
40
  end
41
+ end
51
42
 
43
+ def append_includer(includer)
44
+ if @includer == includer
45
+ self
46
+ elsif @includer
47
+ with_includer(@includer.with_fallback(includer))
48
+ else
49
+ with_includer(includer)
50
+ end
52
51
  end
53
- end
52
+
53
+ end
@@ -1,46 +1,46 @@
1
- module Hocon
2
- class ConfigRenderOptions
3
- def initialize(origin_comments, comments, formatted, json)
4
- @origin_comments = origin_comments
5
- @comments = comments
6
- @formatted = formatted
7
- @json = json
8
- end
1
+ require 'hocon'
9
2
 
10
- attr_writer :origin_comments, :comments, :formatted, :json
3
+ class Hocon::ConfigRenderOptions
4
+ def initialize(origin_comments, comments, formatted, json)
5
+ @origin_comments = origin_comments
6
+ @comments = comments
7
+ @formatted = formatted
8
+ @json = json
9
+ end
11
10
 
12
- def origin_comments?
13
- @origin_comments
14
- end
15
- def comments?
16
- @comments
17
- end
18
- def formatted?
19
- @formatted
20
- end
21
- def json?
22
- @json
23
- end
11
+ attr_writer :origin_comments, :comments, :formatted, :json
24
12
 
25
- #
26
- # Returns the default render options which are verbose (commented and
27
- # formatted). See {@link ConfigRenderOptions#concise} for stripped-down
28
- # options. This rendering will not be valid JSON since it has comments.
29
- #
30
- # @return the default render options
31
- #
32
- def self.defaults
33
- ConfigRenderOptions.new(true, true, true, true)
34
- end
13
+ def origin_comments?
14
+ @origin_comments
15
+ end
16
+ def comments?
17
+ @comments
18
+ end
19
+ def formatted?
20
+ @formatted
21
+ end
22
+ def json?
23
+ @json
24
+ end
25
+
26
+ #
27
+ # Returns the default render options which are verbose (commented and
28
+ # formatted). See {@link ConfigRenderOptions#concise} for stripped-down
29
+ # options. This rendering will not be valid JSON since it has comments.
30
+ #
31
+ # @return the default render options
32
+ #
33
+ def self.defaults
34
+ Hocon::ConfigRenderOptions.new(true, true, true, true)
35
+ end
35
36
 
36
- #
37
- # Returns concise render options (no whitespace or comments). For a
38
- # resolved {@link Config}, the concise rendering will be valid JSON.
39
- #
40
- # @return the concise render options
41
- #
42
- def self.concise
43
- ConfigRenderOptions.new(false, false, false, true)
44
- end
37
+ #
38
+ # Returns concise render options (no whitespace or comments). For a
39
+ # resolved {@link Config}, the concise rendering will be valid JSON.
40
+ #
41
+ # @return the concise render options
42
+ #
43
+ def self.concise
44
+ Hocon::ConfigRenderOptions.new(false, false, false, true)
45
45
  end
46
- end
46
+ end
@@ -1,7 +1,7 @@
1
- module Hocon
2
- module ConfigSyntax
3
- JSON = 0
4
- CONF = 1
5
- PROPERTIES = 2
6
- end
7
- end
1
+ require 'hocon'
2
+
3
+ module Hocon::ConfigSyntax
4
+ JSON = 0
5
+ CONF = 1
6
+ PROPERTIES = 2
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'hocon'
2
+ require 'hocon/impl/config_impl'
3
+
4
+ class Hocon::ConfigValueFactory
5
+ ConfigImpl = Hocon::Impl::ConfigImpl
6
+
7
+ def self.from_any_ref(object, origin_description)
8
+ ConfigImpl.from_any_ref(object, origin_description)
9
+ end
10
+ end
@@ -1,26 +1,26 @@
1
- module Hocon
2
- #
3
- # The type of a configuration value (following the <a
4
- # href="http://json.org">JSON</a> type schema).
5
- #
6
- module ConfigValueType
7
- OBJECT = 0
8
- LIST = 1
9
- NUMBER = 2
10
- BOOLEAN = 3
11
- NULL = 4
12
- STRING = 5
1
+ require 'hocon'
13
2
 
14
- def self.name(config_value_type)
15
- case config_value_type
16
- when OBJECT then "OBJECT"
17
- when LIST then "LIST"
18
- when NUMBER then "NUMBER"
19
- when BOOLEAN then "BOOLEAN"
20
- when NULL then "NULL"
21
- when STRING then "STRING"
22
- else raise ConfigBugError, "Unrecognized value type '#{config_value_type}'"
23
- end
3
+ #
4
+ # The type of a configuration value (following the <a
5
+ # href="http://json.org">JSON</a> type schema).
6
+ #
7
+ module Hocon::ConfigValueType
8
+ OBJECT = 0
9
+ LIST = 1
10
+ NUMBER = 2
11
+ BOOLEAN = 3
12
+ NULL = 4
13
+ STRING = 5
14
+
15
+ def self.name(config_value_type)
16
+ case config_value_type
17
+ when OBJECT then "OBJECT"
18
+ when LIST then "LIST"
19
+ when NUMBER then "NUMBER"
20
+ when BOOLEAN then "BOOLEAN"
21
+ when NULL then "NULL"
22
+ when STRING then "STRING"
23
+ else raise ConfigBugError, "Unrecognized value type '#{config_value_type}'"
24
24
  end
25
25
  end
26
- end
26
+ end
data/lib/hocon/impl.rb CHANGED
@@ -1,4 +1,5 @@
1
- module Hocon
2
- module Impl
3
- end
4
- end
1
+ require 'hocon'
2
+
3
+ module Hocon::Impl
4
+
5
+ end
@@ -5,10 +5,15 @@ require 'hocon/config_object'
5
5
  require 'hocon/config_value_type'
6
6
  require 'hocon/impl/resolve_status'
7
7
  require 'hocon/impl/simple_config_origin'
8
+ require 'hocon/config_error'
9
+ require 'hocon/impl/config_impl'
8
10
 
9
11
  class Hocon::Impl::AbstractConfigObject < Hocon::Impl::AbstractConfigValue
10
12
  include Hocon::ConfigObject
11
13
 
14
+ ConfigBugOrBrokenError = Hocon::ConfigError::ConfigBugOrBrokenError
15
+ ConfigNotResolvedError = Hocon::ConfigError::ConfigNotResolvedError
16
+
12
17
  def initialize(origin)
13
18
  super(origin)
14
19
  @config = Hocon::Impl::SimpleConfig.new(self)
@@ -61,4 +66,48 @@ class Hocon::Impl::AbstractConfigObject < Hocon::Impl::AbstractConfigValue
61
66
 
62
67
  Hocon::Impl::SimpleConfigOrigin.merge_origins(origins)
63
68
  end
69
+
70
+ def peek_assuming_resolved(key, original_path)
71
+ begin
72
+ attempt_peek_with_partial_resolve(key)
73
+ rescue ConfigNotResolvedError => e
74
+ raise Hocon::Impl::ConfigImpl.improve_not_resolved(original_path, e)
75
+ end
76
+ end
77
+
78
+ def peek_path(path)
79
+ begin
80
+ return peek_path_impl(self, path, nil)
81
+ rescue ConfigNotResolvedError => e
82
+ raise ConfigBugOrBrokenError.new("NotPossibleToResolve happened though we had no ResolveContext in peek_path", nil)
83
+ end
84
+ end
85
+
86
+ def peek_path_impl(me, path, context)
87
+ begin
88
+ if not context.nil?
89
+ partially_resolved = context.restrict(path).resolve(me)
90
+ if partially_resolved.is_a?(self.class)
91
+ return peek_path_impl(partially_resolved, path, nil)
92
+ else
93
+ raise ConfigBugOrBrokenError.new(
94
+ "resolved object to non-object #{me} to #{partially_resolved}", nil)
95
+ end
96
+ else
97
+ remainder = path.remainder
98
+ v = me.attempt_peek_with_partial_resolve(path.first)
99
+ if remainder.nil?
100
+ return v
101
+ else
102
+ if v.is_a?(self.class)
103
+ return peek_path_impl(v, remainder, nil)
104
+ else
105
+ return nil
106
+ end
107
+ end
108
+ end
109
+ rescue ConfigNotResolvedError => e
110
+ raise Hocon::Impl::ConfigImpl.improve_not_resolved(path, e)
111
+ end
112
+ end
64
113
  end