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

Nested structure with std::optional types #5

Open
requizm opened this issue Apr 25, 2024 · 6 comments
Open

Nested structure with std::optional types #5

requizm opened this issue Apr 25, 2024 · 6 comments

Comments

@requizm
Copy link

requizm commented Apr 25, 2024

It seems like gives an error when deserializing nested structs that have optional fields. Example:

struct Child
{
  std::optional<int32_t> age; // Error because couldn't find "age"
};

struct Parent
{
  Child child;
};
@fantasy-peak
Copy link
Owner

could you provide a complete example

@fantasy-peak
Copy link
Owner

DefaultTest default_test;

@requizm
Copy link
Author

requizm commented Apr 26, 2024

I'm using v1.0.2 via xmake.

main.cpp:

#include <optional>

#include <yaml_cpp_struct.hpp>

struct Settings
{
  int32_t port;
  std::optional<int32_t> thread_count;
};
YCS_ADD_STRUCT(Settings, port, thread_count);

struct Config
{
  Settings settings;
};
YCS_ADD_STRUCT(Config, settings);

int main(int argc, char **argv)
{
  auto [config_opt, error] = yaml_cpp_struct::from_yaml<Config>("./config.yaml");
}

config.yaml

settings:
    port: 5555

Error:

Exception thrown at 0x00007FFA5FEAAB89 in yaml_cpp_struct_optional_test.exe: Microsoft C++ exception: YAML::InvalidNode at memory location 0x00000091A25DEC90.

Debugging status before throwing exception:
image

@fantasy-peak
Copy link
Owner

fantasy-peak commented Apr 26, 2024

This exception seems to be thrown by yaml cpp,I don't have the Windows development environment at hand. I checked with memory tools on Linux and didn't find any problems. I need to study it carefully

@fantasy-peak
Copy link
Owner

---
settings:
  port: 50


[9:49:06] root:bin git:(main*) # xmake build -v -y  
[100%]: build ok, spent 0.021s
warning: You are working in the project directory(/root/github/yaml_cpp_struct/example) and you can also
force to build in current directory via run `xmake -P .`
[9:49:08] root:bin git:(main*) # xmake install -o . 
installing example ..
installing example to . ..
install ok!
[9:49:10] root:bin git:(main*) # valgrind --log-file=a.txt --tool=memcheck --leak-check=full -s  --track-origins=yes ././example ../config.yaml
50
-1
[9:49:13] root:bin git:(main*) # cat a.txt
==105428== Memcheck, a memory error detector
==105428== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==105428== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==105428== Command: ././example ../config.yaml
==105428== Parent PID: 99174
==105428== 
==105428== 
==105428== HEAP SUMMARY:
==105428==     in use at exit: 0 bytes in 0 blocks
==105428==   total heap usage: 359 allocs, 359 frees, 113,936 bytes allocated
==105428== 
==105428== All heap blocks were freed -- no leaks are possible
==105428== 
==105428== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

@fantasy-peak
Copy link
Owner

could you use test branch for test ? https://github.com/fantasy-peak/yaml_cpp_struct/tree/test

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

No branches or pull requests

2 participants