sysrepo / sysrepo-cpp

C++ bindings for the sysrepo library
BSD 3-Clause "New" or "Revised" License
6 stars 5 forks source link

How to check if a module is installed in sysrepo? #17

Closed ayou0057 closed 1 year ago

ayou0057 commented 1 year ago

std::string moduleName = "examples"; I call auto data = session.getData("/" + moduleName+":*//.").c_str()); and when the module is not installed my application is crashing.

Let say I installed example.yang and I want to check in my c++ code if this module is installed and then based on that I log it.

jktjkt commented 1 year ago

Right now there's no API for querying that information from sysrepo, but you can get that info from the associated libyang::Context -- either use getModule(name, revision), or iterate over libyang::Context::modules().

Your code looks fishy, there's no c_str() method on a return value from sysrepo::Session::getData(). Since Session::getData() returns std::optional<libyang::DataNode>, the code should always check whether the optional is empty (indicating no matching data) or not. Note that a call returning no data does not mean that a module is not installed, and it's quite possible that a module is installed, but there are simply no data, and std::nullopt is returned.

ayou0057 commented 1 year ago

Right now there's no API for querying that information from sysrepo, but you can get that info from the associated libyang::Context -- either use getModule(name, revision), or iterate over libyang::Context::modules().

Your code looks fishy, there's no c_str() method on a return value from sysrepo::Session::getData(). Since Session::getData() returns std::optional<libyang::DataNode>, the code should always check whether the optional is empty (indicating no matching data) or not. Note that a call returning no data does not mean that a module is not installed, and it's quite possible that a module is installed, but there are simply no data, and std::nullopt is returned.

I am using the same as the example provided. https://github.com/sysrepo/sysrepo-cpp/blob/master/examples/module_change_example.cpp#L63

Thank you for all the information.

jktjkt commented 1 year ago

I am using the same as the example provided. https://github.com/sysrepo/sysrepo-cpp/blob/master/examples/module_change_example.cpp#L63

Sorry, I misread the parentheses, that's indeed correct. How exactly does that crash for you?

ayou0057 commented 1 year ago

I am using the same as the example provided. https://github.com/sysrepo/sysrepo-cpp/blob/master/examples/module_change_example.cpp#L63

Sorry, I misread the parentheses, that's indeed correct. How exactly does that crash for you?

No worries I started using libyang::Context::modules() as per your suggestion and it's working fine, please ignore the crash :). you can close the ticket. Thanks again.