Skip to content

Commit

Permalink
add new way to load variables like flags library
Browse files Browse the repository at this point in the history
add consul layer
  • Loading branch information
fzerorubigd committed May 7, 2017
1 parent 0789309 commit e8f28a5
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 58 deletions.
17 changes: 8 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
language: go
dist: trusty
go:
- 1.1
- 1.2
- 1.3
- 1.4
- 1.5
- 1.7
- 1.8
- tip
before_install:
- go get -v github.com/smartystreets/goconvey
- go get -v github.com/axw/gocov/gocov
- go get -v github.com/mattn/goveralls
- go get -v github.com/hashicorp/consul/testutil
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
- go get -v github.com/hashicorp/consul
- go get -v ./...
script:
- goveralls -v -service travis-ci -repotoken $COVERALLS_TOKEN || go test -v
matrix:
allow_failures:
- go: 1.1
- goveralls -v -service travis-ci -repotoken $COVERALLS_TOKEN || go test -v ./...

5 changes: 3 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@


[![Build Status](https://travis-ci.org/fzerorubigd/onion.svg)](https://travis-ci.org/fzerorubigd/onion)
[![Coverage Status](https://coveralls.io/repos/fzerorubigd/onion/badge.svg?branch=v2&service=github)](https://coveralls.io/github/fzerorubigd/onion?branch=v2)
[![GoDoc](https://godoc.org/gopkg.in/fzerorubigd/onion.v2?status.svg)](https://godoc.org/gopkg.in/fzerorubigd/onion.v2)
[![Coverage Status](https://coveralls.io/repos/fzerorubigd/onion/badge.svg?branch=v3&service=github)](https://coveralls.io/github/fzerorubigd/onion?branch=v3)
[![GoDoc](https://godoc.org/gopkg.in/fzerorubigd/onion.v3?status.svg)](https://godoc.org/gopkg.in/fzerorubigd/onion.v3)
[![Go Report Card](https://goreportcard.com/badge/gopkg.in/fzerorubigd/onion.v3)](https://goreportcard.com/report/gopkg.in/fzerorubigd/onion.v3)

--
import "gopkg.in/fzerorubigd/onion.v2"
Expand Down
60 changes: 60 additions & 0 deletions consulloader/consul_layer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package consulloader

import (
"testing"

"fmt"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testutil"
. "github.com/smartystreets/goconvey/convey"
. "gopkg.in/fzerorubigd/onion.v3"
)

func TestConsulLoader(t *testing.T) {
// Create a test Consul server
srv1, err := testutil.NewTestServer()
if err != nil {
t.Fatal(err)
}
defer srv1.Stop()

//// Create a secondary server, passing in configuration
//// to avoid bootstrapping as we are forming a cluster.
//srv2, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) {
// c.Bootstrap = false
//})
//if err != nil {
// t.Fatal(err)
//}
//defer srv2.Stop()

// Join the servers together
//srv1.JoinLAN(t, srv2.LANAddr)

Convey("Extra env in config", t, func() {
client, err := api.NewClient(
&api.Config{
HttpClient: srv1.HTTPClient,
Address: srv1.HTTPAddr,
},
)
So(err, ShouldBeNil)

o := New()
layer := NewConsulLayer(client, "prefix")
o.AddLazyLayer(layer)
Convey("check data from env", func() {
srv1.SetKV(t, "prefix/data/nested", []byte("TDN"))
fmt.Println(string(srv1.GetKV(t, "prefix/data/nested")))
So(o.GetString("data.nested"), ShouldEqual, "TDN")
})

Convey("check data not in env", func() {
So(o.GetString("not.valid.data"), ShouldEqual, "")
So(o.GetString(""), ShouldEqual, "")
})

})

}
36 changes: 36 additions & 0 deletions consulloader/consule_layer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Package consulloader is a simple loader to handle Reading from consul.
// its a simple read only functionality.
package consulloader

import (
"path/filepath"

"github.com/hashicorp/consul/api"
"gopkg.in/fzerorubigd/onion.v3"
)

type layer struct {
client *api.KV
prefix string
}

func (l layer) Get(path ...string) (interface{}, bool) {
p := filepath.Join(l.prefix, filepath.Join(path...))
kv, _, err := l.client.Get(p, nil)
if err != nil {
// I don't think this is correct. may be panic?
return nil, false
}
if kv == nil {
return nil, false
}
return string(kv.Value), true
}

// NewConsulLayer create a new lazy layer from a consul client
func NewConsulLayer(c *api.Client, prefix string) onion.LazyLayer {
return &layer{
client: c.KV(),
prefix: prefix,
}
}
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ 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.
Also nested struct (and embedded ones) are supported too.
*/
package onion
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path/filepath"

"gopkg.in/fzerorubigd/onion.v2"
"gopkg.in/fzerorubigd/onion.v3"
)

func pwd() string {
Expand Down
2 changes: 1 addition & 1 deletion extraenv/env_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"strings"

"gopkg.in/fzerorubigd/onion.v2"
"gopkg.in/fzerorubigd/onion.v3"
)

type envLoader struct {
Expand Down
2 changes: 1 addition & 1 deletion extraenv/env_layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

. "github.com/smartystreets/goconvey/convey"
. "gopkg.in/fzerorubigd/onion.v2"
. "gopkg.in/fzerorubigd/onion.v3"
)

func TestExtraEnvLoader(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion flagslayer/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"time"

"gopkg.in/fzerorubigd/onion.v2"
"gopkg.in/fzerorubigd/onion.v3"
)

// FlagLayer is for handling the layer
Expand Down
2 changes: 1 addition & 1 deletion flagslayer/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"
"time"

. "gopkg.in/fzerorubigd/onion.v2"
. "github.com/smartystreets/goconvey/convey"
. "gopkg.in/fzerorubigd/onion.v3"
)

func TestFlagsLoader(t *testing.T) {
Expand Down
61 changes: 32 additions & 29 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,37 @@ func join(delim string, parts ...string) string {
return res
}

func setField(o *Onion, v reflect.Value, prefix, name string) {
switch v.Kind() {
case reflect.Bool:
if v.CanSet() {
v.SetBool(o.GetBoolDefault(join(o.GetDelimiter(), prefix, name), v.Bool()))
}
case reflect.Int:
if v.CanSet() {
v.SetInt(o.GetInt64Default(join(o.GetDelimiter(), prefix, name), v.Int()))
}
case reflect.Int64:
if v.CanSet() {
v.SetInt(o.GetInt64Default(join(o.GetDelimiter(), prefix, name), v.Int()))
}
case reflect.String:
if v.CanSet() {
v.SetString(o.GetStringDefault(join(o.GetDelimiter(), prefix, name), v.String()))
}
case reflect.Float64:
if v.CanSet() {
v.SetFloat(o.GetFloat64Default(join(o.GetDelimiter(), prefix, name), v.Float()))
}
case reflect.Float32:
if v.CanSet() {
v.SetFloat(o.GetFloat64Default(join(o.GetDelimiter(), prefix, name), v.Float()))
}
case reflect.Struct:
iterateConfig(o, v.Addr(), join(o.GetDelimiter(), prefix, name))
}
}

func iterateConfig(o *Onion, v reflect.Value, op string) {
prefix := op
typ := v.Type()
Expand All @@ -48,35 +79,7 @@ func iterateConfig(o *Onion, v reflect.Value, op string) {
if name == "" {
name = strings.ToLower(p.Name)
}

switch v.Field(i).Kind() {
case reflect.Bool:
if v.Field(i).CanSet() {
v.Field(i).SetBool(o.GetBoolDefault(join(o.GetDelimiter(), prefix, name), v.Field(i).Bool()))
}
case reflect.Int:
if v.Field(i).CanSet() {
v.Field(i).SetInt(o.GetInt64Default(join(o.GetDelimiter(), prefix, name), v.Field(i).Int()))
}
case reflect.Int64:
if v.Field(i).CanSet() {
v.Field(i).SetInt(o.GetInt64Default(join(o.GetDelimiter(), prefix, name), v.Field(i).Int()))
}
case reflect.String:
if v.Field(i).CanSet() {
v.Field(i).SetString(o.GetStringDefault(join(o.GetDelimiter(), prefix, name), v.Field(i).String()))
}
case reflect.Float64:
if v.Field(i).CanSet() {
v.Field(i).SetFloat(o.GetFloat64Default(join(o.GetDelimiter(), prefix, name), v.Field(i).Float()))
}
case reflect.Float32:
if v.Field(i).CanSet() {
v.Field(i).SetFloat(o.GetFloat64Default(join(o.GetDelimiter(), prefix, name), v.Field(i).Float()))
}
case reflect.Struct:
iterateConfig(o, v.Field(i).Addr(), join(o.GetDelimiter(), prefix, name))
}
setField(o, v.Field(i), prefix, name)
} else { // Anonymous structures
name := p.Tag.Get("onion")
if name == "-" {
Expand Down
Loading

0 comments on commit e8f28a5

Please sign in to comment.