Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support flag.Value interface in flags backend #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func (b *Backend) LoadStruct(ctx context.Context, cfg *confita.StructConfig) err
continue
}

// Check if value type implements flag.Value interface and process value accordingly
valuePtr := f.Value
if f.Value.Kind() != reflect.Ptr && f.Value.CanAddr() {
valuePtr = f.Value.Addr()
}
if iface, ok := valuePtr.Interface().(flag.Value); ok {
b.flags.Var(iface, f.Key, f.Description)
continue
}

// Display all the flags and their default values but override the field only if the user has explicitely
// set the flag.
k := f.Value.Kind()
Expand Down
40 changes: 38 additions & 2 deletions backend/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flags
import (
"context"
"flag"
"fmt"
"os"
"testing"
"time"
Expand All @@ -12,6 +13,36 @@ import (
"github.com/stretchr/testify/require"
)

type logLevel int

var (
logLevelDebug logLevel = 1
logLevelInfo logLevel = 2
)

func (l *logLevel) Set(val string) error {
switch val {
case "debug":
*l = logLevelDebug
case "info":
*l = logLevelInfo
default:
return fmt.Errorf("unknown log level: %s", val)
}
return nil
}

func (l logLevel) String() string {
switch l {
case logLevelDebug:
return "debug"
case logLevelInfo:
return "info"
default:
return "<unknown>"
}
}

func runHelper(t *testing.T, cfg interface{}, args ...string) {
t.Helper()

Expand All @@ -30,16 +61,18 @@ func TestFlags(t *testing.T) {
D int `config:"d"`
E uint `config:"e"`
F float32 `config:"f"`
G logLevel `config:"g"`
}
var cfg config
runHelper(t, &cfg, "-a=hello", "-b=true", "-c=10s", "-d=-100", "-e=1", "-f=100.01")
runHelper(t, &cfg, "-a=hello", "-b=true", "-c=10s", "-d=-100", "-e=1", "-f=100.01", "-g=info")
require.Equal(t, config{
A: "hello",
B: true,
C: 10 * time.Second,
D: -100,
E: 1,
F: 100.01,
G: logLevelInfo,
}, cfg)
})

Expand All @@ -51,14 +84,16 @@ func TestFlags(t *testing.T) {
Ddef int `config:"d-def,short=dd"`
Edef uint `config:"e-def,short=ed"`
Fdef float32 `config:"f-def,short=fd"`
Gdef logLevel `config:"g-def,short=gd"`
}
cfg := &config{
Adef: "hello",
Bdef: true,
Cdef: 10 * time.Second,
Ddef: -100,
Gdef: logLevelInfo,
}
runHelper(t, cfg, "-a-def=bye", "-b-def=false", "-c-def=15s", "-d-def=-200", "-e-def=400", "-f-def=2.33")
runHelper(t, cfg, "-a-def=bye", "-b-def=false", "-c-def=15s", "-d-def=-200", "-e-def=400", "-f-def=2.33", "-g-def=debug")

require.Equal(t, &config{
Adef: "bye",
Expand All @@ -67,6 +102,7 @@ func TestFlags(t *testing.T) {
Ddef: -200,
Edef: 400,
Fdef: 2.33,
Gdef: logLevelDebug,
}, cfg)
})
}
Expand Down