config

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 8 Imported by: 0

README

Ayotl

Ayotl is a simple implementation intended to reduce the complexity to manage local configuration files, mapping into defined 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

Environment variables depend on a config file. you can create your config file with placeholders similar to this:

{
    "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,
}

assuming your environment variables are like this:

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 any environment doesn't exist or can not be found, this will be replaced as an empty string.

Integration

You should have a struct with a struct tag mapstructure

following with the example of

{
    "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,
    }
}

You can define a struct similar to:

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

Sometimes is required to have default values in case we don't need to set them inside a config file for those cases, you should implement those default values in the function Setdefaults.

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 overrided 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 overrided 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 added in v0.0.8

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