config

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package config provides hierarchical application configuration loading, profile merging, overrides, type conversion, unmarshaling, and validation for Rays.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(value any, t reflect.Type) (reflect.Value, Error)

Convert converts a source value of any type into a reflect.Value matching target type t. It supports direct assignment, time.Duration strings, []byte, encoding.TextUnmarshaler, boolean conversions, and numeric string parsing and formatting.

func ConvertNumeric

func ConvertNumeric[T2, T1 IntegerConstraint | FloatConstraint](value T1) T2

ConvertNumeric casts a numeric value of type T1 directly to numeric type T2.

func ConvertNumericString

func ConvertNumericString[T IntegerConstraint | FloatConstraint](value T) string

ConvertNumericString converts an integer or float value of type T into its string representation.

func ConvertStringNumeric

func ConvertStringNumeric[T IntegerConstraint | FloatConstraint](value string) (T, Error)

ConvertStringNumeric parses a numeric string into target integer or float type T.

func Validate

func Validate(obj any) Error

Validate validates struct fields on obj using go-playground/validator, dynamically proxying unexported fields to shadow exported fields while preserving validation tags.

Types

type Config

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

Config manages hierarchical configuration data, profiles, defaults, overrides, and unmarshaling into strongly-typed structures.

func NewConfig

func NewConfig() *Config

NewConfig initializes and returns an empty Config container with a default logger.

func NewConfigFor

func NewConfigFor(m map[string]any) *Config

NewConfigFor constructs and returns a new Config preloaded with the given configuration map.

func (*Config) ActiveProfiles

func (this *Config) ActiveProfiles() []string

ActiveProfiles returns the slice of active configuration profile names.

func (*Config) AddConfigFS

func (this *Config) AddConfigFS(fsys fs.FS) Error

AddConfigFS registers a filesystem to load configuration files from during initialization. Returns an error if the container has already been initialized.

func (*Config) AddDefaultConfigFS

func (this *Config) AddDefaultConfigFS(fsys fs.FS) Error

AddDefaultConfigFS registers a filesystem providing default fallback configuration files. Returns an error if the container has already been initialized.

func (*Config) AddOverride

func (this *Config) AddOverride(key, value string) Error

AddOverride registers a key-value override that takes precedence over loaded configuration values. Returns an error if the container has already been initialized.

func (*Config) Clear

func (this *Config) Clear()

Clear clears the cached merged configuration results and resets the initialization state, allowing the configuration container to be re-initialized.

func (*Config) CopyDefaultConfig

func (this *Config) CopyDefaultConfig(from, to string) Error

CopyDefaultConfig registers a duplication rule copying default configuration from one path to another. Returns an error if the container has already been initialized.

func (*Config) CopyDefaultConfigForProfile

func (this *Config) CopyDefaultConfigForProfile(from, to, profile string) Error

CopyDefaultConfigForProfile registers a duplication rule copying default configuration for a specific profile. Returns an error if the container has already been initialized.

func (*Config) GetConfigValue

func (this *Config) GetConfigValue(key string) (string, Error)

GetConfigValue retrieves a string configuration value by dot-notated key path, applying registered overrides and converting the value to string.

func (*Config) GetSubConfig

func (this *Config) GetSubConfig(key string) (*Config, Error)

GetSubConfig extracts and returns an isolated sub-configuration rooted at the specified dot-separated key path, deep-cloning backing maps and propagating matching prefix overrides.

func (*Config) InitConfig

func (this *Config) InitConfig() Error

InitConfig executes configuration initialization: 1. Invokes all discovered ConfigurationParser instances on default and application filesystems. 2. Merges default configurations, copies duplicated subtrees, and merges base configs. 3. Merges active profile configurations into the final result map.

Returns an error if the container has already been initialized.

func (*Config) LoadConfig

func (this *Config) LoadConfig(config map[string]any) Error

LoadConfig merges application configuration values into the container. Returns an error if the container has already been initialized.

func (*Config) LoadConfigAt

func (this *Config) LoadConfigAt(config map[string]any, root string) Error

LoadConfigAt merges configuration values nested under a dot-delimited root key path. Returns an error if the container has already been initialized.

func (*Config) LoadConfigForProfile

func (this *Config) LoadConfigForProfile(config map[string]any, profile string) Error

LoadConfigForProfile merges configuration values for a specific profile into the container. Returns an error if the container has already been initialized.

func (*Config) LoadConfigForProfileAt

func (this *Config) LoadConfigForProfileAt(config map[string]any, profile, root string) Error

LoadConfigForProfileAt merges configuration values for a profile nested under a dot-delimited root key path. Returns an error if the container has already been initialized.

func (*Config) LoadDefaultConfig

func (this *Config) LoadDefaultConfig(config map[string]any) Error

LoadDefaultConfig merges default fallback configuration values into the container. Returns an error if the container has already been initialized.

func (*Config) LoadDefaultConfigAt

func (this *Config) LoadDefaultConfigAt(config map[string]any, root string) Error

LoadDefaultConfigAt merges default configuration values nested under a dot-delimited root key path. Returns an error if the container has already been initialized.

func (*Config) Reset

func (this *Config) Reset()

Reset clears all configurations, profiles, overrides, registered filesystems, and cached state.

func (*Config) SetActiveProfiles

func (this *Config) SetActiveProfiles(profiles []string) Error

SetActiveProfiles configures the list of active profile names to activate during initialization. Returns an error if the container has already been initialized.

func (*Config) SetLogger

func (this *Config) SetLogger(l *logger.Logger)

SetLogger replaces the logger instance used by the configuration container if non-nil.

func (*Config) Unmarshal

func (this *Config) Unmarshal(obj any) Error

Unmarshal maps active configuration values into the fields of the target struct pointer and validates the resulting structure using defined validation tags.

type InvalidTypeKeyError

type InvalidTypeKeyError struct {
	*DefaultError
}

InvalidTypeKeyError represents an error when traversing a key path encounters an incompatible data type.

func NewInvalidTypeKeyError

func NewInvalidTypeKeyError(key, component string) *InvalidTypeKeyError

NewInvalidTypeKeyError constructs an InvalidTypeKeyError for the given key and incompatible path component.

type KeyNotFoundError

type KeyNotFoundError struct {
	*DefaultError
}

KeyNotFoundError represents an error when a requested configuration key path does not exist.

func NewKeyNotFoundError

func NewKeyNotFoundError(key, component string) *KeyNotFoundError

NewKeyNotFoundError constructs a KeyNotFoundError for the given key and failing path component.

type YamlConfigFSLoader

type YamlConfigFSLoader struct{}

YamlConfigFSLoader implements core.ConfigurationParser for YAML files located in an fs.FS.

func (*YamlConfigFSLoader) ParseConfigurationFilesystem

func (this *YamlConfigFSLoader) ParseConfigurationFilesystem(fsys fs.FS, loader func(m map[string]any, profile string)) Error

ParseConfigurationFilesystem walks fsys, unmarshaling any matching YAML configuration files and invoking loader with the parsed map and extracted profile name. Returns an error if filesystem walking, file reading, or YAML unmarshaling fails.

Jump to

Keyboard shortcuts

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