Skip to content

Commit

Permalink
Merge pull request #6 from asaevich/master
Browse files Browse the repository at this point in the history
Add change key delimiter option
  • Loading branch information
sherifabdlnaby authored Mar 9, 2021
2 parents 220592b + 8cd5605 commit 12edf60
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
12 changes: 11 additions & 1 deletion configuro.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Config struct {
validateUsingFunc bool
validateTag string
tag string
keyDelimiter string
viper *viper.Viper
validator *validator.Validate
validatorTrans ut.Translator
Expand Down Expand Up @@ -79,13 +80,14 @@ func DefaultOptions() []ConfigOptions {
WithValidateByTags(),
WithValidateByFunc(false, true),
Tag("config", "validate"),
KeyDelimiter("."),
}
}

func (c *Config) initialize() error {

// Init Viper
c.viper = viper.New()
c.viper = viper.NewWithOptions(viper.KeyDelimiter(c.keyDelimiter))

if c.envDotFileLoad {
// load .env vars
Expand Down Expand Up @@ -276,6 +278,14 @@ func Tag(structTag, validateTag string) ConfigOptions {
}
}

//KeyDelimiter Сhange default key delimiter.
func KeyDelimiter(keyDelimiter string) ConfigOptions {
return func(h *Config) error {
h.keyDelimiter = keyDelimiter
return nil
}
}

var supportedExt = []string{".json", ".toml", ".yaml", ".yml"}

func isSupportedExtension(ext string) bool {
Expand Down
87 changes: 87 additions & 0 deletions configuro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -712,6 +713,92 @@ Object:
}
}

func TestChangeKeyDelimiter(t *testing.T) {
configFileYaml, err := ioutil.TempFile("", "TestChangeKeyDelimiter*.yml")
if err != nil {
t.Fatal(err)
}
defer func() {
configFileYaml.Close()
os.RemoveAll(configFileYaml.Name())
}()

// Write Config to File
configFileYaml.WriteString(`
Object:
dot.key: "aa"
key: "bb"
`)

type Obj struct {
Object map[string]string
}

configLoaderDefaultDelimiter, err := configuro.NewConfig(
configuro.WithoutEnvConfigPathOverload(),
configuro.WithoutExpandEnvVars(),
configuro.WithoutLoadDotEnv(),
configuro.WithoutLoadFromEnvVars(),
configuro.WithLoadFromConfigFile(configFileYaml.Name(), true),
)
if err != nil {
t.Fatal(err)
}

configLoaderNewDelimiter, err := configuro.NewConfig(
configuro.WithoutEnvConfigPathOverload(),
configuro.WithoutExpandEnvVars(),
configuro.WithoutLoadDotEnv(),
configuro.WithoutLoadFromEnvVars(),
configuro.WithLoadFromConfigFile(configFileYaml.Name(), true),
configuro.KeyDelimiter("::"),
)
if err != nil {
t.Fatal(err)
}

tests := []struct {
name string
config *configuro.Config
want Obj
wantErr bool
}{
{
name: "default key delimiter",
config: configLoaderDefaultDelimiter,
want: Obj{
Object: map[string]string{
"key": "bb",
}},
wantErr: true,
},
{
name: "new key delimiter",
config: configLoaderNewDelimiter,
want: Obj{
Object: map[string]string{
"dot.key": "aa",
"key": "bb",
}},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
obj := &Obj{}
err := tt.config.Load(obj)
if (err != nil) != tt.wantErr {
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(*obj, tt.want) {
t.Errorf("Load() obj = %v, want %v", obj, tt.want)
}
})
}
}

func TestValidateByTag(t *testing.T) {
configFileYaml, err := ioutil.TempFile("", "TestValidateByTag*.yml")
if err != nil {
Expand Down

0 comments on commit 12edf60

Please sign in to comment.