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

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Sep 13, 2023

Quickfix for #19300

馃 Generated by Copilot at 83c267e

This pull request enhances the toml module to support custom encoding of complex structs using the to_toml method. It also fixes the encoding of nested structs in arrays, and adds a test case for the new functionality.

馃 Generated by Copilot at 83c267e

  • Add support for custom encoding of complex structs using the to_toml method (link, link)
  • Implement the to_toml method for the Example struct in vlib/toml/tests/encode_and_decode_test.v and assert that it matches the output of toml.encode (link)
  • Modify the encode_struct function in vlib/toml/toml.v to check for nested structs in arrays and use the to_toml method if available, or the encode_struct function otherwise (link)
  • Handle the special cases of Date, Time, and DateTime structs, which are already encoded by the toml module (link)

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work.

@spytheman
Copy link
Member

Hm, the output of this does not seem right:

import toml

struct Problem {
        x int
}

struct Example {
        arr []Problem
}

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

fn test_encoding_of_struct_with_a_field_that_has_itself_custom_to_toml_method() {
        example := Example{ arr: [Problem{}, Problem{}] }
        dump(toml.encode(example))
}

It produces:

[toml_with_custom_method_test.v:19] toml.encode(example): arr = [
  { x = 0 },
  { x = 0 }
]

i.e this pub fn (problem Problem) to_toml() string { is not called, even though it is defined.

@ttytm
Copy link
Member Author

ttytm commented Sep 13, 2023

Thanks for the review. Indeed. It should be fixed.

@spytheman spytheman merged commit 4d8b2e9 into vlang:master Sep 13, 2023
35 checks passed
Wertzui123 pushed a commit to Wertzui123/v that referenced this pull request Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants