Skip to content

Commit 38f518b

Browse files
authored
Merge pull request #79 from Icinga/revert-72
Revert "Merge pull request #72 from Icinga/reflectPtr"
2 parents f25f8a0 + 3b49e51 commit 38f518b

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

config/config.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/jessevdk/go-flags"
99
"github.com/pkg/errors"
1010
"os"
11+
"reflect"
1112
)
1213

1314
// ErrInvalidArgument is the error returned by [ParseFlags] or [FromYAMLFile] if
@@ -18,9 +19,10 @@ var ErrInvalidArgument = stderrors.New("invalid argument")
1819
// FromYAMLFile parses the given YAML file and stores the result
1920
// in the value pointed to by v. If v is nil or not a pointer,
2021
// FromYAMLFile returns an [ErrInvalidArgument] error.
21-
func FromYAMLFile[T any, V validatorPtr[T]](name string, v V) error {
22-
if v == nil {
23-
return errors.Wrap(ErrInvalidArgument, "got nil pointer")
22+
func FromYAMLFile(name string, v Validator) error {
23+
rv := reflect.ValueOf(v)
24+
if rv.Kind() != reflect.Pointer || rv.IsNil() {
25+
return errors.Wrapf(ErrInvalidArgument, "non-nil pointer expected, got %T", v)
2426
}
2527

2628
// #nosec G304 -- Potential file inclusion via variable - Its purpose is to load any file name that is passed to it, so doesn't need to validate anything.
@@ -57,9 +59,10 @@ func FromYAMLFile[T any, V validatorPtr[T]](name string, v V) error {
5759
// ParseFlags prints the help message to [os.Stdout] and exits.
5860
// Note that errors are not printed automatically,
5961
// so error handling is the sole responsibility of the caller.
60-
func ParseFlags[T any](v *T) error {
61-
if v == nil {
62-
return errors.Wrap(ErrInvalidArgument, "got nil pointer")
62+
func ParseFlags(v any) error {
63+
rv := reflect.ValueOf(v)
64+
if rv.Kind() != reflect.Pointer || rv.IsNil() {
65+
return errors.Wrapf(ErrInvalidArgument, "non-nil pointer expected, got %T", v)
6366
}
6467

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

config/contracts.go

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,3 @@ package config
33
type Validator interface {
44
Validate() error
55
}
6-
7-
// validatorPtr combines the [Validator] interface with a pointer constraint.
8-
type validatorPtr[T any] interface {
9-
Validator
10-
*T
11-
}

0 commit comments

Comments
 (0)