configfx

package
v0.6.32 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

ajan/configfx

Overview

The configfx package provides a flexible and powerful configuration loader for Go applications. It supports loading configuration from various sources, including environment files, JSON files, and system environment variables. The package is designed to work seamlessly with the ajan/di package.

The documentation below provides an overview of the package, its types, functions, and usage examples. For more detailed information, refer to the source code and tests.

API

ConfigLoader interface

Defines methods for loading configuration.

type ConfigLoader interface {
	LoadMeta(i any) (ConfigItemMeta, error)
	LoadMap(resources ...ConfigResource) (*map[string]any, error)
	Load(i any, resources ...ConfigResource) error
	LoadDefaults(i any) error

	FromEnvFileDirect(filename string, keyCaseInsensitive bool) ConfigResource
	FromEnvFile(filename string, keyCaseInsensitive bool) ConfigResource
	FromSystemEnv(keyCaseInsensitive bool) ConfigResource

	FromJsonFileDirect(filename string) ConfigResource
	FromJsonFile(filename string) ConfigResource
}
NewConfigManager function

Creates a new ConfigLoader object based on the provided configuration.

// func NewConfigManager() *ConfigManager

cl := configfx.NewConfigManager()
Load function

The Load method loads configuration from multiple resources.

Example:

type AppConfig struct {
	AppName  string `conf:"NAME" default:"go-service"`

  Postgres struct {
		Dsn string `conf:"DSN" default:"postgres://localhost:5432"`
	} `conf:"POSTGRES"`
}

func loadConfig() (*config.AppConfig, error) {
  conf := &config.AppConfig{}

  cl := configfx.NewConfigManager()

  err := cl.Load(
		conf,
                                      // load order:
		cl.FromJsonFile("config.json"),   // - attempts to read from config.json,
                                      //                         config.local.json,
                                      //                         config.[env].json,
                                      //                         config.[env].local.json
		cl.FromEnvFile(".env"),           // - attempts to read from .env
                                      //                         .env.local
                                      //                         .env.[env]
                                      //                         .env.[env].local
		cl.FromSystemEnv(),               // - attempts to read from system environment variables
	)
	if err != nil {
		return nil, fmt.Errorf("failed to load config: %w", err)
	}

  return conf, nil
}

func main() {
	appConfig, err := loadConfig()
	if err != nil {
		log.Fatalf("Error loading config: %v", err)

    return
	}

// Searches JSON files first, then checks the POSTGRES__DSN among environment variables.
	// If the config variable is not specified, it falls back to the default value "postgres://localhost:5432".
  fmt.Println(appConfig.Postgres.Dsn)
}

Documentation

Index

Constants

View Source
const (
	TagConf     = "conf"
	TagDefault  = "default"
	TagRequired = "required"

	Separator = "__"
)

Variables

View Source
var ErrNotStruct = results.Define("ERRBC00001", "not a struct") //nolint:gochecknoglobals

Functions

This section is empty.

Types

type ConfigItemMeta

type ConfigItemMeta struct {
	Type         reflect.Type
	Field        reflect.Value
	Name         string
	DefaultValue string

	Children        []ConfigItemMeta
	IsRequired      bool
	HasDefaultValue bool
}

type ConfigLoader

type ConfigLoader interface {
	LoadMeta(i any) (ConfigItemMeta, error)
	LoadMap(resources ...ConfigResource) (*map[string]any, error)
	Load(i any, resources ...ConfigResource) error
	LoadDefaults(i any) error

	FromEnvFileDirect(filename string, keyCaseInsensitive bool) ConfigResource
	FromEnvFile(filename string, keyCaseInsensitive bool) ConfigResource
	FromSystemEnv(keyCaseInsensitive bool) ConfigResource

	FromJsonFileDirect(filename string) ConfigResource
	FromJsonFile(filename string) ConfigResource
}

type ConfigManager

type ConfigManager struct{}

func NewConfigManager

func NewConfigManager() *ConfigManager

func (*ConfigManager) FromEnvFile

func (cl *ConfigManager) FromEnvFile(filename string, keyCaseInsensitive bool) ConfigResource

func (*ConfigManager) FromEnvFileDirect

func (cl *ConfigManager) FromEnvFileDirect(filename string, keyCaseInsensitive bool) ConfigResource

func (*ConfigManager) FromJsonFile

func (cl *ConfigManager) FromJsonFile(filename string) ConfigResource

func (*ConfigManager) FromJsonFileDirect

func (cl *ConfigManager) FromJsonFileDirect(filename string) ConfigResource

func (*ConfigManager) FromJsonString added in v0.6.25

func (cl *ConfigManager) FromJsonString(jsonStr string) ConfigResource

func (*ConfigManager) FromSystemEnv

func (cl *ConfigManager) FromSystemEnv(keyCaseInsensitive bool) ConfigResource

func (*ConfigManager) Load

func (cl *ConfigManager) Load(i any, resources ...ConfigResource) error

func (*ConfigManager) LoadDefaults

func (cl *ConfigManager) LoadDefaults(i any) error

func (*ConfigManager) LoadMap

func (cl *ConfigManager) LoadMap(resources ...ConfigResource) (*map[string]any, error)

func (*ConfigManager) LoadMeta

func (cl *ConfigManager) LoadMeta(i any) (ConfigItemMeta, error)

type ConfigResource

type ConfigResource func(target *map[string]any) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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