Documentation
¶
Overview ¶
Package config provides a simple, tag-based configuration loader for an Fx application, built on uber/config and go-playground/validator.
Index ¶
- func KnownType(key string) (reflect.Type, bool)
- func Module(opts ...ModuleOption) fx.Option
- func Provide[T any]() func(*uber.YAML) (*T, error)
- func ProvideFromKey[T any](key string) func(provider *uber.YAML) (*T, error)
- func Redact(_ string, v any) any
- func RegisterKnown(key string, sample any)
- func RegisterRequirement(key string, sample any)
- func RegisterRequirementType(key string, tt reflect.Type)
- func ResetDiscoveryForTests()
- func Skeleton(req Requirement) (string, error)
- type CheckResult
- type FieldSpec
- type ModuleOption
- type Requirement
- type Source
- type YAMLProvider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Module ¶
func Module(opts ...ModuleOption) fx.Option
Module wires the core uber/config YAML provider into an Fx application.
This is the foundational component that enables configuration loading. It must be included in your Fx app for `Provide` and `ProvideFromKey` to work.
Configuration is loaded with the following precedence (from lowest to highest, with later values overriding earlier ones): 1. Custom Sources: Provided via `WithSources()` or `WithEmbeddedBytes()`. 2. Base Config: `config/config.yml` 3. Local Overrides: `config/config.local.yml` 4. Service-Specific Overrides: `config/<service-name>.yml` (from the runtimeinfo package). 5. Environment Variables: Any `${...}` placeholders are expanded.
func Provide ¶
Provide returns an Fx provider that loads the entire configuration into type T, validates it, and provides a pointer to it (`*T`) to the Fx container.
It is a convenient shorthand for `ProvideFromKey[T](uber.Root)`.
func ProvideFromKey ¶
ProvideFromKey returns an Fx provider that loads a specific configuration subtree (identified by `key`) into type T, validates it, and provides a pointer to it (`*T`) to the Fx container.
If validation fails based on the `validate` tags in the struct, the Fx application will fail to start with a descriptive error.
func Redact ¶
Redact masks secret-looking values within v for safe logging/display. The key parameter can be used for future, key-specific redaction nuances.
func RegisterKnown ¶
RegisterKnown registers a known module key and its config type, so tools can activate requirements without referencing the type directly. Typical usage from a module's init():
config.RegisterKnown("http", (*http.Config)(nil))
func RegisterRequirement ¶
RegisterRequirement registers a configuration requirement for a given key and sample type. The sample may be a value or a typed nil pointer to the desired type. This is useful for programmatic activation without generics.
func RegisterRequirementType ¶
RegisterRequirementType registers a requirement using a reflect.Type.
func ResetDiscoveryForTests ¶
func ResetDiscoveryForTests()
ResetDiscoveryForTests clears the internal registry. Exported for tests; do not use in application code.
func Skeleton ¶
func Skeleton(req Requirement) (string, error)
Skeleton renders an example YAML snippet for the requirement key.
Types ¶
type CheckResult ¶
type CheckResult struct {
Key string
Type string
OK bool
Err error
Issues []string // formatted validator issues: yaml.path: rule
Unknown []string // unknown keys detected in YAML subtree
}
CheckResult represents the outcome of validating a single requirement against a configuration provider.
func Check ¶
func Check(p *uber.YAML) []CheckResult
Check validates all discovered requirements against the provided YAML provider. It attempts to populate and validate each config subtree using the same rules as ProvideFromKey (including `validate` struct tags).
type FieldSpec ¶
type FieldSpec struct {
Path string // YAML dot path relative to Requirement.Key
Type string // Go kind or type name
Required bool // true if validate tag contains "required"
}
FieldSpec describes a single field in a config struct for documentation purposes.
func Spec ¶
func Spec(req Requirement) ([]FieldSpec, error)
Spec returns a best-effort field specification for the given requirement. It infers YAML field names from `yaml` tags when present, falling back to lowercased field names. Embedded/inline fields are flattened.
type ModuleOption ¶
type ModuleOption func(*moduleOpts)
ModuleOption customizes the behavior of the config Module by adding extra sources.
func WithEmbeddedBytes ¶
func WithEmbeddedBytes(b []byte) ModuleOption
WithEmbeddedBytes adds an embedded YAML payload (e.g., from `//go:embed`) as a low-precedence source for default values.
func WithSources ¶
func WithSources(srcs ...uber.YAMLOption) ModuleOption
WithSources injects additional uber/config sources at the lowest precedence. This is useful for providing default configurations from code.
type Requirement ¶
type Requirement struct {
// Key is the YAML subtree key, e.g. "http" or "telemetry". Root is "".
Key string
// Type is a human-readable Go type string, e.g. "http.Config".
Type string
// PkgPath is the import path for the type's package.
PkgPath string
}
Requirement describes a config requirement declared via ProvideFromKey[T](key).
It identifies the YAML subtree key and the Go type expected to be populated from that subtree.
func Requirements ¶
func Requirements() []Requirement
Requirements returns a snapshot of all discovered configuration requirements registered so far in this process.
type Source ¶
type Source = uber.YAMLOption
Source is an alias for uber/config YAML options (file, reader, expand, etc.).
func DefaultSources ¶
func DefaultSources() []Source
DefaultSources returns the default, low-precedence sources for CLI usage. Precedence (lowest -> highest) when combined by NewYAML:
- Default file: config/config.yml (if present)
- Env override: CONFIG=file.yml (if set, must exist)
- CLI flag: passed via opts (highest precedence)
Note: Services should continue using Module(); DefaultSources is intended for CLIs.
type YAMLProvider ¶
YAMLProvider is the concrete provider type used throughout the repo. It aliases the underlying uber/config YAML type for convenience.
func NewYAML ¶
func NewYAML(_ context.Context, opts ...ModuleOption) (*YAMLProvider, error)
NewYAML builds a YAML provider using the same underlying primitives as Module, but with a CLI-friendly precedence model:
default config file -> $CONFIG override -> explicit sources via opts (highest)
Environment expansion is always applied. If $CONFIG is set but the file is missing, an error is returned.