Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

toml: fix custom to_toml for complex structs #19338

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions vlib/toml/tests/encode_and_decode_test.v
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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