configuration

package module
v0.0.0-...-682a4b9 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: MIT Imports: 2 Imported by: 3

README

configuration-go

Build StatusGo Report Card

The configuration-go package intended for normal work with configuration files. For now we support HJSON format three-like configs. Library is intended to ease getting configuration file values if you know path to them

Install

Make sure you have a working Go environment. See the install instructions.

$ go get -u github.com/ilya1st/configuration-go

Usage as command line tool

Sample:

For example: we have file test.hjson

Usage as a GO library

package main
import(
  "fmt"
  "github.com/ilya1st/configuration-go"
)

func main(){
  config, err:=configuration.GetConfigInstance("mainconfig", "HJSON", "test.hjson")
  if err != nil {
    fmt.Printf("Error occurred %v\n", err);
  }
  val, err:=config.GetValue("section1", "subsection2", "value")
  fmt.Printf("getting section1/subsection2/value: val=%v, error=%v\n", val, err)
  val, err=config.GetValue("test")
  fmt.Printf("getting test: val=%v, error=%v\n", val, err)
}

See examples directory.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigItemNotFound

type ConfigItemNotFound struct {
	// contains filtered or unexported fields
}

ConfigItemNotFound is intended for misconfiguration cases

func NewConfigItemNotFound

func NewConfigItemNotFound(s string) *ConfigItemNotFound

NewConfigItemNotFound generates error object

func (*ConfigItemNotFound) Error

func (e *ConfigItemNotFound) Error() string

Error is standard error interface h

type ConfigNotConfiguredError

type ConfigNotConfiguredError struct {
	// contains filtered or unexported fields
}

ConfigNotConfiguredError is for cases if comesthing is not implemented we implement standard error interface

func NewConfigNotConfiguredError

func NewConfigNotConfiguredError(s string) *ConfigNotConfiguredError

NewConfigNotConfiguredError generates error object

func (*ConfigNotConfiguredError) Error

func (e *ConfigNotConfiguredError) Error() string

Error is standard error interface h

type ConfigNotImplementedError

type ConfigNotImplementedError struct {
	// contains filtered or unexported fields
}

ConfigNotImplementedError is for cases if comesthing is not implemented we implement standard error interface

func NewConfigNotImplementedError

func NewConfigNotImplementedError(s string) *ConfigNotImplementedError

NewConfigNotImplementedError generates error object

func (*ConfigNotImplementedError) Error

func (e *ConfigNotImplementedError) Error() string

Error is standard error interface method

type ConfigTypeMismatchError

type ConfigTypeMismatchError struct {
	// contains filtered or unexported fields
}

ConfigTypeMismatchError is intended for case we get value from object of wrong type

func NewConfigTypeMismatchError

func NewConfigTypeMismatchError(s string) *ConfigTypeMismatchError

NewConfigTypeMismatchError generates error object

func (*ConfigTypeMismatchError) Error

func (e *ConfigTypeMismatchError) Error() string

Error is standard error interface h

type ConfigUsageError

type ConfigUsageError struct {
	// contains filtered or unexported fields
}

ConfigUsageError is intended for misconfiguration cases

func NewConfigUsageError

func NewConfigUsageError(s string) *ConfigUsageError

NewConfigUsageError generates error object

func (*ConfigUsageError) Error

func (e *ConfigUsageError) Error() string

Error is standard error interface h

type HJSONConfig

type HJSONConfig struct {
	// contains filtered or unexported fields
}

HJSONConfig is configuration loader HJSON interface

func NewHJSONConfig

func NewHJSONConfig(sl ...interface{}) (fl *HJSONConfig, err error)

NewHJSONConfig creates new object or gives err0r all arguments are same as HJSONConfig.SetDefaultLoadSetting

func (*HJSONConfig) CheckExternalConfig

func (fl *HJSONConfig) CheckExternalConfig() (err error)

CheckExternalConfig checks external configuration file and it's contents - e.g.check file before reload

func (*HJSONConfig) GetBooleanValue

func (fl *HJSONConfig) GetBooleanValue(path ...string) (b bool, err error)

GetBooleanValue returns boolean value or error by path

func (*HJSONConfig) GetIntValue

func (fl *HJSONConfig) GetIntValue(path ...string) (i int, err error)

GetIntValue returns integer value by path

func (*HJSONConfig) GetStringValue

func (fl *HJSONConfig) GetStringValue(path ...string) (s string, err error)

GetStringValue returns string value or error by path

func (*HJSONConfig) GetSubconfig

func (fl *HJSONConfig) GetSubconfig(path ...string) (c IConfig, err error)

GetSubconfig returns config interface or nil + error

func (*HJSONConfig) GetValue

func (fl *HJSONConfig) GetValue(path ...string) (i interface{}, err error)

GetValue get any type value on programmer mind own usage on initialized object: fl.GetValue("a", "b", "c", "d") on this function would be based functions below

func (*HJSONConfig) LoadFileContents

func (fl *HJSONConfig) LoadFileContents(filename string) (cnt []byte, err error)

LoadFileContents load contents of file. separate function to make tests possible

func (*HJSONConfig) ParseStringContents

func (fl *HJSONConfig) ParseStringContents(cnt []byte) (m map[string]interface{}, err error)

ParseStringContents parses HJSON - separated to method cause I want test that

func (*HJSONConfig) ReloadInternalMap

func (fl *HJSONConfig) ReloadInternalMap() (err error)

ReloadInternalMap (re)loads internal map - if from file. If not - says ConfigUsageError

func (*HJSONConfig) SetDefaultLoadSetting

func (fl *HJSONConfig) SetDefaultLoadSetting(sl ...interface{}) (err error)

SetDefaultLoadSetting sets default config file for loader

type HJSONConfigError

type HJSONConfigError struct {
	// contains filtered or unexported fields
}

HJSONConfigError inform when hjson error occurred there

func NewHJSONConfigError

func NewHJSONConfigError(s string) *HJSONConfigError

NewHJSONConfigError generates error object

func (*HJSONConfigError) Error

func (e *HJSONConfigError) Error() string

Error is standard error interface h

type IConfig

type IConfig interface {
	// Set default config load settings. In case of file loader - filename
	// if first argument is map object -build config from this map object
	SetDefaultLoadSetting(sl ...interface{}) (err error)
	// checks external configuration file and it's contents - for example for reload or restart purposes
	CheckExternalConfig() (err error)
	// (re)loads internal map
	ReloadInternalMap() (err error)
	// get variant type value
	GetValue(path ...string) (i interface{}, err error)
	// functions below will try make from this variant value typed value
	// returns integer value by path
	GetIntValue(path ...string) (i int, err error)
	// returns string value or error by path
	GetStringValue(path ...string) (s string, err error)
	// returns boolean value
	GetBooleanValue(path ...string) (b bool, err error)
	// returns config interface or nil + error
	GetSubconfig(path ...string) (c IConfig, err error)
}

IConfig is basic interface for all optional configuration structures

func GetConfigInstance

func GetConfigInstance(settings ...interface{}) (config IConfig, err error)

GetConfigInstance get instans of config depending of wanted config format for now supported only HJSON format tag is intended not to load configuration files twice and store object hash map inside module thats done specially not to create config instance twice + get already created instance How to call: GetConfigInstance(tag, format, list_of_settings...) tag used not to load config twice. If you need reload them just use CheckExternalConfig() and ReloadInternalMap() format: for now HJSON|hjson|JSON|json this parameter added for future features(may be we would add other configuration formats there) if it was loaded from file. settings must contain config type as first argument and other parameters SetDefaultLoadSetting need For example. First call when we want load configuration file is: GetConfigInstance("mainconfig", "HJSON", "/etc/file.hjson") When you need get instance again, you just must call: GetConfigInstance("mainconfig") and you will get them from internal hash this all is intended not to put variable withyou configuration everywhere. if you do not want tagging and every time get new object, use nil tag e.g. GetConfigInstance(nil, "HJSON", "/etc/file.hjson")

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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