chore: ran v fmt for V 0.3.3 changes

main
Jef Roosens 2023-02-08 10:40:37 +01:00
parent adb161ca6d
commit 3a4b4b334e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 61 additions and 61 deletions

View File

@ -13,14 +13,14 @@ struct SingleConfDefaultTrue {
} }
fn test_bool_present_no_default() { fn test_bool_present_no_default() {
mut conf := load<SingleConf>(default_path: 'test/bool.toml')! mut conf := load[SingleConf](default_path: 'test/bool.toml')!
assert conf == SingleConf{ assert conf == SingleConf{
some_bool: true some_bool: true
} }
} }
fn test_bool_present_no_default_env() { fn test_bool_present_no_default_env() {
mut conf_f := load<SingleConfDefaultFalse>( mut conf_f := load[SingleConfDefaultFalse](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'SOME_BOOL': '1' 'SOME_BOOL': '1'
@ -30,7 +30,7 @@ fn test_bool_present_no_default_env() {
some_bool: true some_bool: true
} }
mut conf_t := load<SingleConfDefaultTrue>( mut conf_t := load[SingleConfDefaultTrue](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'SOME_BOOL': '' 'SOME_BOOL': ''
@ -40,7 +40,7 @@ fn test_bool_present_no_default_env() {
some_bool: false some_bool: false
} }
conf_f = load<SingleConfDefaultFalse>( conf_f = load[SingleConfDefaultFalse](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'TEST_SOME_BOOL': 'true' 'TEST_SOME_BOOL': 'true'
@ -53,12 +53,12 @@ fn test_bool_present_no_default_env() {
} }
fn test_bool_absent_no_default() { fn test_bool_absent_no_default() {
conf := load<SingleConf>(default_path: 'test/empty.toml') or { return } conf := load[SingleConf](default_path: 'test/empty.toml') or { return }
assert false assert false
} }
fn test_bool_absent_no_default_env() { fn test_bool_absent_no_default_env() {
mut conf_f := load<SingleConfDefaultFalse>( mut conf_f := load[SingleConfDefaultFalse](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'SOME_BOOL': '1' 'SOME_BOOL': '1'
@ -68,7 +68,7 @@ fn test_bool_absent_no_default_env() {
some_bool: true some_bool: true
} }
mut conf_t := load<SingleConfDefaultTrue>( mut conf_t := load[SingleConfDefaultTrue](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'SOME_BOOL': '' 'SOME_BOOL': ''
@ -78,7 +78,7 @@ fn test_bool_absent_no_default_env() {
some_bool: false some_bool: false
} }
conf_f = load<SingleConfDefaultFalse>( conf_f = load[SingleConfDefaultFalse](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'TEST_SOME_BOOL': '1' 'TEST_SOME_BOOL': '1'
@ -91,14 +91,14 @@ fn test_bool_absent_no_default_env() {
} }
fn test_bool_present_default() { fn test_bool_present_default() {
conf := load<SingleConfDefaultFalse>(default_path: 'test/bool.toml')! conf := load[SingleConfDefaultFalse](default_path: 'test/bool.toml')!
assert conf == SingleConfDefaultFalse{ assert conf == SingleConfDefaultFalse{
some_bool: true some_bool: true
} }
} }
fn test_bool_present_default_env() { fn test_bool_present_default_env() {
mut conf_f := load<SingleConfDefaultFalse>( mut conf_f := load[SingleConfDefaultFalse](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'SOME_BOOL': '1' 'SOME_BOOL': '1'
@ -108,7 +108,7 @@ fn test_bool_present_default_env() {
some_bool: true some_bool: true
} }
mut conf_t := load<SingleConfDefaultTrue>( mut conf_t := load[SingleConfDefaultTrue](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'SOME_BOOL': '' 'SOME_BOOL': ''
@ -118,7 +118,7 @@ fn test_bool_present_default_env() {
some_bool: false some_bool: false
} }
conf_f = load<SingleConfDefaultFalse>( conf_f = load[SingleConfDefaultFalse](
default_path: 'test/bool.toml' default_path: 'test/bool.toml'
env: { env: {
'TEST_SOME_BOOL': '1' 'TEST_SOME_BOOL': '1'
@ -131,14 +131,14 @@ fn test_bool_present_default_env() {
} }
fn test_bool_absent_default() { fn test_bool_absent_default() {
conf := load<SingleConfDefaultTrue>(default_path: 'test/empty.toml')! conf := load[SingleConfDefaultTrue](default_path: 'test/empty.toml')!
assert conf == SingleConfDefaultTrue{ assert conf == SingleConfDefaultTrue{
some_bool: true some_bool: true
} }
} }
fn test_bool_absent_default_env() { fn test_bool_absent_default_env() {
mut conf_f := load<SingleConfDefaultFalse>( mut conf_f := load[SingleConfDefaultFalse](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'SOME_BOOL': '1' 'SOME_BOOL': '1'
@ -148,7 +148,7 @@ fn test_bool_absent_default_env() {
some_bool: true some_bool: true
} }
mut conf_t := load<SingleConfDefaultTrue>( mut conf_t := load[SingleConfDefaultTrue](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'SOME_BOOL': '' 'SOME_BOOL': ''
@ -158,7 +158,7 @@ fn test_bool_absent_default_env() {
some_bool: false some_bool: false
} }
conf_f = load<SingleConfDefaultFalse>( conf_f = load[SingleConfDefaultFalse](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'TEST_SOME_BOOL': 'true' 'TEST_SOME_BOOL': 'true'

16
conf.v
View File

@ -20,8 +20,8 @@ pub struct LoadConfig {
// function returns an error. It returns two values, with the first indicating // function returns an error. It returns two values, with the first indicating
// whether the env vars were actually present. // whether the env vars were actually present.
fn (ld LoadConfig) get_env_var(field_name string) !(bool, string) { fn (ld LoadConfig) get_env_var(field_name string) !(bool, string) {
env_var_name := '$ld.prefix$field_name.to_upper()' env_var_name := '${ld.prefix}${field_name.to_upper()}'
env_file_name := '$ld.prefix$field_name.to_upper()$ld.file_suffix' env_file_name := '${ld.prefix}${field_name.to_upper()}${ld.file_suffix}'
if env_var_name !in ld.env && env_file_name !in ld.env { if env_var_name !in ld.env && env_file_name !in ld.env {
return false, '' return false, ''
@ -29,7 +29,7 @@ fn (ld LoadConfig) get_env_var(field_name string) !(bool, string) {
// If they're both set, we report a conflict // If they're both set, we report a conflict
if env_var_name in ld.env && env_file_name in ld.env { if env_var_name in ld.env && env_file_name in ld.env {
return error('Only one of $env_var_name or $env_file_name can be defined.') return error('Only one of ${env_var_name} or ${env_file_name} can be defined.')
} }
// If it's the env var itself, we return it. // If it's the env var itself, we return it.
@ -42,7 +42,7 @@ fn (ld LoadConfig) get_env_var(field_name string) !(bool, string) {
// Otherwise, we process the file // Otherwise, we process the file
return true, os.read_file(ld.env[env_file_name]) or { return true, os.read_file(ld.env[env_file_name]) or {
error('Failed to read file defined in $env_file_name: ${err.msg()}.') error('Failed to read file defined in ${env_file_name}: ${err.msg()}.')
} }
} }
@ -50,7 +50,7 @@ fn (ld LoadConfig) get_env_var(field_name string) !(bool, string) {
// 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>(ld LoadConfig) !T { pub fn load[T](ld LoadConfig) !T {
// Ensure all struct fields consist of supported types // Ensure all struct fields consist of supported types
$for field in T.fields { $for field in T.fields {
$if field.typ is string || field.typ is int || field.typ is bool { $if field.typ is string || field.typ is int || field.typ is bool {
@ -59,14 +59,14 @@ pub fn load<T>(ld LoadConfig) !T {
// this seems to be bugged. If I replace this call with a // this seems to be bugged. If I replace this call with a
// $compile_error call, the error *always* happens, even if all // $compile_error call, the error *always* happens, even if all
// fields are correct. // fields are correct.
return error('Field $field.name is of an unsupported type.') return error('Field ${field.name} is of an unsupported type.')
} }
} }
mut res := T{} mut res := T{}
// This array allows us to determine later whether the variable is actually // This array allows us to determine later whether the variable is actually
// zero or just a null'ed struct field // zero or just a null'ed struct field
mut has_value := Set<string>{} mut has_value := Set[string]{}
// Later, this could be read from an env var as well. // Later, this could be read from an env var as well.
path := ld.default_path path := ld.default_path
@ -138,7 +138,7 @@ pub fn load<T>(ld LoadConfig) !T {
// If there's no value provided in any way, we notify the user with an // If there's no value provided in any way, we notify the user with an
// error. // error.
if !has_value.exists(field.name) { if !has_value.exists(field.name) {
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.")
} }
} }
return res return res

View File

@ -5,6 +5,6 @@ struct WrongTypeConfig {
} }
fn test_wrong_type() { fn test_wrong_type() {
conf := load<WrongTypeConfig>() or { return } conf := load[WrongTypeConfig]() or { return }
assert false assert false
} }

View File

@ -9,19 +9,19 @@ struct SingleConfDefault {
} }
fn test_int_present_no_default() { fn test_int_present_no_default() {
mut conf := load<SingleConf>(default_path: 'test/int.toml')! mut conf := load[SingleConf](default_path: 'test/int.toml')!
assert conf == SingleConf{ assert conf == SingleConf{
some_int: 1 some_int: 1
} }
conf = load<SingleConf>(default_path: 'test/int_zero.toml')! conf = load[SingleConf](default_path: 'test/int_zero.toml')!
assert conf == SingleConf{ assert conf == SingleConf{
some_int: 0 some_int: 0
} }
} }
fn test_int_present_no_default_env() { fn test_int_present_no_default_env() {
mut conf := load<SingleConf>( mut conf := load[SingleConf](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'SOME_INT': '3' 'SOME_INT': '3'
@ -31,7 +31,7 @@ fn test_int_present_no_default_env() {
some_int: 3 some_int: 3
} }
conf = load<SingleConf>( conf = load[SingleConf](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'SOME_INT': '' 'SOME_INT': ''
@ -41,7 +41,7 @@ fn test_int_present_no_default_env() {
some_int: 0 some_int: 0
} }
conf = load<SingleConf>( conf = load[SingleConf](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'TEST_SOME_INT': '3' 'TEST_SOME_INT': '3'
@ -54,12 +54,12 @@ fn test_int_present_no_default_env() {
} }
fn test_int_absent_no_default() { fn test_int_absent_no_default() {
conf := load<SingleConf>(default_path: 'test/empty.toml') or { return } conf := load[SingleConf](default_path: 'test/empty.toml') or { return }
assert false assert false
} }
fn test_int_absent_no_default_env() { fn test_int_absent_no_default_env() {
mut conf := load<SingleConf>( mut conf := load[SingleConf](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'SOME_INT': '3' 'SOME_INT': '3'
@ -69,7 +69,7 @@ fn test_int_absent_no_default_env() {
some_int: 3 some_int: 3
} }
conf = load<SingleConf>( conf = load[SingleConf](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'SOME_INT': '' 'SOME_INT': ''
@ -79,7 +79,7 @@ fn test_int_absent_no_default_env() {
some_int: 0 some_int: 0
} }
conf = load<SingleConf>( conf = load[SingleConf](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'TEST_SOME_INT': '3' 'TEST_SOME_INT': '3'
@ -92,14 +92,14 @@ fn test_int_absent_no_default_env() {
} }
fn test_int_present_default() { fn test_int_present_default() {
conf := load<SingleConfDefault>(default_path: 'test/int.toml')! conf := load[SingleConfDefault](default_path: 'test/int.toml')!
assert conf == SingleConfDefault{ assert conf == SingleConfDefault{
some_int: 1 some_int: 1
} }
} }
fn test_int_present_default_env() { fn test_int_present_default_env() {
mut conf := load<SingleConfDefault>( mut conf := load[SingleConfDefault](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'SOME_INT': '3' 'SOME_INT': '3'
@ -109,7 +109,7 @@ fn test_int_present_default_env() {
some_int: 3 some_int: 3
} }
conf = load<SingleConfDefault>( conf = load[SingleConfDefault](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'SOME_INT': '' 'SOME_INT': ''
@ -119,7 +119,7 @@ fn test_int_present_default_env() {
some_int: 0 some_int: 0
} }
conf = load<SingleConfDefault>( conf = load[SingleConfDefault](
default_path: 'test/int.toml' default_path: 'test/int.toml'
env: { env: {
'TEST_SOME_INT': '3' 'TEST_SOME_INT': '3'
@ -132,14 +132,14 @@ fn test_int_present_default_env() {
} }
fn test_int_absent_default() { fn test_int_absent_default() {
conf := load<SingleConfDefault>(default_path: 'test/empty.toml')! conf := load[SingleConfDefault](default_path: 'test/empty.toml')!
assert conf == SingleConfDefault{ assert conf == SingleConfDefault{
some_int: 2 some_int: 2
} }
} }
fn test_int_absent_default_env() { fn test_int_absent_default_env() {
mut conf := load<SingleConfDefault>( mut conf := load[SingleConfDefault](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'SOME_INT': '3' 'SOME_INT': '3'
@ -149,7 +149,7 @@ fn test_int_absent_default_env() {
some_int: 3 some_int: 3
} }
conf = load<SingleConfDefault>( conf = load[SingleConfDefault](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'SOME_INT': '' 'SOME_INT': ''
@ -159,7 +159,7 @@ fn test_int_absent_default_env() {
some_int: 0 some_int: 0
} }
conf = load<SingleConfDefault>( conf = load[SingleConfDefault](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'TEST_SOME_INT': '3' 'TEST_SOME_INT': '3'
@ -176,7 +176,7 @@ struct SingleConfDefaultEmpty {
} }
fn test_int_absent_default_empty() { fn test_int_absent_default_empty() {
conf := load<SingleConfDefaultEmpty>(default_path: 'test/empty.toml')! conf := load[SingleConfDefaultEmpty](default_path: 'test/empty.toml')!
assert conf == SingleConfDefaultEmpty{ assert conf == SingleConfDefaultEmpty{
some_int: 0 some_int: 0
} }

View File

@ -9,19 +9,19 @@ struct SingleConfDefault {
} }
fn test_string_present_no_default() { fn test_string_present_no_default() {
mut conf := load<SingleConf>(default_path: 'test/string.toml')! mut conf := load[SingleConf](default_path: 'test/string.toml')!
assert conf == SingleConf{ assert conf == SingleConf{
some_string: 'hi' some_string: 'hi'
} }
conf = load<SingleConf>(default_path: 'test/string_empty.toml')! conf = load[SingleConf](default_path: 'test/string_empty.toml')!
assert conf == SingleConf{ assert conf == SingleConf{
some_string: '' some_string: ''
} }
} }
fn test_string_present_no_default_env() { fn test_string_present_no_default_env() {
mut conf := load<SingleConf>( mut conf := load[SingleConf](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'SOME_STRING': 'env' 'SOME_STRING': 'env'
@ -31,7 +31,7 @@ fn test_string_present_no_default_env() {
some_string: 'env' some_string: 'env'
} }
conf = load<SingleConf>( conf = load[SingleConf](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'SOME_STRING': '' 'SOME_STRING': ''
@ -41,7 +41,7 @@ fn test_string_present_no_default_env() {
some_string: '' some_string: ''
} }
conf = load<SingleConf>( conf = load[SingleConf](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'TEST_SOME_STRING': 'env' 'TEST_SOME_STRING': 'env'
@ -54,12 +54,12 @@ fn test_string_present_no_default_env() {
} }
fn test_string_absent_no_default() { fn test_string_absent_no_default() {
conf := load<SingleConf>(default_path: 'test/empty.toml') or { return } conf := load[SingleConf](default_path: 'test/empty.toml') or { return }
assert false assert false
} }
fn test_string_absent_no_default_env() { fn test_string_absent_no_default_env() {
mut conf := load<SingleConf>( mut conf := load[SingleConf](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'SOME_STRING': 'env' 'SOME_STRING': 'env'
@ -69,7 +69,7 @@ fn test_string_absent_no_default_env() {
some_string: 'env' some_string: 'env'
} }
conf = load<SingleConf>( conf = load[SingleConf](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'SOME_STRING': '' 'SOME_STRING': ''
@ -79,7 +79,7 @@ fn test_string_absent_no_default_env() {
some_string: '' some_string: ''
} }
conf = load<SingleConf>( conf = load[SingleConf](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'TEST_SOME_STRING': 'env' 'TEST_SOME_STRING': 'env'
@ -92,14 +92,14 @@ fn test_string_absent_no_default_env() {
} }
fn test_string_present_default() { fn test_string_present_default() {
conf := load<SingleConfDefault>(default_path: 'test/string.toml')! conf := load[SingleConfDefault](default_path: 'test/string.toml')!
assert conf == SingleConfDefault{ assert conf == SingleConfDefault{
some_string: 'hi' some_string: 'hi'
} }
} }
fn test_string_present_default_env() { fn test_string_present_default_env() {
mut conf := load<SingleConfDefault>( mut conf := load[SingleConfDefault](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'SOME_STRING': 'env' 'SOME_STRING': 'env'
@ -109,7 +109,7 @@ fn test_string_present_default_env() {
some_string: 'env' some_string: 'env'
} }
conf = load<SingleConfDefault>( conf = load[SingleConfDefault](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'SOME_STRING': '' 'SOME_STRING': ''
@ -119,7 +119,7 @@ fn test_string_present_default_env() {
some_string: '' some_string: ''
} }
conf = load<SingleConfDefault>( conf = load[SingleConfDefault](
default_path: 'test/string.toml' default_path: 'test/string.toml'
env: { env: {
'TEST_SOME_STRING': 'env' 'TEST_SOME_STRING': 'env'
@ -132,14 +132,14 @@ fn test_string_present_default_env() {
} }
fn test_string_absent_default() { fn test_string_absent_default() {
conf := load<SingleConfDefault>(default_path: 'test/empty.toml')! conf := load[SingleConfDefault](default_path: 'test/empty.toml')!
assert conf == SingleConfDefault{ assert conf == SingleConfDefault{
some_string: 'default' some_string: 'default'
} }
} }
fn test_string_absent_default_env() { fn test_string_absent_default_env() {
mut conf := load<SingleConfDefault>( mut conf := load[SingleConfDefault](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'SOME_STRING': 'env' 'SOME_STRING': 'env'
@ -149,7 +149,7 @@ fn test_string_absent_default_env() {
some_string: 'env' some_string: 'env'
} }
conf = load<SingleConfDefault>( conf = load[SingleConfDefault](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'SOME_STRING': '' 'SOME_STRING': ''
@ -159,7 +159,7 @@ fn test_string_absent_default_env() {
some_string: '' some_string: ''
} }
conf = load<SingleConfDefault>( conf = load[SingleConfDefault](
default_path: 'test/empty.toml' default_path: 'test/empty.toml'
env: { env: {
'TEST_SOME_STRING': 'env' 'TEST_SOME_STRING': 'env'
@ -176,7 +176,7 @@ struct SingleConfDefaultEmpty {
} }
fn test_string_absent_default_empty() { fn test_string_absent_default_empty() {
conf := load<SingleConfDefaultEmpty>(default_path: 'test/empty.toml')! conf := load[SingleConfDefaultEmpty](default_path: 'test/empty.toml')!
assert conf == SingleConfDefaultEmpty{ assert conf == SingleConfDefaultEmpty{
some_string: '' some_string: ''
} }