value

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidDataType = fmt.Errorf("not a valid DataType, try [%s]", strings.Join(_DataTypeNames, ", "))
View Source
var ErrInvalidInputMode = fmt.Errorf("not a valid InputMode, try [%s]", strings.Join(_InputModeNames, ", "))
View Source
var ErrInvalidPromptConfig = fmt.Errorf("not a valid PromptConfig, try [%s]", strings.Join(_PromptConfigNames, ", "))
View Source
var (
	ErrUnknownTransformer = errors.New("undefined transform")
)

Functions

func DataTypeNames

func DataTypeNames() []string

DataTypeNames returns a list of possible string values of DataType.

func InputModeNames

func InputModeNames() []string

InputModeNames returns a list of possible string values of InputMode.

func PromptConfigNames

func PromptConfigNames() []string

PromptConfigNames returns a list of possible string values of PromptConfig.

func RegisterTransformer

func RegisterTransformer(t Transformer)

func Transform

func Transform(key string, value any, rule string) (any, error)

func UnregisterTransformer added in v0.5.1

func UnregisterTransformer(t Transformer)

Types

type DataMap

type DataMap map[string]any

DataMap is a shared data structure that each value uses when rendering. This allows for values to reference other values (via template expressions) in their defaults.

func NewDataMap

func NewDataMap() DataMap

NewDataMap returns a new, empty DataMap.

func (DataMap) Get

func (dm DataMap) Get(key string) any

func (DataMap) Set

func (dm DataMap) Set(key string, value any)

type DataType

type DataType string

Specifies the data type of a value.

ENUM(
	bool        // Boolean.
	int         // Integer.
	intSlice    // Integer array/slice.
	string      // String.
	stringSlice // String array/slice.
).
const (
	// DataTypeBool is a DataType of type bool.
	// Boolean.
	DataTypeBool DataType = "bool"
	// DataTypeInt is a DataType of type int.
	// Integer.
	DataTypeInt DataType = "int"
	// DataTypeIntSlice is a DataType of type intSlice.
	// Integer array/slice.
	DataTypeIntSlice DataType = "intSlice"
	// DataTypeString is a DataType of type string.
	// String.
	DataTypeString DataType = "string"
	// DataTypeStringSlice is a DataType of type stringSlice.
	// String array/slice.
	DataTypeStringSlice DataType = "stringSlice"
)

func ParseDataType

func ParseDataType(name string) (DataType, error)

ParseDataType attempts to convert a string to a DataType.

func (DataType) Description added in v0.5.1

func (x DataType) Description() string

Enum implements the jsonschema.Described interface.

func (DataType) Enum added in v0.4.0

func (x DataType) Enum() []any

Enum implements the jsonschema.Enum interface.

func (DataType) EnumComments added in v0.5.1

func (x DataType) EnumComments() []string

EnumComments returns the comment associated with each enum.

func (DataType) IsValid

func (x DataType) IsValid() bool

IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values

func (DataType) MarshalText

func (x DataType) MarshalText() ([]byte, error)

MarshalText implements the text marshaller method.

func (DataType) PrepareJSONSchema added in v0.4.0

func (x DataType) PrepareJSONSchema(schema *jsonschema.Schema) error

PrepareJSONSchema implements the jsonschema.Preparer interface.

func (DataType) String

func (x DataType) String() string

String implements the Stringer interface.

func (*DataType) UnmarshalText

func (x *DataType) UnmarshalText(text []byte) error

UnmarshalText implements the text unmarshaller method.

type InputMode

type InputMode string

Determines how the value can be set.

ENUM(
	arg    // Can be set via positional argument OR prompt.
	flag   // Can be set via flag OR prompt.
	hidden // Can only be set via user config.
).
const (
	// InputModeArg is a InputMode of type arg.
	// Can be set via positional argument OR prompt.
	InputModeArg InputMode = "arg"
	// InputModeFlag is a InputMode of type flag.
	// Can be set via flag OR prompt.
	InputModeFlag InputMode = "flag"
	// InputModeHidden is a InputMode of type hidden.
	// Can only be set via user config.
	InputModeHidden InputMode = "hidden"
)

func ParseInputMode

func ParseInputMode(name string) (InputMode, error)

ParseInputMode attempts to convert a string to a InputMode.

func (InputMode) Description added in v0.5.1

func (x InputMode) Description() string

Enum implements the jsonschema.Described interface.

func (InputMode) Enum added in v0.4.0

func (x InputMode) Enum() []any

Enum implements the jsonschema.Enum interface.

func (InputMode) EnumComments added in v0.5.1

func (x InputMode) EnumComments() []string

EnumComments returns the comment associated with each enum.

func (InputMode) IsValid

func (x InputMode) IsValid() bool

IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values

func (InputMode) MarshalText

func (x InputMode) MarshalText() ([]byte, error)

MarshalText implements the text marshaller method.

func (InputMode) PrepareJSONSchema added in v0.4.0

func (x InputMode) PrepareJSONSchema(schema *jsonschema.Schema) error

PrepareJSONSchema implements the jsonschema.Preparer interface.

func (InputMode) String

func (x InputMode) String() string

String implements the Stringer interface.

func (*InputMode) UnmarshalText

func (x *InputMode) UnmarshalText(text []byte) error

UnmarshalText implements the text unmarshaller method.

type PromptConfig

type PromptConfig string

Determines when a value should prompt for input.

ENUM(
	always   // Always prompt.
	never    // Never prompt.
	on-empty // Only when input OR default is blank/zero.
	on-unset // Only when not explicitly set via CLI.
).
const (
	// PromptConfigAlways is a PromptConfig of type always.
	// Always prompt.
	PromptConfigAlways PromptConfig = "always"
	// PromptConfigNever is a PromptConfig of type never.
	// Never prompt.
	PromptConfigNever PromptConfig = "never"
	// PromptConfigOnEmpty is a PromptConfig of type on-empty.
	// Only when input OR default is blank/zero.
	PromptConfigOnEmpty PromptConfig = "on-empty"
	// PromptConfigOnUnset is a PromptConfig of type on-unset.
	// Only when not explicitly set via CLI.
	PromptConfigOnUnset PromptConfig = "on-unset"
)

func ParsePromptConfig

func ParsePromptConfig(name string) (PromptConfig, error)

ParsePromptConfig attempts to convert a string to a PromptConfig.

func (PromptConfig) Description added in v0.5.1

func (x PromptConfig) Description() string

Enum implements the jsonschema.Described interface.

func (PromptConfig) Enum added in v0.4.0

func (x PromptConfig) Enum() []any

Enum implements the jsonschema.Enum interface.

func (PromptConfig) EnumComments added in v0.5.1

func (x PromptConfig) EnumComments() []string

EnumComments returns the comment associated with each enum.

func (PromptConfig) IsValid

func (x PromptConfig) IsValid() bool

IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values

func (PromptConfig) MarshalText

func (x PromptConfig) MarshalText() ([]byte, error)

MarshalText implements the text marshaller method.

func (PromptConfig) PrepareJSONSchema added in v0.4.0

func (x PromptConfig) PrepareJSONSchema(schema *jsonschema.Schema) error

PrepareJSONSchema implements the jsonschema.Preparer interface.

func (PromptConfig) String

func (x PromptConfig) String() string

String implements the Stringer interface.

func (*PromptConfig) UnmarshalText

func (x *PromptConfig) UnmarshalText(text []byte) error

UnmarshalText implements the text unmarshaller method.

type Transformer

type Transformer struct {
	Name        string
	Description string
	Func        TransformerFunc
}

func GetTransformer

func GetTransformer(name string) (Transformer, error)

GetTransformer returns the transformer registered for name. If name is not found, returns ErrUnknownTransformer.

func RegisteredTransformers added in v0.5.1

func RegisteredTransformers() []Transformer

type TransformerFunc added in v0.5.1

type TransformerFunc func(any) (any, error)

TransformerFunc is a function used to process value data.

func StringTransformerFunc added in v0.5.1

func StringTransformerFunc(f func(s string) string) TransformerFunc

StringTransformerFunc accepts a string function and returns a TransformerFunc that delegates to it.

type Value

type Value struct {
	// Note: have to use `DataType` because `Type()` is a pflag.Value method.
	Key             string       `mapstructure:"key"                           validate:"required"`
	Name            string       `mapstructure:"name"`
	Flag            string       `mapstructure:"flag"`
	Help            string       `mapstructure:"help"`
	DataType        DataType     `mapstructure:"type"      default:"string"    validate:"required,oneof=bool int intSlice string stringSlice"` //nolint:lll
	Default         any          `mapstructure:"default"`
	PromptConfig    PromptConfig `mapstructure:"prompt"    default:"on-unset"  validate:"required,oneof=always never on-empty on-unset"` //nolint:lll
	InputMode       InputMode    `mapstructure:"mode"      default:"flag"      validate:"required,oneof=arg flag hidden"`
	TransformRules  string       `mapstructure:"transform"`
	ValidationRules string       `mapstructure:"validate"`
	Options         []any        `mapstructure:"options"   nullable:"false"`
	If              string       `mapstructure:"if"        default:"true"`
	// contains filtered or unexported fields
}

func NewValue

func NewValue(valueData map[string]any) (*Value, error)

NewValue returns a new Value struct for the given map of data.

func (*Value) DisplayName

func (v *Value) DisplayName() string

DisplayName returns the human friendly display name.

func (*Value) FlagName

func (v *Value) FlagName() string

FlagName returns the kebab-cased flag name.

func (*Value) Get

func (v *Value) Get() any

Get returns the rendered, casted value. Required to implement flag.Getter interface.

func (*Value) IsArg

func (v *Value) IsArg() bool

IsArg returns true if InputMode is "arg".

func (*Value) IsBoolFlag

func (v *Value) IsBoolFlag() bool

IsBoolFlag returns true if the data type is `bool`. Required to implement [pflag.boolFlag] interface.

func (*Value) IsEmpty

func (v *Value) IsEmpty() bool

IsEmpty returns true if the value is empty.

func (*Value) IsEnabled added in v0.3.0

func (v *Value) IsEnabled() bool

IsEnabled returns true when the `If` field evaluates to true. The user is only prompted for enabled values.

func (*Value) IsFlag

func (v *Value) IsFlag() bool

IsFlag returns true if InputMode is "flag".

func (*Value) IsHidden

func (v *Value) IsHidden() bool

IsHidden returns true if InputMode is "hidden".

func (*Value) IsUnset

func (v *Value) IsUnset() bool

IsUnset returns true if a value has not been explicitly set.

func (Value) PrepareJSONSchema added in v0.4.1

func (Value) PrepareJSONSchema(schema *jsonschema.Schema) error

PrepareJSONSchema implements the jsonschema.Preparer interface.

func (*Value) Prompt

func (v *Value) Prompt(prompter ui.Prompter) error

Prompt prompts the user for a value.

func (*Value) Set

func (v *Value) Set(data string) error

Set sets the value. Returns an error if the input data can not be casted to the correct type. Required to implement the pflag.Value interface.

func (*Value) ShouldPrompt

func (v *Value) ShouldPrompt() bool

ShouldPrompt returns true if the user should be prompted for a value.

func (*Value) String

func (v *Value) String() string

Required to implement the pflag.Value interface.

func (*Value) Type

func (v *Value) Type() string

Required to implement the pflag.Value interface.

func (*Value) Validate

func (v *Value) Validate() error

Validate evaluates the configured validation rules.

func (*Value) ValueSet

func (v *Value) ValueSet() *ValueSet

func (*Value) WithValueCache

func (v *Value) WithValueCache(dm DataMap) *Value

WithValueCache sets dm and returns the receiver. Should only be used in tests.

func (*Value) WithValueSet

func (v *Value) WithValueSet(vs *ValueSet) *Value

WithValueSet sets vs and returns the receiver.

type ValueSet

type ValueSet struct {
	// contains filtered or unexported fields
}

ValueSet is a unique set of Values (identified by Value.Key).

func NewValueSet

func NewValueSet() *ValueSet

NewValueSet returns a new ValueSet.

func (*ValueSet) Add

func (vs *ValueSet) Add(value *Value) *ValueSet

Add appends a value to the set. Values are identified by Value.Key and duplicates are overwritten.

func (*ValueSet) All

func (vs *ValueSet) All() []*Value

All returns all values in the set.

func (*ValueSet) Args

func (vs *ValueSet) Args() []*Value

Args returns only the arg values.

func (*ValueSet) Cache

func (vs *ValueSet) Cache() DataMap

Cache returns the materialized data map.

func (*ValueSet) Flags

func (vs *ValueSet) Flags() []*Value

Flags returns only the flag values.

func (*ValueSet) Get

func (vs *ValueSet) Get(key string) any

Get returns the value data for key. If key is not found, then returns the data for key from the cache.

func (*ValueSet) GetAll

func (vs *ValueSet) GetAll() map[string]any

GetAll returns all data in the set.

func (*ValueSet) Len

func (vs *ValueSet) Len() int

Len returns the number of values in the set.

func (*ValueSet) Partition

func (vs *ValueSet) Partition() ([]*Value, []*Value)

Partition partitions values into args and flags.

func (*ValueSet) Prepend added in v0.3.0

func (vs *ValueSet) Prepend(value *Value) *ValueSet

Prepend prepends a value to the set. Values are identified by Value.Key and duplicates are overwritten.

func (*ValueSet) Prompt

func (vs *ValueSet) Prompt(prompter ui.Prompter) error

Prompt calls Value.Prompt() for each value in the set. Returns the first error received.

func (*ValueSet) Set

func (vs *ValueSet) Set(key string, value any) error

Set sets the value data for key. If key is not found, then sets the data in the cache so that it can be used by other values (see SrcPath and DstPath in stamp.Generator).

func (*ValueSet) SetArgs

func (vs *ValueSet) SetArgs(args []string) ([]string, error)

SetArgs attempts to set all positional values with args. If len(args) > len(ValueSet.Args()), then the remaining items in args are returned.

func (*ValueSet) SetCache

func (vs *ValueSet) SetCache(dataMap DataMap) *ValueSet

SetCache replaces the materialized data map.

func (*ValueSet) Validate

func (vs *ValueSet) Validate() error

Validate calls Value.Validate() for each value in the set. Returns the first error received.

func (*ValueSet) Value

func (vs *ValueSet) Value(key string) *Value

Value returns the value for key.

Jump to

Keyboard shortcuts

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