Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7 add bytes and strings slice parsers; #8

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type AppConfig struct {
Float32Field float32 `env:"FLOAT32_FIELD"`
Float64Field float64 `env:"FLOAT64_FIELD"`
TimeDurationField time.Duration `env:"TIME_DURATION_FIELD"`
ByteSliceField []byte `env:"BYTE_SLICE_FIELD"`
StringSliceField []string `env:"STRING_SLICE_FIELD" default:"string1,string2,string3"`
EmptyField string `env:"EMPTY_FIELD,omitempty"`
WithDefaultField string `env:"WITH_DEFAULT_FIELD" default:"ave"`
}
Expand Down Expand Up @@ -104,6 +106,7 @@ func main() {
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- slices: bytes, strings

### .env file

Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (c *ConfigManager) UseCustomKeyTag(tag string) *ConfigManager {
// Float32Field float32 `env:"FLOAT32_FIELD"`
// Float64Field float64 `env:"FLOAT64_FIELD"`
// TimeDurationField time.Duration `env:"TIME_DURATION_FIELD"`
// ByteSliceField []byte `env:"BYTE_SLICE_FIELD"`
// StringSliceField []string `env:"STRING_SLICE_FIELD"`
// EmptyField string `env:"EMPTY_FIELD,omitempty"`
// WithDefaultField string `env:"WITH_DEFAULT_FIELD" default:"ave"`
// }
Expand Down
30 changes: 25 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func Test_UnmarshalFromEnv(t *testing.T) {

TimeField time.Duration `env:"TIME_FIELD"`

ByteSliceField []byte `env:"BYTE_SLICE_FIELD"`
StringSliceField []string `env:"STRING_SLICE_FIELD"`

EmptyField string `env:"EMPTY_FIELD,omitempty"`
}

Expand All @@ -55,6 +58,8 @@ func Test_UnmarshalFromEnv(t *testing.T) {
_ = os.Setenv("FLOAT64_FIELD", "3.14159265359")

_ = os.Setenv("TIME_FIELD", "5s")
_ = os.Setenv("BYTE_SLICE_FIELD", "test")
_ = os.Setenv("STRING_SLICE_FIELD", "test1,test2,test3")

cfg := new(TestConfig)
cfgManager := NewDefault()
Expand All @@ -76,6 +81,8 @@ func Test_UnmarshalFromEnv(t *testing.T) {
assert.Equal(t, float32(3.14), cfg.Float32Field)
assert.Equal(t, float64(3.14159265359), cfg.Float64Field)
assert.Equal(t, 5*time.Second, cfg.TimeField)
assert.Equal(t, []byte("test"), cfg.ByteSliceField)
assert.Equal(t, []string{"test1", "test2", "test3"}, cfg.StringSliceField)
}

func Test_UnmarshalFromDotEnv(t *testing.T) {
Expand All @@ -94,17 +101,20 @@ UINT32_FIELD=4294967295
UINT64_FIELD=18446744073709551615
FLOAT32_FIELD=3.14
FLOAT64_FIELD=3.14159265359
TIME_FIELD=5s`
TIME_FIELD=5s
BYTE_SLICE_FIELD=test
STRING_SLICE_FIELD=test1,test2,test3`
)

tmpFile, _ := os.CreateTemp(".", "test_env_*.env")
defer func() { _ = tmpFile.Close() }()
envFilePath := tmpFile.Name()
defer func() {
_ = tmpFile.Close()
_ = os.Remove(envFilePath)
}()

_, _ = tmpFile.WriteString(envContent)

envFilePath := tmpFile.Name()
defer func() { _ = os.Remove(envFilePath) }()

type TestConfig struct {
BoolField bool `env:"BOOL_FIELD"`
StringField string `env:"STRING_FIELD"`
Expand All @@ -121,6 +131,8 @@ TIME_FIELD=5s`
Float32Field float32 `env:"FLOAT32_FIELD"`
Float64Field float64 `env:"FLOAT64_FIELD"`
TimeDurationField time.Duration `env:"TIME_FIELD"`
ByteSliceField []byte `env:"BYTE_SLICE_FIELD"`
StringSliceField []string `env:"STRING_SLICE_FIELD"`
}

dotEnvProvider, err := values.NewDotEnvProvider(envFilePath)
Expand Down Expand Up @@ -148,6 +160,8 @@ TIME_FIELD=5s`
assert.Equal(t, float32(3.14), cfg.Float32Field)
assert.Equal(t, float64(3.14159265359), cfg.Float64Field)
assert.Equal(t, 5*time.Second, cfg.TimeDurationField)
assert.Equal(t, []byte("test"), cfg.ByteSliceField)
assert.Equal(t, []string{"test1", "test2", "test3"}, cfg.StringSliceField)
}

func Test_EmptyField(t *testing.T) {
Expand Down Expand Up @@ -218,6 +232,8 @@ func Test_OmitEmpty(t *testing.T) {
Float32Field float32 `env:"FLOAT32_FIELD,omitempty"`
Float64Field float64 `env:"FLOAT64_FIELD,omitempty"`
TimeDurationField time.Duration `env:"TIME_FIELD,omitempty"`
ByteSliceField []byte `env:"BYTE_SLICE_FIELD,omitempty"`
StringSliceField []string `env:"STRING_SLICE_FIELD,omitempty"`
}

_ = os.Setenv("BOOL_FIELD", "")
Expand All @@ -235,6 +251,8 @@ func Test_OmitEmpty(t *testing.T) {
_ = os.Setenv("FLOAT32_FIELD", "")
_ = os.Setenv("FLOAT64_FIELD", "")
_ = os.Setenv("TIME_FIELD", "")
_ = os.Setenv("BYTE_SLICE_FIELD", "")
_ = os.Setenv("STRING_SLICE_FIELD", "")

cfg := new(TestConfig)
cfgManager := NewDefault()
Expand All @@ -256,6 +274,8 @@ func Test_OmitEmpty(t *testing.T) {
assert.Equal(t, float32(0), cfg.Float32Field)
assert.Equal(t, float64(0), cfg.Float64Field)
assert.Equal(t, time.Duration(0), cfg.TimeDurationField)
assert.Equal(t, []byte(nil), cfg.ByteSliceField)
assert.Equal(t, []string(nil), cfg.StringSliceField)
}

func Test_DefaultValues(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/parsers/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parsers
import (
"reflect"
"strconv"
"strings"
"time"
)

Expand All @@ -11,6 +12,12 @@ var (
reflect.TypeOf(time.Duration(83)): func(v string) (interface{}, error) {
return time.ParseDuration(v)
},
reflect.TypeOf([]byte{}): func(v string) (interface{}, error) {
return []byte(v), nil
},
reflect.TypeOf([]string{}): func(v string) (interface{}, error) {
return strings.Split(v, ","), nil
},
}

defaultKindParsers = map[reflect.Kind]func(v string) (interface{}, error){
Expand Down
Loading