File: toml-test-decoder-interface.rb

package info (click to toggle)
ruby-toml 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: ruby: 666; sh: 7; makefile: 4
file content (50 lines) | stat: -rwxr-xr-x 1,186 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env ruby
#
# This is an interface for: https://github.com/BurntSushi/toml-test

require 'json'
require '../lib/toml'

# This converts a ruby obj to an obj that can be ran through the json encoder
# to create json for toml-test.
def obj_to_toml_test(obj)
  if obj.kind_of?(Hash)
    toml_test = {}
    obj.each do |key, value|
      toml_test[key] = obj_to_toml_test(value)
    end
  elsif obj.kind_of?(Array)
    toml_test = obj.map do |value|
      value = obj_to_toml_test(value)
    end

    unless obj.first.kind_of?(Hash)
      toml_test = {"type" => "array", "value" => toml_test}
    end
  else
    type = obj.class.name.downcase
    value = obj.to_s

    if obj.kind_of?(FalseClass)
      type = "bool"
      value = "false"
    elsif obj.kind_of?(TrueClass)
      type = "bool"
      value = "true"
    elsif obj.kind_of?(DateTime)
      value = obj.to_time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
    elsif obj.kind_of?(Integer)
      type = "integer"
    elsif obj.kind_of?(Float)
      type = "float"
    end

    toml_test = {"type" => type, "value" => value}
  end

  toml_test
end

hash = obj_to_toml_test(TOML::Parser.new(ARGF.read).parsed)

print JSON.dump(hash)