Documentation
¶
Overview ¶
Package settings is like a Swiss Army knife for handling app configurations in Go. It's designed to make your life easier when dealing with all sorts of settings - think of it as your go-to toolkit for configuration management.
Here's the lowdown on what this package brings to the table:
- Marshaller and Unmarshaller: These interfaces are your friends for transforming settings to and from byte slices. They're super handy for storage or network transmission.
- String, Bool, Int, Uint etc.: Meet the fundamental building blocks for your settings.
- Blueprint: This is the brains of the operation. It lets you define and manage settings schemas, validate inputs, and even extend configurations with more settings.
- Schema: This is the blueprint's state. It's a collection of settings that can be compiled into a schema to store schema version. It also provides a way to create profiles.
- Profile: Think of it as your settings' memory. It keeps track of user preferences and application settings, making sure everything's in place and up-to-date.
- Preferences: This is the profile's state. It's a collection of settings that can be compiled into a profile to store user preferences.
- Reflective Magic: We use reflection (responsibly!) to dynamically handle fields in your structs. This means less boilerplate and more action.
Index ¶
- Constants
- Variables
- func UnmarshalValue[T Value](data []byte, v T) error
- type Blueprint
- func (b *Blueprint) AddSpec(spec SettingSpec) error
- func (b *Blueprint) AddValidator(key, desc string, fn func(s Setting) error)
- func (b *Blueprint) Describe(key string, lang language.Tag, description string)
- func (b *Blueprint) Extend(group string, ext Settings) (err error)
- func (b *Blueprint) GetSpec(key string) (SettingSpec, error)
- func (b *Blueprint) Migrate(keyfrom, keyto string) error
- func (b *Blueprint) PackageSettingsStructName() string
- func (b *Blueprint) Schema(module string, version version.Version) (Schema, error)
- func (b *Blueprint) SetDefault(key string, value Value) error
- func (b *Blueprint) SetDefaultFromString(key string, value string) error
- type Bool
- type Duration
- type ExecutionMode
- type Int
- type Kind
- type Marshaller
- type Mutability
- type Preferences
- type Profile
- func (p *Profile) All() iter.Seq[Setting]
- func (p *Profile) Changed() bool
- func (p *Profile) Get(key string) Setting
- func (p *Profile) Has(key string) bool
- func (p *Profile) Lang() language.Tag
- func (p *Profile) Loaded() bool
- func (p *Profile) Module() string
- func (p *Profile) Name() string
- func (p *Profile) PackageSettingsStructName() string
- func (p *Profile) Preferences() *Preferences
- func (p *Profile) Set(key string, val any) (err error)
- func (p *Profile) ValidatePreference(key string, val any) (err error)
- func (p *Profile) Version() version.Version
- type Schema
- type Setting
- func (s Setting) Default() vars.Variable
- func (s Setting) Description() string
- func (s Setting) Display() string
- func (s Setting) I18nKey() string
- func (s Setting) IsI18n() bool
- func (s Setting) IsSecret() bool
- func (s Setting) IsSet() bool
- func (s Setting) Key() string
- func (s Setting) Kind() Kind
- func (s Setting) Mutability() Mutability
- func (s Setting) Persistent() bool
- func (s Setting) String() string
- func (s Setting) Value() vars.Variable
- type SettingSpec
- type Settings
- type String
- type StringSlice
- type Uint
- type Unmarshaller
- type Value
Constants ¶
const ( KindSettings = Kind(vars.KindInterface) KindCustom = Kind(vars.KindByteSlice) KindInvalid = Kind(vars.KindInvalid) KindBool = Kind(vars.KindBool) KindInt = Kind(vars.KindInt) KindUint = Kind(vars.KindUint) KindString = Kind(vars.KindString) KindDuration = Kind(vars.KindDuration) KindStringSlice = Kind(vars.KindSlice) )
Variables ¶
var ( Error = errors.New("settings") ErrSetting = errors.New("setting") ErrProfile = fmt.Errorf("%w: profile", Error) ErrPreferences = fmt.Errorf("%w: preferences", Error) ErrSpec = errors.New("spec error") )
var (
ErrBlueprint = errors.New("settings blueprint")
)
var (
ErrSchema = errors.New("schema")
)
Functions ¶
func UnmarshalValue ¶ added in v0.7.0
Types ¶
type Blueprint ¶
type Blueprint struct {
// contains filtered or unexported fields
}
func (*Blueprint) AddSpec ¶
func (b *Blueprint) AddSpec(spec SettingSpec) error
func (*Blueprint) AddValidator ¶
func (*Blueprint) PackageSettingsStructName ¶ added in v0.6.0
type Bool ¶
type Bool bool
Bool represents a setting type based on a boolean.
func (Bool) MarshalSetting ¶
MarshalSetting converts the Bool setting to a byte slice, representing "true" or "false".
func (Bool) SettingKind ¶
func (*Bool) UnmarshalSetting ¶
UnmarshalSetting updates the Bool setting from a byte slice, interpreting it as "true" or "false".
type Duration ¶
func (Duration) MarshalSetting ¶
func (Duration) SettingKind ¶
func (*Duration) UnmarshalSetting ¶
type ExecutionMode ¶
type ExecutionMode int
executionMode determines the context in which the application is running.
const ( ModeUnknown ExecutionMode = 1 << iota // Unknown mode ModeProduction // Running as a compiled binary ModeDevel // Running with `go run` ModeTesting // Running in a test context )
func (ExecutionMode) MarshalText ¶
func (e ExecutionMode) MarshalText() ([]byte, error)
func (ExecutionMode) String ¶
func (e ExecutionMode) String() string
func (*ExecutionMode) UnmarshalText ¶
func (e *ExecutionMode) UnmarshalText(data []byte) error
type Int ¶
type Int int
Int represents a setting type based on an integer.
func (Int) MarshalSetting ¶
MarshalSetting converts the Int setting to a byte slice for storage or transmission.
func (Int) SettingKind ¶
func (*Int) UnmarshalSetting ¶
UnmarshalSetting updates the Int setting from a byte slice, interpreting it as an integer.
type Marshaller ¶
Marshaller interface for marshaling settings
type Mutability ¶
type Mutability uint8
const ( // SettingImmutable can not be changed on runtime. SettingImmutable Mutability = 254 // SettingOnce can be set only once on runtime. // When changed typically requires a reload of application. SettingOnce Mutability = 253 // SettingMutable can be changed on runtime. SettingMutable Mutability = 252 )
func (Mutability) String ¶
func (m Mutability) String() string
type Preferences ¶
type Preferences struct {
// contains filtered or unexported fields
}
func NewPreferences ¶
func NewPreferences(schemaVersion version.Version) *Preferences
func (*Preferences) GobDecode ¶ added in v0.6.0
func (p *Preferences) GobDecode(data []byte) error
func (Preferences) GobEncode ¶ added in v0.6.0
func (p Preferences) GobEncode() ([]byte, error)
func (*Preferences) SchemaVersion ¶ added in v0.6.0
func (p *Preferences) SchemaVersion() version.Version
func (*Preferences) Set ¶
func (p *Preferences) Set(key, value string)
func (*Preferences) UnmarshalJSON ¶ added in v0.6.0
func (p *Preferences) UnmarshalJSON(b []byte) error
type Profile ¶
type Profile struct {
// contains filtered or unexported fields
}
func (*Profile) Loaded ¶
Loaded reports true when settings profile is loaded and optional user preferences are applied.
func (*Profile) PackageSettingsStructName ¶ added in v0.6.0
func (*Profile) Preferences ¶ added in v0.6.0
func (p *Profile) Preferences() *Preferences
Preferences returns the profile's preferences which can be saved.
func (*Profile) ValidatePreference ¶ added in v0.6.0
type Setting ¶
type Setting struct {
// contains filtered or unexported fields
}
func (Setting) Description ¶
func (Setting) Display ¶ added in v0.7.0
Display returns the translated value for display purposes. If the setting uses i18n and i18n is initialized, it returns the translated string. Otherwise, it returns the raw value (same as String()). For Persistent settings that are user-defined (isSet=true), i18n is not applied.
func (Setting) I18nKey ¶ added in v1.0.0
I18nKey returns the i18n key for this setting, or empty string if not using i18n.
func (Setting) IsSecret ¶ added in v1.1.0
IsSecret reports whether this setting has been marked as secret via struct tag. Secrets are intended to be redacted by higher-level tooling (e.g. CLI config commands).
func (Setting) Mutability ¶
func (s Setting) Mutability() Mutability
func (Setting) Persistent ¶
type SettingSpec ¶
type SettingSpec struct {
IsSet bool
Kind Kind
Key string
Default string
Mutability Mutability
Value string
Required bool
Persistent bool
Unmarchaler Unmarshaller
Marchaler Marshaller
Settings *Blueprint
Secret bool // true if field has secret:"true" tag
// contains filtered or unexported fields
}
func (SettingSpec) Validate ¶
func (s SettingSpec) Validate() error
func (SettingSpec) ValidateValue ¶
func (s SettingSpec) ValidateValue(value string) error
type String ¶
type String string
String represents a setting type based on a string.
func (*String) MarshalSetting ¶
MarshalSetting converts the String setting to a byte slice for storage or transmission.
func (String) SettingKind ¶
SettingKind returns the kind of setting
func (*String) UnmarshalSetting ¶
UnmarshalSetting updates the String setting from a byte slice, typically read from storage or received in a message.
type StringSlice ¶
type StringSlice []string
func (StringSlice) MarshalSetting ¶
func (ss StringSlice) MarshalSetting() ([]byte, error)
func (StringSlice) SettingKind ¶
func (ss StringSlice) SettingKind() Kind
func (StringSlice) String ¶
func (ss StringSlice) String() string
func (*StringSlice) UnmarshalSetting ¶
func (ss *StringSlice) UnmarshalSetting(data []byte) error
type Uint ¶
type Uint uint
Uint represents a setting type based on an unsigned integer.
func (Uint) MarshalSetting ¶
MarshalSetting converts the Uint setting to a byte slice for storage or transmission.
func (Uint) SettingKind ¶
func (*Uint) UnmarshalSetting ¶
UnmarshalSetting updates the Uint setting from a byte slice, interpreting it as an unsigned integer.
type Unmarshaller ¶
Unmarshaller interface for unmarshaling settings
type Value ¶ added in v0.6.0
type Value interface {
fmt.Stringer
Marshaller
Unmarshaller
}
Value interface that combines multiple interfaces