|
|
@ -557,6 +557,15 @@ func TestUnmarshalIntWithString(t *testing.T) {
|
|
|
|
var in inner
|
|
|
|
var in inner
|
|
|
|
assert.Error(t, UnmarshalKey(m, &in))
|
|
|
|
assert.Error(t, UnmarshalKey(m, &in))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("invalid options", func(t *testing.T) {
|
|
|
|
|
|
|
|
type Value struct {
|
|
|
|
|
|
|
|
Name string `key:"name,options="`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var v Value
|
|
|
|
|
|
|
|
assert.Error(t, UnmarshalKey(emptyMap, &v))
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshalBoolSliceRequired(t *testing.T) {
|
|
|
|
func TestUnmarshalBoolSliceRequired(t *testing.T) {
|
|
|
@ -675,6 +684,7 @@ func TestUnmarshalFloatSliceWithDefault(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshalStringSliceWithDefault(t *testing.T) {
|
|
|
|
func TestUnmarshalStringSliceWithDefault(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("slice with default", func(t *testing.T) {
|
|
|
|
type inner struct {
|
|
|
|
type inner struct {
|
|
|
|
Strs []string `key:"strs,default=[foo,bar,woo]"`
|
|
|
|
Strs []string `key:"strs,default=[foo,bar,woo]"`
|
|
|
|
Strps []*string `key:"strs,default=[foo,bar,woo]"`
|
|
|
|
Strps []*string `key:"strs,default=[foo,bar,woo]"`
|
|
|
@ -697,6 +707,31 @@ func TestUnmarshalStringSliceWithDefault(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, []string{"foo", "bar", "woo"}, sss)
|
|
|
|
assert.ElementsMatch(t, []string{"foo", "bar", "woo"}, sss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("slice with default on errors", func(t *testing.T) {
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
|
|
|
holder struct {
|
|
|
|
|
|
|
|
Chan []chan int
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inner struct {
|
|
|
|
|
|
|
|
Strs []holder `key:"strs,default=[foo,bar,woo]"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var in inner
|
|
|
|
|
|
|
|
assert.Error(t, UnmarshalKey(nil, &in))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("slice with default on errors", func(t *testing.T) {
|
|
|
|
|
|
|
|
type inner struct {
|
|
|
|
|
|
|
|
Strs []complex64 `key:"strs,default=[foo,bar,woo]"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var in inner
|
|
|
|
|
|
|
|
assert.Error(t, UnmarshalKey(nil, &in))
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshalStringSliceWithDefaultHasSpaces(t *testing.T) {
|
|
|
|
func TestUnmarshalStringSliceWithDefaultHasSpaces(t *testing.T) {
|
|
|
@ -1327,6 +1362,7 @@ func TestUnmarshalStructOptionalDependsNotErrorDetails(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshalStructOptionalDependsNotNested(t *testing.T) {
|
|
|
|
func TestUnmarshalStructOptionalDependsNotNested(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("mutal optionals", func(t *testing.T) {
|
|
|
|
type address struct {
|
|
|
|
type address struct {
|
|
|
|
Optional string `key:",optional"`
|
|
|
|
Optional string `key:",optional"`
|
|
|
|
OptionalDepends string `key:",optional=!Optional"`
|
|
|
|
OptionalDepends string `key:",optional=!Optional"`
|
|
|
@ -1346,6 +1382,51 @@ func TestUnmarshalStructOptionalDependsNotNested(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
var in inner
|
|
|
|
var in inner
|
|
|
|
assert.Error(t, UnmarshalKey(m, &in))
|
|
|
|
assert.Error(t, UnmarshalKey(m, &in))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("bad format", func(t *testing.T) {
|
|
|
|
|
|
|
|
type address struct {
|
|
|
|
|
|
|
|
Optional string `key:",optional"`
|
|
|
|
|
|
|
|
OptionalDepends string `key:",optional=!Optional=abcd"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type combo struct {
|
|
|
|
|
|
|
|
Name string `key:"name,optional"`
|
|
|
|
|
|
|
|
Address address `key:"address"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type inner struct {
|
|
|
|
|
|
|
|
Name string `key:"name"`
|
|
|
|
|
|
|
|
Combo combo `key:"combo"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m := map[string]any{
|
|
|
|
|
|
|
|
"name": "kevin",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var in inner
|
|
|
|
|
|
|
|
assert.Error(t, UnmarshalKey(m, &in))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("invalid option", func(t *testing.T) {
|
|
|
|
|
|
|
|
type address struct {
|
|
|
|
|
|
|
|
Optional string `key:",optional"`
|
|
|
|
|
|
|
|
OptionalDepends string `key:",opt=abcd"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type combo struct {
|
|
|
|
|
|
|
|
Name string `key:"name,optional"`
|
|
|
|
|
|
|
|
Address address `key:"address"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type inner struct {
|
|
|
|
|
|
|
|
Name string `key:"name"`
|
|
|
|
|
|
|
|
Combo combo `key:"combo"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m := map[string]any{
|
|
|
|
|
|
|
|
"name": "kevin",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var in inner
|
|
|
|
|
|
|
|
assert.Error(t, UnmarshalKey(m, &in))
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshalStructOptionalNestedDifferentKey(t *testing.T) {
|
|
|
|
func TestUnmarshalStructOptionalNestedDifferentKey(t *testing.T) {
|
|
|
@ -3855,6 +3936,7 @@ func TestUnmarshalValuer(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshal_EnvString(t *testing.T) {
|
|
|
|
func TestUnmarshal_EnvString(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("valid env", func(t *testing.T) {
|
|
|
|
type Value struct {
|
|
|
|
type Value struct {
|
|
|
|
Name string `key:"name,env=TEST_NAME_STRING"`
|
|
|
|
Name string `key:"name,env=TEST_NAME_STRING"`
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -3869,6 +3951,22 @@ func TestUnmarshal_EnvString(t *testing.T) {
|
|
|
|
if assert.NoError(t, UnmarshalKey(emptyMap, &v)) {
|
|
|
|
if assert.NoError(t, UnmarshalKey(emptyMap, &v)) {
|
|
|
|
assert.Equal(t, envVal, v.Name)
|
|
|
|
assert.Equal(t, envVal, v.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("invalid env", func(t *testing.T) {
|
|
|
|
|
|
|
|
type Value struct {
|
|
|
|
|
|
|
|
Name string `key:"name,env=TEST_NAME_STRING=invalid"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
|
|
envName = "TEST_NAME_STRING"
|
|
|
|
|
|
|
|
envVal = "this is a name"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Setenv(envName, envVal)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var v Value
|
|
|
|
|
|
|
|
assert.Error(t, UnmarshalKey(emptyMap, &v))
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshal_EnvStringOverwrite(t *testing.T) {
|
|
|
|
func TestUnmarshal_EnvStringOverwrite(t *testing.T) {
|
|
|
@ -4044,6 +4142,7 @@ func TestUnmarshal_EnvDurationBadValue(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshal_EnvWithOptions(t *testing.T) {
|
|
|
|
func TestUnmarshal_EnvWithOptions(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("valid options", func(t *testing.T) {
|
|
|
|
type Value struct {
|
|
|
|
type Value struct {
|
|
|
|
Name string `key:"name,env=TEST_NAME_ENV_OPTIONS_MATCH,options=[abc,123,xyz]"`
|
|
|
|
Name string `key:"name,env=TEST_NAME_ENV_OPTIONS_MATCH,options=[abc,123,xyz]"`
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -4058,6 +4157,7 @@ func TestUnmarshal_EnvWithOptions(t *testing.T) {
|
|
|
|
if assert.NoError(t, UnmarshalKey(emptyMap, &v)) {
|
|
|
|
if assert.NoError(t, UnmarshalKey(emptyMap, &v)) {
|
|
|
|
assert.Equal(t, envVal, v.Name)
|
|
|
|
assert.Equal(t, envVal, v.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshal_EnvWithOptionsWrongValueBool(t *testing.T) {
|
|
|
|
func TestUnmarshal_EnvWithOptionsWrongValueBool(t *testing.T) {
|
|
|
|