config

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: MIT Imports: 1 Imported by: 0

README

Configuration

Overview

The Configuration component provides a flexible and extensible system for managing application configuration from multiple sources and formats. It supports environment variables, files, and remote configuration services.

Features

  • Multiple Sources: Load configuration from files, environment variables, and remote services
  • Multiple Formats: Support for JSON, YAML, TOML, and other formats
  • Dynamic Reloading: Automatically reload configuration when changes are detected
  • Validation: Validate configuration against schemas
  • Defaults: Provide default values for configuration options

Installation

go get github.com/abitofhelp/servicelib/config

Quick Start

See the Basic Usage example for a complete, runnable example of how to use the configuration component.

Configuration

See the Custom Adapter example for a complete, runnable example of how to configure the configuration component.

API Documentation

Core Types

The configuration component provides several core types for managing configuration.

Config

The main type that provides configuration functionality.

type Config struct {
    // Fields
}
Adapter

Interface for configuration adapters.

type Adapter interface {
    // Methods
}
Key Methods

The configuration component provides several key methods for managing configuration.

Load

Loads configuration from a source.

func (c *Config) Load(ctx context.Context, source string) error
Get

Gets a configuration value.

func (c *Config) Get(ctx context.Context, key string) (interface{}, error)
Set

Sets a configuration value.

func (c *Config) Set(ctx context.Context, key string, value interface{}) error

Examples

For complete, runnable examples, see the following directories in the EXAMPLES directory:

Best Practices

  1. Use Environment Variables: Use environment variables for sensitive configuration
  2. Validate Configuration: Always validate configuration against schemas
  3. Provide Defaults: Always provide default values for configuration options
  4. Handle Errors: Properly handle configuration errors
  5. Use Typed Getters: Use typed getters (GetString, GetInt, etc.) for type safety

Troubleshooting

Common Issues
Configuration Not Loading

If configuration is not loading, check that the source exists and is accessible.

Type Conversion Errors

If you're getting type conversion errors, use typed getters (GetString, GetInt, etc.) instead of Get.

  • Logging - Logging for configuration events
  • Errors - Error handling for configuration

Contributing

Contributions to this component are welcome! Please see the Contributing Guide for more information.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package config provides generic configuration interfaces and adapters.

Package config provides generic configuration interfaces that can be used across different applications.

Package config provides generic configuration interfaces that can be used across different applications.

Package config provides a flexible and extensible configuration management system for applications.

This package offers a set of interfaces and adapters that standardize how applications access configuration data, regardless of the underlying configuration source (files, environment variables, remote services, etc.).

The package is designed around the adapter pattern, allowing different configuration implementations to be used interchangeably while providing a consistent interface for application code.

Key components:

  • Config: The main interface providing access to application and database configuration
  • AppConfig: Interface for accessing application-specific configuration
  • DatabaseConfig: Interface for accessing database-specific configuration
  • GenericConfigAdapter: A flexible adapter that can wrap any configuration type

The package supports:

  • Type-safe configuration access
  • Default values for missing configuration
  • Fluent interface for adapter configuration
  • Backward compatibility with older configuration patterns

Example usage:

// Create a configuration struct
type MyConfig struct {
    AppVersion string
    DBType     string
    DBConnStr  string
}

// Implement the necessary provider interfaces
func (c *MyConfig) GetAppVersion() string {
    return c.AppVersion
}

func (c *MyConfig) GetDatabaseType() string {
    return c.DBType
}

func (c *MyConfig) GetDatabaseConnectionString(dbType string) string {
    return c.DBConnStr
}

// Create and use the adapter
cfg := &MyConfig{
    AppVersion: "1.0.0",
    DBType:     "postgres",
    DBConnStr:  "postgres://user:pass@localhost:5432/mydb",
}

adapter := config.NewGenericConfigAdapter(cfg).
    WithAppName("myapp").
    WithAppEnvironment("production")

// Use the adapter through the Config interface
var configInterface config.Config = adapter
appName := configInterface.GetApp().GetName()
dbConnStr := configInterface.GetDatabase().GetConnectionString()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppConfig

type AppConfig = interfaces.AppConfig

AppConfig is an alias for interfaces.AppConfig for backward compatibility

type AppConfigProvider

type AppConfigProvider interface {
	// GetAppVersion returns the application version.
	// This is typically a semantic version string (e.g., "1.2.3").
	GetAppVersion() string

	// GetAppName returns the application name.
	// This is optional and defaults to "application" if not provided.
	GetAppName() string

	// GetAppEnvironment returns the application environment.
	// Common values include "development", "staging", "production".
	// This is optional and defaults to "development" if not provided.
	GetAppEnvironment() string
}

AppConfigProvider defines the interface for accessing application configuration. This interface should be implemented by configuration types that provide application-specific configuration values.

type Config

type Config = interfaces.Config

Config is an alias for interfaces.Config for backward compatibility

type ConfigInterface

type ConfigInterface interface {
	// GetApp returns the App configuration
	GetApp() interface{}

	// GetDatabase returns the Database configuration
	GetDatabase() interface{}
}

ConfigInterface is an interface for configuration Deprecated: Use Config interface instead

type DatabaseConfig

type DatabaseConfig = interfaces.DatabaseConfig

DatabaseConfig is an alias for interfaces.DatabaseConfig for backward compatibility

type DatabaseConfigProvider

type DatabaseConfigProvider interface {
	// GetDatabaseType returns the database type.
	// Common values include "mongodb", "postgres", "mysql", "sqlite", etc.
	GetDatabaseType() string

	// GetDatabaseConnectionString returns the database connection string.
	// The connection string format depends on the database type provided.
	// The dbType parameter allows for different connection strings based on the database type.
	GetDatabaseConnectionString(dbType string) string
}

DatabaseConfigProvider defines the interface for accessing database configuration. This interface should be implemented by configuration types that provide database-specific configuration values.

type GenericAppConfigAdapter

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

GenericAppConfigAdapter is a generic adapter for application configuration. It implements the AppConfig interface and adapts any type T to provide standardized access to application configuration values.

func (*GenericAppConfigAdapter[T]) GetEnvironment

func (a *GenericAppConfigAdapter[T]) GetEnvironment() string

GetEnvironment returns the application environment. This method implements the AppConfig interface. If the underlying configuration object implements AppConfigProvider, its GetAppEnvironment method is called. Otherwise, the environment provided during adapter creation is returned (defaults to "development").

func (*GenericAppConfigAdapter[T]) GetName

func (a *GenericAppConfigAdapter[T]) GetName() string

GetName returns the application name. This method implements the AppConfig interface. If the underlying configuration object implements AppConfigProvider, its GetAppName method is called. Otherwise, the name provided during adapter creation is returned (defaults to "application").

func (*GenericAppConfigAdapter[T]) GetVersion

func (a *GenericAppConfigAdapter[T]) GetVersion() string

GetVersion returns the application version. This method implements the AppConfig interface. If the underlying configuration object implements AppConfigProvider, its GetAppVersion method is called. Otherwise, a default version "1.0.0" is returned.

type GenericConfigAdapter

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

GenericConfigAdapter is a generic adapter for any config type that provides the necessary methods. It implements the Config interface and adapts any type T to provide standardized configuration access through the AppConfig and DatabaseConfig interfaces.

func NewGenericConfigAdapter

func NewGenericConfigAdapter[T any](cfg T) *GenericConfigAdapter[T]

NewGenericConfigAdapter creates a new GenericConfigAdapter with default values. It initializes the adapter with the provided configuration object and sets default values for application name, environment, and database name.

The default values are:

  • appName: "application"
  • appEnvironment: "development"
  • dbName: "database"

These defaults can be overridden using the With* methods.

func (*GenericConfigAdapter[T]) GetApp

func (a *GenericConfigAdapter[T]) GetApp() AppConfig

GetApp returns the application configuration. This method implements the Config interface and returns an AppConfig that provides access to application-specific configuration values. The returned AppConfig is a GenericAppConfigAdapter that wraps the underlying configuration object.

func (*GenericConfigAdapter[T]) GetDatabase

func (a *GenericConfigAdapter[T]) GetDatabase() DatabaseConfig

GetDatabase returns the database configuration. This method implements the Config interface and returns a DatabaseConfig that provides access to database-specific configuration values. The returned DatabaseConfig is a GenericDatabaseConfigAdapter that wraps the underlying configuration object.

func (*GenericConfigAdapter[T]) WithAppEnvironment

func (a *GenericConfigAdapter[T]) WithAppEnvironment(env string) *GenericConfigAdapter[T]

WithAppEnvironment sets the application environment. This method overrides the default environment ("development") or the environment provided by the underlying configuration object. Common values include "development", "staging", and "production". It returns the adapter itself to allow for method chaining.

func (*GenericConfigAdapter[T]) WithAppName

func (a *GenericConfigAdapter[T]) WithAppName(name string) *GenericConfigAdapter[T]

WithAppName sets the application name. This method overrides the default application name ("application") or the name provided by the underlying configuration object. It returns the adapter itself to allow for method chaining.

func (*GenericConfigAdapter[T]) WithDatabaseName

func (a *GenericConfigAdapter[T]) WithDatabaseName(name string) *GenericConfigAdapter[T]

WithDatabaseName sets the database name. This method overrides the default database name ("database"). The database name is used when generating collection/table names and in other database-related operations. It returns the adapter itself to allow for method chaining.

type GenericDatabaseConfigAdapter

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

GenericDatabaseConfigAdapter is a generic adapter for database configuration. It implements the DatabaseConfig interface and adapts any type T to provide standardized access to database configuration values.

func (*GenericDatabaseConfigAdapter[T]) GetCollectionName

func (a *GenericDatabaseConfigAdapter[T]) GetCollectionName(entityType string) string

GetCollectionName returns the collection/table name for a given entity type. This method implements the DatabaseConfig interface. It performs simple pluralization of the entity type to derive the collection name. Special cases:

  • "family" becomes "families"
  • All other types have "s" appended (e.g., "user" becomes "users")

For more complex pluralization rules, this method should be overridden.

func (*GenericDatabaseConfigAdapter[T]) GetConnectionString

func (a *GenericDatabaseConfigAdapter[T]) GetConnectionString() string

GetConnectionString returns the database connection string. This method implements the DatabaseConfig interface. If the underlying configuration object implements DatabaseConfigProvider, its GetDatabaseConnectionString method is called with the database type. Otherwise, an empty string is returned.

func (*GenericDatabaseConfigAdapter[T]) GetDatabaseName

func (a *GenericDatabaseConfigAdapter[T]) GetDatabaseName() string

GetDatabaseName returns the database name. This method implements the DatabaseConfig interface. It returns the database name provided during adapter creation (defaults to "database").

func (*GenericDatabaseConfigAdapter[T]) GetType

func (a *GenericDatabaseConfigAdapter[T]) GetType() string

GetType returns the database type. This method implements the DatabaseConfig interface. If the underlying configuration object implements DatabaseConfigProvider, its GetDatabaseType method is called. Otherwise, "unknown" is returned. Common return values include "mongodb", "postgres", "mysql", "sqlite", etc.

Directories

Path Synopsis
Package interfaces provides generic configuration interfaces that can be used across different applications.
Package interfaces provides generic configuration interfaces that can be used across different applications.
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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