-
Notifications
You must be signed in to change notification settings - Fork 4
/
doc.go
63 lines (51 loc) · 2.85 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2019 Ottemo. All rights reserved.
/*
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) ) {
...
}
*/
package config