kdl 1.0.6 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +7 -1
  3. data/.gitignore +1 -0
  4. data/.gitmodules +4 -0
  5. data/Gemfile +6 -1
  6. data/README.md +51 -7
  7. data/Rakefile +6 -1
  8. data/bin/kdl +1 -1
  9. data/kdl.gemspec +2 -2
  10. data/lib/kdl/document.rb +58 -2
  11. data/lib/kdl/kdl.tab.rb +303 -228
  12. data/lib/kdl/kdl.yy +57 -49
  13. data/lib/kdl/node.rb +113 -12
  14. data/lib/kdl/parser_common.rb +26 -0
  15. data/lib/kdl/string_dumper.rb +30 -33
  16. data/lib/kdl/tokenizer.rb +350 -113
  17. data/lib/kdl/types/base64.rb +1 -1
  18. data/lib/kdl/types/country/iso3166_countries.rb +1 -1
  19. data/lib/kdl/types/country/iso3166_subdivisions.rb +1 -1
  20. data/lib/kdl/types/country.rb +2 -2
  21. data/lib/kdl/types/currency/iso4217_currencies.rb +1 -1
  22. data/lib/kdl/types/currency.rb +1 -1
  23. data/lib/kdl/types/date_time.rb +3 -3
  24. data/lib/kdl/types/decimal.rb +1 -1
  25. data/lib/kdl/types/duration/iso8601_parser.rb +1 -1
  26. data/lib/kdl/types/duration.rb +1 -1
  27. data/lib/kdl/types/email/parser.rb +1 -1
  28. data/lib/kdl/types/email.rb +1 -1
  29. data/lib/kdl/types/hostname/validator.rb +1 -1
  30. data/lib/kdl/types/hostname.rb +1 -1
  31. data/lib/kdl/types/ip.rb +1 -1
  32. data/lib/kdl/types/irl/parser.rb +1 -1
  33. data/lib/kdl/types/irl.rb +1 -1
  34. data/lib/kdl/types/regex.rb +1 -1
  35. data/lib/kdl/types/url.rb +1 -1
  36. data/lib/kdl/types/url_template.rb +1 -1
  37. data/lib/kdl/types/uuid.rb +1 -1
  38. data/lib/kdl/v1/document.rb +17 -0
  39. data/lib/kdl/v1/kdl.tab.rb +594 -0
  40. data/lib/kdl/v1/kdl.yy +89 -0
  41. data/lib/kdl/v1/node.rb +30 -0
  42. data/lib/kdl/v1/string_dumper.rb +28 -0
  43. data/lib/kdl/v1/tokenizer.rb +296 -0
  44. data/lib/kdl/v1/value.rb +89 -0
  45. data/lib/kdl/v1.rb +11 -0
  46. data/lib/kdl/value.rb +81 -12
  47. data/lib/kdl/version.rb +1 -1
  48. data/lib/kdl.rb +40 -1
  49. metadata +13 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: def27c3b00371120b10ced4e07cf10500c2e1968d4912d0fb17b925989292e1d
4
- data.tar.gz: e3cbf6c787d14fecc6c746826b299ffac1a553be7fca8de12f93c53727568d12
3
+ metadata.gz: f41fc74596026506785cc654df47d91fcd5d0f067e8438cb5a55576076368ca5
4
+ data.tar.gz: 0a32c65029c336f01a39cdab2e5259fe366ac5439b96b408da9f31b6d44ddf9d
5
5
  SHA512:
6
- metadata.gz: 0235d846c7c031867ff0141cca46e56c156c093a6d33529156c833319ee1dbf95a25c7f2ad117884278b1a8c17495bddb6a29ecacfdea54d4ae0be4d4d52f96d
7
- data.tar.gz: d6916fc08c7f66441ee9b96660602e8fc40ddb3e0e2bd1278e55a51826d9f2edd0c402c70e0d4d10e7513f2f17e81d5b30f0223c1f9a33b1196ebcad9f4404a4
6
+ metadata.gz: 7d67ae26465031d210157ac38393084f6aa65863022934981054d5b6806c0e92b11344002ae12e77fecb4c0017fbd38dda03bf0c6cf60c93a917e98f427bf2c5
7
+ data.tar.gz: 0ade99c140539aa3e1c167019967985cd4bf91df1283a975ca7d6f12c9ab6f86fb1c657bc31592eff0df7d71a3e003010eb77d91df69601de4ea7d1cd13eba1f
@@ -17,7 +17,7 @@ jobs:
17
17
  test:
18
18
  strategy:
19
19
  matrix:
20
- ruby: [2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, head]
20
+ ruby: [3.1, 3.2, 3.3, head]
21
21
 
22
22
  runs-on: ubuntu-latest
23
23
 
@@ -25,12 +25,18 @@ jobs:
25
25
  - uses: actions/checkout@v2
26
26
  with:
27
27
  submodules: true
28
+
28
29
  - name: Set up Ruby
29
30
  uses: ruby/setup-ruby@v1
30
31
  with:
31
32
  ruby-version: ${{ matrix.ruby }}
32
33
  bundler-cache: true # install and cache dependencies
34
+
33
35
  - name: Build parser
34
36
  run: bundle exec racc lib/kdl/kdl.yy
37
+
35
38
  - name: Run tests
36
39
  run: bundle exec rake test
40
+
41
+ - name: Report Coveralls
42
+ uses: coverallsapp/github-action@v2
data/.gitignore CHANGED
@@ -10,4 +10,5 @@
10
10
  Gemfile.lock
11
11
 
12
12
  lib/kdl/kdl.tab.rb
13
+ lib/kdl/v1/kdl.tab.rb
13
14
  kdl.output
data/.gitmodules CHANGED
@@ -1,3 +1,7 @@
1
1
  [submodule "test/kdl-org"]
2
2
  path = test/kdl-org
3
3
  url = git@github.com:kdl-org/kdl
4
+ [submodule "test/v1/kdl-org"]
5
+ path = test/v1/kdl-org
6
+ url = git@github.com:kdl-org/kdl
7
+ branch = release/v1
data/Gemfile CHANGED
@@ -4,4 +4,9 @@ source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
- gem "minitest", "~> 5.0"
7
+
8
+ group :test do
9
+ gem "minitest", "~> 5.0"
10
+ gem "simplecov", require: false
11
+ gem "coveralls_reborn", require: false
12
+ end
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # KDL
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/kdl.svg)](https://badge.fury.io/rb/kdl)
3
4
  [![Actions Status](https://github.com/danini-the-panini/kdl-rb/workflows/Ruby/badge.svg)](https://github.com/danini-the-panini/kdl-rb/actions)
5
+ [![Coverage Status](https://coveralls.io/repos/github/danini-the-panini/kdl-rb/badge.svg?branch=main)](https://coveralls.io/github/danini-the-panini/kdl-rb?branch=main)
4
6
 
5
7
  This is a Ruby implementation of the [KDL Document Language](https://kdl.dev)
6
8
 
@@ -25,22 +27,64 @@ Or install it yourself as:
25
27
  ```ruby
26
28
  require 'kdl'
27
29
 
28
- KDL.parse_document(a_string) #=> KDL::Document
30
+ KDL.parse(a_string) #=> KDL::Document
31
+ KDL.load_file('path/to/file') #=> KDL::Document
29
32
  ```
30
33
 
31
34
  You can optionally provide your own type annotation handlers:
32
35
 
33
36
  ```ruby
34
- KDL.parse_document(a_string, type_parsers: {
35
- 'foo' => -> (value, type) {
36
- Foo.new(value.value, type: type)
37
- }
37
+ class Foo < KDL::Value::Custom
38
+ end
39
+
40
+ KDL.parse(a_string, type_parsers: {
41
+ 'foo' => Foo
38
42
  })
39
43
  ```
40
44
 
41
- The `foo` proc will be called with instances of Value or Node with the type annotation `(foo)`.
45
+ The `foo` custom type will be called with instances of Value or Node with the type annotation `(foo)`.
46
+
47
+ Custom types are expected to have a `call` method that takes the Value or Node, and the type annotation itself, as arguments, and is expected to return either an instance of `KDL::Value::Custom` or `KDL::Node::Custom` (depending on the input type) or `nil` to return the original value as is. Take a look at [the built in custom types](lib/kdl/types) as a reference.
48
+
49
+ You can also disable type annotation parsing entirely (including the built in ones):
50
+
51
+ ```ruby
52
+ KDL.parse(a_string, parse_types: false)
53
+ ```
54
+
55
+ ## KDL v1
56
+
57
+ kdl-rb maintains backwards compatibility with the KDL v1 spec. By default, KDL will attempt to parse a file with the v1 parser if it fails to parse with v2. This behaviour can be changed by specifying the `version` option:
58
+
59
+ ```ruby
60
+ KDL.parse(a_string, version: 2)
61
+ ```
62
+
63
+ The resulting document will also serialize back to the same version it was parsed as. For example, if you parse a v2 document and call `to_s` on it, it will output a v2 document, and similarly with v1. This behaviour can be changed by specifying the `output_version` option:
64
+
65
+ ```ruby
66
+ KDL.parse(a_string, output_version: 2)
67
+ ```
68
+
69
+ This allows you to to convert document between versions:
70
+
71
+ ```ruby
72
+ KDL.parse('foo "bar" true', version: 1, output_version: 2).to_s #=> 'foo bar #true'
73
+ ```
74
+
75
+ You can also set the default version globally:
42
76
 
43
- Parsers are expected to have a `call` method that takes the Value or Node, and the type annotation itself, as arguments, and is expected to return either an instance of Value or Node (depending on the input type) or `nil` to return the original value as is. Take a look at [the built in parsers](lib/kdl/types) as a reference.
77
+ ```ruby
78
+ KDL.default_version = 2
79
+ KDL.default_output_version = 2
80
+ ```
81
+
82
+ Version directives are also respected:
83
+
84
+ ```ruby
85
+ KDL.parse("/- kdl-version 2\nfoo bar", version: 1)
86
+ #=> Version mismatch, document specified v2, but this is a v1 parser (Racc::ParseError)
87
+ ```
44
88
 
45
89
  ## Development
46
90
 
data/Rakefile CHANGED
@@ -6,7 +6,12 @@ file 'lib/kdl/kdl.tab.rb' => ['lib/kdl/kdl.yy'] do
6
6
  end
7
7
  task :racc => 'lib/kdl/kdl.tab.rb'
8
8
 
9
- Rake::TestTask.new(:test => :racc) do |t|
9
+ file 'lib/kdl/v1/kdl.tab.rb' => ['lib/kdl/v1/kdl.yy'] do
10
+ raise "racc command failed (v1)" unless system 'bin/racc lib/kdl/v1/kdl.yy'
11
+ end
12
+ task :racc_v1 => 'lib/kdl/v1/kdl.tab.rb'
13
+
14
+ Rake::TestTask.new(:test => [:racc, :racc_v1]) do |t|
10
15
  t.libs << 'test'
11
16
  t.libs << 'lib'
12
17
  t.test_files = FileList['test/**/*_test.rb']
data/bin/kdl CHANGED
@@ -5,4 +5,4 @@ require "kdl"
5
5
 
6
6
  system 'bin/rake racc'
7
7
 
8
- puts KDL.parse_document(ARGF.read).to_s
8
+ puts KDL.parse(ARGF.read).to_s
data/kdl.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.description = %q{Ruby implementation of the KDL Document Language Spec}
11
11
  spec.homepage = "https://kdl.dev"
12
12
  spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.1.0")
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
16
16
  spec.metadata["source_code_uri"] = "https://github.com/danini-the-panini/kdl-rb"
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
21
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
22
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
- end + ['lib/kdl/kdl.tab.rb']
23
+ end + ['lib/kdl/kdl.tab.rb', 'lib/kdl/v1/kdl.tab.rb']
24
24
  spec.bindir = "exe"
25
25
  spec.require_paths = ["lib"]
26
26
 
data/lib/kdl/document.rb CHANGED
@@ -1,13 +1,57 @@
1
1
  module KDL
2
2
  class Document
3
+ include Enumerable
4
+
3
5
  attr_accessor :nodes
4
-
6
+
5
7
  def initialize(nodes)
6
8
  @nodes = nodes
7
9
  end
8
10
 
11
+ def [](key)
12
+ case key
13
+ when Integer
14
+ nodes[key]
15
+ when String, Symbol
16
+ nodes.find { _1.name == key.to_s }
17
+ else
18
+ raise ArgumentError, "document can only be indexed by Integer, String, or Symbol"
19
+ end
20
+ end
21
+
22
+ def arg(key)
23
+ self[key]&.arguments&.first&.value
24
+ end
25
+
26
+ def args(key)
27
+ self[key]&.arguments&.map(&:value)
28
+ end
29
+
30
+ def each_arg(key, &block)
31
+ args(key)&.each(&block)
32
+ end
33
+
34
+ def dash_vals(key)
35
+ self[key]
36
+ &.children
37
+ &.select { _1.name == "-" }
38
+ &.map { _1.arguments.first&.value }
39
+ end
40
+
41
+ def each_dash_val(key, &block)
42
+ dash_vals(key)&.each(&block)
43
+ end
44
+
45
+ def each(&block)
46
+ nodes.each(&block)
47
+ end
48
+
9
49
  def to_s
10
- @nodes.map(&:to_s).join("\n") + "\n"
50
+ nodes.map(&:to_s).join("\n") + "\n"
51
+ end
52
+
53
+ def inspect
54
+ nodes.map(&:inspect).join("\n") + "\n"
11
55
  end
12
56
 
13
57
  def ==(other)
@@ -15,5 +59,17 @@ module KDL
15
59
 
16
60
  nodes == other.nodes
17
61
  end
62
+
63
+ def version
64
+ 2
65
+ end
66
+
67
+ def to_v2
68
+ self
69
+ end
70
+
71
+ def to_v1
72
+ KDL::V1::Document.new(nodes.map(&:to_v1))
73
+ end
18
74
  end
19
75
  end