diff --git a/conf.v b/conf.v index 0cb2250..f92361d 100644 --- a/conf.v +++ b/conf.v @@ -51,6 +51,17 @@ fn (ld LoadConfig) get_env_var(field_name string) !(bool, string) { // given from an environment variable, a value defined in the config file or a // configured default if present, in that order. pub fn load(ld LoadConfig) !T { + // Ensure all struct fields consist of supported types + $for field in T.fields { + $if field.typ is string || field.typ is int { + } $else { + // I'd prefer changing this to $compile_error, but as of V 0.3.2, + // this seems to be bugged. If I replace this call with a + // $compile_error call, the error *always* happens, even if all + // fields are correct. + return error('Field $field.name is of an unsupported type.') + } + } mut res := T{} // This array allows us to determine later whether the variable is actually @@ -73,9 +84,6 @@ pub fn load(ld LoadConfig) !T { res.$(field.name) = s.string() } $else $if field.typ is int { res.$(field.name) = s.int() - // This seems to not work in V 0.3.2 - //} $else { - // $compile_error('Unsupported config struct field type detected.') } has_value.add(field.name) @@ -93,9 +101,6 @@ pub fn load(ld LoadConfig) !T { res.$(field.name) = env_value } $else $if field.typ is int { res.$(field.name) = env_value.int() - // This seems to not work in V 0.3.2 - //} $else { - // $compile_error('Unsupported config struct field type detected.') } has_value.add(field.name) diff --git a/conf_test.v b/conf_test.v new file mode 100644 index 0000000..edc985f --- /dev/null +++ b/conf_test.v @@ -0,0 +1,10 @@ +module conf + +struct WrongTypeConfig { + f map[string]string +} + +fn test_wrong_type() { + conf := load() or { return } + assert false +} diff --git a/int_test.v b/int_test.v index e248319..f02ef9e 100644 --- a/int_test.v +++ b/int_test.v @@ -181,3 +181,8 @@ fn test_int_absent_default_empty() { some_int: 0 } } + +/* fn test_int_wrong_type() { */ +/* conf := load(default_path: 'test/int_wrong_type.toml') or { return } */ +/* assert false */ +/* } */ diff --git a/test/int_wrong_type.toml b/test/int_wrong_type.toml new file mode 100644 index 0000000..e63b96b --- /dev/null +++ b/test/int_wrong_type.toml @@ -0,0 +1 @@ +some_int = '1'