config

package module
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 9 Imported by: 0

README

Config

Configuration manager with default Env, Json and Memory driver.

Mote: Config use Caster for get dependencies by type.

Create New Config Driver

Config library contains three different driver by default.

Env Driver

Env driver use environment file (.env) for managing configuration.

import "github.com/bopher/config"
envConf, err := config.NewEnvConfig("app.env", "db.env", ".env")
JSON Driver

JSON driver use json file for managing configuration.

Caution: When you pass multiple file, for accessing config you must pass file name as first part of config path!

import "github.com/bopher/config"
jsonConf, err := config.NewJSONConfig("app.json", "db.json", "global.json")
Memory Driver

Use in-memory array for keeping and managing configuration.

import "github.com/bopher/config"
memConf, err := config.NewMemoryConfig(map[string]any{
    "name": "My First App",
    "key": "My Secret Key",
})

Usage

Config interface contains following methods:

Load

Load/Reload configurations.

// Signature:
Load() error

// Example
err := envConf.Load()
Set

Set configuration item. this function override preloaded config.

// Signature:
Set(key string, value any) error

// Example
err := memConf.Set("name", "My App")
err = envConf.Set("APP_NAME", "My App")
err = jsonConf.Set("app_name", "My App")

Cation: For setting/overriding config item in JSON driver with multiple files pass filename as first part of config path.

import "github.com/bopher/config"
jsonConf, err := config.NewJSONConfig("file1.json", "file2.json")
err = jsonConf.Set("file1.app.title", "Some")
Get

Get configuration. Get function return config item as any. if you need get config with type use helper get functions described later.

// Signature:
Get(key string) any

Caution: For JSON driver with multiple file you must pass filename as first part of config path!

item := jsonConf.Get("file1.app.title")
Exists

Check if config item exists.

// Signature:
Exists(key string) bool
Cast

Parse config as caster.

// Signature:
Cast(name string) caster.Caster

// Example:
v, err := conf.Cast("timeout").Int()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config interface {
	// Load configurations
	Load() error
	// Set configuration
	//
	// return error if driver not support set or error happend
	//
	// set override local configuration
	Set(key string, value any) error
	// Get configuration
	//
	// return nil if value not exists
	Get(key string) any
	// Exists check if config item exists
	Exists(key string) bool
	// Cast parse config as caster
	Cast(key string) caster.Caster
}

Config is the interface for configuration manager drivers.

func NewEnvConfig

func NewEnvConfig(filenames ...string) (Config, error)

NewEnvConfig create a new env file configuration manager instance

func NewJSONConfig

func NewJSONConfig(filenames ...string) (Config, error)

NewJSONConfig create a new json file configuration manager instance

func NewMemoryConfig

func NewMemoryConfig(config map[string]any) (Config, error)

NewMemoryConfig create a new in-memory configuration manager instance

Jump to

Keyboard shortcuts

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