1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

use compile-reflection for method call

This commit is contained in:
Turiiya
2023-04-16 22:59:48 +02:00
committed by Turiiya
parent 1c6937960b
commit e497872d06

View File

@@ -14,12 +14,13 @@ pub struct Null {
} }
// decode decodes a TOML `string` into the target type `T`. // decode decodes a TOML `string` into the target type `T`.
// `T` can have a custom `.from_toml()` method to be used for decoding.
pub fn decode[T](toml_txt string) !T { pub fn decode[T](toml_txt string) !T {
doc := parse_text(toml_txt)! doc := parse_text(toml_txt)!
mut typ := T{} mut typ := T{}
$for method in T.methods { $for method in T.methods {
$if method.name == 'from_toml' { $if method.name == 'from_toml' {
typ.from_toml(doc.to_any()) typ.$method(doc.to_any())
return typ return typ
} }
} }
@@ -49,11 +50,11 @@ pub fn decode[T](toml_txt string) !T {
} }
// encode encodes the type `T` into a TOML string. // encode encodes the type `T` into a TOML string.
// Currently encode expects the method `.to_toml()` exists on `T`. // `T` can have a custom `.to_toml()` method to be used for decoding.
pub fn encode[T](typ T) string { pub fn encode[T](typ T) string {
$for method in T.methods { $for method in T.methods {
$if method.name == 'to_toml' { $if method.name == 'to_toml' {
return typ.to_toml() return typ.$method()
} }
} }
mut mp := map[string]Any{} mut mp := map[string]Any{}