Documentation
¶
Overview ¶
Package settings provides synchronized in-memory preferences and user-safe validation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSettingNotFound reports an unknown setting within a category. ErrSettingNotFound = errors.New("setting not found") // ErrSettingTypeMismatch reports a value incompatible with the declared setting type. ErrSettingTypeMismatch = errors.New("setting type mismatch") // ErrSettingCategoryNotFound reports an unknown category. ErrSettingCategoryNotFound = errors.New("setting category not found") // ErrInvalidSettingID reports an ID without nonempty category and setting components. ErrInvalidSettingID = errors.New("invalid setting ID") )
var AllSettingTypes = []struct { Value SettingType TSName string }{ {Text, "TEXT"}, {Select, "SELECT"}, {MultiSelect, "MULTISELECT"}, {Integer, "INTEGER"}, {Float, "FLOAT"}, {Toggle, "TOGGLE"}, {Color, "COLOR"}, {DateTime, "DATETIME"}, {Password, "PASSWORD"}, }
AllSettingTypes is a list of all setting types. Necessary for Wails to bind the enums.
Functions ¶
This section is empty.
Types ¶
type Category ¶
type Category struct {
Settings map[string]Setting `json:"settings"`
ID string `json:"id"`
Label string `json:"label"`
Description string `json:"description"`
Icon string `json:"icon"`
}
Category is a group of settings. This is used to group settings together in the UI.
type CategoryChangeFunc ¶ added in v0.3.0
CategoryChangeFunc is called after settings in a category are saved. The map contains all current setting values for the category.
type Provider ¶
type Provider interface {
// ListSettings returns the settings store
ListSettings() Store
// RegisterSetting registers a setting with the provider
RegisterSetting(categoryID string, setting Setting) error
// RegisterSettings registers a list of settings with the provider to a category
RegisterSettings(categoryID string, settings ...Setting) error
// GetSetting returns the setting by ID. This ID should be in the form of a dot separated string
// that represents the path to the setting. For example, "appearance.theme"
GetSetting(id string) (Setting, error)
// GetSettingValue returns the value of the setting by ID
GetSettingValue(id string) (any, error)
// GetString returns the value of the setting by ID as a string.
GetString(id string) (string, error)
// GetStringSlice returns the value of the setting by ID as a string slice.
GetStringSlice(id string) ([]string, error)
// GetInt returns the value of the setting by ID as an int.
GetInt(id string) (int, error)
// GetIntSlice returns the value of the setting by ID as an int slice.
GetIntSlice(id string) ([]int, error)
// GetFloat returns the value of the setting by ID as a float64.
GetFloat(id string) (float64, error)
// GetFloatSlice returns the value of the setting by ID as a float64 slice.
GetFloatSlice(id string) ([]float64, error)
// GetBool returns the value of the setting by ID as a bool.
GetBool(id string) (bool, error)
// SetSetting sets the value of the setting by ID
SetSetting(id string, value any) error
// SetSettings sets multiple settings at once
SetSettings(settings map[string]any) error
// RegisterChangeHandler registers a callback that fires after settings in the
// given category are saved. Only one handler per category.
RegisterChangeHandler(categoryID string, fn CategoryChangeFunc)
}
Provider manages the settings for the application as a pure in-memory store.
func NewProvider ¶
func NewProvider(opts ProviderOpts) Provider
NewProvider creates an independent in-memory store. A nil logger disables logging. Validators and change handlers run outside store locks and may call provider methods.
type ProviderOpts ¶
type ProviderOpts struct {
// Logger is the logger for the provider
Logger *zap.SugaredLogger
// PluginID is the ID of the plugin
PluginID string
// PluginSettings
PluginSettings []Category
}
ProviderOpts are the options for creating a new settings provider.
type Setting ¶
type Setting struct {
// ID is the unique identifier of the setting
ID string `json:"id"`
// Label is the human readable label of the setting
Label string `json:"label"`
// Description is the human readable description of the setting
Description string `json:"description"`
// Type is the type of the setting
Type SettingType `json:"type"`
// Value is the value of the setting
Value any `json:"value"`
// Default is the default value of the setting
Default any `json:"default"`
// Validator returns user-safe contextual feedback. Never include secrets in messages.
Validator func(any) ValidationResult `json:"-"`
// Options is an optional list of options for a select setting
Options []SettingOption `json:"options"`
// FileSelection is an optional setting for file selection
FileSelection *SettingFileSelection `json:"fileSelection"`
// Sensitive is a flag to indicate if the setting is sensitive and should not be
// shown in the UI, nor allowed to be used by any other plugin.
Sensitive bool `json:"sensitive"`
// DevOnly indicates the setting should only be visible when dev mode is active.
DevOnly bool `json:"devOnly"`
}
Setting describes a typed preference, its default, and optional validation. Sensitive values are hidden by GetValue; validators must not disclose secrets.
func (*Setting) ResetValue ¶
func (s *Setting) ResetValue()
ResetValue resets the value of the setting to its default value.
func (*Setting) SetValue ¶
SetValue sets the value of the setting and validates it using the validator function defined.
func (*Setting) Validate ¶
Validate checks if the value of the setting is valid using an optional validation function. If no validation function is provided, it returns nil.
func (*Setting) Validation ¶ added in v0.7.0
func (s *Setting) Validation(value any) ValidationResult
Validation evaluates the registered validator without changing the setting.
type SettingFileSelection ¶
type SettingFileSelection struct {
// Whether file selection should be allowed.
Enabled bool `json:"enabled"`
// Allow the selection of folders.
AllowFolders bool `json:"allowFolders"`
// The allowed extensions that should be selectable.
Extensions []string `json:"extensions"`
// Multiple files can be selected.
Multiple bool `json:"multiple"`
// Whether the file selection should be saved as a relative path.
Relative bool `json:"relative"`
// DefaultPath is the default path for the file selection.
DefaultPath string `json:"defaultPath"`
}
SettingFileSelection describes file-picker capabilities and initial selection. Extensions is caller-owned; an empty slice does not restrict extensions.
type SettingOption ¶
type SettingOption struct {
// Label is the human readable label of the option
Label string `json:"label"`
// Description is an optional human readable description of the option
Description string `json:"description"`
// Value is the value of the option
Value any `json:"value"`
}
SettingOption pairs a selectable value with its display metadata.
type SettingType ¶
type SettingType string
SettingType identifies the form control and expected value family for a preference.
const ( // Text is the type for a text field entry. Text SettingType = "text" // Select is the type for a single-select field entry. Select SettingType = "select" // MultiSelect is the type for a multi-select field entry. MultiSelect SettingType = "multiselect" // Integer is the type for an integer field entry. Integer SettingType = "integer" // Float is the type for a float field entry. Float SettingType = "float" // Toggle is the type for a toggle field entry. Toggle SettingType = "toggle" // Color is the type for a color field entry. Color SettingType = "color" // DateTime is the type for a date time field entry. DateTime SettingType = "datetime" // Password is the type for a password field entry. Password SettingType = "password" )
type ValidationResult ¶ added in v0.7.0
type ValidationResult struct {
Valid bool `json:"valid"`
Code string `json:"code"`
Message string `json:"message"`
}
ValidationResult is safe to show in settings forms. Codes are stable machine-readable identifiers.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package settingstest provides test doubles for the settings.Provider interface.
|
Package settingstest provides test doubles for the settings.Provider interface. |