Skip to content

Commit

Permalink
Merge pull request #19 from heetch/custom-tag
Browse files Browse the repository at this point in the history
Add custom tag
  • Loading branch information
asdine authored Apr 10, 2018
2 parents 4f60776 + 0da276b commit f78db28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
// Loader loads configuration keys from backends and stores them is a struct.
type Loader struct {
backends []backend.Backend

// Tag specifies the tag name used to parse
// configuration keys and options.
// If empty, "config" is used.
Tag string
}

// NewLoader creates a Loader. If no backend is specified, the loader uses the environment.
Expand Down Expand Up @@ -64,7 +69,12 @@ func (l *Loader) parseStruct(ctx context.Context, ref *reflect.Value) error {
continue
}

tag := field.Tag.Get("config")
tagKey := l.Tag
if tagKey == "" {
tagKey = "config"
}

tag := field.Tag.Get(tagKey)
if tag == "-" {
continue
}
Expand Down
23 changes: 23 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,26 @@ func TestTags(t *testing.T) {
assert.Equal(t, "", cfg.Key)
})
}

func TestCustomTag(t *testing.T) {
s := struct {
Name string `custom:"name"`
Age int `custom:"age"`
}{}

st := store{
"name": "name",
"age": "10",
}

err := confita.NewLoader(st).Load(context.Background(), &s)
require.NoError(t, err)
require.Empty(t, &s)

l := confita.NewLoader(st)
l.Tag = "custom"
err = l.Load(context.Background(), &s)
require.NoError(t, err)
require.Equal(t, "name", s.Name)
require.Equal(t, 10, s.Age)
}

0 comments on commit f78db28

Please sign in to comment.