mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
allow for customisation when .to_toml and .from_toml methods are defined
This commit is contained in:
@@ -17,6 +17,12 @@ pub struct Null {
|
|||||||
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 {
|
||||||
|
$if method.name == 'from_toml' {
|
||||||
|
typ.from_toml(doc.to_any())
|
||||||
|
return typ
|
||||||
|
}
|
||||||
|
}
|
||||||
$for field in T.fields {
|
$for field in T.fields {
|
||||||
$if field.is_enum {
|
$if field.is_enum {
|
||||||
typ.$(field.name) = doc.value(field.name).int()
|
typ.$(field.name) = doc.value(field.name).int()
|
||||||
@@ -45,6 +51,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`.
|
// Currently encode expects the method `.to_toml()` exists on `T`.
|
||||||
pub fn encode[T](typ T) string {
|
pub fn encode[T](typ T) string {
|
||||||
|
$for method in T.methods {
|
||||||
|
$if method.name == 'to_toml' {
|
||||||
|
return typ.to_toml()
|
||||||
|
}
|
||||||
|
}
|
||||||
mut mp := map[string]Any{}
|
mut mp := map[string]Any{}
|
||||||
$if T is $struct {
|
$if T is $struct {
|
||||||
$for field in T.fields {
|
$for field in T.fields {
|
||||||
|
|||||||
Reference in New Issue
Block a user