optionutils

package
v0.0.0-...-e28bee9 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: Apache-2.0 Imports: 13 Imported by: 70

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyArbitraryOptions

func ApplyArbitraryOptions[I ArbitraryOption, O any](opts O, list ...I)

ApplyArbitraryOptions applies options to an option target O. O must either be a target interface type or a target struct pointer type. I is the option interface declaring the apply method.

func ApplyOption

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

func ApplyOptionByFunc

func ApplyOptionByFunc[T any](opt *T, set func(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 BoolOption

func BoolOption(b ...bool) bool

BoolOption can be used to implement Flag options, with optional vool argument enabled by default.

func BoolP

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

func DefaultedFileSystem

func DefaultedFileSystem(def vfs.FileSystem, fss ...vfs.FileSystem) vfs.FileSystem

func EvalArbitraryOptions

func EvalArbitraryOptions[I ArbitraryOption, O any](opts ...I) *O

EvalArbitraryOptions applies options to a new options object of type O and returns a pointer to this object. O must be a struct type.

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 FileSystem

func FileSystem(fss ...vfs.FileSystem) vfs.FileSystem

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 deprecated

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

Deprecated: use generics.PointerTo.

func ReadFile

func ReadFile(in string, fss ...vfs.FileSystem) ([]byte, error)

ReadFile maps a data specification (typically given via command line) to data like ResolveData, but uses reading from a file as default.

func ResolveData

func ResolveData(in string, fss ...vfs.FileSystem) ([]byte, error)

ResolveData maps a data specification (typically given via command line) to data. It uses various prefixes to specify different content sources.

  • = direct data passed as string
  • ! base64 encoded binary data
  • @ taken from a file

The default (no such prefix given) is using content as direct data.

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 ArbitraryOption

type ArbitraryOption interface {
}

ArbitraryOption is just a generic interface name to indicate the purpose for parameters. Because Go does not support parameter overloading, different option targets may need to use different apply method names, if an option should be usable for multiple target types. Therefore, a simple generic common Eval method is not possible, because it must call the method with a dedicated name for the intended target. This package provides some generic implementation being able to call apply methods based on a given non-standard option interface. This type is used to indicate such a type. The concrete type must be an option interface type with a single apply method.

type MappedOption

type MappedOption[B any] interface {
	Unwrap() Option[B]
}

MappedOption is the interface for an option mapped to another option target. It returns the original option.

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 FilterMappedOptions

func FilterMappedOptions[T any, S any](opts ...S) []Option[T]

FilterMappedOptions filters options (for S) for options mapped to another option interface given by type parameter 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[B] 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 then 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