Skip to content

Commit

Permalink
Merge pull request #79 from Icinga/revert-72
Browse files Browse the repository at this point in the history
Revert "Merge pull request #72 from Icinga/reflectPtr"
  • Loading branch information
yhabteab authored Oct 21, 2024
2 parents f25f8a0 + 3b49e51 commit 38f518b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 9 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
"os"
"reflect"
)

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

// #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.
Expand Down Expand Up @@ -57,9 +59,10 @@ func FromYAMLFile[T any, V validatorPtr[T]](name string, v V) error {
// ParseFlags prints the help message to [os.Stdout] and exits.
// Note that errors are not printed automatically,
// so error handling is the sole responsibility of the caller.
func ParseFlags[T any](v *T) error {
if v == nil {
return errors.Wrap(ErrInvalidArgument, "got nil pointer")
func ParseFlags(v any) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Pointer || rv.IsNil() {
return errors.Wrapf(ErrInvalidArgument, "non-nil pointer expected, got %T", v)
}

parser := flags.NewParser(v, flags.Default^flags.PrintErrors)
Expand Down
6 changes: 0 additions & 6 deletions config/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,3 @@ package config
type Validator interface {
Validate() error
}

// validatorPtr combines the [Validator] interface with a pointer constraint.
type validatorPtr[T any] interface {
Validator
*T
}

0 comments on commit 38f518b

Please sign in to comment.