From 031fce4361f13d4c60454ec3f75c1db6dcdc1f95 Mon Sep 17 00:00:00 2001 From: larpon <768942+larpon@users.noreply.github.com> Date: Sat, 10 May 2025 10:40:12 +0200 Subject: [PATCH] toml: add compile error when passing `encode/1` types of T != struct (fix #24435) (#24443) --- vlib/toml/toml.v | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index 65a4b91cf0..2233d4b7ee 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -167,13 +167,18 @@ fn decode_struct[T](doc Any, mut typ T) { // encode encodes the type `T` into a TOML string. // If `T` has a custom `.to_toml()` method, it will be used instead of the default. pub fn encode[T](typ T) string { - $for method in T.methods { - $if method.name == 'to_toml' { - return typ.$method() + $if T is $struct { + $for method in T.methods { + $if method.name == 'to_toml' { + return typ.$method() + } } + mp := encode_struct[T](typ) + return mp.to_toml() + } $else { + $compile_error('Currently only type `struct` is supported for `T` to encode as TOML') } - mp := encode_struct[T](typ) - return mp.to_toml() + return '' } fn encode_struct[T](typ T) map[string]Any {