config

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package config provides a unified configuration loader for OpenChoreo components.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldError added in v0.13.0

type FieldError struct {
	Field   string
	Message string
}

FieldError represents a validation error for a specific config field.

func Invalid added in v0.13.0

func Invalid(path *Path, msg string) *FieldError

Invalid returns a generic validation error with a custom message.

func MustBeGreaterThan added in v0.13.0

func MustBeGreaterThan[T constraints.Ordered](path *Path, value, min T) *FieldError

MustBeGreaterThan returns an error if value is not greater than min.

func MustBeInRange added in v0.13.0

func MustBeInRange[T constraints.Ordered](path *Path, value, min, max T) *FieldError

MustBeInRange returns an error if value is not within [min, max].

func MustBeLessThanOrEqual added in v0.13.0

func MustBeLessThanOrEqual[T constraints.Ordered](path *Path, value, max T) *FieldError

MustBeLessThanOrEqual returns an error if value is greater than max.

func MustBeNonNegative added in v0.13.0

func MustBeNonNegative[T constraints.Ordered](path *Path, value T) *FieldError

MustBeNonNegative returns an error if value is negative.

func MustBeOneOf added in v0.13.0

func MustBeOneOf(path *Path, value string, allowed []string) *FieldError

MustBeOneOf returns an error if value is not in the allowed list.

func MustNotBeEmpty added in v0.13.0

func MustNotBeEmpty(path *Path, value string) *FieldError

MustNotBeEmpty returns an error if the string value is empty.

func Required added in v0.13.0

func Required(path *Path) *FieldError

Required returns an error indicating a field is required.

func (*FieldError) Error added in v0.13.0

func (e *FieldError) Error() string

Error implements the error interface.

type Loader

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

Loader handles configuration loading from multiple sources.

func NewLoader

func NewLoader(envPrefix string) *Loader

NewLoader creates a new configuration loader. envPrefix should be like "OC_API" (without trailing delimiter). Environment variables use double underscore (__) for nesting: OC_API__SERVER__PORT -> server.port

func (*Loader) DumpYAML added in v0.13.0

func (l *Loader) DumpYAML(w io.Writer) error

DumpYAML writes the loaded configuration as YAML to the provided writer.

func (*Loader) LoadFlags

func (l *Loader) LoadFlags(flags *pflag.FlagSet, mappings map[string]string) error

LoadFlags applies CLI flag overrides using explicit mappings. Only flags that were explicitly set by the user are applied. Call this after LoadWithDefaults for highest priority overrides.

func (*Loader) LoadWithDefaults

func (l *Loader) LoadWithDefaults(defaults any, configPath string) error

LoadWithDefaults loads configuration with the following priority (highest to lowest):

  1. Environment variables (OC_API__SERVER__PORT -> server.port)
  2. Config file (YAML)
  3. Struct defaults

If configPath is specified but the file does not exist, an error is returned. If configPath is empty, only defaults and environment variables are used.

func (*Loader) Raw

func (l *Loader) Raw() map[string]any

Raw returns all loaded configuration as a nested map.

func (*Loader) Set

func (l *Loader) Set(key string, value any) error

Set manually sets a configuration value.

func (*Loader) Unmarshal

func (l *Loader) Unmarshal(path string, out any) error

Unmarshal unmarshals the loaded configuration into the provided struct.

func (*Loader) UnmarshalAndValidate

func (l *Loader) UnmarshalAndValidate(path string, out any) error

UnmarshalAndValidate unmarshals the configuration and validates it. If out implements Validator, Validate() is called after unmarshaling.

type Path added in v0.13.0

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

Path represents a path to a config field for error reporting. It builds paths like "server.middlewares.jwt.jwks.url" for clear error messages.

func NewPath added in v0.13.0

func NewPath(root string) *Path

NewPath creates a new path with a root segment.

func (*Path) Child added in v0.13.0

func (p *Path) Child(name string) *Path

Child returns a new path with the child segment appended.

func (*Path) Index added in v0.13.0

func (p *Path) Index(i int) *Path

Index returns a new path with an array index appended to the last segment. Example: path.Child("user_types").Index(0) produces "parent.user_types[0]"

func (*Path) String added in v0.13.0

func (p *Path) String() string

String returns the dot-separated path string.

type ValidationErrors added in v0.13.0

type ValidationErrors []*FieldError

ValidationErrors collects multiple validation errors.

func (ValidationErrors) Error added in v0.13.0

func (ve ValidationErrors) Error() string

Error implements the error interface, formatting all errors.

func (ValidationErrors) OrNil added in v0.13.0

func (ve ValidationErrors) OrNil() error

OrNil returns nil if there are no errors, otherwise returns the ValidationErrors.

type Validator

type Validator interface {
	Validate() error
}

Validator can be implemented by config structs to enable validation.

Jump to

Keyboard shortcuts

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