refactor: switch to new ! syntax
							parent
							
								
									79a22e3bc1
								
							
						
					
					
						commit
						8b0cb4ab2d
					
				
							
								
								
									
										8
									
								
								conf.v
								
								
								
								
							
							
						
						
									
										8
									
								
								conf.v
								
								
								
								
							| 
						 | 
				
			
			@ -8,7 +8,7 @@ import toml
 | 
			
		|||
// `${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
 | 
			
		||||
// 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_file_name := '$prefix$field_name.to_upper()$file_suffix'
 | 
			
		||||
	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
 | 
			
		||||
// given from an environment variable, a value defined in the config file or a
 | 
			
		||||
// configured default if present, in that order.
 | 
			
		||||
pub fn load<T>(conf LoadConfig) ?T {
 | 
			
		||||
pub fn load<T>(conf LoadConfig) !T {
 | 
			
		||||
	mut res := T{}
 | 
			
		||||
 | 
			
		||||
    // 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) {
 | 
			
		||||
		// 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
 | 
			
		||||
		doc := toml.parse_file(path)?
 | 
			
		||||
		doc := toml.parse_file(path)!
 | 
			
		||||
 | 
			
		||||
		$for field in T.fields {
 | 
			
		||||
			s := doc.value(field.name)
 | 
			
		||||
| 
						 | 
				
			
			@ -74,7 +74,7 @@ pub fn load<T>(conf LoadConfig) ?T {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	$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
 | 
			
		||||
		// file.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue