optionutils

package
v0.0.0-...-afe3bd2 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2024 License: Apache-2.0 Imports: 6 Imported by: 72

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

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

func AsValue

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

func BoolP

func BoolP[T ~bool](b T) *bool

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

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

func Transfer

func Transfer[T comparable](t *T, v T)

Transfer transfers an option value from aan option object to a target value. If the option value in initial is is not transferred.

func TransferOptional

func TransferOptional[T comparable](t *T, v *T)

TransferOptional transfers an optional option value given as pointer type (nil means not set) from an option object to a target value, if it is set.

func TransferSlice

func TransferSlice[T comparable](t *[]T, v []T, empty ...bool)

TransferSlice transfers an optional slice option value from an option object to a target value, if it is set. It is assumed to be set, if it is non-nil or (if empty is set to true) it is an empty slice.

func WithDefaults

func WithDefaults[O any](opts []O, defaults ...O) []O

WithDefaults prepends a given option list by an arbitrary number of default options. Those options will be evaluated before the given option set. They will be overridden later by the explicitly specified option set.

For example:

func FuncWithOptions(ctx SomeType, opts...Option) {
   doSomethingWithDefaultedOptions(WithDefaults(opts, WithOther(ctx.Other())...)
}

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 NoOption

type NoOption[T any] struct{}

NoOption is an option that does nothing. This can be returned by Option Factory functions instead of nil to avoid consecutive nil pointer exceptions.

func (NoOption[T]) ApplyTo

func (NoOption[T]) ApplyTo(T)

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]

func WithGenericOption

func WithGenericOption[S, B any, T any](v T) Option[B]

WithGenericOption provides a generic option implementation for Option[T] intended for options based on an option setter interface S implemented by the option set B implementing S for the value type T. Hereby, B must implement S, which cannot be expressed by Go generics.

func WithOptionalGenericOption

func WithOptionalGenericOption[S, B any, T any](v *T) Option[B]

WithOptionalGenericOption povides an option if the given pointer is not nil. It the behaves like WithGenericOption for the de-referenced value.

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