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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_node() implementations for STL container types #465

Merged
merged 12 commits into from
Jan 14, 2025

Conversation

fktn-k
Copy link
Owner

@fktn-k fktn-k commented Jan 14, 2025

This PR has added builtin implementations of the to_node() function which are used when constructing a fkyaml::basic_node object with arbitrary types.
Before this PR, minimal builtin implementation was provided so that converting to a basic_node object from its template parameter types (like fkyaml::basic_node::sequence_type), which wasn't much helpful.
Now, objects of many existing STL container types (like std::vector, std::unordered_map or std::tuple) can be used as an argument of the fkyaml::basic_node constructor.
See the updated documentation for supported STL container types.

Furthermore, you can, of course, provide your own custom to_node() implementation for non-standard types just as before and such a type can be used as the element type of STL container types.

If you run the following code, for example:

// define a type and to_node implementation for it
namespace ns {
struct novel {
    std::string title;
    int price;
    int published_at;
};

template <typename BasicNodeType>
void to_node(BasicNodeType& node, const novel& novel) {
   // convert the `novel` to a mapping.
   node = {{"title", novel.title}, {"price", novel.price}, {"published_at", novel.published_at}};
}
} // namespace ns

// use them in the code
// fkYAML provides builtin to_node() implementation for std::list<T>
// to_node() for the nested `ns::novel` as T will be recursively executed in the constructor.
std::list<ns::novel> novels = {
    {"foo", 100, 2023}, {"bar", 120, 2024}, {"baz", 80, 2025}
};
fkyaml::node n = novels;

// print the node value
std::cout << n << std::endl;

then the output will be:

-
  price: 120
  published_at: 2024
  title: bar
-
  price: 80
  published_at: 2025
  title: baz
-
  price: 100
  published_at: 2023
  title: foo

Pull Request Checklist

Read the CONTRIBUTING.md file for detailed information.

  • Changes are described in the pull request or in a referenced issue.
  • The test suite compiles and runs without any error.
  • The code coverage on your branch is 100%.
  • The documentation is updated if you added/changed a feature.

Please don't

  • The C++11 support varies between different compilers and versions. Please note the list of supported compilers. Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with #ifdefs or other means.
  • Please refrain from proposing changes that would break YAML specifications. If you propose a conformant extension of YAML to be supported by the library, please motivate this extension.
  • Please do not open pull requests that address multiple issues.

@fktn-k fktn-k added the new feature new feature possibly with public API changes label Jan 14, 2025
@fktn-k fktn-k added this to the Release v0.4.2 milestone Jan 14, 2025
@fktn-k fktn-k self-assigned this Jan 14, 2025
@fktn-k fktn-k changed the title Add builtin implementations of to_node() for STL container types Add to_node() implementations for STL container types Jan 14, 2025
Copy link

github-actions bot commented Jan 14, 2025

:octocat: Upload Coverage Event Notification

Coverage data has been uploaded for the commit 0967aee22bc4a92d96bddc1e38fd2acae37897f7.
You can download the artifact which contains the same file uploaded to the Coveralls and its HTML version.

Name fkYAML_coverage.pr465.zip
ID 2429814295
URL https://github.com/fktn-k/fkYAML/actions/runs/12773275170/artifacts/2429814295

@fktn-k fktn-k merged commit 236acff into develop Jan 14, 2025
162 checks passed
@fktn-k fktn-k deleted the add_builtin_to_node_impls branch January 14, 2025 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature new feature possibly with public API changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant