Skip to content

Commit

Permalink
Merge pull request #31 from heetch/fix-bug
Browse files Browse the repository at this point in the history
Check if the flag has been set before to set the value.
  • Loading branch information
Yasss authored Jul 16, 2018
2 parents b2c3464 + 9c774b1 commit 67096f8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
12 changes: 9 additions & 3 deletions backend/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@ func (b *Backend) LoadStruct(ctx context.Context, cfg *confita.StructConfig) err
case k >= reflect.Uint && k <= reflect.Uint64:
v := flag.Uint(f.Key, uint(f.Default.Uint()), "")
defer func() {
f.Value.SetUint(uint64(*v))
if isFlagSet(f.Key) {
f.Value.SetUint(uint64(*v))
}
}()
case k >= reflect.Float32 && k <= reflect.Float64:
v := flag.Float64(f.Key, f.Default.Float(), "")
defer func() {
f.Value.SetFloat(*v)
if isFlagSet(f.Key) {
f.Value.SetFloat(*v)
}
}()
case k == reflect.String:
v := flag.String(f.Key, f.Default.String(), "")
defer func() {
f.Value.SetString(*v)
if isFlagSet(f.Key) {
f.Value.SetString(*v)
}
}()
default:
flag.Var(&flagValue{f}, f.Key, "")
Expand Down
45 changes: 45 additions & 0 deletions backend/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/heetch/confita"
"github.com/heetch/confita/backend"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -97,3 +98,47 @@ func TestHelperProcess(t *testing.T) {
require.NoError(t, err)
os.Exit(0)
}

type store map[string]string

func (s store) Get(ctx context.Context, key string) ([]byte, error) {
data, ok := s[key]
if !ok {
return nil, backend.ErrNotFound
}

return []byte(data), nil
}

func (store) Name() string {
return "store"
}

func TestWithAnotherBackend(t *testing.T) {
s := struct {
String string `config:"string,required"`
Bool bool `config:"bool,required"`
Int int `config:"int,required"`
Uint uint `config:"uint,required"`
Float float64 `config:"float,required"`
Duration time.Duration `config:"duration,required"`
}{}

st := store{
"string": "string",
"bool": "true",
"int": "42",
"uint": "42",
"float": "42.42",
"duration": "1ns",
}

err := confita.NewLoader(st, NewBackend()).Load(context.Background(), &s)
require.NoError(t, err)
require.Equal(t, "string", s.String)
require.Equal(t, true, s.Bool)
require.Equal(t, 42, s.Int)
require.EqualValues(t, 42, s.Uint)
require.Equal(t, 42.42, s.Float)
require.Equal(t, time.Duration(1), s.Duration)
}

0 comments on commit 67096f8

Please sign in to comment.