optionutils

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyOption

func ApplyOption[T any](opt *T, tgt **T)

func ApplyOptions

func ApplyOptions[O any](opts O, list ...Option[O])

ApplyOptions applies options to an option target O. O must either be a target interface type or a target struct pointer type.

func AsBool added in v0.8.0

func AsBool(b *bool, def ...bool) bool

func AsValue

func AsValue[T any](p *T) T

func EvalOptions

func EvalOptions[O any](opts ...Option[*O]) *O

EvalOptions applies options to a new options object and returns this object. O must be a struct type.

func GetOptionFlag added in v0.10.0

func GetOptionFlag(list ...bool) bool

GetOptionFlag returns the flag value used to set a bool option based on optionally specified explicit value(s). The default value is to enable the option (true).

func PointerTo

func PointerTo[T any](v T) *T

Types

type NestedOptionsProvider

type NestedOptionsProvider[T any] interface {
	NestedOptions() T
}

NestedOptionsProvider is the interface for a more specific options object to provide access to a nested options object of type T. T must be a pointer type.

type Option

type Option[T any] interface {
	ApplyTo(T)
}

func MapOptionTarget

func MapOptionTarget[W, B any](opt Option[B]) Option[W]

MapOptionTarget maps the option target interface from B to W, hereby, W must be a subtype of B, which cannot be expressed with Go generics (Type constraint should be W B). If this constraint is not met, there will be a runtime error.

func OptionWrapper

func OptionWrapper[N, O any, P OptionTargetProvider[N, O]](o Option[N]) Option[P]

OptionWrapper genericly wraps a nested option of type Option[N] to an option of type Option[*O], assuming that the nested option source N implements NestedOptionsProvider[N]. P is only a helper type parameter for Go and doesn't need to be given. It is the pointer type for O (P = *O).

create a wrap option function for all wrappable options with

 func WrapXXX[O any, P optionutils.OptionTargetProvider[*Options, O]](args...any) optionutils.Option[P] {
		return optionutils.OptionWrapper[*Options, O, P](WithXXX(args...))
 }

where *Options is the type of the pointer type to the options object to be nested.

The outer option functions wrapping the nested one can then be defined as

func WithXXX(h string) Option {
	return optionutils.WrapXXX[Options](h)
}

For an example see package github.com/open-component-model/ocm/pkg/contexts/ocm/resourcetypes/rpi.

func OptionWrapperFunc

func OptionWrapperFunc[N, O any](o Option[N], nested func(outer O) N) Option[O]

type OptionTargetProvider

type OptionTargetProvider[N, O any] interface {
	NestedOptionsProvider[N]
	*O
}

OptionTargetProvider is helper interface to declare a pointer type (*O) for an options object providing access to a nested options object of type N (must be a pointer type).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL