Git Product home page Git Product logo

ruby-hocon's Introduction

ruby-hocon

Gem Version Build Status

This is a port of the Typesafe Config library to Ruby.

The library provides Ruby support for the HOCON configuration file format.

At present, it supports parsing and modification of existing HOCON/JSON files via the ConfigFactory class and the ConfigValueFactory class, and rendering parsed config objects back to a String (see examples below). It also supports the parsing and modification of HOCON/JSON files via ConfigDocumentFactory.

Note: While the project is production ready, since not all features in the Typesafe library are supported, you may still run into some issues. If you find a problem, feel free to open a github issue.

The implementation is intended to be as close to a line-for-line port as the two languages allow, in hopes of making it fairly easy to port over new changesets from the Java code base over time.

Support

For best results, if you find an issue with this library, please open an issue on our Jira issue tracker. Issues filed there tend to be more visible to the current maintainers than issues on the Github issue tracker.

Basic Usage

gem install hocon

To use the simple API, for reading config values:

require 'hocon'

conf = Hocon.load("myapp.conf")
puts "Here's a setting: #{conf["foo"]["bar"]["baz"]}"

By default, the simple API will determine the configuration file syntax/format based on the filename extension of the file; .conf will be interpreted as HOCON, .json will be interpreted as strict JSON, and any other extension will cause an error to be raised since the syntax is unknown. If you'd like to use a different file extension, you manually specify the syntax, like this:

require 'hocon'
require 'hocon/config_syntax'

conf = Hocon.load("myapp.blah", {:syntax => Hocon::ConfigSyntax::HOCON})

Supported values for :syntax are: JSON, CONF, and HOCON. (CONF and HOCON are aliases, and both map to the underlying HOCON syntax.)

To use the ConfigDocument API, if you need both read/write capability for modifying settings in a config file, or if you want to retain access to things like comments and line numbers:

require 'hocon/parser/config_document_factory'
require 'hocon/config_value_factory'

# The below 4 variables will all be ConfigDocument instances
doc = Hocon::Parser::ConfigDocumentFactory.parse_file("myapp.conf")
doc2 = doc.set_value("a.b", "[1, 2, 3, 4, 5]")
doc3 = doc.remove_value("a")
doc4 = doc.set_config_value("a.b", Hocon::ConfigValueFactory.from_any_ref([1, 2, 3, 4, 5]))

doc_has_value = doc.has_value?("a") # returns boolean
orig_doc_text = doc.render # returns string

Note that a ConfigDocument is used primarily for simple configuration manipulation while preserving whitespace and comments. As such, it is not powerful as the regular Config API, and will not resolve substitutions.

CLI Tool

The hocon gem comes bundles with a hocon command line tool which can be used to get and set values from hocon files

Usage: hocon [options] {get,set,unset} PATH [VALUE]

Example usages:
  hocon -i settings.conf -o new_settings.conf set some.nested.value 42
  hocon -f settings.conf set some.nested.value 42
  cat settings.conf | hocon get some.nested.value

Subcommands:
  get PATH - Returns the value at the given path
  set PATH VALUE - Sets or adds the given value at the given path
  unset PATH - Removes the value at the given path

Options:
    -i, --in-file HOCON_FILE         HOCON file to read/modify. If omitted, STDIN assumed
    -o, --out-file HOCON_FILE        File to be written to. If omitted, STDOUT assumed
    -f, --file HOCON_FILE            File to read/write to. Equivalent to setting -i/-o to the same file
    -j, --json                       Output values from the 'get' subcommand in json format
    -h, --help                       Show this message
    -v, --version                    Show version

CLI Examples

Basic Usage

$ cat settings.conf
{
  foo: bar
}

$ hocon -i settings.conf get foo
bar

$ hocon -i settings.conf set foo baz

$ cat settings.conf
{
  foo: baz
}

# Write to a different file
$ hocon -i settings.conf -o new_settings.conf set some.nested.value 42
$ cat new_settings.conf
{
  foo: bar
  some: {
    nested: {
      value: 42

    }
  }
}

# Write back to the same file
$ hocon -f settings.conf set some.nested.value 42
$ cat settings.conf
{
  foo: bar
  some: {
    nested: {
      value: 42

    }
  }
}

Complex Values

If you give set a properly formatted hocon dictionary or array, it will try to accept it

$ hocon -i settings.conf set foo "{one: [1, 2, 3], two: {hello: world}}"
{
  foo: {one: [1, 2, 3], two: {hello: world}}
}

Chaining

If --in-file or --out-file aren't specified, STDIN and STDOUT are used for the missing options. Therefore it's possible to chain hocon calls

$ cat settings.conf
{
  foo: bar
}

$ cat settings.conf | hocon set foo 42 | hocon set one.two three
{
  foo: 42
  one: {
    two: three
  }
}

JSON Output

Calls to the get subcommand will return the data in HOCON format by default, but setting the -j/--json flag will cause it to return a valid JSON object

$ cat settings.conf
foo: {
  bar: {
    baz: 42
  }
}

$ hocon -i settings.conf get foo --json
{
    "bar": {
        "baz": 42
    }
}

Testing

bundle install --path .bundle
bundle exec rspec spec

Unsupported Features

This supports many of the same things as the Java library, but there are some notable exceptions. Unsupported features include:

  • Non file includes
  • Loading resources from the class path or URLs
  • Properties files
  • Parsing anything other than files and strings
  • Duration and size settings
  • Java system properties

ruby-hocon's People

Contributors

call avatar camlow325 avatar ciprianbadescu avatar cprice404 avatar cthorn42 avatar dakatsuka avatar evgeni avatar haus avatar iristyle avatar janelu2 avatar jmccure avatar joshcooper avatar jpinsonault avatar justinstoller avatar kevincorcoran avatar lucywyman avatar magisus avatar mhashizume avatar mihaibuzgau avatar mslilah avatar mtasaka avatar mwbutcher avatar pcarlisle avatar puppetlabs-jenkins avatar rlinehan avatar steveax avatar theshanx avatar traylenator avatar tvpartytonight avatar waynr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ruby-hocon's Issues

No trailing newline breaks parser

If there is no trailing newline in a conf file (with v0.0.6) then the parser breaks:

gems/hocon-0.0.6/lib/hocon/impl/parser.rb:482:in `parse_object': uninitialized constant Hocon::Impl::Tokens::END (NameError)
from gems/hocon-0.0.6/lib/hocon/impl/parser.rb:581:in `parse'
from gems/hocon-0.0.6/lib/hocon/impl/parser.rb:973:in `parse'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:186:in `raw_parse_value_from_io'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:180:in `block in raw_parse_value'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:34:in `block in open'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:33:in `open'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:33:in `open'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:179:in `raw_parse_value'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:163:in `parse_value_from_origin'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:116:in `parse_value'
from gems/hocon-0.0.6/lib/hocon/impl/parseable.rb:99:in `parse'
from gems/hocon-0.0.6/lib/hocon/config_factory.rb:7:in `parse_file'

HOCON with empty array throws parse error

test {
    z {
        a: "text",
        b: [],
        c: 50
    }
}

in value for key 'test': List should have ended with ] or had a comma, instead had token: ':' (if you want ':' to be part of a string value, then double-quote it) (Hocon::ConfigError::ConfigParseError)
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:604:in parse_array' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:343:inparse_value'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:298:in consolidate_value_tokens' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:469:inparse_object'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:341:in parse_value' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:477:inparse_object'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:341:in parse_value' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:477:inparse_object'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:341:in parse_value' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:477:inparse_object'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:341:in parse_value' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:477:inparse_object'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:654:in parse' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parser.rb:880:inparse'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:149:in raw_parse_value_from_io' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:143:inblock in raw_parse_value'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:32:in block in open' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:31:inopen'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:31:in open' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:142:inraw_parse_value'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:126:in parse_value_from_origin' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:79:inparse_value'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/impl/parseable.rb:62:in parse' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.1/lib/hocon/config_factory.rb:7:inparse_file'
/Users/jaym/work/purchase-transformer/features/support/env.rb:27:in <top (required)>' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/rb_support/rb_language.rb:95:inload'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/rb_support/rb_language.rb:95:in load_code_file' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/runtime/support_code.rb:180:inload_file'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/runtime/support_code.rb:83:in block in load_files!' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/runtime/support_code.rb:82:ineach'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/runtime/support_code.rb:82:in load_files!' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/runtime.rb:184:inload_step_definitions'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/runtime.rb:42:in run!' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/lib/cucumber/cli/main.rb:47:inexecute!'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/cucumber-1.3.15/bin/cucumber:13:in <top (required)>' /Users/jaym/.rbenv/versions/2.0.0-p451/bin/cucumber:23:inload'
/Users/jaym/.rbenv/versions/2.0.0-p451/bin/cucumber:23:in `

'

Properties file

Isn't a properties file a valid HOCON? Is there any reason it is not supported? The HOCON parser should be able to parse a properties file, should it not?

rspec fails with ruby3.2.0preview3 and onwards due to File.exists? removal

With ruby 3.2.0 preview3 and onwards, rspec for ruby-hocon git head fails like:

$ rspec spec
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_document_spec.rb:11: warning: already initialized constant ConfigParseOptions
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_document_parser_spec.rb:10: warning: previous definition of ConfigParseOptions was here
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_document_spec.rb:12: warning: already initialized constant ConfigSyntax
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_document_parser_spec.rb:11: warning: previous definition of ConfigSyntax was here
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_value_spec.rb:26: warning: already initialized constant ConfigValueFactory
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_document_spec.rb:14: warning: previous definition of ConfigValueFactory was here
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/public_api_spec.rb:16: warning: already initialized constant ConfigFactory
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_value_spec.rb:27: warning: previous definition of ConfigFactory was here
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/public_api_spec.rb:17: warning: already initialized constant ConfigValueFactory
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_value_spec.rb:26: warning: previous definition of ConfigValueFactory was here
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/public_api_spec.rb:18: warning: already initialized constant SimpleConfigObject
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_value_spec.rb:15: warning: previous definition of SimpleConfigObject was here
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/public_api_spec.rb:19: warning: already initialized constant SimpleConfigList
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_value_spec.rb:16: warning: previous definition of SimpleConfigList was here

An error occurred while loading ./spec/unit/typesafe/config/public_api_spec.rb.
Failure/Error: elsif File.exists?(sibling)

NoMethodError:
  undefined method `exists?' for File:Class
# ./lib/hocon/impl/parseable.rb:445:in `relative_to'
# ./lib/hocon/impl/simple_include_context.rb:28:in `relative_to'
# ./lib/hocon/impl/simple_includer.rb:115:in `name_to_parseable'
# ./lib/hocon/impl/simple_includer.rb:136:in `from_basename'
# ./lib/hocon/impl/simple_includer.rb:63:in `include_without_fallback'
# ./lib/hocon/impl/simple_includer.rb:37:in `include'
# ./lib/hocon/impl/config_parser.rb:173:in `parse_include'
# ./lib/hocon/impl/config_parser.rb:224:in `parse_object'
# ./lib/hocon/impl/config_parser.rb:102:in `parse_value'
# ./lib/hocon/impl/config_parser.rb:254:in `parse_object'
# ./lib/hocon/impl/config_parser.rb:102:in `parse_value'
# ./lib/hocon/impl/config_parser.rb:396:in `block in parse'
# ./lib/hocon/impl/config_parser.rb:378:in `each'
# ./lib/hocon/impl/config_parser.rb:378:in `parse'
# ./lib/hocon/impl/config_parser.rb:43:in `parse'
# ./lib/hocon/impl/parseable.rb:251:in `raw_parse_value_from_io'
# ./lib/hocon/impl/parseable.rb:244:in `block in raw_parse_value'
# ./lib/hocon/impl/parseable.rb:417:in `block in open'
# ./lib/hocon/impl/parseable.rb:416:in `open'
# ./lib/hocon/impl/parseable.rb:416:in `open'
# ./lib/hocon/impl/parseable.rb:243:in `raw_parse_value'
# ./lib/hocon/impl/parseable.rb:179:in `parse_value_from_origin'
# ./lib/hocon/impl/parseable.rb:174:in `parse_value'
# ./lib/hocon/impl/parseable.rb:149:in `parse'
# ./lib/hocon/config_factory.rb:15:in `parse_file'
# ./spec/unit/typesafe/config/public_api_spec.rb:335:in `block (2 levels) in <top (required)>'
# ./spec/unit/typesafe/config/public_api_spec.rb:325:in `block in <top (required)>'
# ./spec/unit/typesafe/config/public_api_spec.rb:261:in `<top (required)>'
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/token_spec.rb:10: warning: already initialized constant Tokens
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/config_node_spec.rb:8: warning: previous definition of Tokens was here
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/tokenizer_spec.rb:10: warning: already initialized constant Tokens
/builddir/build/GIT/ruby-hocon/spec/unit/typesafe/config/token_spec.rb:10: warning: previous definition of Tokens was here


Finished in 0.00018 seconds (files took 0.57425 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

File.exists? is already deprecated since ruby2.1 and will be removed with ruby3.2.
https://github.com/ruby/ruby/blob/062c5a9e6b9337078bea65f5d2f136dc3e47509c/NEWS.md?plain=1#L390
https://bugs.ruby-lang.org/issues/17391

People are expected to simply replace this with File.exist?

Hocon file includes with puppet?

The readme says that file includes are unsupported. What does that mean? I found a implementation of file include at /lib/hocon/impl/simple_includer.rb. It seems to work if I use it in a ruby script, readhocon.rb:

require 'hocon'
conf = Hocon.load("hocon-test-with-include.conf")
puts "Here's the setting: #{conf}"

hocon-test-with-include.conf:

{
    include file("hocon-include.conf")
    include file("hocon-include.json")
}

hocon-include.conf:

{
    "j": "foo",
    "k": "bar",
    "l": "lo"
}

hocon-include.json:

{
    "l": "loo",
    "m": "mar"
}

Run script

> ruby readhocon.rb
Here's the setting: {"j"=>"foo", "k"=>"bar", "l"=>"loo", "m"=>"mar"}

But when I try to use includes with puppet it is not working. Puppet complains about missing parameters which are defined in an included file:

 Error while evaluating a Function Call, Class[Profile::Haproxy]: expects a value for parameter 'cluster_name'

Can I use HOCON file includes with puppet? Are there documentation or any examples about it?

Environment

  • Puppet 4.10.12
  • Debian Buster

Hocon.load takes exponential time to load with += seperator

Encountering a problem with Hocon.load and Hocon.ConfigFactory.load_file; when loading a file with more than 5 '+=' to a key it will take exponentially longer with each concatenation.

For example, a file with the following will take more than 30 minutes to load.

asdf.s += 'asdf'
asdf.s += 'asdf.asd.asd.afs.asf.a.sf.asf.asf.asf.a.sf.'
asdf.s += 'asdf.qwe.qwe.q.we.qwe.qw'
asdf.s += 'asdf.q.weqwe.qwe.qwe.'
asdf.s += 'asdf.qweq.weq.e.qwe.'
asdf.s += 'asdfqeqweqwe.qwe'
asdf.s += 'asdf.asfd.asf.af'
asdf.s += 'asd.asdfasff'
asdf.s += 'asdf.asdfsadf'
asdf.s += 'asd.aaaaaf'
asdf.s += 'asd.fdsgwer.f'
asdf.s += 'asdf.lkjp.poiu.zsdf'
asdf.s += 'asdf.erwerwer.werwer'
asdf.s += 'asdf.llfllwelpwelr.sdf'
asdf.s += 'asdf.asdfasdf'
asdf.s += 'asdf.sdfsdfppowerwer'
asdf.s += 'asdf.ggggg'

It looks like we can get away with converting that into an array, but I am wondering if this can be fixed?

Convert symbols to strings

Right now, if you supply a hash with symbols in it to ConfigValueFactory.parse:

irb(main):014:0> b = Hocon::ConfigValueFactory.from_map( { a: :b } )
Hocon::ConfigError::ConfigBugOrBrokenError: bug in method caller: not valid to create ConfigObject from map with non-String key: a
    from /Users/kwong/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hocon-0.9.2/lib/hocon/impl/config_impl.rb:119:in `block in from_any_ref_mode'
    from /Users/kwong/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hocon-0.9.2/lib/hocon/impl/config_impl.rb:117:in `each'
    from /Users/kwong/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hocon-0.9.2/lib/hocon/impl/config_impl.rb:117:in `from_any_ref_mode'
    from /Users/kwong/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hocon-0.9.2/lib/hocon/impl/config_impl.rb:72:in `from_any_ref'
    from /Users/kwong/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hocon-0.9.2/lib/hocon/config_value_factory.rb:72:in `from_map'
    from (irb):14
    from /Users/kwong/.rbenv/versions/2.1.2/bin/irb:11:in `<main>'

It gives an error. Can the symbols be automatically converted to Strings when converting to a HOCON document? Is there a downside of doing so?

No definition of entry_set

I can't find any implementation of entry_set or any other way to iterate over all the path-value pairs of a configuration.

Add a to_hocon method.

This gem currently doesn't seem to be able to contruct a file without loading a dummy file off disk. Most gems like json and yaml have a to_ style method that takes a ruby object like a hash and does a render on it. I think this would be a good idea to do this and mirror whatever .render does now by while accepting at least a hash.

URL support

Hello,

Would adding include by URL be as easy as implementing the include_url in the simple_includer.rb? I might be interested to see that happen, since we could retrieve files from S3 that way.

Hocon::Impl::TokenType.name causes "wrong number of arguments (0 for 1)" with specinfra gem

The hocon gem appears to be causing problems when used in Puppet beaker tests as the Hocon::Impl::TokenType.name method overrides the regular Class#name (well, Module#name) method.

The new method in https://github.com/puppetlabs/ruby-hocon/blob/0.9.5/lib/hocon/impl/token_type.rb#L24 requires an argument and doesn't return the class name.

This causes failures such as:

  5) postgresql::server::recovery should manage recovery File "/tmp/recovery.conf" should contain /restore_command = 'restore_command'/
     Failure/Error: it { is_expected.to contain /restore_command = 'restore_command'/ }
     ArgumentError:
       wrong number of arguments (0 for 1)

     # ./vendor/bundle/ruby/2.1.0/gems/hocon-0.9.5/lib/hocon/impl/token_type.rb:24:in `name'
     # ./vendor/bundle/ruby/2.1.0/gems/specinfra-2.59.1/lib/specinfra/ext/class.rb:5:in `block in subclasses'
     # ./vendor/bundle/ruby/2.1.0/gems/specinfra-2.59.1/lib/specinfra/ext/class.rb:4:in `each_object'
     # ./vendor/bundle/ruby/2.1.0/gems/specinfra-2.59.1/lib/specinfra/ext/class.rb:4:in `subclasses'
     # ./vendor/bundle/ruby/2.1.0/gems/beaker-rspec-5.4.0/lib/beaker-rspec/helpers/serverspec.rb:86:in `detect_os'

(https://travis-ci.org/puppetlabs/puppetlabs-postgresql/jobs/138923171)

as the specinfra gem tries to iterate over every class and calls .name: mizzy/specinfra@8d686d5.

It's probably best if the class method on TokenType is renamed to prevent overriding the regular .name method.

How to include a file?

I'm using ruby-hocon to construct a HOCON file.

Setting values is pretty straightforward, e.g. myconf.set_value("whatever.somewhere", "some_value"), but I'm having trouble tracking down how to set an include.

This is what I want the output to be:

top { 
  something {
    include "junk/something.conf"
  }
}

I've got this already:

doc = Hocon::Parser::ConfigDocumentFactory.parse_file("config/application.conf")

And #set_value and #set_config_value seem to work great for basic types, but I can't for the life of me figure out how to set an include within a key's context. I see that non-file includes are not supported, but have a feeling that includes are only supported in the context of reading config files, not writing them.

How can I accomplish this? I've pored over the docs and the code for about an hour and I'm coming up short ๐Ÿ˜ž

example load with substitutions

Calling Hocon.load on a file containing a substitution such as:

a = yes
b = ${a}

throws an error, and trying some other obvious things don't work either:

1- calling Hocon::ConfigFactory.parse_file(filename) works, but the SimpleConfig object that gets returned has no render method.

2- calling Hocon::Parser::ConfigDocumentFactory.parse_file(filename).resolve() works and has a render method, but does not have a resolve method, so the rendered string show the value of b as being ${a}.

What does work is:

Hocon::ConfigFactory.load_file('test.conf').root()

Which leads me to the issue:

Could the README or the API docs have a paragraph on how to parse config containing substitutions, resolve those substitutions, and then allow you to render the resolved config? Also, is there any reason why Hocon.load does not do this?

no exception if file doesn't exist

Describe the Bug

I'm currently writing a Puppet function that relies on Hocon. I noticed that Hocon can parse files but it will return an empty hash if the provided file doesn't exist:

root@puppet ~ # /opt/puppetlabs/puppet/bin/irb
irb(main):001:0> require 'hocon'
=> true
irb(main):002:0> Hocon.load('/tmp/foo.conwf')
/opt/puppetlabs/puppet/lib/ruby/vendor_gems/gems/hocon-1.4.0/lib/hocon.rb:23:in `load': Unrecognized file extension '.conwf' and no value provided for :syntax option (Hocon::ConfigError::ConfigParseError)
	from (irb):2:in `<main>'
	from /opt/puppetlabs/puppet/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
	from /opt/puppetlabs/puppet/bin/irb:25:in `load'
	from /opt/puppetlabs/puppet/bin/irb:25:in `<main>'
irb(main):003:0> Hocon.load('/tmp/foow.conf')
=> {}
irb(main):004:0>
root@puppet ~ # ls -la /tmp/foow.conf
ls: cannot access '/tmp/foow.conf': No such file or directory
root@puppet ~ #

Expected Behavior

The Hocon gem should raise an exception if a file is not found / is not readable. Now you cannot determine if the file doesn't exist or contains an empty hash.

Steps to Reproduce

  • require 'hocon'
  • Hocon.load('/non/existing/path')

Environment


*** LOCAL GEMS ***

hocon (1.4.0)
    Authors: Chris Price, Wayne Warren, Preben Ingvaldsen, Joe
    Pinsonault, Kevin Corcoran, Jane Lu
    Homepage: https://github.com/puppetlabs/ruby-hocon
    License: Apache License, v2
    Installed at: /opt/puppetlabs/puppet/lib/ruby/vendor_gems

    HOCON Config Library
root@puppet ~ #

Additional Context

Add any other context about the problem here.

Error when doing Hocon.load where there is a substitution

Config file example:
a.b {
c = ${x.y.z}
}

x {
y.z = 10 seconds
}

Error:
undefined local variable or method pull_substitution' for #<Hocon::Impl::Tokenizer::TokenIterator:0x007fe821fb7708> (NameError) /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/tokenizer.rb:309:inpull_next_token'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/tokenizer.rb:342:in queue_next_token' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/tokenizer.rb:354:innext'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parser.rb:842:in pop_token' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parser.rb:698:innext_token'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parser.rb:770:in check_element_separator' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parser.rb:536:inparse_object'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parser.rb:341:in parse_value' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parser.rb:477:inparse_object'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parser.rb:654:in parse' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parser.rb:880:inparse'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:186:in raw_parse_value_from_io' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:180:inblock in raw_parse_value'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:34:in block in open' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:33:inopen'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:33:in open' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:179:inraw_parse_value'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:163:in parse_value_from_origin' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:116:inparse_value'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/impl/parseable.rb:99:in parse' /Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon/config_factory.rb:7:inparse_file'
/Users/jaym/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/hocon-0.0.4/lib/hocon.rb:5:in `load'

CLI tool does not work with substition

Describe the Bug

Given a file test.conf

a = yes
b = ${a}

and the command

hocon -i test.conf get b

Gives the error

/opt/homebrew/lib/ruby/gems/3.2.0/gems/hocon-1.4.0/lib/hocon/impl/config_reference.rb:89:in `value_type': need to Config#resolve, see the API docs for Config#resolve; substitution not resolved: ConfigReference(${a}) (Hocon::ConfigError::ConfigNotResolvedError)
	from /opt/homebrew/lib/ruby/gems/3.2.0/gems/hocon-1.4.0/lib/hocon/impl/simple_config.rb:297:in `has_path?'
	from /opt/homebrew/lib/ruby/gems/3.2.0/gems/hocon-1.4.0/lib/hocon/cli.rb:135:in `do_get'
	from /opt/homebrew/lib/ruby/gems/3.2.0/gems/hocon-1.4.0/lib/hocon/cli.rb:116:in `main'
	from /opt/homebrew/lib/ruby/gems/3.2.0/gems/hocon-1.4.0/bin/hocon:4:in `<top (required)>'
	from /opt/homebrew/opt/ruby/bin/hocon:25:in `load'
	from /opt/homebrew/opt/ruby/bin/hocon:25:in `<main>'

Environment

  • Version
    • 1.4.0
  • Platform
    • macOS Ventura 13.5.1
    • ruby 3.2.2

Additional Context

This should be fixed according to #67. Not sure why it is not working under CLI mode.

How to print HOCON style string?

I tried to convert to HOCON style string from JSON, but I could not.
I thought that use it such as:

json = <<-EOS
{
  \"hoge\": \"fuga\"
}
EOS

Hocon.parse(json).to_hocon
#=> "\"hoge\" = \"fuga\""

Can you tell me how to do it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.