Skip to content

Commit

Permalink
Merge pull request #8 from luizvnasc/feature/toml
Browse files Browse the repository at this point in the history
Feature/toml
  • Loading branch information
luizvnasc committed Dec 17, 2019
2 parents 5b8ded2 + 6b2058b commit a6233da
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 3 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions examples/toml/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version = "1.0.0"
desc = "An example using toml"
[redis]
host = "127.0.0.1"
port = 6379
23 changes: 23 additions & 0 deletions examples/toml/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
3 changes: 3 additions & 0 deletions gonfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"

"github.com/pelletier/go-toml"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 29 additions & 2 deletions gonfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = "1.0.0"
project_name = "gonfig"
2 changes: 2 additions & 0 deletions test/invalid_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: 1.0.0
project_name: gonfig

0 comments on commit a6233da

Please sign in to comment.