config

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: MIT Imports: 8 Imported by: 0

README

Ayotl

Ayotl is a lightweight library designed to simplify the management of local configuration files by mapping their contents into well-defined Go structs.

Config Files

supported config files
  • yaml
  • json
Example of a config json file:
{
    "stage": "development",
    "app": {
        "host": "127.0.0.1",
        "port": 3001
    },
    "services": {
        "login": {
            "host": "127.0.0.1",
            "port": 3002,
            "user": "123",
            "password": "4567",
        },
        "enabled": true,
    }
}

Environment variables

Configuration values can be sourced from environment variables using placeholders in your config file:

{
    "stage": "${STAGE}",
    "app": {
        "host": "${APPLICATION_HOST}",
        "port": "${APPLICATION_PORT}"
    },
   
    "services": {
        "login": {
            "host": "${LOGIN_SERVICE_HOST}",
            "port": "${LOGIN_SERVICE_PORT}",
            "user": "${LOGIN_SERVICE_USER}",
            "password": "${LOGIN_SERVICE_PASSWORD}",
        },
        "enabled": true,
}

Set your environment variables as follows:

STAGE=development
APPLICATION_PORT=3001
APPLICATION_HOST=127.0.0.1
LOGIN_SERVICE_HOST=127.0.0.1
LOGIN_SERVICE_PORT=3002
LOGIN_SERVICE_USER=1234
LOGIN_SERVICE_PASSWORD=5678

If an environment variable is not set, its value will default to an empty string.

Integration

Define your configuration struct using the mapstructure struct tag. For example:

type Config struct {
	Stage    string        `mapstructure:"stage"`
	App      App           `mapstructure:"app"`
	Services Services      `mapstructure:"services"`
}

// ApplicationConfig is a struct to define configurations for the http server
type App struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"port"`
}

// LoggerConfig is a struct to define configurations for Logger
type Services struct {
	Login   LoginService `mapstructure:"login"`
}
// LoggerConfig is a struct to define configurations for Logger
type LoginService struct {
	Host string     `mapstructure:"host"`
	Port int        `mapstructure:"port"`
	User string     `mapstructure:"user"`
	Pass string     `mapstructure:"password"`
}
Default Values

To specify default values for configuration fields, implement the SetDefaults function:

func (c *Config) SetDefaults() ConfigMap {
    // create a new configMap
	defaults := make(ConfigMap)
	// application defaults values
	defaults["services.login.host"] = "127.0.0.1"
	defaults["services.login.port"] = "3002"
    // return the default configMap with our mapping
	return defaults
}

Using the dot-notation can set the default value, this will be appended in the struct if this config value doesn't exist in our config file.

if those values are defied in our config file, those will be overridden for the one existing in the config file

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flatten

func Flatten(m map[string]interface{}) map[string]interface{}

Flatten is a init wrapper for flatten

func GetValue

func GetValue(m map[string]interface{}, keysToFind []string) interface{}

GetValue is a function to search recursively a key in a map[string]interface{}

func MergeEnvVar

func MergeEnvVar(m, envVars ConfigMap) map[string]interface{}

MergeEnvVar merge Env variables into placeholders

func MergeKeys

func MergeKeys(m1, m2 ConfigMap) map[string]interface{}

MergeKeys merge 2 ConfigMap given

func SetValue

func SetValue(m map[string]interface{}, keysToFind []string, value interface{}) map[string]interface{}

SetValue is a function to search recursively a key in a map[string]interface{} and add it with a set of keys given

Types

type Config

type Config struct {
	ConfigMap    ConfigMap
	EnvConfigMap ConfigMap
	// contains filtered or unexported fields
}

func New

func New() *Config

New return a New Config

func (*Config) ConfigFileMerge

func (c *Config) ConfigFileMerge(s string) error

ConfigFileMerge read configs from file and merge the config into ConfigMap if Key exist previosly in ConfigMap, the value will be overridden by the value from the file

func (*Config) Get

func (c *Config) Get(k string) interface{}

Get return value from given key, and return empty string if key don't exist key can be passed in `dot-notation`

func (*Config) LoadConfigs

func (c *Config) LoadConfigs(configFiles ...string) (err error)

LoadConfig is a function to load the configurations in ConfigMap

func (*Config) MustBool

func (c *Config) MustBool(key string, must bool) bool

MustBool returns the value associated with the key as a int64 or a default value if 0.

func (*Config) MustEnvString

func (c *Config) MustEnvString(key, must string) string

MustString returns the value associated with the key as a string or a default value if empty string.

func (*Config) MustInt

func (c *Config) MustInt(key string, must int) int

MustInt returns the value associated with the key int or a default value if 0.

func (*Config) MustInt32

func (c *Config) MustInt32(key string, must int32) int32

MustInt32 returns the value associated with the key as a int32 or a default value if 0.

func (*Config) MustInt64

func (c *Config) MustInt64(key string, must int64) int64

MustInt64 returns the value associated with the key as a int64 or a default value if 0.

func (*Config) MustString

func (c *Config) MustString(key, must string) string

MustString returns the value associated with the key as a string or a default value if empty string.

func (*Config) Set

func (c *Config) Set(k string, v interface{})

Set add or update value from given key key can be passed in `dot-notation`

func (*Config) SetConfigImpl

func (c *Config) SetConfigImpl(impl Configuration) *Config

func (*Config) SetConfigMap

func (c *Config) SetConfigMap(cm ConfigMap) *Config

func (*Config) SetDefault

func (c *Config) SetDefault(key string, val interface{})

func (*Config) Unmarshal

func (c *Config) Unmarshal(s any) error

Unmarshal function convert a ConfigMap type into a struct using mapStructure Decoder

func (*Config) WithEnv

func (c *Config) WithEnv(envs ...string) *Config

WithEnv Load env variables and add into ConfigMap

type ConfigMap

type ConfigMap map[string]interface{}

func ReadFile

func ReadFile(file string) (ConfigMap, error)

ReadFile is a function to read a file and decode, supporting only yaml and json at the moment

type Configuration

type Configuration interface {
	SetDefaults() ConfigMap
}

Jump to

Keyboard shortcuts

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