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

#2 add allow empty tag support for all types; upd tests #3

Merged
merged 1 commit into from
Apr 10, 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
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func (c *ConfigManager) Unmarshal(cfg interface{}) error {
value = c.getValue(key)
}

if allowEmpty && value == "" && defaultValue == "" {
continue
}

if !allowEmpty && value == "" && (defaultValue == "" || !c.useDefaults) {
return fmt.Errorf("%s cannot be empty", key)
}
Expand Down
49 changes: 44 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,20 +203,59 @@ func Test_StructField(t *testing.T) {

func Test_OmitEmpty(t *testing.T) {
type TestConfig struct {
StringField string `env:"STRING_FIELD,omitempty"`
BoolField bool `env:"BOOL_FIELD"`
IntField int `env:"INT_FIELD"`
BoolField bool `env:"BOOL_FIELD,omitempty"`
StringField string `env:"STRING_FIELD,omitempty"`
IntField int `env:"INT_FIELD,omitempty"`
Int8Field int8 `env:"INT8_FIELD,omitempty"`
Int16Field int16 `env:"INT16_FIELD,omitempty"`
Int32Field int32 `env:"INT32_FIELD,omitempty"`
Int64Field int64 `env:"INT64_FIELD,omitempty"`
UintField uint `env:"UINT_FIELD,omitempty"`
Uint8Field uint8 `env:"UINT8_FIELD,omitempty"`
Uint16Field uint16 `env:"UINT16_FIELD,omitempty"`
Uint32Field uint32 `env:"UINT32_FIELD,omitempty"`
Uint64Field uint64 `env:"UINT64_FIELD,omitempty"`
Float32Field float32 `env:"FLOAT32_FIELD,omitempty"`
Float64Field float64 `env:"FLOAT64_FIELD,omitempty"`
TimeDurationField time.Duration `env:"TIME_FIELD,omitempty"`
}

_ = os.Setenv("BOOL_FIELD", "false")
_ = os.Setenv("BOOL_FIELD", "")
_ = os.Setenv("STRING_FIELD", "")
_ = os.Setenv("INT_FIELD", "0")
_ = os.Setenv("INT_FIELD", "")
_ = os.Setenv("INT8_FIELD", "")
_ = os.Setenv("INT16_FIELD", "")
_ = os.Setenv("INT32_FIELD", "")
_ = os.Setenv("INT64_FIELD", "")
_ = os.Setenv("UINT_FIELD", "")
_ = os.Setenv("UINT8_FIELD", "")
_ = os.Setenv("UINT16_FIELD", "")
_ = os.Setenv("UINT32_FIELD", "")
_ = os.Setenv("UINT64_FIELD", "")
_ = os.Setenv("FLOAT32_FIELD", "")
_ = os.Setenv("FLOAT64_FIELD", "")
_ = os.Setenv("TIME_FIELD", "")

cfg := new(TestConfig)
cfgManager := NewDefault()
err := cfgManager.Unmarshal(cfg)

assert.NoError(t, err)
assert.Equal(t, false, cfg.BoolField)
assert.Equal(t, "", cfg.StringField)
assert.Equal(t, 0, cfg.IntField)
assert.Equal(t, int8(0), cfg.Int8Field)
assert.Equal(t, int16(0), cfg.Int16Field)
assert.Equal(t, int32(0), cfg.Int32Field)
assert.Equal(t, int64(0), cfg.Int64Field)
assert.Equal(t, uint(0), cfg.UintField)
assert.Equal(t, uint8(0), cfg.Uint8Field)
assert.Equal(t, uint16(0), cfg.Uint16Field)
assert.Equal(t, uint32(0), cfg.Uint32Field)
assert.Equal(t, uint64(0), cfg.Uint64Field)
assert.Equal(t, float32(0), cfg.Float32Field)
assert.Equal(t, float64(0), cfg.Float64Field)
assert.Equal(t, time.Duration(0), cfg.TimeDurationField)
}

func Test_DefaultValues(t *testing.T) {
Expand Down
Loading