storage

package
v0.81.12 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 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 storage defines the public storage-backend contract: backend capabilities and metadata, install/device/user/tenant scopes with their enforcement policies, and the configuration shape hosts use to construct a backend.

Stability: Stable — no breaking change without a minor version bump and a changelog callout.

Index

Constants

View Source
const LocalInstallID = "local"

LocalInstallID is the install identifier NormalizeScope assigns to a scope with no install, device, user, or tenant: the no-config local default, keyed "install:local".

Variables

View Source
var (
	// ErrScopeUserRequired reports a scope without a UserID under
	// [ScopeUserRequired].
	ErrScopeUserRequired = errors.New("speechkit storage: user scope is required")
	// ErrScopeTenantRequired reports a scope without a TenantID under
	// [ScopeTenantRequired].
	ErrScopeTenantRequired = errors.New("speechkit storage: tenant scope is required")
)

Errors returned by ScopePolicy.Validate.

Functions

func NormalizeBackendName

func NormalizeBackendName(name string) string

NormalizeBackendName canonicalizes a backend name with NormalizeIdentifier so registrations and lookups agree.

func NormalizeIdentifier

func NormalizeIdentifier(value string) string

NormalizeIdentifier trims value and lowercases it; storage identifiers such as backend names are compared in this form.

func WithScope

func WithScope(ctx context.Context, scope Scope) context.Context

WithScope returns a child of ctx carrying the normalized scope; a nil ctx starts from context.Background. Stores read it back with ScopeFromContext.

Types

type BackendInfo

type BackendInfo struct {
	Name         string       `json:"name"`
	DisplayName  string       `json:"displayName,omitempty"`
	ScopePolicy  ScopePolicy  `json:"scopePolicy"`
	Capabilities Capabilities `json:"capabilities"`
}

BackendInfo describes a registered backend: the Name it registers under, an optional DisplayName for settings surfaces, the ScopePolicy it enforces, and its Capabilities.

type Capabilities

type Capabilities struct {
	Scopes       bool `json:"scopes"`
	AudioAssets  bool `json:"audioAssets"`
	FullText     bool `json:"fullText"`
	Embeddings   bool `json:"embeddings"`
	VectorSearch bool `json:"vectorSearch"`
}

Capabilities advertises the optional features a backend implements so hosts can adapt: per-scope isolation, audio asset storage, full-text search, stored embeddings, and vector similarity search.

type Config

type Config struct {
	Backend      string
	DefaultScope Scope
	ScopePolicy  ScopePolicy
	Options      map[string]string
}

Config is what a host passes to a Factory: the registered Backend name, the DefaultScope used when a context carries none, the ScopePolicy the backend must enforce, and free-form backend-specific Options such as a path or DSN.

type Factory

type Factory[T any] func(Config) (T, error)

Factory constructs a backend of type T from a Config.

type Registry

type Registry[T any] struct {
	// contains filtered or unexported fields
}

Registry maps normalized backend names to factories for one backend type T. The zero value is ready to use; registration is not safe for concurrent use.

func NewRegistry

func NewRegistry[T any]() *Registry[T]

NewRegistry returns an empty Registry for backend type T.

func (*Registry[T]) RegisterBackend

func (r *Registry[T]) RegisterBackend(name string, factory Factory[T]) error

RegisterBackend stores factory under the normalized form of name, replacing any earlier registration. It returns an error when the name normalizes to "" or factory is nil.

type Scope

type Scope struct {
	InstallID string            `json:"installId,omitempty"`
	DeviceID  string            `json:"deviceId,omitempty"`
	UserID    string            `json:"userId,omitempty"`
	TenantID  string            `json:"tenantId,omitempty"`
	Labels    map[string]string `json:"labels,omitempty"`
}

Scope identifies who owns a stored record: the installation, device, user, and tenant, plus free-form Labels (for example workspace or session) that partition data further. Every field is optional; the empty Scope is the local default.

func NormalizeScope

func NormalizeScope(scope Scope) Scope

NormalizeScope trims every identifier and label, drops labels with a blank key or value, and sets InstallID to LocalInstallID when no install, device, user, or tenant is given.

func ScopeFromContext

func ScopeFromContext(ctx context.Context) Scope

ScopeFromContext returns the normalized scope stored by WithScope, or the normalized local default when ctx is nil or carries none.

func (Scope) Key

func (s Scope) Key() string

Key returns a stable, collision-free string identity for the normalized scope: "tenant:", "user:", "device:", "install:", and sorted "label:k=v" parts joined by "|", each query-escaped so separators inside values cannot collide with the delimiters.

type ScopePolicy

type ScopePolicy string

ScopePolicy states which scope identity a backend requires before it stores or reads user data. The zero value behaves like ScopeOptional.

const (
	// ScopeOptional accepts any scope, including the local default.
	ScopeOptional ScopePolicy = "optional"
	// ScopeUserRequired rejects scopes without a UserID.
	ScopeUserRequired ScopePolicy = "user-required"
	// ScopeTenantRequired rejects scopes without a TenantID.
	ScopeTenantRequired ScopePolicy = "tenant-required"
)

Scope policies a backend can declare.

func (ScopePolicy) Validate

func (p ScopePolicy) Validate(scope Scope) error

Validate normalizes scope and returns ErrScopeUserRequired or ErrScopeTenantRequired when the identity the policy requires is missing. Unknown policies, including the zero value, accept every scope.

Jump to

Keyboard shortcuts

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