forked from goraz/onion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc38f9c
commit 291e3c2
Showing
7 changed files
with
255 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,5 @@ func TestFileLayer(t *testing.T) { | |
err = o.AddLayer(fl) | ||
So(err, ShouldNotBeNil) | ||
}) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tomlloader | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
|
||
"github.com/BurntSushi/toml" | ||
|
||
"github.com/fzerorubigd/onion" | ||
) | ||
|
||
type tomlLoader struct { | ||
} | ||
|
||
func (tl tomlLoader) SupportedEXT() []string { | ||
return []string{".toml"} | ||
} | ||
|
||
func (tl tomlLoader) Convert(r io.Reader) (map[string]interface{}, error) { | ||
data, _ := ioutil.ReadAll(r) | ||
ret := make(map[string]interface{}) | ||
err := toml.Unmarshal(data, &ret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
func init() { | ||
onion.RegisterLoader(&tomlLoader{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package tomlloader | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
. "github.com/fzerorubigd/onion" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestYamlLoader(t *testing.T) { | ||
Convey("Load a yaml structure into a json", t, func() { | ||
// Make sure there is two ile available in the tmp file system | ||
tmp := os.TempDir() | ||
dir := tmp + "/onion_f" | ||
So(os.MkdirAll(dir, 0744), ShouldBeNil) | ||
path := dir + "/test.toml" | ||
path2 := dir + "/invalid.toml" | ||
|
||
buf := bytes.NewBufferString(` | ||
str = "string_data" | ||
bool = true | ||
integer = 10 | ||
[nested] | ||
key1 = "string" | ||
key2 = 100 | ||
`) | ||
bufInvalid := bytes.NewBufferString(`invalid toml file`) | ||
f, err := os.Create(path) | ||
So(err, ShouldBeNil) | ||
_, err = io.Copy(f, buf) | ||
So(err, ShouldBeNil) | ||
|
||
f2, err := os.Create(path2) | ||
So(err, ShouldBeNil) | ||
_, err = io.Copy(f2, bufInvalid) | ||
So(err, ShouldBeNil) | ||
|
||
defer func() { | ||
_ = os.Remove(path) | ||
}() | ||
So(f.Close(), ShouldBeNil) | ||
So(f2.Close(), ShouldBeNil) | ||
|
||
Convey("Check if the file is loaded correctly ", func() { | ||
fl := NewFileLayer(path) | ||
o := New() | ||
err := o.AddLayer(fl) | ||
So(err, ShouldBeNil) | ||
So(o.GetString("str", ""), ShouldEqual, "string_data") | ||
So(o.GetString("nested.key1", ""), ShouldEqual, "string") | ||
So(o.GetInt("nested.key2", 0), ShouldEqual, 100) | ||
So(o.GetBool("bool", false), ShouldBeTrue) | ||
|
||
a := New() // Just for test load again | ||
So(a.AddLayer(fl), ShouldBeNil) | ||
}) | ||
|
||
Convey("Check for the invalid file content", func() { | ||
fl := NewFileLayer(path2) | ||
o := New() | ||
err = o.AddLayer(fl) | ||
So(err, ShouldNotBeNil) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package yamlloader | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
|
||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/fzerorubigd/onion" | ||
) | ||
|
||
type yamlLoader struct { | ||
} | ||
|
||
func (yl yamlLoader) SupportedEXT() []string { | ||
return []string{".yaml", ".yml"} | ||
} | ||
|
||
func (yl yamlLoader) Convert(r io.Reader) (map[string]interface{}, error) { | ||
data, _ := ioutil.ReadAll(r) | ||
ret := make(map[string]interface{}) | ||
err := yaml.Unmarshal(data, &ret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
func init() { | ||
onion.RegisterLoader(&yamlLoader{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package yamlloader | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
. "github.com/fzerorubigd/onion" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestYamlLoader(t *testing.T) { | ||
Convey("Load a yaml structure into a json", t, func() { | ||
// Make sure there is two ile available in the tmp file system | ||
tmp := os.TempDir() | ||
dir := tmp + "/onion_f" | ||
So(os.MkdirAll(dir, 0744), ShouldBeNil) | ||
path := dir + "/test.yaml" | ||
path2 := dir + "/invalid.yml" | ||
|
||
buf := bytes.NewBufferString(`--- | ||
str: "string_data" | ||
bool: true | ||
integer: 10 | ||
nested: | ||
key1: "string" | ||
key2: 100 | ||
`) | ||
bufInvalid := bytes.NewBufferString(`--- | ||
str: - inv | ||
lid | ||
s | ||
ALALA`) | ||
f, err := os.Create(path) | ||
So(err, ShouldBeNil) | ||
_, err = io.Copy(f, buf) | ||
So(err, ShouldBeNil) | ||
|
||
f2, err := os.Create(path2) | ||
So(err, ShouldBeNil) | ||
_, err = io.Copy(f2, bufInvalid) | ||
So(err, ShouldBeNil) | ||
|
||
defer func() { | ||
_ = os.Remove(path) | ||
_ = os.Remove(path2) | ||
}() | ||
So(f.Close(), ShouldBeNil) | ||
So(f2.Close(), ShouldBeNil) | ||
|
||
Convey("Check if the file is loaded correctly ", func() { | ||
fl := NewFileLayer(path) | ||
o := New() | ||
err := o.AddLayer(fl) | ||
So(err, ShouldBeNil) | ||
So(o.GetString("str", ""), ShouldEqual, "string_data") | ||
So(o.GetString("nested.key1", ""), ShouldEqual, "string") | ||
So(o.GetInt("nested.key2", 0), ShouldEqual, 100) | ||
So(o.GetBool("bool", false), ShouldBeTrue) | ||
|
||
a := New() // Just for test load again | ||
So(a.AddLayer(fl), ShouldBeNil) | ||
}) | ||
|
||
Convey("Check for the invalid file content", func() { | ||
fl := NewFileLayer(path2) | ||
o := New() | ||
err = o.AddLayer(fl) | ||
So(err, ShouldNotBeNil) | ||
}) | ||
}) | ||
} |