Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrSettingNotFound = errors.New("setting not found") ErrSettingTypeMismatch = errors.New("setting type mismatch") ErrSettingCategoryNotFound = errors.New("setting category not found") 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
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 interface{} `json:"value"`
// Default is the default value of the setting
Default interface{} `json:"default"`
// Validator returns user-safe contextual feedback. Never include secrets in messages.
Validator func(interface{}) 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"`
}
func (*Setting) GetValue ¶
func (s *Setting) GetValue() interface{}
GetValue returns the value of the setting.
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 interface{}) 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"`
}
type SettingOption ¶
type SettingType ¶
type SettingType string
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" // SettingTypePassword is the type for a password field entry. Password SettingType = "password" )
type Store ¶
The settings store is a map of maps. The first map is the category of the settings, and the second map is the settings themselves. The key of the first map is the category name, and the key of the second map is the setting ID.
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. |