Skip to content

Latest commit

 

History

History
176 lines (124 loc) · 4.43 KB

PITCHME.md

File metadata and controls

176 lines (124 loc) · 4.43 KB

@title[gosettings]

@snap[midpoint slide1]

gosettings

@size[80%](For applications and libraries) @snapend

@snap[south-east author-box] @fa[envelope]([email protected] - R Pratap Chakravarthy)
@fagithub
@snapend


In a nutshell

@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 from JSON.
  • 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


{key,value} pairs

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


Accessing Values

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)


A topology of settings

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


Filtering settings

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}))


Trimming settings

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}))


From JSON

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 !


Addprefix and Mixin

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."),
)

Thank you

If gosettings sounds useful please check out the following links.


@fa[book] Project README.
@fa[code] API doc.
@fa[github] Please contribute.