Skip to content

Commit 12dd6e8

Browse files
authored
toml: add comptime check if a supported type (struct) was passed to toml.decode, when the type has no custom .from_toml method defined (#19317)
1 parent 1218d88 commit 12dd6e8

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

vlib/toml/tests/encode_and_decode_test.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,13 @@ times = [
177177
assert toml.encode[Arrs](a) == s
178178
assert toml.decode[Arrs](s)! == a
179179
}
180+
181+
fn test_unsupported_type() {
182+
s := 'name = "Peter"'
183+
err_msg := 'toml.decode: expected struct, found '
184+
toml.decode[string](s) or { assert err.msg() == err_msg + 'string' }
185+
toml.decode[[]string](s) or { assert err.msg() == err_msg + '[]string' }
186+
toml.decode[int](s) or { assert err.msg() == err_msg + 'int' }
187+
toml.decode[[]f32](s) or { assert err.msg() == err_msg + '[]f32' }
188+
// ...
189+
}

vlib/toml/toml.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub fn decode[T](toml_txt string) !T {
2323
return typ
2424
}
2525
}
26+
$if T !is $struct {
27+
return error('toml.decode: expected struct, found ${T.name}')
28+
}
2629
decode_struct[T](doc.to_any(), mut typ)
2730
return typ
2831
}

0 commit comments

Comments
 (0)