Skip to content

Commit 437111f

Browse files
committed
config.ParseFlags(): enforce type constraints via generics, not reflection
1 parent faefe72 commit 437111f

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

config/config.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/jessevdk/go-flags"
99
"github.com/pkg/errors"
1010
"os"
11-
"reflect"
1211
)
1312

1413
// ErrInvalidArgument is the error returned by [ParseFlags] or [FromYAMLFile] if
@@ -58,10 +57,9 @@ func FromYAMLFile[T any, V validatorPtr[T]](name string, v V) error {
5857
// ParseFlags prints the help message to [os.Stdout] and exits.
5958
// Note that errors are not printed automatically,
6059
// so error handling is the sole responsibility of the caller.
61-
func ParseFlags(v any) error {
62-
rv := reflect.ValueOf(v)
63-
if rv.Kind() != reflect.Pointer || rv.IsNil() {
64-
return errors.Wrapf(ErrInvalidArgument, "non-nil pointer expected, got %T", v)
60+
func ParseFlags[T any](v *T) error {
61+
if v == nil {
62+
return errors.Wrap(ErrInvalidArgument, "got nil pointer")
6563
}
6664

6765
parser := flags.NewParser(v, flags.Default^flags.PrintErrors)

0 commit comments

Comments
 (0)