toml: fix custom to_toml for complex structs (#19338)

This commit is contained in:
Turiiya 2023-09-13 11:48:14 +02:00 committed by GitHub
parent 0244ae6fe9
commit 4d8b2e9995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -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']

View File

@ -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 {