onlineconf

package module
v1.0.1-0...-e242871 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2020 License: MIT Imports: 12 Imported by: 0

README

onlineconf

Go package onlineconf reads configuration files generated by OnlineConf.

Simple usage

Read parameters from a module:

reloader := MustReloader("TREE") // conofig reloader.
module := reloader.Module()      // consistent and immutable copy of module params
                                 // It is sutable to get module pointer by this method
                                 // for each http request, for example. It does not copy anything.

i, ok := module.Int("/path/to/int")                // i contains value and ok indicates if such key was in config
i, ok := module.IntWithDef("/path/to/int", 100500) // i contains value but if no such key in config i will be set to 100500
module.MustInt("/path/to/int")                     // panics if no such key in config

// see also folowing methods
// module.String*
// module.Map*

Documentation

Overview

Package onlineconf reads configuration files generated by OnlineConf.

It opens indexed CDB files and maps them in the memory. If onlineconf-updater modifies them, they are automatically reopened.

Index

Constants

View Source
const DefaultModulesDir = "/usr/local/etc/onlineconf"

DefaultModulesDir defines default directory for modules

Variables

View Source
var ErrInvalidCDB = errors.New("cdb is inconsistent")

ErrInvalidCDB means that cdb is invalid

Functions

This section is empty.

Types

type Mod

type Mod struct {
	StringParams map[string]string
	IntParams    map[string]int

	RawJSONParams            map[string]string // Here will be all JSON params (not parsed)
	MapStringInterfaceParams map[string]map[string]interface{}
	MapIntIntParams          map[string]map[int]int
	MapIntStringParams       map[string]map[int]string
	MapStringIntParams       map[string]map[string]int
	MapStringStringParams    map[string]map[string]string
}

Mod is a structure that associated with onlineconf module file.

func Module

func Module(moduleName string) (*Mod, error)

Module returns current copy of Module by name in default onlineconf module directory. This function is STRONGLY NOT RECOMENDED FOR USE. It is very unefficient. This module will never be updated. This function parses all the parameters in onlineconf and copies it to memory. Thats why this operations is expencive. You should prefer to use ModuleReloader if its possible.

func ModuleFromContext

func ModuleFromContext(ctx context.Context) (*Mod, bool)

ModuleFromContext retrieves a config module from context.

func MustModule

func MustModule(moduleName string) *Mod

MustModule returns Module by name in default onlineconf module directory. Panics on error. This module will never be updated. This function parses all the parameters in onlineconf and copies it to memory. Thats why this operations is expencive. You should prefer to use ModuleReloader if its possible.

func MustTree

func MustTree() *Mod

MustTree returns Module TREE in default onlineconf module directory Panics on error. This module will never be updated. This function parses all the parameters in onlineconf and copies it to memory. Thats why this operations is expencive. You should prefer to use ModuleReloader if its possible.

func NewModule

func NewModule(reader io.ReaderAt) (*Mod, error)

NewModule parses cdb file and copies all content to local maps. Module returned by this method will never be updated

func Tree

func Tree() (*Mod, error)

Tree returns current copy of Module TREE in default onlineconf module directory. This module will never be updated. This function parses all the parameters in onlineconf and copies it to memory. Thats why this operations is expencive. You should prefer to use ModuleReloader if its possible.

func (*Mod) Bool

func (m *Mod) Bool(path string) (bool, bool)

Bool returns bool interpretation of param. If length of string parameter with same path is greater than 0, returns true. In other case false.

func (*Mod) BoolWithDef

func (m *Mod) BoolWithDef(path string, defaultValue bool) (bool, bool)

BoolWithDef the same as Bool but is no such parameter? it returns default value

func (*Mod) Int

func (m *Mod) Int(path string) (int, bool)

Int returns value of a named parameter from the module. It returns the boolean true if the parameter exists and is an int. In the other case it returns the boolean false and zero.

func (*Mod) IntWithDef

func (m *Mod) IntWithDef(path string, defaultValue int) (int, bool)

IntWithDef returns value of a named parameter from the module. It returns the boolean true if the parameter exists and is an int. In the other case it returns the boolean false and zero.

func (*Mod) MapIntInt

func (m *Mod) MapIntInt(path string) (map[int]int, bool)

MapIntInt

func (*Mod) MapIntIntWithDef

func (m *Mod) MapIntIntWithDef(path string, defaultValue map[int]int) (map[int]int, bool)

MapIntIntWithDef default valur will not be copied!

func (*Mod) MapIntString

func (m *Mod) MapIntString(path string) (map[int]string, bool)

MapIntString

func (*Mod) MapIntStringWithDef

func (m *Mod) MapIntStringWithDef(path string, defaultValue map[int]string) (map[int]string, bool)

MapIntStringWithDef default valur will not be copied!

func (*Mod) MapStringInt

func (m *Mod) MapStringInt(path string) (map[string]int, bool)

MapStringInt

func (*Mod) MapStringIntWithDef

func (m *Mod) MapStringIntWithDef(path string, defaultValue map[string]int) (map[string]int, bool)

MapStringIntWithDef default valur will not be copied!

func (*Mod) MapStringInterface

func (m *Mod) MapStringInterface(path string) (map[string]interface{}, bool)

MapStringInterface Interfaces will not be copied!

func (*Mod) MapStringInterfaceWithDef

func (m *Mod) MapStringInterfaceWithDef(path string, defaultValue map[string]interface{}) (map[string]interface{}, bool)

MapStringInterfaceWithDef default valur will not be copied!

func (*Mod) MapStringString

func (m *Mod) MapStringString(path string) (map[string]string, bool)

MapStringString

func (*Mod) MapStringStringWithDef

func (m *Mod) MapStringStringWithDef(path string, defaultValue map[string]string) (map[string]string, bool)

MapStringStringWithDef default valur will not be copied!

func (*Mod) MustBool

func (m *Mod) MustBool(path string) bool

MustBool returns value of a named parameter from the module. It panics if no such parameter or this parameter is not a string.

func (*Mod) MustInt

func (m *Mod) MustInt(path string) int

MustInt returns value of a named parameter from the module. It panics if no such parameter or this parameter is not an int

func (*Mod) MustMapIntInt

func (m *Mod) MustMapIntInt(path string) map[int]int

func (*Mod) MustMapIntString

func (m *Mod) MustMapIntString(path string) map[int]string

func (*Mod) MustMapStringInt

func (m *Mod) MustMapStringInt(path string) map[string]int

func (*Mod) MustMapStringInterface

func (m *Mod) MustMapStringInterface(path string) map[string]interface{}

func (*Mod) MustMapStringString

func (m *Mod) MustMapStringString(path string) map[string]string

func (*Mod) MustRawJSON

func (m *Mod) MustRawJSON(path string) string

func (*Mod) MustString

func (m *Mod) MustString(path string) string

MustString returns value of a named parameter from the module. It panics if no such parameter or this parameter is not a string.

func (*Mod) RawJSON

func (m *Mod) RawJSON(path string) (string, bool)

RawJSON returns raw json string

func (*Mod) RawJSONWithDef

func (m *Mod) RawJSONWithDef(path string, defaultValue string) (string, bool)

RawJSONWithDef default valur will not be copied!

func (*Mod) String

func (m *Mod) String(path string) (string, bool)

String returns value of a named parameter from the module. It returns the boolean true if the parameter exists and is a string. In the other case it returns the boolean false and an empty string.

func (*Mod) StringWithDef

func (m *Mod) StringWithDef(path string, defaultValue string) (string, bool)

StringWithDef returns value of a named parameter from the module. It returns the boolean true if the parameter exists and is a string. In the other case it returns the boolean false and an empty string.

func (*Mod) WithContext

func (m *Mod) WithContext(ctx context.Context) context.Context

WithContext returns a new Context that carries value module

type ModuleReloader

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

ModuleReloader watchers for module updates and reloads it

func ModuleReloaderFromContext

func ModuleReloaderFromContext(ctx context.Context) (*ModuleReloader, bool)

ModuleReloaderFromContext retrieves a config module from context.

func MustReloader

func MustReloader(moduleName string) *ModuleReloader

MustReloader returns reloader for specified module. Panics on error.

func NewModuleReloader

func NewModuleReloader(ops *ReloaderOptions) (*ModuleReloader, error)

NewModuleReloader creates new module reloader

func Reloader

func Reloader(moduleName string) (*ModuleReloader, error)

Reloader returns reloader for specified module

func (*ModuleReloader) Close

func (mr *ModuleReloader) Close() error

Close closes inofitify watcher. Module will not be updated anymore.

func (*ModuleReloader) Module

func (mr *ModuleReloader) Module() *Mod

Module returns the last successfully updated version of module

func (*ModuleReloader) WithContext

func (mr *ModuleReloader) WithContext(ctx context.Context) context.Context

WithContext returns a new Context that carries value module reloader

type ReloaderOptions

type ReloaderOptions struct {
	Name     string
	Dir      string // default in `DefaultModulesDir`
	FilePath string
}

ReloaderOptions specify loader options You can specify either FilePath or Name + Dir. If you sprcified only Name, DefaultModulesDir Dir will be used

Jump to

Keyboard shortcuts

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