From 3b47254dd81caeb38e70fbc8177c7c2cdbc37171 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+tobealive@users.noreply.github.com> Date: Sun, 16 Apr 2023 08:46:15 +0200 Subject: [PATCH] toml: add direct decoding and encoding --- vlib/toml/toml.v | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index dd783f3264..68d2f0b55f 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -7,6 +7,7 @@ import toml.ast import toml.input import toml.scanner import toml.parser +import time // Null is used in sumtype checks as a "default" value when nothing else is possible. pub struct Null { @@ -16,14 +17,47 @@ pub struct Null { pub fn decode[T](toml_txt string) !T { doc := parse_text(toml_txt)! mut typ := T{} - typ.from_toml(doc.to_any()) + $for field in T.fields { + $if field.is_enum { + // TODO: check enums + doc.value(field.name).int() + } $else $if field.typ is string { + typ.$(field.name) = doc.value(field.name).string() + } $else $if field.typ is bool { + typ.$(field.name) = doc.value(field.name).bool() + } $else $if field.typ is int { + typ.$(field.name) = doc.value(field.name).int() + } $else $if field.typ is i64 { + typ.$(field.name) = doc.value(field.name).i64() + } $else $if field.typ is u64 { + typ.$(field.name) = doc.value(field.name).u64() + } $else $if field.typ is f32 { + typ.$(field.name) = doc.value(field.name).f32() + } $else $if field.typ is f64 { + typ.$(field.name) = doc.value(field.name).f64() + // TODO: extend + } $else $if field.typ is time.Time { + typ.$(field.name) = doc.value(field.name).datetime() + } + } return typ } // encode encodes the type `T` into a TOML string. // Currently encode expects the method `.to_toml()` exists on `T`. pub fn encode[T](typ T) string { - return typ.to_toml() + mut mp := map[string]Any{} + $if T is $struct { + $for field in T.fields { + value := typ.$(field.name) + $if field.is_enum { + mp[field.name] = Any(value.str()) + } $else { + mp[field.name] = Any(value) + } + } + } + return mp.to_toml() } // DateTime is the representation of an RFC 3339 datetime string.