@title[gosettings]
@snap[midpoint slide1]
@size[80%](For applications and libraries) @snapend@snap[south-east author-box]
@fa[envelope](prataprc@gmail.com - R Pratap Chakravarthy)
@fagithub
@snapend
@ul
- Easy to learn and easy to use settings for your libraries and applications.
- Settings object is represented as @colorblue object.
- Settings can be marshalled to
JSON
or un-marshalled fromJSON
. - Possible to add more formats for marshalling and un-marshalling settings.
- All methods exported on settings object are immutable, except @colorblue.
- Stable APIs, existing APIs are not going to change.
@ulend
example := Settings{
"level": "info",
"flags": "",
"file": "",
"timeformat": "2006-01-02T15:04:05",
"prefix": "[%v]",
}
@ul
- Keys are strings, identify the settings name (aka: configuration parameter).
- Values can be @color[blue](nil, bool, number, string, array, property-map).
- Nested settings are not allowed, as in, if value is a property-map it shall not be interpreted as {key,value} pairs of settings.
@ulend
setts.Bool("mvcc.enable")
setts.Int64("minkeysize")
setts.Uint64("maxvalsize")
setts.String("log.file")
setts.Strings("services")
setts := {"epoch": "1125899906842624"}
fmt.Println("%d", setts.Uint64("epoch"))
@[1](return @coloryellow as boolean value) @[2](return @coloryellow as int64 value) @[3](return @coloryellow as uint64 value) @[4](return @coloryellow as string value) @[5](parse @coloryellow for comma seprated text, and return array of string)
@[7](To encode large numbers that can fit within int64 and uint64, settings value can be encoded as decimal strings) @[8](stdout: @coloryellow)
setts := make(Settings)
setts["storage.llrb.maxkeylen"] = 1024
setts["storage.llrb.maxvallen"] = 1024
@ul[mt20]
- While building applications that use several components, and each component allows itself to be configured via predefined set parameters, it becomes imperative that these components and sub-components need to be organized at application level. This can be done by organizing the settings keys in namespace format.
- Component and sub-components are separated by a
.
dot.
@ulend
Say we have a component @colorblue and we need to filter out parameters relevant for storage.llrb, use the Section() API.
setts := make(Settings)
setts["storage.numvbuckets"] = 8
setts["storage.memalloc"] = 1000000
setts["storage.llrb.maxkeylen"] = 1024
setts["storage.llrb.maxvallen"] = 1024
llrbsetts := setts.Section("llrb.") // Section
@[7](@coloryellow will be @color[cyan](Settings{"storage.llrb.maxkeylen": 1024, "storage.llrb.maxvallen": 1024}))
While passing llrbsetts to the llrb component, it may not expect the @colorblue prefixed to its settings key-name.
llrbsetts = setts.Section("storage.llrb.").Trim("storage.llrb.")
@[1](Now, @coloryellow will just be @color[cyan](Settings{"maxkeylen": 1024, "maxvallen": 1024}))
Most often, settings are obtained from JSON text. One of the reason for using @colorblue as the underlying data-structure is to keep it friendly for JSON. To initialize Settings from JSON:
var setts Settings
json.Unmarshal(data, &setts)
Can't get simpler than that !
When default settings from different components need to be consolidated into application level settings.
import comp1
import comp2
comp1setts := comp1.Defaultsettings()
comp2setts := comp1.Defaultsettings()
appsetts := make(Settings).Mixin(
comp1setts.AddPrefix("comp1."), comp2setts.AddPrefix("comp2."),
)
If gosettings sounds useful please check out the following links.
@fa[book] Project README.
@fa[code] API doc.
@fa[github] Please contribute.