mirror of
https://github.com/vlang/v.git
synced 2025-09-12 08:57:09 -04:00
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)
This commit is contained in:
parent
1218d88cfd
commit
12dd6e8b39
@ -177,3 +177,13 @@ times = [
|
||||
assert toml.encode[Arrs](a) == s
|
||||
assert toml.decode[Arrs](s)! == a
|
||||
}
|
||||
|
||||
fn test_unsupported_type() {
|
||||
s := 'name = "Peter"'
|
||||
err_msg := 'toml.decode: expected struct, found '
|
||||
toml.decode[string](s) or { assert err.msg() == err_msg + 'string' }
|
||||
toml.decode[[]string](s) or { assert err.msg() == err_msg + '[]string' }
|
||||
toml.decode[int](s) or { assert err.msg() == err_msg + 'int' }
|
||||
toml.decode[[]f32](s) or { assert err.msg() == err_msg + '[]f32' }
|
||||
// ...
|
||||
}
|
||||
|
@ -23,6 +23,9 @@ pub fn decode[T](toml_txt string) !T {
|
||||
return typ
|
||||
}
|
||||
}
|
||||
$if T !is $struct {
|
||||
return error('toml.decode: expected struct, found ${T.name}')
|
||||
}
|
||||
decode_struct[T](doc.to_any(), mut typ)
|
||||
return typ
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user