diff --git a/README.md b/README.md index 79061ba..c797420 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,44 @@ A simple module to load configurations file to a struct. +## Getting Started + +To install this package run: + +``` +go get github.com/luizvnasc/gonfig +``` + +### Example + +``` golang + +package main + +import ( + "fmt" + "github.com/luizvnasc/gonfig" +) + +//Config struct to store the app configuration +type Config struct { + Version string `toml:"version"` + Description string `toml:"desc"` + Redis struct { + Host string `toml:"host"` + Port uint `toml:"port"` + } `toml:"redis"` +} + +func main() { + var config Config + gonfig.Load("config.toml", &config) + fmt.Printf("%v", config) +} + + +``` +You can see more examples [here](https://github.com/luizvnasc/gonfig/tree/master/examples). ## Files supported @@ -15,7 +53,14 @@ A simple module to load configurations file to a struct. | .json | :heavy_check_mark: | | .xml | :heavy_check_mark: | | .yaml/.yml | :heavy_check_mark: | -| .toml | :x: | +| .toml | :heavy_check_mark: | + +## Dependencies + +| Dependency | Repository | +| :--------: | :----------------: | +| gopkg.in/yaml.v3 | [https://github.com/go-yaml/yaml](https://github.com/go-yaml/yaml) | +| go-toml | [github.com/pelletier/go-toml](github.com/pelletier/go-toml) | ## Authors * Luiz Augusto Volpi Nascimento - Initial work - [@luizvnasc](https://github.com/luizvnasc) diff --git a/examples/toml/config.toml b/examples/toml/config.toml new file mode 100644 index 0000000..6747ba7 --- /dev/null +++ b/examples/toml/config.toml @@ -0,0 +1,5 @@ +version = "1.0.0" +desc = "An example using toml" +[redis] + host = "127.0.0.1" + port = 6379 diff --git a/examples/toml/main.go b/examples/toml/main.go new file mode 100644 index 0000000..4c58c7d --- /dev/null +++ b/examples/toml/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + + "github.com/luizvnasc/gonfig" +) + +//Config struct to store the app configuration +type Config struct { + Version string `toml:"version"` + Description string `toml:"desc"` + Redis struct { + Host string `toml:"host"` + Port uint `toml:"port"` + } `toml:"redis"` +} + +func main() { + var config Config + gonfig.Load("config.toml", &config) + fmt.Printf("%v", config) +} diff --git a/go.mod b/go.mod index 7e533fa..dc6294e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/luizvnasc/gonfig go 1.12 require ( + github.com/pelletier/go-toml v1.6.0 gopkg.in/yaml.v2 v2.2.7 gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2 ) diff --git a/go.sum b/go.sum index a781f65..c761238 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,10 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4= +github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2 h1:XZx7nhd5GMaZpmDaEHFVafUZC7ya0fuo7cSJ3UCKYmM= diff --git a/gonfig.go b/gonfig.go index 466fee4..936748a 100644 --- a/gonfig.go +++ b/gonfig.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" + "github.com/pelletier/go-toml" "gopkg.in/yaml.v3" ) @@ -45,6 +46,8 @@ func getUnmarshaler(path string) (unmarshalerFunc, error) { return xml.Unmarshal, nil case ext == ".yaml" || ext == ".yml": return yaml.Unmarshal, nil + case ext == ".toml": + return toml.Unmarshal, nil default: return nil, UnsupportedFileError } diff --git a/gonfig_test.go b/gonfig_test.go index a6bbcaf..7511d7e 100644 --- a/gonfig_test.go +++ b/gonfig_test.go @@ -19,11 +19,13 @@ const ( unsupportedFile = "./test/config.xyz" validYamlFile = "./test/config.yaml" invalidYamlBodyFile = "./test/invalid_config.yaml" + validTomlFile = "./test/config.toml" + invalidTomlBodyFile = "./test/invalid_config.toml" ) type SomeConfiguration struct { - Version string `json:"version" xml:"version" yaml:"version" ` - ProjectName string `json:"project_name" xml:"project-name" yaml:"project_name" ` + Version string `json:"version" xml:"version" yaml:"version" toml:"version"` + ProjectName string `json:"project_name" xml:"project-name" yaml:"project_name" toml:"project_name"` } var configValid SomeConfiguration @@ -119,6 +121,31 @@ func TestGonfig(t *testing.T) { } }) }) + + t.Run("TOML tests", func(t *testing.T) { + t.Run("Load a configuration from a valid toml file", func(t *testing.T) { + config := SomeConfiguration{} + err := gonfig.Load(validTomlFile, &config) + if err != nil { + t.Errorf("Error loading the configuration: %v", err) + } + if !reflect.DeepEqual(config, configValid) { + t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config) + } + }) + + t.Run("Load a configuration from an invalid toml body", func(t *testing.T) { + config := SomeConfiguration{} + err := gonfig.Load(invalidTomlBodyFile, &config) + if err == nil { + t.Errorf("It was expected to get an error. Got nil") + } + if err != gonfig.LoadError { + t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err) + } + }) + }) + t.Run("Unsupported file", func(t *testing.T) { config := SomeConfiguration{} err := gonfig.Load(unsupportedFile, &config) diff --git a/test/config.toml b/test/config.toml new file mode 100644 index 0000000..f987b7f --- /dev/null +++ b/test/config.toml @@ -0,0 +1,2 @@ +version = "1.0.0" +project_name = "gonfig" diff --git a/test/invalid_config.toml b/test/invalid_config.toml new file mode 100644 index 0000000..6c7e764 --- /dev/null +++ b/test/invalid_config.toml @@ -0,0 +1,2 @@ +version: 1.0.0 +project_name: gonfig