provider

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2025 License: MIT Imports: 4 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoValidProvider indicates that no suitable provider could be found after attempting all available options.
	ErrNoValidProvider = errors.New("no valid provider found")
	// ErrNotMatched indicates that a specific condition for selecting a provider was not met.
	ErrNotMatched = errors.New("provider not matched")
	// ErrNilProvider indicates that a case returned a nil Provider without an error.
	ErrNilProvider = errors.New("provider is nil")
)

Functions

func If added in v0.0.3

func If[T any](cond func(T) bool, then func(T) Provider) func(T) (Provider, error)

If creates a case function for Selector. It takes a condition function and a then function. If the condition returns true for the given parameter, it calls the then function to get the Provider. If the condition returns false, it returns an error indicating no match.

func IfE added in v0.0.3

func IfE[T any](cond func(T) bool, then func(T) (Provider, error)) func(T) (Provider, error)

IfE is like If, but allows the then function to return an error. Useful when provider construction can fail and the error should be surfaced.

Types

type ExpandEnv

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

ExpandEnv is a Provider adapter that expands environment variables present in the underlying provider's raw configuration bytes.

It treats the configuration as UTF-8 text and replaces $var or ${var} using os.ExpandEnv rules. Undefined variables expand to an empty string. This is useful when your config file or HTTP payload includes placeholders like "${PORT}" that should be resolved at runtime.

Note: This adapter is text-oriented. If the underlying data is non-text or contains many literal '$' characters, expansion may be undesirable.

func NewExpandEnv

func NewExpandEnv(provider Provider) *ExpandEnv

NewExpandEnv wraps an existing Provider and returns a new Provider that expands environment variable placeholders in the returned bytes.

func (*ExpandEnv) Read

func (e *ExpandEnv) Read(ctx context.Context) ([]byte, error)

Read implements Provider. It reads bytes from the wrapped provider and then applies os.ExpandEnv to expand environment variables. If there is no '$' in the content, the original bytes are returned without allocation.

type Provider

type Provider interface {
	// Read returns the entire configuration as raw []bytes to be parsed.
	// The provided context controls cancellation and deadlines.
	Read(ctx context.Context) ([]byte, error)
}

Provider represents a configuration provider. Providers can read configuration from a source (file, HTTP, etc.)

func Selector added in v0.0.3

func Selector[T any](param T, cases ...func(T) (Provider, error)) (Provider, error)

Selector tries each case function in order with the given parameter. It returns the first Provider that does not return an error. If all cases return an error, it returns an error indicating no valid provider was found.

func SelectorWithErrors added in v0.0.3

func SelectorWithErrors[T any](param T, cases ...func(T) (Provider, error)) (Provider, error)

SelectorWithErrors behaves like Selector but aggregates non-matching errors (excluding ErrNotMatched) and returns them joined with ErrNoValidProvider when no provider is selected. Callers can use errors.Is to test for ErrNoValidProvider while still getting detailed context for debugging.

type ReaderFunc added in v0.0.4

type ReaderFunc func(ctx context.Context) ([]byte, error)

func (ReaderFunc) Read added in v0.0.4

func (f ReaderFunc) Read(ctx context.Context) ([]byte, error)

type Select added in v0.0.4

type Select[T any] struct {
	// contains filtered or unexported fields
}

Select is a helper struct to hold the parameter and case functions for Selector.

func NewSelect added in v0.0.4

func NewSelect[T any](param T, cases ...func(T) (Provider, error)) *Select[T]

NewSelect creates a new Select instance with the given parameter and case functions.

func (*Select[T]) Read added in v0.0.4

func (s *Select[T]) Read(ctx context.Context) ([]byte, error)

Read implements the Provider interface for Select. It uses Selector to choose a Provider based on the parameter and cases, then calls Read on the selected Provider.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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