settings

package
v0.19.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 12 Imported by: 2 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package settings provides synchronized in-memory preferences and user-safe validation.

Index

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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

func CloneValue added in v0.10.0

func CloneValue(v any) any

CloneValue copies JSON maps/arrays and typed string, int, float64, and bool slices. Scalars are immutable. Unsupported arbitrary Go objects pass through.

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

type CategoryChangeFunc func(values map[string]any)

CategoryChangeFunc is called after settings in a category are saved. The map contains all current setting values for the category.

type CommitResult added in v0.10.0

type CommitResult struct {
	Committed bool `json:"committed"`
	Applied   bool `json:"applied"`
	Uncertain bool `json:"uncertain"`
}

CommitResult distinguishes durable storage from application.

type PersistFunc added in v0.10.0

type PersistFunc func(map[string]any) error

PersistFunc durably saves normalized changes. The base Provider supplies resolved category.setting IDs, including plugin.name for an unscoped plugin setting name. Transport adapters may map these IDs to their public namespace. It may read the provider but must not mutate it.

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)

	// GetCategoryValues returns detached, unredacted values for trusted consumers.
	GetCategoryValues(category string) (map[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

	// CommitSettings validates once, persists normalized changes, then publishes.
	// Validators and persist may read the provider but must not mutate it.
	CommitSettings(ctx context.Context, values map[string]any, persist PersistFunc) (CommitResult, 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 may read the provider. Change handlers may read and mutate it.

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"`
	// RequiresRestart declares that changes apply after restart.
	RequiresRestart bool `json:"requiresRestart"`
	// PendingRestart is host snapshot projection, not provider-owned state.
	PendingRestart bool `json:"pendingRestart"`
}

Setting describes a typed preference, its default, and optional validation. Sensitive values are hidden by GetValue; validators must not disclose secrets.

func (*Setting) GetValue

func (s *Setting) GetValue() any

GetValue returns the value of the setting.

func (*Setting) NormalizeValue added in v0.10.0

func (s *Setting) NormalizeValue(value any) (any, error)

NormalizeValue converts supported JSON and typed values to the setting's canonical shape. It does not invoke custom validation or mutate the setting.

func (*Setting) ResetValue

func (s *Setting) ResetValue()

ResetValue resets the value of the setting to its default value.

func (*Setting) SetValue

func (s *Setting) SetValue(value any) error

SetValue sets the value of the setting and validates it using the validator function defined.

func (*Setting) Validate

func (s *Setting) Validate(value any) error

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 Store

type Store map[string]Category

Store groups settings by category ID, then by setting ID.

type ValidationError added in v0.10.0

type ValidationError struct {
	Err error
}

ValidationError rejects a proposed transaction before persistence.

func (*ValidationError) Error added in v0.10.0

func (e *ValidationError) Error() string

func (*ValidationError) Unwrap added in v0.10.0

func (e *ValidationError) Unwrap() error

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.

Jump to

Keyboard shortcuts

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