Skip to content

Commit

Permalink
Merge pull request #4 from luizvnasc/enhancement/xml
Browse files Browse the repository at this point in the history
Enhancement/xml
  • Loading branch information
luizvnasc authored Dec 15, 2019
2 parents 7042617 + 7f8ef60 commit e0da408
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A simple module to load configurations file to a struct.
| Extension | is supported? |
|:---------:|:-----------------:|
| .json |:heavy_check_mark: |
| .xml |:x: |
| .xml |:heavy_check_mark: |
| .yaml |:x: |
| .toml |:x: |

Expand Down
4 changes: 3 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package gonfig

const (
//LoadError = "Error loading the configuration"
// LoadError = "Error loading the configuration"
LoadError = GonfigError("Error loading the configuration")
// UnsupportedFileError = "Unsupported file extension"
UnsupportedFileError = GonfigError("Unsupported file extension")
)

// GonfigError is an error in the module.
Expand Down
5 changes: 5 additions & 0 deletions examples/xml/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<config>
<version>1.0.0</version>
<description>A xml exemple</description>
<redis host="127.0.0.1" port="6379"/>
</config>
23 changes: 23 additions & 0 deletions examples/xml/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 `xml:"version"`
Description string `xml:"description"`
Redis struct {
Host string `xml:"host,attr"`
Port uint `xml:"port,attr"`
} `xml:"redis"`
}

func main() {
var config Config
gonfig.Load("config.xml", &config)
fmt.Printf("%v", config)
}
22 changes: 18 additions & 4 deletions gonfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@ package gonfig

import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"os"
"path/filepath"
)

const (
typeJSON = "json"
)
type unmarshalerFunc func(data []byte, v interface{}) error

// Load a struct with the configuration from a config file.
func Load(path string, config interface{}) error {
configFile, err := os.Open(path)
if err != nil {
config = nil
return LoadError
}
defer configFile.Close()

b, err := ioutil.ReadAll(configFile)
if err != nil {
config = nil
return LoadError
}
err = json.Unmarshal(b, &config)

ext := filepath.Ext(path)

var unmarshaler unmarshalerFunc
switch ext {
case ".json":
unmarshaler = json.Unmarshal
case ".xml":
unmarshaler = xml.Unmarshal
default:
return UnsupportedFileError
}
err = unmarshaler(b, &config)
if err != nil {
config = nil
return LoadError
Expand Down
57 changes: 46 additions & 11 deletions gonfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ import (
)

const (
validJsonFile = "config.json"
invalidJsonFile = "invalid.json"
invalidJsonBodyFile = "invalid_config.json"
validJsonFile = "./test/config.json"
invalidJsonFile = "./test/invalid.json"
invalidJsonBodyFile = "./test/invalid_config.json"
validXMLFile = "./test/config.xml"
invalidXMLFile = "./test/invalid.xml"
invalidXMLBodyFile = "./test/invalid_config.xml"
)

type SomeConfiguration struct {
Version string `json:"version" xml:"version"`
ProjectName string `json:"project_name" xml:"project_name"`
ProjectName string `json:"project_name" xml:"project-name"`
}

var configJSON SomeConfiguration
var configValid SomeConfiguration

func init() {
file, err := os.Open(validJsonFile)
Expand All @@ -32,7 +35,7 @@ func init() {
if err != nil {
panic("Error Loading teste sample")
}
json.Unmarshal(b, &configJSON)
json.Unmarshal(b, &configValid)
}
func TestLoadJSON(t *testing.T) {
config := SomeConfiguration{}
Expand All @@ -42,12 +45,12 @@ func TestLoadJSON(t *testing.T) {
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configJSON) {
t.Errorf("Error loading the configuration: expected %v, got %v", configJSON, config)
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})

t.Run("Load a configuration from a invalid json file", func(t *testing.T) {
t.Run("Load a configuration from an invalid json file", func(t *testing.T) {
err := gonfig.Load(invalidJsonFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
Expand All @@ -57,8 +60,31 @@ func TestLoadJSON(t *testing.T) {
}
})

t.Run("Load a configuration from a invalid json body", func(t *testing.T) {
err := gonfig.Load(invalidJsonFile, &config)
t.Run("Load a configuration from an invalid json body", func(t *testing.T) {
err := gonfig.Load(invalidJsonBodyFile, &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)
}
})
}

func TestLoadXML(t *testing.T) {
config := SomeConfiguration{}

t.Run("Load a configuration from a valid xml file", func(t *testing.T) {
err := gonfig.Load(validXMLFile, &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 json file", func(t *testing.T) {
err := gonfig.Load(invalidXMLFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
Expand All @@ -67,4 +93,13 @@ func TestLoadJSON(t *testing.T) {
}
})

t.Run("Load a configuration from an invalid json body", func(t *testing.T) {
err := gonfig.Load(invalidXMLBodyFile, &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)
}
})
}
File renamed without changes.
4 changes: 4 additions & 0 deletions test/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<config>
<version>1.0.0</version>
<project-name>gonfig</project-name>
</config>
File renamed without changes.
4 changes: 4 additions & 0 deletions test/invalid_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<config>
<version>1.0.0</version>
<project-name>gonfig</project-name>
</config2>

0 comments on commit e0da408

Please sign in to comment.