Skip to content

Commit

Permalink
toml: fix custom to_toml for complex structs (#19338)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Sep 13, 2023
1 parent 0244ae6 commit 4d8b2e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
28 changes: 28 additions & 0 deletions vlib/toml/tests/encode_and_decode_test.v
Expand Up @@ -121,6 +121,34 @@ title = 2'
assert y.title == .worker
}

struct Example1 {
arr []Problem
}

struct Example2 {
arr []Problem
}

struct Problem {
x int
}

pub fn (example Example1) to_toml() string {
return '[This is Valid]'
}

pub fn (problem Problem) to_toml() string {
return 'a problem'
}

fn test_custom_encode_of_complex_struct() {
assert toml.encode(Example1{}) == '[This is Valid]'
assert toml.encode(Example2{[Problem{}, Problem{}]}) == 'arr = [
"a problem",
"a problem"
]'
}

fn test_array_encode_decode() {
a := Arrs{
strs: ['foo', 'bar']
Expand Down
12 changes: 11 additions & 1 deletion vlib/toml/toml.v
Expand Up @@ -101,7 +101,17 @@ fn encode_struct[T](typ T) map[string]Any {
} $else $if field.is_array {
mut arr := []Any{}
for v in value {
arr << Any(v)
$if v is Date {
arr << Any(v)
} $else $if v is Time {
arr << Any(v)
} $else $if v is DateTime {
arr << Any(v)
} $else $if v is $struct {
arr << Any(encode(v))
} $else {
arr << Any(v)
}
}
mp[field.name] = arr
} $else {
Expand Down

0 comments on commit 4d8b2e9

Please sign in to comment.