providers

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package providers contains custom provider implementations and wrappers for the koanf configuration library. This file implements a CLI provider wrapper that handles key name mapping and flattening for command-line arguments.

Package providers contains example implementations of custom providers that demonstrate how to implement the ParserProvider interface. These providers show how to create self-describing providers that specify their required parser type.

Package providers implements a factory pattern for creating koanf providers with automatic parser detection and configuration management. It supports zero-configuration setup for common provider types while maintaining flexibility for custom provider implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CliProviderWrapper

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

CliProviderWrapper wraps a CLI provider to handle key name mapping and transformation. It processes command-line arguments and converts them into a format compatible with the configuration structure, including flattening nested maps and applying key transformations.

func NewCliProviderWrapper

func NewCliProviderWrapper(original koanf.Provider, cmdName, delim string) *CliProviderWrapper

NewCliProviderWrapper creates a new CLI provider wrapper with the specified parameters.

Parameters:

  • original: The underlying koanf.Provider to wrap
  • cmdName: The command name for logging and identification purposes
  • delim: The delimiter to use for key separation (typically ".")

Returns a configured CliProviderWrapper ready for use.

func (*CliProviderWrapper) Read

func (w *CliProviderWrapper) Read() (map[string]any, error)

Read implements the koanf.Provider interface by reading data from the wrapped provider, processing it through key transformations, and flattening nested structures. It handles the conversion of CLI arguments into a standardized configuration format.

func (*CliProviderWrapper) ReadBytes

func (w *CliProviderWrapper) ReadBytes() ([]byte, error)

ReadBytes implements the koanf.Provider interface

type CustomJSONProvider

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

CustomJSONProvider is an example implementation of a provider that implements the ParserProvider interface. It demonstrates how custom providers can specify their required parser type (JSON in this case) for automatic parser detection.

func NewCustomJSONProvider

func NewCustomJSONProvider(data []byte) *CustomJSONProvider

NewCustomJSONProvider creates a new custom JSON provider with the provided data. The provider will automatically use the JSON parser when processed by the configuration system.

func (*CustomJSONProvider) Read

func (p *CustomJSONProvider) Read() (map[string]interface{}, error)

Read implements the koanf.Provider interface but is not used in this implementation. The provider relies on ReadBytes() and the JSON parser for data processing.

func (*CustomJSONProvider) ReadBytes

func (p *CustomJSONProvider) ReadBytes() ([]byte, error)

ReadBytes implements the koanf.Provider interface by returning the raw JSON data. This method is used by koanf to obtain the configuration data for parsing.

func (*CustomJSONProvider) RequiredParser

func (p *CustomJSONProvider) RequiredParser() koanf.Parser

RequiredParser implements the ParserProvider interface by returning the JSON parser. This allows the configuration system to automatically detect and use the correct parser for this provider without manual specification.

type CustomYAMLProvider

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

CustomYAMLProvider is another example implementation that demonstrates how to create a provider that requires the YAML parser. It shows the flexibility of the ParserProvider interface for different data formats.

func NewCustomYAMLProvider

func NewCustomYAMLProvider(data []byte) *CustomYAMLProvider

NewCustomYAMLProvider creates a new custom YAML provider with the provided data. The provider will automatically use the YAML parser when processed by the configuration system.

func (*CustomYAMLProvider) Read

func (p *CustomYAMLProvider) Read() (map[string]interface{}, error)

Read implements koanf.Provider interface

func (*CustomYAMLProvider) ReadBytes

func (p *CustomYAMLProvider) ReadBytes() ([]byte, error)

ReadBytes implements the koanf.Provider interface by returning the raw YAML data. This method is used by koanf to obtain the configuration data for parsing.

func (*CustomYAMLProvider) RequiredParser

func (p *CustomYAMLProvider) RequiredParser() koanf.Parser

RequiredParser implements ParserProvider interface

type FileWatcher

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

FileWatcher wraps the koanf file provider with enhanced watching capabilities that monitor the parent directory to handle atomic file operations properly.

func NewFileWatcher

func NewFileWatcher(filePath string) (*FileWatcher, error)

NewFileWatcher creates a new FileWatcher that monitors the parent directory of the given file path to handle atomic file operations properly.

func (*FileWatcher) GetFilePath

func (fw *FileWatcher) GetFilePath() string

GetFilePath returns the absolute path of the file being watched

func (*FileWatcher) IsWatching

func (fw *FileWatcher) IsWatching() bool

IsWatching returns true if the file is currently being watched

func (*FileWatcher) Read

func (fw *FileWatcher) Read() (map[string]any, error)

Read implements the koanf.Provider interface

func (*FileWatcher) ReadBytes

func (fw *FileWatcher) ReadBytes() ([]byte, error)

ReadBytes implements the koanf.Provider interface

func (*FileWatcher) Unwatch

func (fw *FileWatcher) Unwatch() error

Unwatch stops monitoring the file for changes

func (*FileWatcher) Watch

func (fw *FileWatcher) Watch(cb func(event any, err error)) error

Watch starts monitoring the parent directory for changes to the target file. This approach handles atomic file operations (like those performed by text editors) that would otherwise break direct file watching.

type ParserProvider

type ParserProvider interface {
	// RequiredParser returns the parser required by this provider.
	// Return nil if the provider handles parsing internally.
	RequiredParser() koanf.Parser
}

ParserProvider is an optional interface that providers can implement to explicitly specify their required parser. This takes precedence over automatic parser detection.

type ProviderConfig

type ProviderConfig struct {
	// Provider is the koanf data provider
	Provider koanf.Provider
	// Parser is the associated parser, nil if provider handles parsing internally
	Parser koanf.Parser
}

ProviderConfig represents a complete provider configuration containing both the data provider and its associated parser. Parser can be nil for providers that handle parsing internally.

type ProviderFactory

type ProviderFactory struct{}

ProviderFactory is responsible for creating provider configurations from various input sources with automatic parser detection.

func NewProviderFactory

func NewProviderFactory() *ProviderFactory

NewProviderFactory creates a new provider factory

func (*ProviderFactory) CreateProviders

func (f *ProviderFactory) CreateProviders(sources ...any) ([]ProviderConfig, error)

CreateProviders creates provider configurations from various input sources. Supported source types:

  • string: treated as file path, automatically detects parser from extension
  • koanf.Provider: uses zero-config auto-detection for parser requirement

Returns a slice of ProviderConfig with appropriate parsers assigned, or an error if any source type is unsupported.

Jump to

Keyboard shortcuts

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