config

package
v0.6.13 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DependsSatisfied

func DependsSatisfied(structure any) (map[string]bool, error)

DependsSatisfied returns a map, indicating, for each field, if all of its dependencies are satisfied. structure must be a config structure, or a pointer to a config structure.

func InitDefault

func InitDefault(structure any)

InitDefault initializes the given structure (which must be a pointer to a struct) to its actual default value specified in the struct definition (note that this is distinct from the zero value). Panicks if the struct definition contains any errors.

func MarshalFunc

func MarshalFunc(structure any, getField func(name string) (value string, ok bool)) (err error)

MarshalFunc reads values into a structure (which must be pointer to a struct). getField should return the string-representation of the requested value, or return ok as false if the field doesn't exist. If the field doesn't exist, the default value will be used (see Field.DefaultValue) If the returned error relates to a specific field, it will be of type MarshalErr.

Types

type Field

type Field struct {
	// Full name (e.g. MyOption or MyCategory.MySubOption)
	Name string
	// Field is a category, i.e. Type.Kind == reflect.Struct
	IsCategory bool
	// Underlying type
	Type reflect.Type
	// Tag options or nil
	*Tag
}

func (Field) DefaultValue

func (f Field) DefaultValue() any

FieldDefault returns the computed default value for the field. The following default values will be tried: - If a valid default value exists, that will be used. - If the field is of type option, the first option will be used. - Otherwise, the zero value for the type will be used.

type FieldSet

type FieldSet struct {
	// All fields in order
	Fields []*Field
	// Full name (with dots) to field
	ByName map[string]*Field
}

func Fields

func Fields(structure any) (*FieldSet, error)

Fields returns the parsed config fields of the (nested) structure. structure must be a config structure, or a pointer to a config structure.

func MustFields

func MustFields(structure any) *FieldSet

MustFields is like Fields, but panicks if there is an error.

func (*FieldSet) FieldFormatHint

func (fs *FieldSet) FieldFormatHint(
	field string,
	formatFieldName func(field string) string,
) string

FieldFormatHint returns a text describing a field's tag. Example: "range: 1...100, default: 50, depends: [Field1 Field2]". formatFieldName determines how field names should be printed in depends, nil for default.

type MarshalErr

type MarshalErr struct {
	// Field name
	Field string
	// Underlying error
	Err error
}

func (*MarshalErr) Error

func (err *MarshalErr) Error() string

func (*MarshalErr) Unwrap

func (err *MarshalErr) Unwrap() error

type Tag

type Tag struct {
	// Single-character short name,
	// e.g. for use in command-line options, or 0
	// (NOT checked to be unique)
	Short byte
	// Description string, or empty
	Help string
	// String enum options, or nil
	Options []string
	// Default values in order of priority (highest first)
	// (type-checked), or nil
	Default []any
	// Numerical range (type-checked), or nil
	RangeMin any
	RangeMax any
	// Dependencies (checked for validity), or nil
	Depends []TagDependency
	// Tags for custom purposes, or nil
	Tags []string
}

type TagDependency

type TagDependency struct {
	Field string
	Value any
}

Jump to

Keyboard shortcuts

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