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

Revert "Merge pull request #72 from Icinga/reflectPtr" #79

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
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
}
Loading