Skip to content

Commit

Permalink
revisiting the idea after a fair amount of usage
Browse files Browse the repository at this point in the history
  • Loading branch information
fzerorubigd committed Jun 16, 2019
1 parent e4a7b68 commit 6aa5e5c
Show file tree
Hide file tree
Showing 22 changed files with 181 additions and 1,367 deletions.
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: go
go:
- 1.1
- 1.2
- 1.3
- 1.4
- 1.5
- 1.9
- "1.10"
- 1.11
- 1.12
- tip
before_install:
- go get -v github.com/smartystreets/goconvey
Expand Down
100 changes: 0 additions & 100 deletions default_layer.go

This file was deleted.

78 changes: 0 additions & 78 deletions default_layer_test.go

This file was deleted.

90 changes: 2 additions & 88 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,91 +1,5 @@
/*Package onion is a layer based, pluggable config manager for golang.
Layers
Each config object can has more than one config layer. currently there is 3 layer
type is supported.
Default layer
This layer is special layer to set default for configs. usage is simple :
l := onion.NewDefaultLayer()
l.SetDefault("my.daughter.name", "bita")
This layer must be added before all other layer, and defaults must be added before adding it to onion
File layer
File layer is the basic one.
l := onion.NewFileLayer("/path/to/the/file.ext")
the onion package only support for json extension by itself, and there is toml
and yaml loader available as sub package for this one.
Also writing a new loader is very easy, just implement the FileLoader interface
and call the RegisterLoader function with your loader object
Folder layer
Folder layer is much like file layer but it get a folder and search for the
first file with tha specific name and supported extension
l := onion.NewFolderLayer("/path/to/folder", "filename")
the file name part is WHITOUT extension. library check for supported loader
extension in that folder and return the first one.
ENV layer
The other layer is env layer. this layer accept a whitelist of env variables
and use them as value .
l := onion.NewEnvLayer("PORT", "STATIC_ROOT", "NEXT")
this layer currently dose not support nested variables.
YOUR layer
Just implement the onion.Layer interface!
Getting from config
After adding layers to config, its easy to get the config values.
o := onion.New()
o.AddLayer(l1)
o.AddLayer(l2)
o.GetString("key", "default")
o.GetBool("anotherkey", true)
o.GetInt("worker.count", 10) // Nested value
library also support for mapping data to a structure. define your structure :
type MyStruct struct {
Key1 string
Key2 int
Key3 bool `onion:"boolkey"` // struct tag is supported to change the name
Other struct {
Nested string
}
}
o := onion.New()
// Add layers.....
c := MyStruct{}
o.GetStruct("prefix", &c)
the the c.Key1 is equal to o.GetStringDefault("prefix.key1", c.Key1) , note that the
value before calling this function is used as default value, when the type is
not matched or the value is not exists, the the default is returned
For changing the key name, struct tag is supported. for example in the above
example c.Key3 is equal to o.GetBoolDefault("prefix.boolkey", c.Key3)
Also nested struct (and embeded ones) are supported too.
The goal is to have multiple layer and load value base on this layers. the first layer with the response
is used to provide the response.
*/
package onion
30 changes: 7 additions & 23 deletions env_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,14 @@ import (
"strings"
)

//TODO : support for nested value (maybe?)
type envLoader struct {
whiteList []string

loaded bool
data map[string]interface{}
}

func (el *envLoader) Load() (map[string]interface{}, error) {
if el.loaded {
return el.data, nil
}
for _, env := range el.whiteList {
v := os.Getenv(strings.ToUpper(env))
if v != "" {
el.data[env] = v
// NewEnvLayer create new layer using the whitelist of environment values.
func NewEnvLayer(separator string, whiteList ...string) Layer {
data := make(map[string]interface{})
for i := range whiteList {
if s := os.Getenv(whiteList[i]); s != "" {
data[strings.ToLower(whiteList[i])] = s
}
}
el.loaded = true
return el.data, nil
}

// NewEnvLayer create a environment loader. this loader accept a whitelist of allowed variables
// TODO : find a way to map env variable with different name
func NewEnvLayer(whiteList ...string) Layer {
return &envLoader{whiteList, false, make(map[string]interface{})}
return NewMapLayerSeparator(data, separator)
}
46 changes: 0 additions & 46 deletions env_layer_test.go

This file was deleted.

0 comments on commit 6aa5e5c

Please sign in to comment.