refactor: Simplify SmartREST publish topics#3292
Conversation
64e559d to
aa48c0c
Compare
Codecov ReportAttention: Patch coverage is Additional details and impacted files📢 Thoughts on this report? Let us know! |
Robot Results
|
didier-wenzek
left a comment
There was a problem hiding this comment.
Approved - with 2 suggestions.
| // Skipping the last ancestor as it is the main device represented by the root topic itself | ||
| target_topic.push('/'); | ||
| target_topic.push_str(ancestor.as_ref()); | ||
| pub fn publish_topic_from_parent(parent: Option<&str>, prefix: &TopicPrefix) -> Topic { |
There was a problem hiding this comment.
A doc comment would be helpful, at least some examples.
| pub fn publish_topic_from_parent(parent: Option<&str>, prefix: &TopicPrefix) -> Topic { | |
| /// (None, prefix) -> ??? | |
| /// (Some(parent), prefix) -> ??? | |
| pub fn publish_topic_from_parent(parent: Option<&str>, prefix: &TopicPrefix) -> Topic { |
| Ok(topic) | ||
| } | ||
|
|
||
| pub fn parent_xid_if_not_main_device( |
There was a problem hiding this comment.
A function name doesn't have to tell the whole story. The signature is enough to understand that this will return None for a device with no parent.
| pub fn parent_xid_if_not_main_device( | |
| pub fn parent_xid( |
There was a problem hiding this comment.
Oh yeah, I was kinda expecting some comment on this API, as I really struggled to name it properly. By bad, for not adding the rustdocs for it and hoping that a longer/detailed name would convey it properly.
So, I didn't name it just parent_xid because what's returned is not quite the parent_xid in all cases. For any child devices at level 2 and beyond, this function returns Some(<parent-xid>) as expected. But for the immediate child devices of the main device, where the parent xid is the device id of the main device, this function must return None instead of returning Some(<main-device-id>). We're returning None in that case, because all the existing SmartREST APIs like child_device_creation_message, service_creation_message_payload etc expects None when the message must be published to s/us directly and Some(<parent-xid>) when the message must be published to s/us/<parent-xid>. That's the reason why this function impl has this additional filtering step: filter(|tid| *tid != &self.device_topic_id) to switch it to None when the parent is the main device.
The alternatives were the following:
- Implement a simple
parent_xidfunction as suggested and call the additional filtering at the places where the SmartREST APIs are used. - Update those SmartREST APIs to take the main device xid as an additional parameter and do the filtering there
I implemented it this way it is now, to just have all that logic at one place and reuse it everywhere. But, if that API looks weird, then I can implement one of the above approaches.
PS: Writing so much to explain the API contract, I kinda feel that it was the wrong choice. Option. 2 above may have been a better choice.
There was a problem hiding this comment.
I got it.
Option 2 sounds the correct choice (because moving away the c8y details under c8y_api).
However, it would be better to first stabilize the main PR and add such improvements only in a second step.
For now, simply add this note as a doc comment.
21c694f to
3c37347
Compare
3c37347 to
7169605
Compare
While publishing to a nested child device, publish directly to the SmartREST topic of that device (`c8y/s/us/<xid>`), without specifying all of its ancestors in the topic.
7169605 to
4b8213a
Compare
Proposed changes
While publishing to a nested child device, publish directly to the SmartREST topic
of that device (
c8y/s/us/<xid>), without specifying all of all its ancestors.Types of changes
Paste Link to the issue
Checklist
cargo fmtas mentioned in CODING_GUIDELINEScargo clippyas mentioned in CODING_GUIDELINESFurther comments