Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TOML inputs #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in config-env.gemspec
gemspec

gem "toml"

group :test do
gem "guard"
gem "guard-minitest"
Expand Down
4 changes: 2 additions & 2 deletions examples/01_merge_to_json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This example sets up three levels:
#
# * "Base" from examples/01_base.rb
# * "Prod" from examples/01_prod.json
# * "Prod" from examples/01_prod.toml
# * "System Environment" with no prefix.
#
# Base defines the possible keys with default vaules, Prod changes a few of
Expand All @@ -23,5 +23,5 @@ bundle exec levels \
--level "Prod" \
--system \
$examples/01_base.rb \
$examples/01_prod.json
$examples/01_prod.toml

8 changes: 0 additions & 8 deletions examples/01_prod.json

This file was deleted.

5 changes: 5 additions & 0 deletions examples/01_prod.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[webserver]
hostname = "example.com"

[task_queue]
workers = 5
7 changes: 7 additions & 0 deletions lib/levels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
require "yaml"
require "open3"

begin
require "toml"
rescue
# TOML is optional.
end

require "levels/version"

require "levels/method_missing"
Expand All @@ -21,6 +27,7 @@
require "levels/input/json"
require "levels/input/ruby"
require "levels/input/system"
require "levels/input/toml"
require "levels/input/yaml"

require "levels/output/json"
Expand Down
18 changes: 18 additions & 0 deletions lib/levels/input/toml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Levels
module Input
# This input loads TOML documents.
class TOML

def initialize(toml_string)
@toml = ::TOML.load(toml_string)
end

def read(level)
@toml.each do |group_name, group|
level.set_group(group_name, group)
end
end
end
end
end

3 changes: 3 additions & 0 deletions lib/levels/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def input
when :custom then source
when :ruby then Levels::Input::Ruby.new(source, *args)
when :json then Levels::Input::JSON.new(source)
when :toml then Levels::Input::TOML.new(source)
when :yaml then Levels::Input::YAML.new(source)
else raise ArgumentError, "Could not identify the format: #{format.inspect}"
end
Expand All @@ -115,12 +116,14 @@ def identify
case pn.extname
when ".rb" then [:ruby, pn.read, pn.to_s, 1]
when ".json" then [:json, pn.read]
when ".toml" then [:toml, pn.read]
when ".yaml", ".yml" then [:yaml, pn.read]
else raise ArgumentError, "Could not identify the file type: #{pn.extname}"
end
else
case @source
when /\A\w*{/ then [:json, @source]
when /\A\w*\[/ then [:toml, @source]
when /\A---$/ then [:yaml, @source]
when /\A\w*group/ then [:ruby, @source, "Code from String", 1]
else raise ArgumentError, "Could not identify the source: #{@source.inspect}"
Expand Down
32 changes: 32 additions & 0 deletions test/acceptance/read_toml_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'helper'

describe "acceptance: read toml" do

def read_toml(level_name, toml_string)
level = Levels::Level.new(level_name)
input = Levels::Input::TOML.new(toml_string)
input.read(level)
level
end

let(:toml) {
<<-TOML
[types]
string = "hello"
integer = 123
float = 1.5
boolean_true = true
boolean_false = false
array_of_string = ["a", "b", "c"]
array_of_integer = [1, 2, 3]
null = "TOML does not have a way to define NULL"
[group2]
message = "hello world"
TOML
}

subject { read_toml("test", toml) }

assert_sample_data_set
end

18 changes: 18 additions & 0 deletions test/acceptance/setup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
JSON
end

let(:toml_code) do
<<-TOML
[group]
a = 1
TOML
end

let(:yaml_code) do
<<-YAML
---
Expand Down Expand Up @@ -53,6 +60,11 @@ def read(level)
subject.merge.group.a.must_equal 1
end

it "adds TOML code" do
subject.add("code", toml_code)
subject.merge.group.a.must_equal 1
end

it "adds YAML code" do
subject.add("code", yaml_code)
subject.merge.group.a.must_equal 1
Expand All @@ -70,6 +82,12 @@ def read(level)
subject.merge.group.a.must_equal 1
end

it "adds a TOML file" do
file = w("input.toml", toml_code)
subject.add("file", file.to_s)
subject.merge.group.a.must_equal 1
end

it "adds a YAML file (.yml)" do
file = w("input.yml", yaml_code)
subject.add("file", file.to_s)
Expand Down
29 changes: 29 additions & 0 deletions test/unit/input/toml_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'helper'

describe Levels::Input::TOML do

let(:toml_string) {
<<-STR
[group1]
key1 = "string"
key2 = 123
[group2]
key = [1, 2, 3]
STR
}

subject { Levels::Input::TOML.new(toml_string) }

it "reads data from the TOML structure" do
assert_input_equals_hash(
group1: {
key1: "string",
key2: 123
},
group2: {
key: [1, 2, 3]
}
)
end
end