config

package
v1.4.5-alpha1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2019 License: MPL-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package config is a default implementation of InterfaceConfig declared in "github.com/ottemo/commerce/env" package.

Default config values service is a database based storage of application configuration values. Is using one collection within current DB storage service to do this. Config using [StructConfigItem] structure to represent its values. Except key/value information it contains a lot more information to describe config value. This extra information supposed for using within frontend editors, so it consist of:

"description" - config value information
"editor"      - coma separated editors frontend can use for value edit
"label"       - readable config value name
"options"     - any value(s) related to editor
"type"        - config value type (refer to utils package)
"image"       - some icon for config value (like paypal or visa icon, blank for most cases)

In addition to this information struct contains "type" value which using during DB to GO type conversion. Refer to utils package on possible values application understands. It needed as some db service in most cases type based, so in SQLite engine config value stored as text value and before usage it should be converted to appropriate type.

There is special type env.ConstConfigTypeGroup which represents top group for some config group, so it just a special type application using to filter groups. For an instance path "app.checkout" could be a group then paths like "app.checkout.allow_oversell" and "app.checkout.oversell_limit" are config values related to this group. So, with usage of functions config.GetGroupItems() and config.GetItemsInfo() you can filter group based items.

Each config value can have validator function associated, which can also modify value puring verification.

To be more consistent and clear it is highly recommended to declare config value paths as a package constants.

    Example 1:
    ----------
    	const ConstConfigPathAllowOversell = "checkout.allow_oversell"

	    config := env.GetConfig()
	    if config != nil {
			validator := func(value interface{}) (interface{}, error) {
				return utils.InterfaceToBool(value), nil
			}

	        err := config.RegisterItem(env.StructConfigItem {
	            Path:        ConstConfigPathAllowOversell,
	            Value:       false,
	            Type:        env.ConstConfigTypeBoolean,
	            Editor:      "boolean",
	            Options:     nil,
	            Label:       "Allow oversell",
	            Description: "Allows oversell for out of stock items",
	            Image:       "",
	        }, validator)

	        if err != nil {
	        	return env.ErrorDispatch(err)
	        }
	    }

    Example 2:
    ----------
        if utils.InterfaceToBool( env.ConfigGetValue(checkout.ConstConfigPathAllowOversell) ) {
        	...
        }

Index

Constants

View Source
const (
	ConstCollectionNameConfig = "config"

	ConstErrorModule = "env/config"
	ConstErrorLevel  = env.ConstErrorLevelService
)

Package global constants

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultConfig

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

DefaultConfig is a default implementer of InterfaceConfig

func (*DefaultConfig) Export

func (it *DefaultConfig) Export(iterator func(map[string]interface{}) bool) error

Export exports config values through Impex

func (*DefaultConfig) GetGroupItems

func (it *DefaultConfig) GetGroupItems() []env.StructConfigItem

GetGroupItems returns information about config items with type [ConstConfigTypeGroup]

func (*DefaultConfig) GetItemsInfo

func (it *DefaultConfig) GetItemsInfo(Path string) []env.StructConfigItem

GetItemsInfo returns information about config items with given path

  • use '*' to list sub-items (like "paypal.*" or "paypal*" if group item also needed)

func (*DefaultConfig) GetValue

func (it *DefaultConfig) GetValue(Path string) interface{}

GetValue returns value for config item of nil if not present

func (*DefaultConfig) Import

func (it *DefaultConfig) Import(item map[string]interface{}, testMode bool) (map[string]interface{}, error)

Import imports value in config through Impex

func (*DefaultConfig) ListPathes

func (it *DefaultConfig) ListPathes() []string

ListPathes enumerates registered pathes for config

func (*DefaultConfig) Load

func (it *DefaultConfig) Load() error

Load loads config data from DB on app startup

  • calls env.OnConfigStart() after

func (*DefaultConfig) RegisterItem

func (it *DefaultConfig) RegisterItem(Item env.StructConfigItem, Validator env.FuncConfigValueValidator) error

RegisterItem registers new config value in system

func (*DefaultConfig) Reload

func (it *DefaultConfig) Reload() error

Reload updates all config values from database

func (*DefaultConfig) SetValue

func (it *DefaultConfig) SetValue(Path string, Value interface{}) error

SetValue updates config item with new value, returns error if not possible

func (*DefaultConfig) UnregisterItem

func (it *DefaultConfig) UnregisterItem(Path string) error

UnregisterItem removes config value from system

Jump to

Keyboard shortcuts

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