refactor: switch to new ! syntax

main
Jef Roosens 2022-11-01 20:57:04 +01:00
parent 79a22e3bc1
commit 8b0cb4ab2d
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 4 additions and 4 deletions

8
conf.v
View File

@ -8,7 +8,7 @@ import toml
// `${env.prefix}${field_name.to_upper()}${env.file_suffix}`, returning the // `${env.prefix}${field_name.to_upper()}${env.file_suffix}`, returning the
// contents of the file instead if the latter. If both or neither exist, the // contents of the file instead if the latter. If both or neither exist, the
// function returns an error. // function returns an error.
fn get_env_var(prefix string, field_name string, file_suffix string) ?string { fn get_env_var(prefix string, field_name string, file_suffix string) !string {
env_var_name := '$prefix$field_name.to_upper()' env_var_name := '$prefix$field_name.to_upper()'
env_file_name := '$prefix$field_name.to_upper()$file_suffix' env_file_name := '$prefix$field_name.to_upper()$file_suffix'
env_var := os.getenv(env_var_name) env_var := os.getenv(env_var_name)
@ -49,7 +49,7 @@ pub struct LoadConfig {
// file & environment variables. For each field, it will select either a value // file & environment variables. For each field, it will select either a value
// given from an environment variable, a value defined in the config file or a // given from an environment variable, a value defined in the config file or a
// configured default if present, in that order. // configured default if present, in that order.
pub fn load<T>(conf LoadConfig) ?T { pub fn load<T>(conf LoadConfig) !T {
mut res := T{} mut res := T{}
// Later, this could be read from an env var as well. // Later, this could be read from an env var as well.
@ -58,7 +58,7 @@ pub fn load<T>(conf LoadConfig) ?T {
if os.exists(path) { if os.exists(path) {
// We don't use reflect here because reflect also sets any fields not // We don't use reflect here because reflect also sets any fields not
// in the toml back to their zero value, which we don't want // in the toml back to their zero value, which we don't want
doc := toml.parse_file(path)? doc := toml.parse_file(path)!
$for field in T.fields { $for field in T.fields {
s := doc.value(field.name) s := doc.value(field.name)
@ -74,7 +74,7 @@ pub fn load<T>(conf LoadConfig) ?T {
} }
$for field in T.fields { $for field in T.fields {
env_value := get_env_var(conf.prefix, field.name, conf.file_suffix)? env_value := get_env_var(conf.prefix, field.name, conf.file_suffix)!
// The value of an env var will always take precedence over the toml // The value of an env var will always take precedence over the toml
// file. // file.