config

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: MIT Imports: 8 Imported by: 0

README

Config

Configuration manager with default Env, Json and Memory driver.

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]interface{}{
    "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 interface{}) 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 interface{}. if you need get config with type use helper get functions described later.

// Signature:
Get(key string) interface{}

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
Getters

Getters function allow you to cast config item directly as type. Getters item return error when item not exists or type cast failed!

// BoolE parse item as boolean or return error on fail
BoolE(key string) (bool, error)

// IntE parse item as int or return error on fail
IntE(key string) (int, error)

// Int8E parse item as int8 or return error on fail
Int8E(key string) (int8, error)

// Int16E parse item as int16 or return error on fail
Int16E(key string) (int16, error)

// Int32E parse item as int32 or return error on fail
Int32E(key string) (int32, error)

// Int64E parse item as int64 or return error on fail
Int64E(key string) (int64, error)

// UIntE parse item as uint or return error on fail
UIntE(key string) (uint, error)

// UInt8E parse item as uint8 or return error on fail
UInt8E(key string) (uint8, error)

// UInt16E parse item as uint16 or return error on fail
UInt16E(key string) (uint16, error)

// UInt32E parse item as uint32 or return error on fail
UInt32E(key string) (uint32, error)

// UInt64E parse item as uint64 or return error on fail
UInt64E(key string) (uint64, error)

// Float64E parse item as float64 or return error on fail
Float64E(key string) (float64, error)

// StringE parse item as string or return error on fail
StringE(key string) (string, error)
Error Safe Getters

You can use safe getters to cast config item and pass fallback value in case of item casting failed!

// Bool parse item as boolean or return fallback
Bool(key string, fallback bool) bool

// Int parse item as int or return fallback
Int(key string, fallback int) int

// Int8 parse item as int8 or return fallback
Int8(key string, fallback int8) int8

// Int16 parse item as int16  or return fallback
Int16(key string, fallback int16) int16

// Int32 parse item as int32 or return fallback
Int32(key string, fallback int32) int32

// Int64 parse item as int64 or return fallback
Int64(key string, fallback int64) int64

// UInt parse item as uint or return fallback
UInt(key string, fallback uint) uint

// UInt8 parse item as uint8 or return fallback
UInt8(key string, fallback uint8) uint8

// UInt16 parse item as uint16 or return fallback
UInt16(key string, fallback uint16) uint16

// UInt32 parse item as uint32 or return fallback
UInt32(key string, fallback uint32) uint32

// UInt64 parse item as uint64 or return fallback
UInt64(key string, fallback uint64) uint64

// Float64 parse item as float64 or return fallback
Float64(key string, fallback float64) float64

// String parse item as string or return fallback
String(key string, fallback string) string

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 interface{}) error
	// Get configuration
	//
	// return nil if value not exists
	Get(key string) interface{}
	// Exists check if config item exists
	Exists(key string) bool
	// BoolE parse item as boolean or return error on fail
	BoolE(key string) (bool, error)
	// Bool parse item as boolean or return fallback
	Bool(key string, fallback bool) bool
	// IntE parse item as int or return error on fail
	IntE(key string) (int, error)
	// Int parse item as int or return fallback
	Int(key string, fallback int) int
	// Int8E parse item as int8 or return error on fail
	Int8E(key string) (int8, error)
	// Int8 parse item as int8 or return fallback
	Int8(key string, fallback int8) int8
	// Int16E parse item as int16 or return error on fail
	Int16E(key string) (int16, error)
	// Int16 parse item as int16  or return fallback
	Int16(key string, fallback int16) int16
	// Int32E parse item as int32 or return error on fail
	Int32E(key string) (int32, error)
	// Int32 parse item as int32 or return fallback
	Int32(key string, fallback int32) int32
	// Int64E parse item as int64 or return error on fail
	Int64E(key string) (int64, error)
	// Int64 parse item as int64 or return fallback
	Int64(key string, fallback int64) int64
	// UIntE parse item as uint or return error on fail
	UIntE(key string) (uint, error)
	// UInt parse item as uint or return fallback
	UInt(key string, fallback uint) uint
	// UInt8E parse item as uint8 or return error on fail
	UInt8E(key string) (uint8, error)
	// UInt8 parse item as uint8 or return fallback
	UInt8(key string, fallback uint8) uint8
	// UInt16E parse item as uint16 or return error on fail
	UInt16E(key string) (uint16, error)
	// UInt16 parse item as uint16 or return fallback
	UInt16(key string, fallback uint16) uint16
	// UInt32E parse item as uint32 or return error on fail
	UInt32E(key string) (uint32, error)
	// UInt32 parse item as uint32 or return fallback
	UInt32(key string, fallback uint32) uint32
	// UInt64E parse item as uint64 or return error on fail
	UInt64E(key string) (uint64, error)
	// UInt64 parse item as uint64 or return fallback
	UInt64(key string, fallback uint64) uint64
	// Float64E parse item as float64 or return error on fail
	Float64E(key string) (float64, error)
	// Float64 parse item as float64 or return fallback
	Float64(key string, fallback float64) float64
	// StringE parse item as string or return error on fail
	StringE(key string) (string, error)
	// String parse item as string or return fallback
	String(key string, fallback string) string
}

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]interface{}) (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