Skip to content

Commit

Permalink
Support for slices
Browse files Browse the repository at this point in the history
  • Loading branch information
codingconcepts authored and codingconcepts committed Jul 12, 2017
1 parent 64ba553 commit 58baaab
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ Env currently supports the following data types. If you'd like to have more, pl
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- time.Duration
- time.Duration

## Todo

- [ ] Allow user to provide custom delimiter for slice types
6 changes: 6 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func setField(t reflect.StructField, v reflect.Value, value string) (err error)
return
}

// if the given type is a slice, create a slice and return,
// otherwise, we're dealing with a primitive type
if v.Kind() == reflect.Slice {
return setSlice(t, v, value)
}

if err = setBuiltInField(v, value); err != nil {
return errors.Wrapf(err, "error setting %s", t.Name)
}
Expand Down
24 changes: 24 additions & 0 deletions envExampleSet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package env

import (
"fmt"
"os"
"time"
)

func ExampleSet() {
os.Setenv("HOSTS", "NEHOST1:1234, NEHOST2:1234")
os.Setenv("PORT", "1234")
os.Setenv("PEER_TIMEOUT", "2m500ms")

config := struct {
Hosts []string `env:"HOSTS" required:"true"`
Port int16 `env:"PORT" required:"true"`
PeerConnectTimeout time.Duration `env:"PEER_TIMEOUT" default:"1s500ms"`
}{}

err := Set(&config)

fmt.Println(config, err)
// OUTPUT: {[NEHOST1:1234 NEHOST2:1234] 1234 2m0.5s} <nil>
}
23 changes: 0 additions & 23 deletions env_example_test.go

This file was deleted.

10 changes: 10 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ func TestEnvDuration(t *testing.T) {
Equals(t, "1m30s", config.Prop.String())
}

func TestEnvSlice(t *testing.T) {
os.Setenv("PROPS", "a, b, c")
config := struct {
Items []string `env:"PROPS"`
}{}

ErrorNil(t, Set(&config))
Equals(t, []string{"a", "b", "c"}, config.Items)
}

func TestEnvSetUnexportedProperty(t *testing.T) {
os.Setenv("PROP", "hello")

Expand Down
21 changes: 21 additions & 0 deletions setter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package env
import (
"reflect"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -59,3 +60,23 @@ func setDuration(fieldValue reflect.Value, value string) (err error) {
fieldValue.SetInt(d.Nanoseconds())
return
}

func setSlice(t reflect.StructField, v reflect.Value, value string) (err error) {
switch v.Type() {
case reflect.TypeOf([]string{}):
out := setStringSlice(v, value)
v.Set(out)
return
}
return
}

func setStringSlice(v reflect.Value, value string) (out reflect.Value) {
out = reflect.MakeSlice(reflect.TypeOf([]string{}), 0, 0)

for _, part := range strings.Split(value, ",") {
out = reflect.Append(out, reflect.ValueOf(strings.Trim(part, " ")))
}

return
}

0 comments on commit 58baaab

Please sign in to comment.