eclipse-uprotocol / eclipse-uprotocol/up-cpp
Consider adding equality operators for various objects in the datamodel
- Dominant language
- C++
- Stars
- 29
- Forks
- 33
- PR merge metrics
- No merged PRs in 30d
Description
The logic in the '==' operators for URI (and its components do not seem to be correct).
https://github.com/eclipse-uprotocol/up-cpp/blob/494b3313b55aaefe5b8b0376dd761b75fa96a78a/include/up-cpp/uri/tools/Utils.h#L188C1-L201C6
This code is only comparing the instance portion of the URI if the left hand side includes the instance. However the logic should really be like this:
```
[[nodiscard]] [[maybe_unused]] auto static
operator==(const uprotocol::v1::UResource &s, const uprotocol::v1::UResource &o) -> bool {
// Resources are consider equal if:
// 1. Names are equal
// 2. Both Resources do not have an instance OR
// both Resource do have an instance and they are the same
// 3. Both Resources do not have a message OR
// both resources have a message and they are the same
return (s.name() == o.name()) && // Condition 1
// Condition 2
((!s.has_instance() && !o.has_instance()) ||
(s.has_instance() && o.has_instance() && s.instance() == o.instance())) && // Condition 2
// Condition 3
((!s.has_message() && !o.has_message()) ||
(s.has_message() && o.has_message() && s.message() == o.message())); // if exists must be same message
}
```
A simple test case (it fails right now) shows the problem with the code as is:
```
TEST(URI_UTILS, testUResourceEquals) {
using uprotocol::uri::BuildUResource;
using uprotocol::v1::UResource;
using uprotocol::uri::operator==;
// Resource 1 - No Instance
auto resource_1{BuildUResource().setName("rpc").build()};
// Resource 2 - With Instance
auto resource_2{BuildUResource().setName("rpc").setInstance("CreateTopic").build()};
EXPECT_FALSE(resource_1 == resource_2);
}
```
Contributor guide
Assessment
This issue has not been assessed yet.