Documentation
¶
Overview ¶
Package userprefs provides a flexible, concurrent-safe user preferences management system.
It supports multiple storage backends (PostgreSQL, SQLite), optional caching (Redis, in-memory), and a type-safe preference system. While originally designed for Discord bots, it can be used in any application requiring user-specific preference storage.
Package userprefs defines error variables used throughout the user preferences system.
Package userprefs defines interfaces for storage, caching, and logging used in user preferences management.
Package userprefs provides default logging implementations.
Package userprefs provides the Manager responsible for handling user preferences.
Package userprefs defines the core types used in the user preferences management system.
Package userprefs provides validation functions for user preferences.
Index ¶
- Variables
- type Cache
- type Config
- type Logger
- type Manager
- func (m *Manager) DefinePreference(def PreferenceDefinition) error
- func (m *Manager) Delete(ctx context.Context, userID, key string) error
- func (m *Manager) Get(ctx context.Context, userID, key string) (*Preference, error)
- func (m *Manager) GetAll(ctx context.Context, userID string) (map[string]*Preference, error)
- func (m *Manager) GetByCategory(ctx context.Context, userID, category string) (map[string]*Preference, error)
- func (m *Manager) Set(ctx context.Context, userID, key string, value interface{}) error
- type Option
- type Preference
- type PreferenceDefinition
- type Storage
Constants ¶
This section is empty.
Variables ¶
ErrCacheUnavailable indicates that the cache backend is unavailable.
var ErrInvalidInput = errors.New("invalid input parameters")
ErrInvalidInput indicates that the input parameters provided to a function are invalid.
var ErrInvalidKey = errors.New("invalid preference key")
ErrInvalidKey indicates that the provided preference key is invalid.
var ErrInvalidType = errors.New("invalid preference type")
ErrInvalidType indicates that the provided preference type is invalid.
var ErrInvalidValue = errors.New("invalid preference value")
ErrInvalidValue indicates that the provided preference value is invalid.
var ErrNotFound = errors.New("preference not found")
ErrNotFound indicates that the requested preference was not found.
var ErrPreferenceNotDefined = errors.New("preference not defined")
ErrPreferenceNotDefined indicates that the preference has not been defined in the system.
ErrStorageUnavailable indicates that the storage backend is unavailable.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { Get(ctx context.Context, key string) (interface{}, error) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error Delete(ctx context.Context, key string) error Close() error }
Cache defines the methods required for a caching backend.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config holds the configuration for the Manager, including storage, cache, logger, and preference definitions.
type Logger ¶
type Logger interface { Debug(msg string, args ...interface{}) Info(msg string, args ...interface{}) Warn(msg string, args ...interface{}) Error(msg string, args ...interface{}) }
Logger defines the methods required for logging within the user preferences system.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages user preferences by interfacing with storage and cache backends.
func (*Manager) DefinePreference ¶
func (m *Manager) DefinePreference(def PreferenceDefinition) error
DefinePreference registers a new preference definition. It returns an error if the key is invalid or the type is unsupported.
func (*Manager) Get ¶
Get retrieves a preference for a user by key. It returns the preference or an error if not found.
func (*Manager) GetByCategory ¶
func (m *Manager) GetByCategory(ctx context.Context, userID, category string) (map[string]*Preference, error)
GetByCategory retrieves all preferences for a user within a specific category.
type Option ¶
type Option func(*Config)
Option defines a configuration option for the Manager.
func WithStorage ¶
WithStorage sets the storage backend for the Manager.
type Preference ¶
type Preference struct { UserID string `json:"user_id"` Key string `json:"key"` Value interface{} `json:"value"` DefaultValue interface{} `json:"default_value,omitempty"` Type string `json:"type"` Category string `json:"category,omitempty"` UpdatedAt time.Time `json:"updated_at"` }
Preference represents a single user preference.
type PreferenceDefinition ¶
type PreferenceDefinition struct { Key string `json:"key"` Type string `json:"type"` DefaultValue interface{} `json:"default_value,omitempty"` Category string `json:"category,omitempty"` AllowedValues []interface{} `json:"allowed_values,omitempty"` }
PreferenceDefinition defines the schema for a preference, including its type and allowed values.
type Storage ¶
type Storage interface { Get(ctx context.Context, userID, key string) (*Preference, error) Set(ctx context.Context, pref *Preference) error Delete(ctx context.Context, userID, key string) error GetAll(ctx context.Context, userID string) (map[string]*Preference, error) GetByCategory(ctx context.Context, userID, category string) (map[string]*Preference, error) Close() error }
Storage defines the methods required for a storage backend.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package cache provides in-memory caching implementations.
|
Package cache provides in-memory caching implementations. |
examples
|
|
advanced
examples/advanced/main.go
|
examples/advanced/main.go |
basic
examples/basic/main.go
|
examples/basic/main.go |
discordgo-bot
examples/discordgo-bot/main.go
|
examples/discordgo-bot/main.go |
sqlite-advanced
examples/sqlite-advanced/main.go
|
examples/sqlite-advanced/main.go |
Package storage provides a PostgreSQL-based implementation of the Storage interface.
|
Package storage provides a PostgreSQL-based implementation of the Storage interface. |