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 3 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
16 changes: 16 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,22 @@ title = 2'
assert y.title == .worker
}

struct Example {
array []Problem
}

struct Problem {
}

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

fn test_custom_encode_of_complex_struct() {
example := Example{}
assert toml.encode(example) == '[This is Valid]'
}

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_struct(v))
} $else {
arr << Any(v)
}
}
mp[field.name] = arr
} $else {
Expand Down