config

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package config provides runtime configuration entries and store interface.

Index

Constants

View Source
const (
	TypeString   = "string"
	TypeInt      = "int"
	TypeFloat    = "float"
	TypeBool     = "bool"
	TypeJSON     = "json"
	TypeDuration = "duration"
)

The value types a config entry can carry and the write service accepts. Entry.ValueType is a free-form label in the store, so an entry stored with any other label is read-only to the write service.

Variables

This section is empty.

Functions

func DescribeValue

func DescribeValue(v any) string

DescribeValue names v's kind ("a string", "an object", "null") without echoing its value, which for a bad value could be anything the caller sent. It is what the write service uses to word a refusal.

func KnownType

func KnownType(t string) bool

KnownType reports whether t is one of the value types the write service supports.

func ParseBool

func ParseBool(s string) (bool, error)

ParseBool tries to parse a string as a boolean.

func ParseFloat

func ParseFloat(s string) (float64, error)

ParseFloat tries to parse a string as a float64.

func ParseInt

func ParseInt(s string) (int, error)

ParseInt tries to parse a string as an integer.

func ValidateValue

func ValidateValue(valueType string, v any) error

ValidateValue reports whether v is an acceptable value for an entry of type valueType. No backend checks: an int entry holding "abc" is stored happily, and every typed read then silently answers the caller's fallback.

A value that arrives through JSON is a float64, so int accepts a float64 with no fractional part, up to 2^53 in magnitude, as well as any Go integer. duration is a string time.ParseDuration accepts. nil is refused for every type except json, where it is the JSON null.

An unknown valueType returns a *ValidationError naming "valueType". Any other refusal is a plain error describing the value; the caller knows which field it came from and wraps it.

Types

type Entry

type Entry struct {
	core.Entity
	ID          id.ID             `json:"id"`
	Key         string            `json:"key"`
	Value       any               `json:"value"`
	ValueType   string            `json:"value_type"`
	Version     int64             `json:"version"`
	Description string            `json:"description"`
	AppID       string            `json:"app_id"`
	Metadata    map[string]string `json:"metadata,omitempty"`
}

Entry represents a runtime configuration entry.

type EntryVersion

type EntryVersion struct {
	ID        id.ID     `json:"id"`
	ConfigKey string    `json:"config_key"`
	AppID     string    `json:"app_id"`
	Version   int64     `json:"version"`
	Value     any       `json:"value"`
	CreatedBy string    `json:"created_by"`
	CreatedAt time.Time `json:"created_at"`
}

EntryVersion represents a historical version of a config entry.

type ListOpts

type ListOpts struct {
	Limit  int
	Offset int
	AppID  string

	// KeyPrefix restricts the list to keys that start with it, matched as a
	// literal: %, _ and regex characters carry no special meaning. Empty
	// means every key.
	KeyPrefix string
}

ListOpts configures list queries for config entries.

type Service

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

Service provides type-safe config reading, writing, and watch support.

func NewService

func NewService(store Store, opts ...ServiceOption) *Service

NewService creates a config service.

func (*Service) Bool

func (s *Service) Bool(ctx context.Context, key string, defaultVal bool) bool

Bool returns a boolean config value. Returns defaultVal on error or type mismatch.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, key, appID string) error

Delete removes a config entry and all its versions.

func (*Service) Duration

func (s *Service) Duration(ctx context.Context, key string, defaultVal time.Duration) time.Duration

Duration returns a time.Duration config value. Returns defaultVal on error or type mismatch. Parses from a string using time.ParseDuration (e.g. "5s", "1m30s").

func (*Service) Float

func (s *Service) Float(ctx context.Context, key string, defaultVal float64) float64

Float returns a float64 config value. Returns defaultVal on error or type mismatch. Handles float64, int, and int64.

func (*Service) Get

func (s *Service) Get(ctx context.Context, key, appID string) (*Entry, error)

Get retrieves a config entry by key.

func (*Service) Int

func (s *Service) Int(ctx context.Context, key string, defaultVal int) int

Int returns an integer config value. Returns defaultVal on error or type mismatch. Handles int, float64 (from JSON), and int64.

func (*Service) JSON

func (s *Service) JSON(ctx context.Context, key string, target any) error

JSON reads a config value and unmarshals it into target. target must be a pointer. Returns an error if the config is not found or the value cannot be converted to JSON and unmarshaled into target.

func (*Service) List

func (s *Service) List(ctx context.Context, appID string, opts ListOpts) ([]*Entry, error)

List returns config entries for an app.

func (*Service) Notify

func (s *Service) Notify(ctx context.Context, key string, oldValue, newValue any)

Notify runs the watchers registered for key, exactly as Set does after a write. It exists for the write service (package configmgr), which writes through the store itself and so must tell watchers what changed: oldValue is nil for a create, newValue nil for a delete.

func (*Service) Set

func (s *Service) Set(ctx context.Context, key string, value any, appID string, opts ...SetOption) error

Set creates or updates a config entry, auto-versioning on update.

func (*Service) String

func (s *Service) String(ctx context.Context, key, defaultVal string) string

String returns a string config value. Returns defaultVal on error or type mismatch.

func (*Service) Watch

func (s *Service) Watch(key string, cb WatchCallback)

Watch registers a callback that fires when the given config key is updated via Set.

type ServiceOption

type ServiceOption func(*Service)

ServiceOption configures the Service.

func WithAppID

func WithAppID(appID string) ServiceOption

WithAppID sets the default app ID for the config service.

func WithResolver

func WithResolver(r ValueResolver) ServiceOption

WithResolver sets the override resolver for tenant-aware config resolution.

type SetOption

type SetOption func(*setConfig)

SetOption configures a Set operation.

func WithDescription

func WithDescription(desc string) SetOption

WithDescription sets the description on the config entry.

func WithMetadata

func WithMetadata(m map[string]string) SetOption

WithMetadata sets metadata on the config entry.

func WithValueType

func WithValueType(vt string) SetOption

WithValueType sets an explicit value type label (e.g. "string", "int", "json").

type Store

type Store interface {
	// GetConfig retrieves a config entry by key and app ID.
	GetConfig(ctx context.Context, key, appID string) (*Entry, error)

	// SetConfig creates or updates a config entry. A new version is created on update.
	SetConfig(ctx context.Context, e *Entry) error

	// DeleteConfig removes a config entry.
	DeleteConfig(ctx context.Context, key, appID string) error

	// ListConfig returns the config entries for an app in key order, filtered
	// by opts.KeyPrefix and then paged. An empty result is an empty slice,
	// never nil.
	ListConfig(ctx context.Context, appID string, opts ListOpts) ([]*Entry, error)

	// GetConfigVersion retrieves a specific version of a config entry.
	GetConfigVersion(ctx context.Context, key, appID string, version int64) (*Entry, error)

	// ListConfigVersions returns all versions of a config entry.
	ListConfigVersions(ctx context.Context, key, appID string) ([]*EntryVersion, error)

	// CountConfig returns the total number of config entries for an app,
	// independent of any paging.
	CountConfig(ctx context.Context, appID string) (int64, error)

	// CountConfigMatching returns the number of config entries for an app
	// that match opts.KeyPrefix. Limit and Offset are ignored, so it is the
	// total behind a page returned by ListConfig with the same opts.
	CountConfigMatching(ctx context.Context, appID string, opts ListOpts) (int64, error)
}

Store defines the persistence interface for runtime configuration.

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError is a refusal of caller input. Field names the part of the request at fault in the wire's own spelling, so a handler can hand it straight to the client.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error returns "config: <field>: <message>".

type ValueResolver

type ValueResolver interface {
	Resolve(ctx context.Context, key, appID string) (any, error)
	Invalidate(key, appID string)
}

ValueResolver resolves config values with tenant-aware override support. The override.Resolver type satisfies this interface.

type WatchCallback

type WatchCallback func(ctx context.Context, key string, oldValue, newValue any)

WatchCallback is invoked when a config entry is updated.

Jump to

Keyboard shortcuts

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