config

package
v0.1.0-preview.4 Latest Latest
Warning

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

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

Documentation

Overview

Package config provides the small reflection-free runtime used by generated Spice configuration binders.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode[T any](
	ctx context.Context,
	snapshot Snapshot,
	decoder Decoder[T],
	validators ...Validator[T],
) (T, error)

Decode invokes a generated binder and typed validators in declaration order.

Types

type Decoder

type Decoder[T any] func(Snapshot) (T, error)

Decoder is implemented by generated reflection-free binders.

type Entry

type Entry struct {
	Key    string
	Value  string
	Origin Origin
	Secret bool
}

Entry is one resolved raw value with provenance and redaction metadata.

type EnvironmentSource

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

EnvironmentSource reads only schema-declared environment variables.

func NewEnvironmentSource

func NewEnvironmentSource(
	name string,
	prefix string,
	lookup LookupEnv,
) (EnvironmentSource, error)

NewEnvironmentSource creates an explicit environment source. prefix must be empty or a portable uppercase environment prefix.

func OSEnvironment

func OSEnvironment(prefix string) (EnvironmentSource, error)

OSEnvironment creates a source backed by os.LookupEnv.

func (EnvironmentSource) Load

func (s EnvironmentSource) Load(
	ctx context.Context,
	request Request,
) (map[string]string, error)

Load checks only schema-derived or explicitly named environment variables.

func (EnvironmentSource) Name

func (s EnvironmentSource) Name() string

Name returns the source identity used in provenance.

type JSONOptions

type JSONOptions struct {
	// Required makes the base <name>.json file mandatory. Profile files remain
	// optional because deployment profiles commonly share one artifact.
	Required bool
	// MaxBytes bounds each individual file. Zero uses 1 MiB.
	MaxBytes int64
}

JSONOptions controls a rooted JSON file source.

type JSONSource

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

JSONSource loads one base object and ordered active-profile objects from a rooted directory.

func NewJSONSource

func NewJSONSource(
	name string,
	directory string,
	baseName string,
	options JSONOptions,
) (JSONSource, error)

NewJSONSource validates a rooted JSON source. baseName is a portable filename stem, not a relative path.

func (JSONSource) Load

func (s JSONSource) Load(
	ctx context.Context,
	request Request,
) (map[string]string, error)

Load reads the base file followed by one optional file per active profile.

func (JSONSource) Name

func (s JSONSource) Name() string

Name returns the source identity used in provenance.

type Kind

type Kind string

Kind identifies one generated property's scalar representation.

const (
	// KindString identifies a string property.
	KindString Kind = "string"
	// KindBoolean identifies a Boolean property accepted by conversion.Boolean.
	KindBoolean Kind = "boolean"
	// KindInteger identifies a base-10 signed 64-bit integer property.
	KindInteger Kind = "integer"
	// KindDuration identifies a conversion.Duration property.
	KindDuration Kind = "duration"
)

type LookupEnv

type LookupEnv func(string) (string, bool)

LookupEnv is an injectable environment lookup function.

type MapSource

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

MapSource is an immutable in-memory source useful for explicit overrides and tests.

func NewMapSource

func NewMapSource(name string, values map[string]string) (MapSource, error)

NewMapSource copies values into a named source.

func (MapSource) Load

func (s MapSource) Load(ctx context.Context, _ Request) (map[string]string, error)

Load returns a fresh value map.

func (MapSource) Name

func (s MapSource) Name() string

Name returns the source identity used in provenance.

type Options

type Options struct {
	Profiles     []string
	AllowUnknown bool
}

Options controls deterministic source resolution.

type Origin

type Origin struct {
	Source  string
	Default bool
}

Origin records where the winning value came from.

type Property

type Property struct {
	Key         string
	Kind        Kind
	Description string
	Module      string
	Environment string
	Default     string
	HasDefault  bool
	Required    bool
	Secret      bool
}

Property is generated metadata for one exact configuration key.

type Request

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

Request is the immutable source-load request.

func (Request) Profiles

func (r Request) Profiles() []string

Profiles returns active profiles in caller precedence order.

func (Request) Properties

func (r Request) Properties() []Property

Properties returns the generated schema in key order.

type Schema

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

Schema is immutable-by-construction generated configuration metadata.

func MustSchema

func MustSchema(properties ...Property) Schema

MustSchema returns a validated schema or panics for invalid package-owned generated metadata.

func NewSchema

func NewSchema(properties ...Property) (Schema, error)

NewSchema validates and sorts generated property metadata.

func (Schema) Properties

func (s Schema) Properties() []Property

Properties returns property metadata in key order.

type Snapshot

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

Snapshot is one immutable-by-construction resolved configuration view.

func Resolve

func Resolve(
	ctx context.Context,
	schema Schema,
	options Options,
	sources ...Source,
) (Snapshot, error)

Resolve merges defaults and sources from lowest to highest precedence, validates required/known/scalar properties, and preserves winning origins.

func (Snapshot) Boolean

func (s Snapshot) Boolean(key string) (bool, error)

Boolean decodes one required Boolean without including its raw value in an error, which keeps secret values out of diagnostics.

func (Snapshot) Duration

func (s Snapshot) Duration(key string) (time.Duration, error)

Duration decodes one required Go duration.

func (Snapshot) Entry

func (s Snapshot) Entry(key string) (Entry, bool)

Entry returns one resolved value and its provenance.

func (Snapshot) Integer

func (s Snapshot) Integer(key string) (int64, error)

Integer decodes one required base-10 signed 64-bit integer.

func (Snapshot) Keys

func (s Snapshot) Keys() []string

Keys returns resolved keys in lexical order.

func (Snapshot) Lookup

func (s Snapshot) Lookup(key string) (string, bool)

Lookup returns one raw value. Generated binders use this for optional string and custom scalar decoding.

func (Snapshot) Redacted

func (s Snapshot) Redacted() map[string]string

Redacted returns a complete key/value copy safe for logs and diagnostics.

func (Snapshot) RequiredString

func (s Snapshot) RequiredString(key string) (string, error)

RequiredString returns one required raw string.

func (Snapshot) String

func (s Snapshot) String() string

String renders deterministic redacted key/value lines.

type Source

type Source interface {
	Name() string
	Load(context.Context, Request) (map[string]string, error)
}

Source contributes configuration values. Sources must perform no hidden network access; source ordering is the resolver's precedence contract.

type Validator

type Validator[T any] func(context.Context, T) error

Validator applies one typed post-decode validation rule.

Jump to

Keyboard shortcuts

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