config

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config populates struct fields from registered configuration sources.

Use this package when you need to load configuration values into a struct from external sources such as environment variables. Fields are marked for processing using struct tags that specify the source type and optional default values.

The package includes a built-in environment variable source that maps struct field names to SNAKE_CASE environment variable names. Custom sources can be registered to support additional configuration backends.

Processed configurations can optionally be validated to ensure all required values are present and conform to expected constraints.

Index

Constants

View Source
const (
	// ProcessorTag specifies which configuration processor should populate the struct field.
	ProcessorTag = "config"

	// DefaultTag supplies a fallback value when a processor does not return a value for the field.
	DefaultTag = "config_default"
)
View Source
const (
	// ProcessorTypeEnv identifies the environment variable processor.
	ProcessorTypeEnv = "ENV"
)

Variables

This section is empty.

Functions

func MustRegisterProcessor

func MustRegisterProcessor(name string, fn SourceFunc)

MustRegisterProcessor registers a SourceFunc for a given processor name.

func Process

func Process[T any]() (*T, error)

Process sets struct field values using registered configuration sources. A field is processed only when it specifies the `config` tag with a source type. If the source returns no value and a default is not provided via the `config_default` tag, an error is returned.

func ProcessAndValidate

func ProcessAndValidate[T any]() (*T, error)

ProcessAndValidate processes configuration and validates the resulting struct.

Types

type SourceFunc

type SourceFunc func(fieldName string, fieldMetadata *structs.FieldMetadata) (string, bool, error)

SourceFunc fetches a configuration value for a field. It should return the value and whether it was found.

Jump to

Keyboard shortcuts

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