feat: allow "zeroed" values as defaults

main
Jef Roosens 2022-12-28 18:50:12 +01:00
parent 6678040d30
commit 8bd7ce0b60
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 16 additions and 2 deletions

7
conf.v
View File

@ -102,8 +102,11 @@ pub fn load<T>(ld LoadConfig) !T {
}
// Finally, if there's no env var present either, we check whether the
// variable has a default value
if !has_value.exists(field.name) {
// variable has a default value. Variables defined with an "empty
// default" will always be marked as containing a value.
if 'empty_default' in field.attrs {
has_value.add(field.name)
} else if !has_value.exists(field.name) {
mut has_default := false
$if field.typ is string {

View File

@ -170,3 +170,14 @@ fn test_string_absent_default_env() {
some_string: 'env'
}
}
struct SingleConfDefaultEmpty {
some_string string [empty_default]
}
fn test_string_absent_default_empty() {
conf := load<SingleConfDefaultEmpty>(default_path: 'test/empty.toml')!
assert conf == SingleConfDefaultEmpty{
some_string: ''
}
}