Documentation
¶
Index ¶
- Constants
- func GenerateUseString(cmdName string, pds *Definitions) string
- func ParseDate(value string) (time.Time, error)
- func RenderValue(type_ Type, value interface{}) (string, error)
- func StructToDataMap(s interface{}) (map[string]interface{}, error)
- type Definition
- func (p *Definition) CheckDefaultValueValidity() (interface{}, error)
- func (p *Definition) CheckValueValidity(v interface{}) (interface{}, error)
- func (p *Definition) Clone() *Definition
- func (p *Definition) GatherValueFromInterface(value reflect.Value) error
- func (p *Definition) InitializeValueToEmptyValue(value reflect.Value) error
- func (p *Definition) IsEqualToDefault(i interface{}) bool
- func (p *Definition) ParseField(v []string, options ...ParseOption) (*FieldValue, error)
- func (p *Definition) ParseFromReader(f io.Reader, filename string, options ...ParseOption) (*FieldValue, error)
- func (p *Definition) SetDefaultFromValue(value reflect.Value) error
- func (p *Definition) SetValueFromDefault(value reflect.Value) error
- func (p *Definition) SetValueFromInterface(value reflect.Value, v interface{}) error
- func (p *Definition) String() string
- type Definitions
- func (pds *Definitions) AddFieldsToCobraCommand(cmd *cobra.Command, prefix string) error
- func (pds *Definitions) Clone() *Definitions
- func (pds *Definitions) FieldValuesFromDefaults() (*FieldValues, error)
- func (pds *Definitions) ForEach(f func(definition *Definition))
- func (pds *Definitions) ForEachE(f func(definition *Definition) error) error
- func (pds *Definitions) GatherArguments(args []string, onlyProvided bool, ignoreRequired bool, ...) (*FieldValues, error)
- func (pds *Definitions) GatherFieldsFromMap(m map[string]interface{}, onlyProvided bool, options ...ParseOption) (*FieldValues, error)
- func (pds *Definitions) GatherFlagsFromCobraCommand(cmd *cobra.Command, onlyProvided bool, ignoreRequired bool, prefix string, ...) (*FieldValues, error)
- func (pds *Definitions) GatherFlagsFromStringList(args []string, onlyProvided bool, ignoreRequired bool, prefix string, ...) (*FieldValues, []string, error)
- func (pds *Definitions) GatherFlagsFromViper(onlyProvided bool, prefix string, options ...ParseOption) (*FieldValues, error)deprecated
- func (pds *Definitions) GetArguments() *Definitions
- func (pds *Definitions) GetDefaultValue(key string, defaultValue interface{}) interface{}
- func (pds *Definitions) GetFlags() *Definitions
- func (pds *Definitions) InitializeDefaultsFromMap(ps map[string]interface{}) error
- func (pds *Definitions) InitializeDefaultsFromStruct(s interface{}) error
- func (pds *Definitions) InitializeStructFromDefaults(s interface{}) error
- func (pds *Definitions) MarshalYAML() (interface{}, error)
- func (pds *Definitions) Merge(m *Definitions) *Definitions
- func (pds *Definitions) ToList() []*Definition
- func (pds *Definitions) UnmarshalYAML(value *yaml.Node) error
- type DefinitionsOption
- type ErrInvalidValue
- type FieldValue
- func (p *FieldValue) Clone() *FieldValue
- func (p *FieldValue) GetInterfaceValue() (interface{}, error)
- func (p *FieldValue) Merge(v *FieldValue, options ...ParseOption)
- func (p *FieldValue) RenderValue() (string, error)
- func (p *FieldValue) Set(value interface{}, log ...ParseStep)
- func (p *FieldValue) Update(value interface{}, options ...ParseOption) error
- func (p *FieldValue) UpdateWithLog(value interface{}, log ...ParseStep) error
- type FieldValues
- func (p *FieldValues) Clone() *FieldValues
- func (p *FieldValues) DecodeInto(s interface{}) error
- func (p *FieldValues) ForEach(f func(key string, value *FieldValue))
- func (p *FieldValues) ForEachE(f func(key string, value *FieldValue) error) error
- func (p *FieldValues) GetValue(key string) interface{}
- func (p *FieldValues) MarshalJSON() ([]byte, error)
- func (p *FieldValues) MarshalYAML() (interface{}, error)
- func (p *FieldValues) Merge(other *FieldValues) (*FieldValues, error)
- func (p *FieldValues) MergeAsDefault(other *FieldValues, options ...ParseOption) (*FieldValues, error)
- func (p *FieldValues) MustUpdateValue(key string, v interface{}, options ...ParseOption) error
- func (p *FieldValues) SetAsDefault(key string, pd *Definition, v interface{}, options ...ParseOption) error
- func (p *FieldValues) ToInterfaceMap() (map[string]interface{}, error)
- func (p *FieldValues) ToMap() map[string]interface{}
- func (p *FieldValues) Update(key string, pp *FieldValue)
- func (p *FieldValues) UpdateExistingValue(key string, v interface{}, options ...ParseOption) (bool, error)
- func (p *FieldValues) UpdateValue(key string, pd *Definition, v interface{}, options ...ParseOption) error
- func (p *FieldValues) UpdateWithLog(key string, pd *Definition, v interface{}, log ...ParseStep) error
- type FieldValuesOption
- type FileData
- type FileType
- type Option
- type ParseOption
- type ParseStep
- type SerializableFieldValue
- type SerializableFieldValues
- type Type
Constants ¶
const SourceDefaults = "defaults"
Variables ¶
This section is empty.
Functions ¶
func GenerateUseString ¶ added in v1.0.0
func GenerateUseString(cmdName string, pds *Definitions) string
GenerateUseString creates a string representation of the 'Use' field for a given cobra command and a list of field definitions. The first word of the existing 'Use' field is treated as the verb for the command. The resulting string briefly describes how to use the command respecting the following conventions:
- Required fields are enclosed in '<>'.
- Optional fields are enclosed in '[]'.
- Optional fields that accept multiple input (TypeStringList or TypeIntegerList) are followed by '...'.
- If a field has a default value, it is specified after field name like 'field (default: value)'.
For example:
- If there is a required field 'name', and an optional field 'age' with a default value of '30', the resulting string will be: 'verb <name> [age (default: 30)]'.
- If there is a required field 'name', and an optional field 'colors' of type TypeStringList, the resulting Use string will be: 'verb <name> [colors...]'
func ParseDate ¶ added in v1.0.0
ParseDate parses a string into a time.Time based on predefined date formats.
It first tries parsing with dateparse.ParseAny using standard formats. If that fails, it tries naturaldate.Parse which handles relative natural language dates.
If both parsing attempts fail, an error is returned. The reference time passed to naturaldate.Parse defaults to time.Now().
func RenderValue ¶ added in v1.0.0
RenderValue renders the given value to string so that it can be parsed as a cobra command line flag. TODO(manuel, 2023-09-09) Refactor rendering of values to strings that can be parsed. This is only applicable to parsing using cobra, but really we now have many more ways of parsing a flag out of a string, among which GET query and FORM input fields.
func StructToDataMap ¶ added in v1.0.0
StructToDataMap transforms a struct into a map[string]interface{} based on the `glazed` annotations.
If a struct field is annotated with `glazed:"<pattern>*"` (contains a wildcard `*`), the field is expected to be a map. The function will match the map keys against the wildcard pattern and include the matching key-value pairs in the resulting data map.
Returns an error if: - Parsing the `glazed` tag fails for any field. - A field annotated with a wildcard is not a map.
Types ¶
type Definition ¶
type Definition struct {
Name string `yaml:"name"`
ShortFlag string `yaml:"shortFlag,omitempty"`
Type Type `yaml:"type"`
Help string `yaml:"help,omitempty"`
Default *interface{} `yaml:"default,omitempty"`
Choices []string `yaml:"choices,omitempty"`
Required bool `yaml:"required,omitempty"`
IsArgument bool `yaml:"-"`
}
Definition is a declarative way of describing a command line field. A Definition can be either a Flag or an Argument. Along with metadata (Name, Help) that is useful for help, it also specifies a Type, a Default value and if it is Required.
func (*Definition) CheckDefaultValueValidity ¶ added in v1.0.0
func (p *Definition) CheckDefaultValueValidity() (interface{}, error)
CheckDefaultValueValidity checks if the Definition's Default is valid. This is used when validating loading from a YAML file or setting up cobra flag definitions.
func (*Definition) CheckValueValidity ¶ added in v1.0.0
func (p *Definition) CheckValueValidity(v interface{}) (interface{}, error)
CheckValueValidity checks if the given value is valid for the Definition, and returns the value cast to the correct type.
func (*Definition) Clone ¶ added in v1.0.0
func (p *Definition) Clone() *Definition
func (*Definition) GatherValueFromInterface ¶ added in v1.0.0
func (p *Definition) GatherValueFromInterface(value reflect.Value) error
func (*Definition) InitializeValueToEmptyValue ¶ added in v1.0.0
func (p *Definition) InitializeValueToEmptyValue(value reflect.Value) error
InitializeValueToEmptyValue initializes the given value to the empty value of the type of the field.
func (*Definition) IsEqualToDefault ¶ added in v1.0.0
func (p *Definition) IsEqualToDefault(i interface{}) bool
func (*Definition) ParseField ¶ added in v1.0.0
func (p *Definition) ParseField(v []string, options ...ParseOption) (*FieldValue, error)
ParseField parses command line arguments according to the given Definition. It returns the parsed field value and a non-nil error if parsing failed.
The function takes a list of strings that can be gathered from the command line arguments. This is because cobra for example allows slice flags to be passed by reusing the same flag multiple times (or by parsing comma-separated values).
If the field is required and not provided, an error is returned. If the field is optional and not provided, the default value is returned.
## Expected type parsing
The Definition specifies the expected type and how to parse the arguments:
- TypeString: parsed from a single string value
- TypeInteger, TypeFloat, TypeBool: parsed from a single value
- TypeStringList, TypeIntegerList, TypeFloatList: parsed from multiple values
- TypeFile: load file contents into a FileData struct
- TypeFileList: load multiple files into []*FileData
- TypeChoice, TypeChoiceList: validated against allowed choices
- TypeKeyValue: parsed from colon separated strings or files
- TypeObjectListFromFile, TypeObjectListFromFiles: deserialized object lists from JSON/YAML files
- TypeObjectFromFile: deserialized a single object from a JSON/YAML file
- TypeStringFromFile, TypeStringFromFiles: load file contents as strings
- TypeStringListFromFile, TypeStringListFromFiles: load file lines as a string list
- TypeDate: parsed into time.Time
The parsing logic depends on the Type in the Definition.
## Type -> Bype mappings
TypeString -> string TypeInteger -> int TypeFloat -> float64 TypeBool -> bool TypeStringList -> []string TypeIntegerList -> []int TypeFloatList -> []float64 TypeChoice -> string TypeChoiceList -> []string TypeDate -> time.Time TypeFile -> *FileData TypeFileList -> []*FileData TypeObjectListFromFile -> []interface{} TypeObjectFromFile -> map[string]interface{} TypeStringFromFile -> string TypeStringFromFiles -> string TypeStringListFromFile -> []string TypeStringListFromFiles -> []string TypeKeyValue -> map[string]interface{}
TODO(manuel, 2023-12-22) We should provide the parsing context from higher up here, instead of just calling it strings
func (*Definition) ParseFromReader ¶ added in v1.0.0
func (p *Definition) ParseFromReader( f io.Reader, filename string, options ...ParseOption, ) (*FieldValue, error)
ParseFromReader parses a single element for the type from the reader. In the case of fields taking multiple files, this needs to be called for each file and merged at the caller level.
func (*Definition) SetDefaultFromValue ¶ added in v1.0.0
func (p *Definition) SetDefaultFromValue(value reflect.Value) error
SetDefaultFromValue sets the Default field of the Definition to the provided value. It handles nil values and dereferencing pointers. The value is type checked before being set.
func (*Definition) SetValueFromDefault ¶ added in v1.0.0
func (p *Definition) SetValueFromDefault(value reflect.Value) error
SetValueFromDefault assigns the default value of the Definition to the given value. If the Default value is nil, the value is set to the zero value of the type.
func (*Definition) SetValueFromInterface ¶ added in v1.0.0
func (p *Definition) SetValueFromInterface(value reflect.Value, v interface{}) error
SetValueFromInterface assigns the value v to the given reflect.Value based on the Definition's type. It handles type checking and conversion for the various supported field types.
func (*Definition) String ¶ added in v1.0.0
func (p *Definition) String() string
type Definitions ¶
type Definitions struct {
*orderedmap.OrderedMap[string, *Definition]
}
Definitions is an ordered map of Definition.
func LoadDefinitionsFromYAML ¶ added in v1.0.0
func LoadDefinitionsFromYAML(yamlContent []byte) *Definitions
LoadDefinitionsFromYAML loads a map of Definitions from a YAML file. It checks that default values are valid. It returns the Definitions as a map indexed by name, and as a list.
func NewDefinitions ¶
func NewDefinitions(options ...DefinitionsOption) *Definitions
func (*Definitions) AddFieldsToCobraCommand ¶ added in v1.0.0
func (pds *Definitions) AddFieldsToCobraCommand( cmd *cobra.Command, prefix string, ) error
AddFieldsToCobraCommand takes the fields from a CommandDescription and converts them to cobra flags, before adding them to the Fields() of a the passed cobra command.
func (*Definitions) Clone ¶ added in v1.0.0
func (pds *Definitions) Clone() *Definitions
Clone returns a cloned copy of the Definitions. The field definitions are cloned as well.
func (*Definitions) FieldValuesFromDefaults ¶ added in v1.0.0
func (pds *Definitions) FieldValuesFromDefaults() (*FieldValues, error)
FieldValuesFromDefaults uses the field definitions default values to create a FieldValues object.
func (*Definitions) ForEach ¶ added in v1.0.0
func (pds *Definitions) ForEach(f func(definition *Definition))
ForEach calls the given function f on each field definition in p.
func (*Definitions) ForEachE ¶ added in v1.0.0
func (pds *Definitions) ForEachE(f func(definition *Definition) error) error
ForEachE calls the given function f on each field definition in p. If f returns an error, ForEachE stops iterating and returns the error immediately.
func (*Definitions) GatherArguments ¶ added in v1.0.0
func (pds *Definitions) GatherArguments( args []string, onlyProvided bool, ignoreRequired bool, parseOptions ...ParseOption, ) (*FieldValues, error)
GatherArguments parses positional string arguments into a map of values.
It takes the command-line arguments, the expected argument definitions, and a boolean indicating whether to only include explicitly provided arguments, not the default values of missing arguments.
Only the last field definitions can be a list field type.
Required arguments missing from the input will result in an error. Arguments with default values can be included based on the onlyProvided flag.
The result is a map with argument names as keys and parsed values. Argument order is maintained.
Any extra arguments not defined will result in an error. Parsing errors for individual arguments will also return errors.
func (*Definitions) GatherFieldsFromMap ¶ added in v1.0.0
func (pds *Definitions) GatherFieldsFromMap( m map[string]interface{}, onlyProvided bool, options ...ParseOption, ) (*FieldValues, error)
GatherFieldsFromMap gathers field values from a map.
For each Definition, it checks if a matching value is present in the map:
- If the field is missing and required, an error is returned. - If the field is missing and optional, the default value is used. - If the value is provided, it is validated against the definition.
Values are looked up by field name, as well as short flag if provided.
The returned map contains the gathered field values, with defaults filled in for any missing optional fields.
func (*Definitions) GatherFlagsFromCobraCommand ¶ added in v1.0.0
func (pds *Definitions) GatherFlagsFromCobraCommand( cmd *cobra.Command, onlyProvided bool, ignoreRequired bool, prefix string, options ...ParseOption, ) (*FieldValues, error)
GatherFlagsFromCobraCommand gathers the flags from the cobra command, and parses them according to the field description passed in params. The result is a map of field names to parsed values.
If onlyProvided is true, only fields that are provided by the user are returned (i.e. not the default values).
If a field cannot be parsed correctly, or is missing even though it is not optional, an error is returned.
The required argument checks that all the required field definitions are present. The provided argument only checks that the provided flags are passed. Prefix is prepended to all flag names.
func (*Definitions) GatherFlagsFromStringList ¶ added in v1.0.0
func (pds *Definitions) GatherFlagsFromStringList( args []string, onlyProvided bool, ignoreRequired bool, prefix string, parseOptions ...ParseOption, ) (*FieldValues, []string, error)
GatherFlagsFromStringList parses command line arguments into a FieldValues map. It accepts a slice of string arguments, bools to control required/provided flag handling, and a prefix to prepend to flag names.
It returns the parsed fields map, any non-flag arguments, and any error encountered during parsing.
onlyProvided controls whether to only include flags that were explicitly provided on the command line. If false, default values will be included for any flags that were not provided.
ignoreRequired controls whether required flags are enforced. If true, missing required flags will not trigger an error.
prefix allows prepending a string to flag names to namespace them. For example, a prefix of "user" would namespace flags like "--name" to "--user-name". This allows reuse of a flag set across different sub-commands. The prefix has "-" appended automatically.
func (*Definitions) GatherFlagsFromViper
deprecated
added in
v1.0.0
func (pds *Definitions) GatherFlagsFromViper( onlyProvided bool, prefix string, options ...ParseOption, ) (*FieldValues, error)
Deprecated: Use config files + env middlewares instead.
func (*Definitions) GetArguments ¶ added in v1.0.0
func (pds *Definitions) GetArguments() *Definitions
GetArguments returns a new Definitions containing only the argument fields. The field definitions are not cloned.
func (*Definitions) GetDefaultValue ¶ added in v1.0.0
func (pds *Definitions) GetDefaultValue(key string, defaultValue interface{}) interface{}
func (*Definitions) GetFlags ¶ added in v1.0.0
func (pds *Definitions) GetFlags() *Definitions
GetFlags returns a new Definitions containing only the flag fields. The field definitions are not cloned.
func (*Definitions) InitializeDefaultsFromMap ¶ added in v1.0.0
func (pds *Definitions) InitializeDefaultsFromMap( ps map[string]interface{}, ) error
func (*Definitions) InitializeDefaultsFromStruct ¶ added in v1.0.0
func (pds *Definitions) InitializeDefaultsFromStruct( s interface{}, ) error
InitializeDefaultsFromStruct initializes the fields definitions from a struct. Each field in the struct annotated with tag `glazed` will be used to set the default value of the corresponding definition in `fieldDefinitions`. If no `Definition` is found for a field, an error is returned. This is the inverse of InitializeStructFromDefaults.
func (*Definitions) InitializeStructFromDefaults ¶ added in v1.0.0
func (pds *Definitions) InitializeStructFromDefaults(s interface{}) error
InitializeStructFromDefaults initializes a struct from a map of field definitions.
Each field in the struct annotated with tag `glazed` will be set to the default value of the corresponding `Definition`. If no `Definition` is found for a field, an error is returned.
func (*Definitions) MarshalYAML ¶ added in v1.0.0
func (pds *Definitions) MarshalYAML() (interface{}, error)
func (*Definitions) Merge ¶ added in v1.0.0
func (pds *Definitions) Merge(m *Definitions) *Definitions
Merge merges the field definitions from m into p. It clones each field definition before adding it to p so that updates to p do not affect m.
func (*Definitions) ToList ¶ added in v1.0.0
func (pds *Definitions) ToList() []*Definition
func (*Definitions) UnmarshalYAML ¶ added in v1.0.0
func (pds *Definitions) UnmarshalYAML(value *yaml.Node) error
type DefinitionsOption ¶ added in v1.0.0
type DefinitionsOption func(*Definitions)
func WithDefinitionList ¶ added in v1.0.0
func WithDefinitionList(fieldDefinitions []*Definition) DefinitionsOption
func WithDefinitions ¶ added in v1.0.0
func WithDefinitions(fieldDefinitions *Definitions) DefinitionsOption
type ErrInvalidValue ¶ added in v1.0.0
func (ErrInvalidValue) Error ¶ added in v1.0.0
func (e ErrInvalidValue) Error() string
type FieldValue ¶ added in v1.0.0
type FieldValue struct {
Value interface{}
Definition *Definition
// Log contains a history of the parsing steps that were taken to arrive at the value.
// Last step is the final value.
Log []ParseStep
}
func (*FieldValue) Clone ¶ added in v1.0.0
func (p *FieldValue) Clone() *FieldValue
func (*FieldValue) GetInterfaceValue ¶ added in v1.0.0
func (p *FieldValue) GetInterfaceValue() (interface{}, error)
GetInterfaceValue returns the value as an interface{}. If the type of the field is a list, it will return a []interface{}. If the type is an object, it will return a map[string]interface{}. If the type is a list of objects, it will return a []interface{} of map[string]interface{}.
func (*FieldValue) Merge ¶ added in v1.0.0
func (p *FieldValue) Merge(v *FieldValue, options ...ParseOption)
func (*FieldValue) RenderValue ¶ added in v1.0.0
func (p *FieldValue) RenderValue() (string, error)
func (*FieldValue) Set ¶ added in v1.0.0
func (p *FieldValue) Set(value interface{}, log ...ParseStep)
Set sets the value of the fieldValue, and manually updates the log
func (*FieldValue) Update ¶ added in v1.0.0
func (p *FieldValue) Update(value interface{}, options ...ParseOption) error
Update sets the value of the fieldValue, and appends a new parseStep.
func (*FieldValue) UpdateWithLog ¶ added in v1.0.0
func (p *FieldValue) UpdateWithLog(value interface{}, log ...ParseStep) error
UpdateWithLog sets the value of the fieldValue, and appends the given log.
type FieldValues ¶ added in v1.0.0
type FieldValues struct {
*orderedmap.OrderedMap[string, *FieldValue]
}
func NewFieldValues ¶ added in v1.0.0
func NewFieldValues(options ...FieldValuesOption) *FieldValues
func (*FieldValues) Clone ¶ added in v1.0.0
func (p *FieldValues) Clone() *FieldValues
func (*FieldValues) DecodeInto ¶ added in v1.0.0
func (p *FieldValues) DecodeInto(s interface{}) error
DecodeInto decodes a struct from a FieldValues map.
It iterates through the struct fields looking for those tagged with "glazed". For each tagged field, it will lookup the corresponding field value in the FieldValues map and set the field's value.
If the tag open `from_json` is appended to `glazed` and the field value is a string, bytes, rawMessage or FileData, the value is parsed from json.
If the tag contains a wildcard, the function will match field names against the wildcard pattern and store the matches in a map in the destination field. The map can be of any type, as long as it is a map of strings. The same logic as for normal fields will be applied to the map entries.
Struct fields that are pointers to other structs are handled recursively.
Struct fields that are pointers will be dereferenced. If the pointer is nil, a new value will be allocated and set.
s should be a pointer to the struct to initialize.
ps is the FieldValues map to lookup field values from.
Example struct:
type CreateIndexSettings struct {
Index string `glazed:"index"`
Settings *IndexSettings `glazed:"settings,from_json"`
Mappings *fields.FileData `glazed:"mappings"`
Aliases *map[string]Alias `glazed:"aliases,from_json"`
WaitForActiveShards string `glazed:"wait_for_active_shards"`
ApiKeys map[string]string `glazed:"*_api_key"`
}
Corresponding Definitions:
fields.New(
"index",
fields.TypeString,
fields.WithHelp("Name of the index to create"),
fields.WithRequired(true),
),
fields.New(
"settings",
fields.TypeFile,
fields.WithHelp("JSON file containing index settings"),
),
fields.New(
"mappings",
fields.TypeFile,
fields.WithHelp("JSON file containing index mappings"),
),
fields.New(
"aliases",
fields.TypeFile,
fields.WithHelp("JSON file containing index aliases"),
),
fields.New(
"wait_for_active_shards",
fields.TypeString,
fields.WithHelp("Set the number of active shards to wait for before the operation returns."),
),
fields.New(
"openai_api_key",
fields.TypeString,
fields.WithHelp("OpenAI API key"),
),
fields.New(
"google_api_key",
fields.TypeString,
fields.WithHelp("Google API key"),
),
Returns an error if: - s is not a pointer to a struct - A tagged field does not have a matching field value in ps - Failed to set the value of a field
func (*FieldValues) ForEach ¶ added in v1.0.0
func (p *FieldValues) ForEach(f func(key string, value *FieldValue))
ForEach applies the passed function to each key-value pair from oldest to newest in FieldValues.
func (*FieldValues) ForEachE ¶ added in v1.0.0
func (p *FieldValues) ForEachE(f func(key string, value *FieldValue) error) error
ForEachE applies the passed function (that returns an error) to each pair in FieldValues. It stops at, and returns, the first error encountered.
func (*FieldValues) GetValue ¶ added in v1.0.0
func (p *FieldValues) GetValue(key string) interface{}
func (*FieldValues) MarshalJSON ¶ added in v1.0.0
func (p *FieldValues) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for FieldValues
func (*FieldValues) MarshalYAML ¶ added in v1.0.0
func (p *FieldValues) MarshalYAML() (interface{}, error)
MarshalYAML implements yaml.Marshaler for FieldValues
func (*FieldValues) Merge ¶ added in v1.0.0
func (p *FieldValues) Merge(other *FieldValues) (*FieldValues, error)
Merge is actually more complex than it seems, other takes precedence. If the key already exists in the map, we actually merge the FieldValue themselves, by appending the entire history of the other field to the current one.
func (*FieldValues) MergeAsDefault ¶ added in v1.0.0
func (p *FieldValues) MergeAsDefault(other *FieldValues, options ...ParseOption) (*FieldValues, error)
MergeAsDefault only sets the value if the key does not already exist in the map.
func (*FieldValues) MustUpdateValue ¶ added in v1.0.0
func (p *FieldValues) MustUpdateValue( key string, v interface{}, options ...ParseOption, ) error
func (*FieldValues) SetAsDefault ¶ added in v1.0.0
func (p *FieldValues) SetAsDefault( key string, pd *Definition, v interface{}, options ...ParseOption, ) error
SetAsDefault sets the current value of the field if no value has yet been set.
func (*FieldValues) ToInterfaceMap ¶ added in v1.0.0
func (p *FieldValues) ToInterfaceMap() (map[string]interface{}, error)
ToInterfaceMap converts FieldValues to map[string]interface{} by converting each FieldValue's value to interface{}. It returns an error if it fails to convert any FieldValue's value.
func (*FieldValues) ToMap ¶ added in v1.0.0
func (p *FieldValues) ToMap() map[string]interface{}
ToMap converts FieldValues to map[string]interface{} by assigning each FieldValue's value to its key.
func (*FieldValues) Update ¶ added in v1.0.0
func (p *FieldValues) Update( key string, pp *FieldValue, )
func (*FieldValues) UpdateExistingValue ¶ added in v1.0.0
func (p *FieldValues) UpdateExistingValue( key string, v interface{}, options ...ParseOption, ) (bool, error)
UpdateExistingValue updates the value of an existing field, and returns true if the field existed. If the field did not exist, it returns false.
func (*FieldValues) UpdateValue ¶ added in v1.0.0
func (p *FieldValues) UpdateValue( key string, pd *Definition, v interface{}, options ...ParseOption, ) error
XXX Add proper error return handling here
func (*FieldValues) UpdateWithLog ¶ added in v1.0.0
func (p *FieldValues) UpdateWithLog( key string, pd *Definition, v interface{}, log ...ParseStep, ) error
type FieldValuesOption ¶ added in v1.0.0
type FieldValuesOption func(*FieldValues)
func WithFieldValue ¶ added in v1.0.0
func WithFieldValue(pd *Definition, key string, value interface{}) FieldValuesOption
type FileData ¶ added in v1.0.0
type FileData struct {
Content string
ParsedContent interface{}
ParseError error
RawContent []byte
StringContent string
IsList bool
IsObject bool
BaseName string
Extension string
FileType FileType
Path string
RelativePath string
AbsolutePath string
Size int64
LastModifiedTime time.Time
Permissions os.FileMode
IsDirectory bool
}
func GetFileData ¶ added in v1.0.0
func (*FileData) PrettyPrint ¶ added in v1.0.0
type Option ¶
type Option func(*Definition)
func WithChoices ¶
func WithDefault ¶
func WithDefault(defaultValue interface{}) Option
func WithIsArgument ¶
func WithRequired ¶
func WithShortFlag ¶
type ParseOption ¶ added in v1.0.0
type ParseOption func(*ParseStep)
func WithMetadata ¶ added in v1.0.0
func WithMetadata(metadata map[string]interface{}) ParseOption
func WithParseStepValue ¶ added in v1.0.0
func WithParseStepValue(value interface{}) ParseOption
func WithSource ¶ added in v1.0.0
func WithSource(source string) ParseOption
type ParseStep ¶ added in v1.0.0
func NewParseStep ¶ added in v1.0.0
func NewParseStep(options ...ParseOption) ParseStep
type SerializableFieldValue ¶ added in v1.0.0
type SerializableFieldValue struct {
Value interface{} `yaml:"value" json:"value"`
Log []ParseStep `yaml:"log" json:"log"`
}
SerializableFieldValue represents a parsed field in a format suitable for YAML/JSON serialization, excluding the Definition
func ToSerializableFieldValue ¶ added in v1.0.0
func ToSerializableFieldValue(pp *FieldValue) *SerializableFieldValue
ToSerializableFieldValue converts a FieldValue to its serializable representation
type SerializableFieldValues ¶ added in v1.0.0
type SerializableFieldValues struct {
// Using orderedmap to maintain field order while having name-based access
Fields *orderedmap.OrderedMap[string, *SerializableFieldValue] `yaml:"fields" json:"fields"`
}
SerializableFieldValues represents a collection of parsed fields in a format suitable for YAML/JSON serialization, maintaining the order of fields
func ToSerializableFieldValues ¶ added in v1.0.0
func ToSerializableFieldValues(pp *FieldValues) *SerializableFieldValues
ToSerializableFieldValues converts a FieldValues collection to its serializable representation
func (*SerializableFieldValues) MarshalJSON ¶ added in v1.0.0
func (spp *SerializableFieldValues) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for SerializableFieldValues
func (*SerializableFieldValues) MarshalYAML ¶ added in v1.0.0
func (spp *SerializableFieldValues) MarshalYAML() (interface{}, error)
MarshalYAML implements yaml.Marshaler for SerializableFieldValues
type Type ¶
type Type string
const ( TypeString Type = "string" TypeSecret Type = "secret" TypeStringFromFile Type = "stringFromFile" TypeStringFromFiles Type = "stringFromFiles" // TypeFile and TypeFileList are a more elaborate version that loads and parses // the file content and returns a list of FileData objects (or a single object in the case // of TypeFile). TypeFile Type = "file" TypeFileList Type = "fileList" TypeObjectListFromFile Type = "objectListFromFile" TypeObjectListFromFiles Type = "objectListFromFiles" TypeObjectFromFile Type = "objectFromFile" TypeStringListFromFile Type = "stringListFromFile" TypeStringListFromFiles Type = "stringListFromFiles" // TypeKeyValue signals either a string with comma separate key-value options, // or when beginning with @, a file with key-value options TypeKeyValue Type = "keyValue" TypeInteger Type = "int" TypeFloat Type = "float" TypeBool Type = "bool" TypeDate Type = "date" TypeStringList Type = "stringList" TypeIntegerList Type = "intList" TypeFloatList Type = "floatList" TypeChoice Type = "choice" TypeChoiceList Type = "choiceList" )
func (Type) IsKeyValue ¶ added in v1.0.0
func (Type) IsList ¶ added in v1.0.0
IsList returns true if the field has to be parsed from a list of strings, not if its value is actually a list.
func (Type) IsObjectList ¶ added in v1.0.0
func (Type) NeedsFileContent ¶ added in v1.0.0
NeedsFileContent returns true if the field type is one that loads one or more files, when provided with the given value. This slightly odd API is because some types like TypeKeyValue can be either a string or a file. A beginning character of @ indicates a file.
func (Type) NeedsMultipleFileContent ¶ added in v1.0.0
NeedsMultipleFileContent returns true if the field type is one that loads multiple files.