settings

package
v0.0.0-...-51ec7a6 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package settings provides engine-scoped configuration that is declared in code, stored per deployment, and optionally overridden per tenant.

Declaration is the contract: an engine states which settings it owns, their types, defaults, and validation. Nothing outside the owning engine may declare or write them, which keeps one engine's configuration out of another engine's migrations.

Index

Constants

This section is empty.

Variables

View Source
var ErrFrozen = fmt.Errorf("settings registry is frozen")

Functions

This section is empty.

Types

type Choice

type Choice struct {
	Value string
	Label string
}

Choice is one permitted value of an Enum setting.

type Definition

type Definition struct {
	// Key is the engine-local name. It is namespaced by the owning engine, so
	// two engines may both declare "timeout" without colliding.
	Key         string
	Kind        Kind
	Default     any
	Label       string
	Description string
	// Choices enumerates permitted values and is required for Enum.
	Choices []Choice
	// Overridable allows a tenant to replace the deployment-wide value. A
	// setting that governs platform behavior rather than tenant behavior
	// should leave this false so tenants cannot change it.
	Overridable bool
	// Secret redacts the value wherever settings are serialized or logged.
	Secret bool
	// Check applies engine rules beyond the kind, such as a permitted range.
	// It runs after the value is coerced to its declared type.
	Check func(any) error
}

Definition declares one setting owned by one engine.

func (Definition) Coerce

func (definition Definition) Coerce(raw any) (any, error)

Coerce converts a raw value to the declared kind and applies validation. Values arrive from JSON storage and from application code, so integers may legitimately present as float64.

func (Definition) Validate

func (definition Definition) Validate() error

Validate reports whether the declaration itself is well formed. It does not consider stored values.

type Kind

type Kind string

Kind is the declared type of a setting value.

const (
	String   Kind = "string"
	Bool     Kind = "bool"
	Int      Kind = "int"
	Float    Kind = "float"
	Enum     Kind = "enum"
	Duration Kind = "duration"
)

type Registry

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

Registry holds every engine's declarations. It is frozen once mounting finishes so no engine can introduce or redefine a setting at runtime.

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Declare

func (registry *Registry) Declare(owner string, definitions ...Definition) error

Declare records definitions owned by one engine. Redeclaring a key is an error rather than an overwrite, so a mistaken duplicate cannot silently change another declaration's type or default.

func (*Registry) Freeze

func (registry *Registry) Freeze()

func (*Registry) Frozen

func (registry *Registry) Frozen() bool

func (*Registry) Lookup

func (registry *Registry) Lookup(owner, key string) (Definition, bool)

Lookup returns the declaration for one owned setting.

func (*Registry) Owned

func (registry *Registry) Owned(owner string) []Definition

Owned returns one engine's declarations ordered by key, for rendering a settings surface without the host knowing what the engine declared.

func (*Registry) Owners

func (registry *Registry) Owners() []string

Owners returns every engine that declared at least one setting, ordered.

type Scope

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

Scope binds a store to one owning engine so reads and writes cannot address another engine's keys.

func (*Scope) Bool

func (scope *Scope) Bool(ctx context.Context, key string, tenant uuid.UUID) (bool, error)

Bool resolves a Bool setting.

func (*Scope) Clear

func (scope *Scope) Clear(ctx context.Context, key string, tenant uuid.UUID) error

Clear removes a stored value so resolution falls back.

func (*Scope) Definitions

func (scope *Scope) Definitions() []Definition

Definitions returns this engine's declarations ordered by key.

func (*Scope) Duration

func (scope *Scope) Duration(ctx context.Context, key string, tenant uuid.UUID) (time.Duration, error)

Duration resolves a Duration setting.

func (*Scope) Get

func (scope *Scope) Get(ctx context.Context, key string, tenant uuid.UUID) (any, error)

Get resolves one of this engine's settings for a tenant. Pass uuid.Nil to read the deployment-wide value.

func (*Scope) Int

func (scope *Scope) Int(ctx context.Context, key string, tenant uuid.UUID) (int64, error)

Int resolves an Int setting.

func (*Scope) Owner

func (scope *Scope) Owner() string

Owner reports which engine this scope belongs to.

func (*Scope) Set

func (scope *Scope) Set(ctx context.Context, key string, tenant uuid.UUID, value any) error

Set writes one of this engine's settings.

func (*Scope) String

func (scope *Scope) String(ctx context.Context, key string, tenant uuid.UUID) (string, error)

String resolves a String or Enum setting.

func (*Scope) Values

func (scope *Scope) Values(ctx context.Context, tenant uuid.UUID) (map[string]any, error)

Values returns every effective value this engine owns, secrets redacted.

type Store

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

Store persists declared setting values. It reads and writes only keys the registry knows, so a stale row for a removed setting can never be resolved.

func NewStore

func NewStore(db *database.DB, registry *Registry) *Store

func (*Store) Clear

func (store *Store) Clear(ctx context.Context, owner, key string, tenant uuid.UUID) error

Clear removes a stored value so resolution falls back to the deployment-wide value or the declared default.

func (*Store) Load

func (store *Store) Load(ctx context.Context) error

Load reads every stored value into the cache. Settings are read on nearly every request while changing rarely, so the read path must not add a query per access — especially since a request transaction holds a pooled connection for its whole lifetime.

func (*Store) Registry

func (store *Store) Registry() *Registry

Registry exposes the declaration registry this store resolves against.

func (*Store) Resolve

func (store *Store) Resolve(ctx context.Context, owner, key string, tenant uuid.UUID) (any, error)

Resolve returns the effective value for one setting: the tenant override if one exists and the setting permits overrides, otherwise the deployment-wide value, otherwise the declared default.

func (*Store) Scoped

func (store *Store) Scoped(owner string) *Scope

Scoped returns the view of this store belonging to one engine.

func (*Store) Set

func (store *Store) Set(ctx context.Context, owner, key string, tenant uuid.UUID, raw any) error

Set writes a value. Passing uuid.Nil as the tenant sets the deployment-wide value; any other tenant sets an override, which the declaration must permit.

func (*Store) Values

func (store *Store) Values(ctx context.Context, owner string, tenant uuid.UUID) (map[string]any, error)

Values returns every effective value one engine owns for a tenant, ordered by key, with secrets redacted.

Jump to

Keyboard shortcuts

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