From 6269eacb6f91dfb09aad3a56bf8d4ed72985ad78 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+tobealive@users.noreply.github.com> Date: Mon, 17 Apr 2023 11:38:14 +0200 Subject: [PATCH] re-add condition to call custom `from_toml` method * test custom encoding logic to cause failure if the methods won't be used. --- vlib/toml/tests/encode_and_decode_test.v | 16 ++++++++-------- vlib/toml/toml.v | 9 ++++++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/vlib/toml/tests/encode_and_decode_test.v b/vlib/toml/tests/encode_and_decode_test.v index 4b7c7dd4d0..f43344b2ae 100644 --- a/vlib/toml/tests/encode_and_decode_test.v +++ b/vlib/toml/tests/encode_and_decode_test.v @@ -41,9 +41,9 @@ pub fn (e Employee) to_toml() string { mut mp := map[string]toml.Any{} mp['name'] = toml.Any(e.name) mp['age'] = toml.Any(e.age) - mp['salary'] = toml.Any(e.salary) + mp['salary'] = toml.Any(f32(e.salary) + 5000.0) mp['is_human'] = toml.Any(e.is_human) - mp['rank'] = toml.Any(int(e.rank)) + mp['rank'] = toml.Any(int(e.rank) + 1) return mp.to_toml() } @@ -51,9 +51,9 @@ pub fn (mut e Employee) from_toml(any toml.Any) { mp := any.as_map() e.name = mp['name'] or { toml.Any('') }.string() e.age = mp['age'] or { toml.Any(0) }.int() - e.salary = mp['salary'] or { toml.Any(0) }.f32() + e.salary = mp['salary'] or { toml.Any(0) }.f32() - 15000.0 e.is_human = mp['is_human'] or { toml.Any(false) }.bool() - e.rank = unsafe { Rank(mp['rank'] or { toml.Any(0) }.int()) } + e.rank = unsafe { Rank(mp['rank'] or { toml.Any(0) }.int() - 2) } } fn test_custom_encode_and_decode() { @@ -62,9 +62,9 @@ fn test_custom_encode_and_decode() { eprintln('Employee x: ${s}') assert s == r'name = "Peter" age = 28 -salary = 95000.5 +salary = 100000.5 is_human = true -rank = 1' +rank = 2' y := toml.decode[Employee](s) or { println(err) @@ -74,7 +74,7 @@ rank = 1' eprintln('Employee y: ${y}') assert y.name == 'Peter' assert y.age == 28 - assert y.salary == 95000.5 + assert y.salary == 85000.5 assert y.is_human == true - assert y.rank == .medium + assert y.rank == .low } diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index 85273f9000..e3f70a1746 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -17,7 +17,14 @@ pub struct Null { // `T` can have a custom `.from_toml()` method that will be used in decode. pub fn decode[T](toml_txt string) !T { doc := parse_text(toml_txt)! - typ := decode_struct[T](doc.to_any()) + mut typ := T{} + $for method in T.methods { + $if method.name == 'from_toml' { + typ.$method(doc.to_any()) + return typ + } + } + typ = decode_struct[T](doc.to_any()) return typ }