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
- 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) Schema(module, version string) (Schema, error)
- func (b *Blueprint) SetDefault(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() []Setting
- 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) Pkg() string
- func (p *Profile) Set(key string, val any) (err error)
- func (p *Profile) Validate(key string, val any) (err error)
- func (p *Profile) Version() string
- type Schema
- type Setting
- func (s Setting) Default() vars.Variable
- func (s Setting) Description() string
- 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) UserDefined() bool
- func (s Setting) Value() vars.Variable
- type SettingField
- type SettingSpec
- type Settings
- type String
- type StringSlice
- type Uint
- type Unmarshaller
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 ( ErrSettings = errors.New("settings") ErrSetting = errors.New("setting") ErrProfile = fmt.Errorf("%w: profile", ErrSettings) ErrSpec = errors.New("spec error") )
var (
ErrBlueprint = errors.New("settings blueprint")
)
var (
ErrSchema = errors.New("schema")
)
Functions ¶
This section is empty.
Types ¶
type Blueprint ¶
type Blueprint struct {
// contains filtered or unexported fields
}
func (*Blueprint) AddSpec ¶
func (b *Blueprint) AddSpec(spec SettingSpec) error
func (*Blueprint) AddValidator ¶
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() *Preferences
func (*Preferences) Consume ¶
func (p *Preferences) Consume()
func (*Preferences) Set ¶
func (p *Preferences) Set(key, val string)
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.
type Setting ¶
type Setting struct {
// contains filtered or unexported fields
}
func (Setting) Description ¶
func (Setting) Mutability ¶
func (s Setting) Mutability() Mutability
func (Setting) Persistent ¶
func (Setting) UserDefined ¶ added in v0.3.0
type SettingField ¶
type SettingField interface { fmt.Stringer Marshaller Unmarshaller SettingKind() Kind }
SettingField interface that combines multiple interfaces
type SettingSpec ¶
type SettingSpec struct { IsSet bool Kind Kind Key string Default string Mutability Mutability Value string Required bool Persistent bool UserDefined bool Unmarchaler Unmarshaller Marchaler Marshaller Settings *Blueprint // 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