storage

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package storage manages the on-disk data layout (container for the FingerGo app data).

Directory structure created by Init():

{root}/
├── texts/
│   ├── index.json           # metadata: categories, text entries
│   └── content/
│       └── {id}.txt         # actual text content by ID
├── sessions.json            # typing session history
└── settings.json            # user preferences

On first run, embedded defaults are copied to {root}/. Existing files are never overwritten (idempotent).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTextNotFound        = errors.New("storage: text not found")
	ErrContentUnavailable  = errors.New("storage: text content unavailable")
	ErrTextExists          = errors.New("storage: text already exists")
	ErrEmptyTextID         = errors.New("storage: text id is empty")
	ErrInvalidTextID       = errors.New("storage: text id contains invalid characters")
	ErrEmptyTextTitle      = errors.New("storage: text title is empty")
	ErrTextTitleTooLong    = errors.New("storage: text title too long")
	ErrEmptyTextContent    = errors.New("storage: text content is empty")
	ErrTextContentTooLarge = errors.New("storage: text content too large")
	ErrInvalidLanguage     = errors.New("storage: invalid language")
)

Text validation errors.

View Source
var (
	ErrCategoryExists      = errors.New("storage: category already exists")
	ErrCategoryNotFound    = errors.New("storage: category not found")
	ErrEmptyCategoryID     = errors.New("storage: category id is empty")
	ErrInvalidCategoryID   = errors.New("storage: category id contains invalid characters")
	ErrEmptyCategoryName   = errors.New("storage: category name is empty")
	ErrCategoryNameTooLong = errors.New("storage: category name too long")
)

Category validation errors.

Functions

func DefaultRoot

func DefaultRoot() string

DefaultRoot returns platform-specific application data directory. - Linux: $XDG_DATA_HOME/FingerGo or ~/.local/share/FingerGo - macOS: ~/Library/Application Support/FingerGo - Windows: %APPDATA%\FingerGo (e.g., C:\Users\Name\AppData\Roaming\FingerGo)

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager owns the on-disk data layout for FingerGo.

func New

func New(root string) (*Manager, error)

New creates a storage manager rooted at the provided path.

func (*Manager) Init

func (m *Manager) Init() error

Init ensures the expected directory structure exists and seeds fallback data. Safe to call multiple times — existing files are not overwritten.

Creates:

  • {root}/texts/
  • {root}/texts/content/
  • {root}/texts/index.json (from embedded)
  • {root}/texts/content/{id}.txt (from embedded)
  • {root}/sessions.json (empty array)

func (*Manager) Root

func (m *Manager) Root() string

Root returns the absolute root path used by the manager.

type SessionRepository

type SessionRepository struct {
	// contains filtered or unexported fields
}

SessionRepository persists typing sessions in sessions.json.

func NewSessionRepository

func NewSessionRepository(mgr *Manager) (*SessionRepository, error)

NewSessionRepository wires the repository to the storage manager.

func (*SessionRepository) List

func (r *SessionRepository) List(limit int) ([]domain.TypingSession, error)

List returns recent sessions (newest first). limit <= 0 returns all.

func (*SessionRepository) Record

Record persists a session payload and returns the stored session.

type SettingsRepository

type SettingsRepository struct {
	// contains filtered or unexported fields
}

SettingsRepository persists user settings in settings.json.

func NewSettingsRepository

func NewSettingsRepository(mgr *Manager) (*SettingsRepository, error)

NewSettingsRepository wires the repository to the storage manager.

func (*SettingsRepository) Load

func (r *SettingsRepository) Load() (domain.Settings, error)

Load returns current settings, loading from disk on first access.

func (*SettingsRepository) Save

Save persists the entire settings object.

func (*SettingsRepository) Update

func (r *SettingsRepository) Update(key string, value any) error

Update modifies a single setting by key and persists the change. Supported keys: "theme", "showKeyboard", "showStatsBar", "zenMode", "strictMode", "lastTextId", "keyboardLayout".

type TextRepository

type TextRepository struct {
	// contains filtered or unexported fields
}

TextRepository manages the text library with lazy loading and caching.

Design:

  • Metadata (index.json) loaded once on first access
  • Content files loaded on demand and cached in memory
  • All public methods are thread-safe (guarded by RWMutex)
  • Writes persist both in-memory state and disk atomically
  • O(1) lookups via textIndex and sliceIndex maps

func NewTextRepository

func NewTextRepository(mgr *Manager) (*TextRepository, error)

NewTextRepository wires repository to the storage manager.

func (*TextRepository) DefaultText

func (r *TextRepository) DefaultText() (domain.Text, error)

DefaultText resolves and returns the configured default text with content.

func (*TextRepository) DeleteCategory

func (r *TextRepository) DeleteCategory(id string) error

DeleteCategory removes a category entry by ID and all texts belonging to it. Returns ErrCategoryNotFound if category doesn't exist.

func (*TextRepository) DeleteText

func (r *TextRepository) DeleteText(id string) error

DeleteText removes a text entry by ID.

func (*TextRepository) Library

func (r *TextRepository) Library() (domain.TextLibrary, error)

Library returns metadata for texts and categories (content stripped).

func (*TextRepository) SaveCategory

func (r *TextRepository) SaveCategory(cat *domain.Category) error

SaveCategory creates a new category entry. Returns ErrCategoryExists if a category with the same ID or name already exists.

func (*TextRepository) SaveText

func (r *TextRepository) SaveText(text *domain.Text) error

SaveText creates a new text entry with content. Returns ErrTextExists if a text with the same ID already exists.

func (*TextRepository) Text

func (r *TextRepository) Text(id string) (domain.Text, error)

Text returns a text (metadata + content) by identifier.

func (*TextRepository) UpdateText

func (r *TextRepository) UpdateText(text *domain.Text) error

UpdateText modifies an existing text entry.

Jump to

Keyboard shortcuts

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