Skip to content

Commit

Permalink
support arrays in env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-kent committed May 16, 2016
1 parent 271d66a commit 20b634c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 55,24 @@ For example, the `envPrefix` field is split into `env` and `prefix`,
and the tag value is passed to the environment variable source as
the `prefix` parameter.

### Arrays and environment variables

Array support for environment variables is currently experimental.

To enable it, set `GOFIGURE_ENV_ARRAYS=1`.

When enabled, the environment variable is split on commas, e.g.

```
struct {
EnvArray []string `env:"MY_ENV_VAR"`
}
MY_ENV_VAR=a,b,c
EnvArray = []string{"a", "b", "c"}
```

### Licence

Copyright ©‎ 2014, Ian Kent (http://www.iankent.eu).
Expand Down
14 changes: 14 additions & 0 deletions gofigure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 531,20 @@ func TestArrayField(t *testing.T) {
So(cfg.ArrayStringField, ShouldResemble, []string{"foo", "bar"})
})

Convey("String array should work (env)", t, func() {
os.Clearenv()
os.Setenv("GOFIGURE_ENV_ARRAY", "1")
os.Setenv("ARRAY_STRING_FIELD", "foo,bar,baz")
os.Args = []string{
"gofigure",
}
var cfg MyConfigFull
err := Gofigure(&cfg)
So(err, ShouldBeNil)
So(cfg, ShouldNotBeNil)
So(cfg.ArrayStringField, ShouldResemble, []string{"foo", "bar", "baz"})
})

Convey("Int array should work", t, func() {
os.Args = []string{
"gofigure",
Expand Down
24 changes: 18 additions & 6 deletions sources/environment.go
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
package sources

import (
"os"
"reflect"
"regexp"
"strings"
Expand All @@ -10,9 11,10 @@ import (

// Environment implements environment variable configuration using envconf
type Environment struct {
prefix string
infix string
fields map[string]string
prefix string
infix string
fields map[string]string
supportArrays bool
}

var camelRe1 = regexp.MustCompile("(.)([A-Z][a-z] )")
Expand All @@ -37,6 39,10 @@ func (env *Environment) Init(args map[string]string) error {
env.infix = envInfix
}

if v := os.Getenv("GOFIGURE_ENV_ARRAY"); v == "1" || strings.ToLower(v) == "true" || strings.ToLower(v) == "y" {
env.supportArrays = true
}

return nil
}

Expand All @@ -63,8 69,6 @@ func (env *Environment) Get(key string, overrideDefault *string) (string, error)

// GetArray is called to retrieve an array value
func (env *Environment) GetArray(key string, overrideDefault *[]string) ([]string, error) {
// This doesn't actually support arrays... yet!
// It just proxies to Environment.Get()
var oD *string
if overrideDefault != nil {
if len(*overrideDefault) > 0 {
Expand All @@ -73,8 77,16 @@ func (env *Environment) GetArray(key string, overrideDefault *[]string) ([]strin
}
}
v, e := env.Get(key, oD)
arr := []string{v}

if env.supportArrays {
if strings.Contains(v, ",") {
arr = strings.Split(v, ",")
}
}

if len(v) > 0 {
return []string{v}, e
return arr, e
}
return []string{}, e
}
Expand Down

0 comments on commit 20b634c

Please sign in to comment.