Git Product home page Git Product logo

toml's Introduction

TOML

A Ruby parser for TOML, built on parslet.

This is far superior to YAML and JSON because it doesn't suck. Really it doesn't.

Gem Version

Usage

Install this library:

gem "toml", "~> 0.3.0"
gem install "toml"

It's simple, really.

content = <<-TOML

# Hello, this is an example.
[things]
other = "things"
what = 900000

TOML

parser = TOML::Parser.new(content).parsed
# => { "things" => { "other" => "things", "what" => 900000 } }

You can also use the same API as YAML if you'd like:

TOML.load("thing = 9")
# => {"thing" => 9}

TOML.load_file("my_file.toml")
# => {"whatever" => "keys"}

In case a syntax error occurs, the parser will raise a Parslet::ParseFailed exception.

There's also a beta feature for generating a TOML file from a Ruby hash. Please note this will likely not give beautiful output right now.

hash = {
  "integer" => 1,
  "float" => 3.14159,
  "true" => true,
  "false" => false,
  "string" => "hi",
  "array" => [[1], [2], [3]],
  "key" => {
    "group" => {
      "value" => "lol"
    }
  }
}
doc = TOML::Generator.new(hash).body
# doc will be a string containing a proper TOML document.

Contributors

Written by Jeremy McAnally (@jm) and Dirk Gadsden (@dirk) based on TOML from Tom Preston-Werner (@mojombo).

toml's People

Contributors

anatol avatar antekone avatar chrisbloom7 avatar dirk avatar dromazmj avatar jm avatar jroes avatar kolen avatar koraktor avatar leereilly avatar merovius avatar mhmoudgmal avatar parkr avatar paulhammond avatar ress avatar rjocoleman avatar samaaron avatar samsonjs avatar sandstrom avatar stevenhaddox avatar thewatts 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  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

Watchers

 avatar  avatar  avatar  avatar

toml's Issues

parsing fails on empty multiline arrays

hi, i'm writing an example/default config with comments in toml, and it seems to choke your parser. i'm using toml 0.0.4.

[example]
  empty_array = [] # works
  empty_multi_line_array = [
           # fails
  ]

The exception i get is:

Failed to match sequence (ALL_SPACE (KEY_GROUP / KEY_VALUE / COMMENT_LINE){0, } ALL_SPACE) at line 2 char 3.
`- Extra input after last repetition at line 2 char 3.
   `- Failed to match [ \t\r\n] at line 2 char 3.

i believe it is valid TOML, per the spec:
"Arrays can also be multiline. So in addition to ignoring whitespace, arrays also ignore newlines between the brackets. Terminating commas are ok before the closing bracket."

i'm interested in digging into the parser/grammar myself and making a PR, but before I did I just wanted to make sure this was a valid issue and that you would be interested in a fix

Failed to parse numbers with underscores

Hi.

It is said here that

For large numbers, you may use underscores to enhance readability

but it seems to be broken.

content = <<-TOML
foo = 10_000_000
TOML
parser = TOML::Parser.new(content).parsed

Failed to match sequence (ALL_SPACE (TABLE / TABLE_ARRAY / KEY_VALUE / COMMENT_LINE){0, } ALL_SPACE) at line 1 char 1.
`- Don't know what to do with "foo = 10_0" at line 1 char 1.

Is it parslet related issue?

Given a hash, output toml

It's great to be able to parse TOML, but I'd also like the ability to write out TOML. Any plans to support this?

Array of table doesn't work

Trying to convert an array of table and an error was raised:

irb(main):001:0> require "toml"
=> true
irb(main):006:0> hash = { "credentials" => [ {"host" => "127.0.0.1"} ] }
=> {"credentials"=>[{"host"=>"127.0.0.1"}]}
irb(main):007:0> TOML::Generator.new(hash).body
NoMethodError: undefined method `to_toml' for {"host"=>"127.0.0.1"}:Hash
        from /opt/boxen/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/toml-0.1.1/lib/toml/monkey_patch.rb:19:in `block in to_toml'
        from /opt/boxen/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/toml-0.1.1/lib/toml/monkey_patch.rb:19:in `map'
        from /opt/boxen/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/toml-0.1.1/lib/toml/monkey_patch.rb:19:in `to_toml'
        from /opt/boxen/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/toml-0.1.1/lib/toml/generator.rb:73:in `format'
        from /opt/boxen/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/toml-0.1.1/lib/toml/generator.rb:55:in `block in visit'
        from /opt/boxen/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/toml-0.1.1/lib/toml/generator.rb:49:in `each'
        from /opt/boxen/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/toml-0.1.1/lib/toml/generator.rb:49:in `visit'
        from /opt/boxen/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/toml-0.1.1/lib/toml/generator.rb:14:in `initialize'
        from (irb):7:in `new'
        from (irb):7
        from /opt/boxen/rbenv/versions/2.1.0/bin/irb:11:in `<main>'

Why is this the default TOML gem on rubygems.org?

This appears to be neither up-to-date, nor the original TOML gem. Is there a reason for this to be the default gem given by rubygems.org when someone puts gem 'toml' in their Gemfile?

If not, is there a way for the author(s) of this gem to correct the entry on rubygems.org, or does that matter need to be brought up elsewhere?

If it needs to be taken up elsewhere, would I have the support of this gems author(s) if I request that the gem under toml-lang be used instead?

Nest arrays are parsed improperly

Issue discovered using toml-test; see the test methodology and output in this gist.

I believe there are two (related?) errors when parsing nested arrays.

#  Shouldn't this produce an Array?
TOML.load("a = [[]]")["a"][0].class # => "Parslet::Slice"

# Extra nesting levels are discarded
TOML.load("a = [[[[1]]]]")["a"].inspect # => "[1]"

Further support?

Ahoy! We'd like to add TOML configuration file support to Jekyll (jekyll/jekyll#1765) but we're not sure which Ruby parser to use. Is this a library you're willing to continue to support for our use? Do you need any other support to make this parser even more amazing? โค๏ธ

parsing breaks on multi-line strings

This gem is breaking for me when I try to parse a multi-line string. For example:

irb(main):020:0> text = <<EOT
irb(main):021:0" [foo]
irb(main):022:0" 
irb(main):023:0"   bar = '''
irb(main):024:0" baz
irb(main):025:0" '''
irb(main):026:0" EOT
=> "[foo]\n\n  bar = '''\nbaz\n'''\n"
irb(main):027:0> TOML.load(text)
NoMethodError: undefined method `ascii_tree' for nil:NilClass
from /Library/Ruby/Gems/2.3.0/gems/toml-0.2.0/lib/toml/parser.rb:12:in
      `rescue in initialize'
from /Library/Ruby/Gems/2.3.0/gems/toml-0.2.0/lib/toml/parser.rb:9:in `initialize'
from /Library/Ruby/Gems/2.3.0/gems/toml-0.2.0/lib/toml.rb:19:in `new'
from /Library/Ruby/Gems/2.3.0/gems/toml-0.2.0/lib/toml.rb:19:in `load'
from (irb):27
from /usr/bin/irb:11:in `<main>'

The problem occurs with either ''' or """ strings and with both load and load_file.
Here is some environment info:

$ gem list | grep toml
toml (0.2.0)
$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]

Failure at \n (newline)

Hi! This is a part of a scheme that I'm working with (

[section.subsection]
tab = false
approximate = true
cost =
currency =
leads =

and here is the error

TOML.load_file("schema.toml")
Failed to match sequence (ALL_SPACE (TABLE / TABLE_ARRAY / KEY_VALUE / COMMENT_LINE){0, } ALL_SPACE) at line 16 char 1.
`- Don't know what to do with "cost = \ncu" at line 16 char 1.

If you will not be able reproduce the error I will send an actual file

Parse error for any file / content with Ruby 1.9.3-p392 & toml v0.0.2

$ bundle show toml
Resolving dependencies...
/Users/steven/.rvm/gems/ruby-1.9.3-p392/gems/toml-0.0.2
$ cat config/support.toml
pub_key_path = "$HOME/id_dsa.pub"
$ b ruby -e "require 'toml';TOML.load('config/support.toml');"
/Users/steven/.rvm/gems/ruby-1.9.3-p392/gems/parslet-1.5.0/lib/parslet/cause.rb:63:in `raise': Extra input after last repetition at line 1 char 1. (Parslet::ParseFailed)
    from /Users/steven/.rvm/gems/ruby-1.9.3-p392/gems/parslet-1.5.0/lib/parslet/atoms/base.rb:46:in `parse'
    from /Users/steven/.rvm/gems/ruby-1.9.3-p392/gems/toml-0.0.2/lib/toml/parser.rb:9:in `initialize'
    from /Users/steven/.rvm/gems/ruby-1.9.3-p392/gems/toml-0.0.2/lib/toml.rb:16:in `new'
    from /Users/steven/.rvm/gems/ruby-1.9.3-p392/gems/toml-0.0.2/lib/toml.rb:16:in `load'
    from -e:1:in `<main>'

Can't parse multiline string

In _config.toml :

description = """
 Write an awesome description for your new site here. You can edit this 
 line in _config.toml. It will appear in your document head meta (for 
 Google search results) and in your feed.xml site description."""

TOML.load_file("_config.toml")

Failed to match sequence (ALL_SPACE (TABLE / TABLE_ARRAY / KEY_VALUE / COMMENT_LINE){0, } ALL_SPACE) at line 1 char 1.
`- Don't know what to do with "descriptio" at line 1 char 1.
=> {}

I tried three single quotes and three double quotes , it doesn't work ๐Ÿ˜•

Fails on dots in hash keys

To replicate:

$ data = {"foo.bar" => "value" }
$ File.write('test.tml',  TOML::Generator.new(data).body)
$ TOML.load_file("test.tml")
Failed to match sequence (ALL_SPACE (KEY_GROUP / KEY_VALUE / COMMENT_LINE){0, } ALL_SPACE) at line 1 char 1.
`- Don't know what to do with "foo.bar = " at line 1 char 1.
NoMethodError: undefined method `each' for nil:NilClass
  from /Users/username/.rvm/gems/ruby-1.9.3-p448@gemset/gems/toml-0.0.4/lib/toml/parser.rb:21:in `initialize'
  from /Users/username/.rvm/gems/ruby-1.9.3-p448@gemset/gems/toml-0.0.4/lib/toml.rb:24:in `new'
  from /Users/username/.rvm/gems/ruby-1.9.3-p448@gemset/gems/toml-0.0.4/lib/toml.rb:24:in `load_file'
  from (irb):8
  from /Users/username/.rvm/rubies/ruby-1.9.3-p448/bin/irb:16:in `<main>'

Strings containing dots as keys are misgenerated into invalid TOML and not parsed properly.

Title says it all: If the key contains dots / periods / whatever you wanna call them, the generator outputs something like this.

Behaviour:
TOML::Generator.new({"my.string" => "some.thing"}).body # => "my.string = 'some.thing'"

Expected behaviour:
TOML::Generator.new({"my.string" => "some.thing"}).body # => "'my.string' = 'some.thing'"

Parsing actually valid files following the specs for toml which contain these strings also throw errors.

Can't serialize empty arrays

> TOML::Generator.new({:foo => []})
RuntimeError: All array values must be the same type

The array map here will return an array of length 0

Quoted keys are not un-quoted

Given the input file...

[foo]
  "bar" = "baz"

... The key bar is returned as "bar". To make this a bit clearer:

require "toml"
pp TOML.load(<<'__eos__')
[foo]
  "bar" = "baz"
__eos__
# {"foo"=>{"\"bar\""=>"baz"}}

The returned hash includes the key bar erroneously still wrapped in quotes.

Parser fails when config contains array of tables with literal strings

Hello.

Following minimal file fails to parse:

[[test]]
foo = 'bar'
irb(main):017:0> TOML.load_file("test.toml")
/home/bs/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/parslet-2.0.0/lib/parslet/cause.rb:70:in `raise': Failed to match sequence (ALL_SPACE (COMMENT_LINE / TABLE / TABLE_ARRAY / KEY_VALUE){0, } ALL_SPACE) at line 2 char 1. (Parslet::ParseFailed)

When i quote bar with double quotes it works. I want to use single quotes because i need literal strings (no escaping).
Im using Version 0.3.0.

Is not able to parse hard example?

After error with my file I've tried to parse an https://github.com/mojombo/toml/blob/master/tests/hard_example.toml and its failed

TOML.load_file('hard_example.toml')
Failed to match sequence (ALL_SPACE (KEY_GROUP / KEY_VALUE / COMMENT_LINE){0, } ALL_SPACE) at line 18 char 9.
`- Extra input after last repetition at line 18 char 9.
   `- Failed to match [ \t\r\n] at line 18 char 9.
NoMethodError: undefined method `each' for nil:NilClass
    from /home/lessless/.rvm/gems/ruby-2.0.0-p247/gems/toml-0.0.4/lib/toml/parser.rb:21:in `initialize'
    from /home/lessless/.rvm/gems/ruby-2.0.0-p247/gems/toml-0.0.4/lib/toml.rb:24:in `new'
    from /home/lessless/.rvm/gems/ruby-2.0.0-p247/gems/toml-0.0.4/lib/toml.rb:24:in `load_file'
    from (irb):2
    from /home/lessless/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'

Is this gem broken?

Comments with no space are parsed incorrectly

It seems like the parser is having trouble with comments which are not followed by spaces before the next value. See below:

>> TOML.load("Host = 6")
=> {"Host"=>6}
>> TOML.load("#Host = 6")
=> {"#Host"=>6}
>> TOML.load("#\nHost = 6")
=> {"#\nHost"=>6}
>> TOML.load("#\nHost = 6")
=> {"#\nHost"=>6}
>> TOML.load("#\n Host = 6")
=> {"Host"=>6}
>> TOML.load("# \nHost = 6")
=> {"Host"=>6}

Parsing with CRLF EOL's sometimes fail (on WSL2/Linux)

This problem occurs on Linux/WSL2 only, running the test on native Windows, all examples are parsed correctly. Seems related to issue #13: "Add support for CR and CRLF newlines".

With CRLF line ending this parses correctly:

[test]
key = "test"

This fails with CRLF-endings, but parses correctly with LF (unixy) line endings.

[[test]]
key = "test"
TOML.load_file('/home/eflukx/delme.toml')"
Traceback (most recent call last):
        5: from -e:1:in `<main>'
        4: from /home/eflukx/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/toml-0.3.0/lib/toml.rb:23:in `load_file'
        3: from /home/eflukx/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/toml-0.3.0/lib/toml.rb:23:in `new'
        2: from /home/eflukx/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/toml-0.3.0/lib/toml/parser.rb:9:in `initialize'
        1: from /home/eflukx/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parslet-2.0.0/lib/parslet/atoms/base.rb:49:in `parse'
/home/eflukx/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parslet-2.0.0/lib/parslet/cause.rb:70:in `raise': Failed to match sequence (ALL_SPACE (COMMENT_LINE / TABLE / TABLE_ARRAY / KEY_VALUE){0, } ALL_SPACE) at line 1 char 1. (Parslet::ParseFailed)

`#@` is getting (erroneously?) escaped

Here's a minimal test case:

require 'toml'
TOML::Generator.inject!
puts({a:{c:"#@"}}.to_toml)

This prints:


[a]
c = "\#@"

I'd expect to see "#@" with no backslash.

Are curly brackets a correct and supported syntax?

Hi,

I'm wondering whether hashes are part of the correct syntax, or they have been misused in this example?

[dependencies]
libc = "0.2.10"
pnet = "0.8.1"
mysql = { version = "2.2.1", default-features = false, features = ["socket"] }

Ref: https://raw.githubusercontent.com/AgilData/gibbs-mysql-spyglass/master/Cargo.toml

Trying to parse this file fails with the following errors:

Failed to match sequence (ALL_SPACE (TABLE / TABLE_ARRAY / KEY_VALUE / COMMENT_LINE){0, } ALL_SPACE) at line 9 char 1.
`- Don't know what to do with "mysql = { " at line 9 char 1.

Semantic Versioning?

Does your project use the Semantic Versioning system? (semver.org) Because if it does, you should already be at 1.0.0-; one of my Ruby projects has your gem as one of its dependencies. It would be much more convenient for me (and for others) if you used SemVer.

Question: How do I make variables out of the TOML file?

This is what I have so far:

user.toml:

[user]
name = "guest"
password = "test"

main.rb:

#!/usr/bin/env ruby
require 'toml'

def clear
	Gem.win_platform? ? (system "cls") : (system "clear")
end

clear
userData = TOML.load_file("user.toml")
puts userData

Then I get the output, {"user"=>{"name"=>"guest", "password"=>"test"}}
How am I supposed to parse this, so I could just do puts user.name and get guest?

Error while parsing valid toml

Reproduction:

cd /tmp
cat > foo.toml <<EOF
api = "0.4"

[buildpack]
id = "heroku/java-function"
version = "0.3.21"
name = "Java Function"
homepage = "https://github.com/heroku/buildpacks-jvm"
keywords = ["java", "function"]

[[licenses]]
type = "MIT"

[[order]]

[[order.group]]
id = "heroku/jvm"
version = "0.1.8"

[[order.group]]
id = "heroku/maven"
version = "0.2.5"

[[order.group]]
id = "heroku/jvm-function-invoker"
version = "0.5.3"

[metadata]

[metadata.release]

[metadata.release.docker]
repository = "public.ecr.aws/heroku-buildpacks/heroku-java-function-buildpack"
EOF

gem install toml
ruby -rtoml -e "TOML.load(File.read('foo.toml'))"

Expected: Toml to be parsed
Actual:

/Users/rschneeman/.gem/ruby/3.0.2/gems/toml-0.3.0/lib/toml/parser.rb:57:in `resolve_table_array': undefined method `has_key?' for nil:NilClass (NoMethodError)
	from /Users/rschneeman/.gem/ruby/3.0.2/gems/toml-0.3.0/lib/toml/parser.rb:37:in `block in initialize'
	from /Users/rschneeman/.gem/ruby/3.0.2/gems/toml-0.3.0/lib/toml/parser.rb:16:in `each'
	from /Users/rschneeman/.gem/ruby/3.0.2/gems/toml-0.3.0/lib/toml/parser.rb:16:in `initialize'
	from /Users/rschneeman/.gem/ruby/3.0.2/gems/toml-0.3.0/lib/toml.rb:19:in `new'
	from /Users/rschneeman/.gem/ruby/3.0.2/gems/toml-0.3.0/lib/toml.rb:19:in `load'
	from -e:1:in `<main>'

Ruby: 3.0.2
I checked the toml on disk is valid with https://www.toml-lint.com/

Too accepting of malformed input

Thanks for the great gem. I'm looking forward to the next gem release because I'm using the git version to get TOML generation.

This parser accepts several kinds of input specifically described as invalid at https://github.com/mojombo/toml. I discovered these errors using toml-test; see the test methodology and output in this gist.

I ran tests against the latest head of master (4793537)

  1. Accepts arrays with mixed types, which are not valid toml.

    Spec:

    No, you can't mix data types, that's stupid.

    Example TOML:

    arrays-and-ints =  [1, ["Arrays are not integers."]]
  2. Accepts mutually duplicated keys/keygroups.

    Spec:

    Be careful not to overwrite previous keys. That's dumb. And should produce an error.

    Example 1:

    [fruit]
    type = "apple"
    
    [fruit.type]
    apple = "yes"

    Example 2:

    [a]
    [a]

    Example 3:

    dupe = false
    dupe = true

Release 0.3.0 to rubygems

We're starting to have problems due to the pessimistic version lock on parslet which exists in 0.2.0. I see that this is fixed in the master branch here however it has not been released yet. You can see more detail on the issue we're hitting here inspec/inspec#5389.

Emulate YAML API

I think it would behoove any parser of TOML to emulate the YAML API. Specifically:

TOML.load(toml_markup)
TOML.load_file("config.toml")
TOML.dump(hash)

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.