feat: hopefully allow configuring zero as value
parent
8b0cb4ab2d
commit
d705dc0ec1
22
conf.v
22
conf.v
|
@ -52,6 +52,10 @@ pub struct LoadConfig {
|
||||||
pub fn load<T>(conf LoadConfig) !T {
|
pub fn load<T>(conf LoadConfig) !T {
|
||||||
mut res := T{}
|
mut res := T{}
|
||||||
|
|
||||||
|
// This array allows us to determine later whether the variable is actually
|
||||||
|
// zero or just a null'ed struct field
|
||||||
|
mut has_value := map[string]bool{}
|
||||||
|
|
||||||
// Later, this could be read from an env var as well.
|
// Later, this could be read from an env var as well.
|
||||||
path := conf.default_path
|
path := conf.default_path
|
||||||
|
|
||||||
|
@ -68,7 +72,11 @@ pub fn load<T>(conf LoadConfig) !T {
|
||||||
res.$(field.name) = s.string()
|
res.$(field.name) = s.string()
|
||||||
} $else $if field.typ is int {
|
} $else $if field.typ is int {
|
||||||
res.$(field.name) = s.int()
|
res.$(field.name) = s.int()
|
||||||
|
} $else {
|
||||||
|
$compile_error('Unsupported config struct field type detected.')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
has_value[field.name] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,20 +91,14 @@ pub fn load<T>(conf LoadConfig) !T {
|
||||||
res.$(field.name) = env_value
|
res.$(field.name) = env_value
|
||||||
} $else $if field.typ is int {
|
} $else $if field.typ is int {
|
||||||
res.$(field.name) = env_value.int()
|
res.$(field.name) = env_value.int()
|
||||||
}
|
} $else {
|
||||||
|
$compile_error('Unsupported config struct field type detected.')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now, we check whether a value is present. If there isn't, that means
|
has_value[field.name] = true
|
||||||
// it isn't in the config file, nor is there a default or an env var.
|
|
||||||
mut has_value := false
|
|
||||||
|
|
||||||
$if field.typ is string {
|
|
||||||
has_value = res.$(field.name) != ''
|
|
||||||
} $else $if field.typ is int {
|
|
||||||
has_value = res.$(field.name) != 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !has_value {
|
if !(has_values[field.name] or { false }) {
|
||||||
return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.")
|
return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue