mirror of
https://github.com/vlang/v.git
synced 2025-08-03 17:57:59 -04:00
toml: fix custom to_toml
for complex structs (#19338)
This commit is contained in:
parent
0244ae6fe9
commit
4d8b2e9995
@ -121,6 +121,34 @@ title = 2'
|
|||||||
assert y.title == .worker
|
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() {
|
fn test_array_encode_decode() {
|
||||||
a := Arrs{
|
a := Arrs{
|
||||||
strs: ['foo', 'bar']
|
strs: ['foo', 'bar']
|
||||||
|
@ -101,7 +101,17 @@ fn encode_struct[T](typ T) map[string]Any {
|
|||||||
} $else $if field.is_array {
|
} $else $if field.is_array {
|
||||||
mut arr := []Any{}
|
mut arr := []Any{}
|
||||||
for v in value {
|
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
|
mp[field.name] = arr
|
||||||
} $else {
|
} $else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user