Handling null value in chat-message 89/217889/1
authorSukHyung, Kang <shine.kang@samsung.com>
Fri, 15 Nov 2019 08:05:56 +0000 (17:05 +0900)
committerSukHyung, Kang <shine.kang@samsung.com>
Fri, 15 Nov 2019 08:05:56 +0000 (17:05 +0900)
Change-Id: I564d2ef5978116fb3949ff7d09601f9b088d7f5f
Signed-off-by: SukHyung, Kang <shine.kang@samsung.com>
notification-ex/chat_message_item.cc
notification-ex/stub.cc

index ea95e39bb81b88844218dafe2a2c3f3140e22c6c..be4e80babbbfdcff80f2995961351c84b00fe6f3 100644 (file)
@@ -61,14 +61,22 @@ Bundle ChatMessageItem::Serialize() const {
   Bundle b;
   b = AbstractItem::Serialize();
 
-  b.Add(CHATMESSAGE_NAME_KEY,
-    reinterpret_cast<char*>(impl_->name_->Serialize().ToRaw().first.get()));
-  b.Add(CHATMESSAGE_TEXT_KEY,
-    reinterpret_cast<char*>(impl_->text_->Serialize().ToRaw().first.get()));
-  b.Add(CHATMESSAGE_IMAGE_KEY,
-    reinterpret_cast<char*>(impl_->image_->Serialize().ToRaw().first.get()));
-  b.Add(CHATMESSAGE_TIME_KEY,
-    reinterpret_cast<char*>(impl_->time_->Serialize().ToRaw().first.get()));
+  if (impl_->name_ != nullptr)
+    b.Add(CHATMESSAGE_NAME_KEY,
+      reinterpret_cast<char*>(impl_->name_->Serialize().ToRaw().first.get()));
+
+  if (impl_->text_ != nullptr)
+    b.Add(CHATMESSAGE_TEXT_KEY,
+      reinterpret_cast<char*>(impl_->text_->Serialize().ToRaw().first.get()));
+
+  if (impl_->image_ != nullptr)
+    b.Add(CHATMESSAGE_IMAGE_KEY,
+      reinterpret_cast<char*>(impl_->image_->Serialize().ToRaw().first.get()));
+
+  if (impl_->time_ != nullptr)
+    b.Add(CHATMESSAGE_TIME_KEY,
+      reinterpret_cast<char*>(impl_->time_->Serialize().ToRaw().first.get()));
+
   b.Add(CHATMESSAGE_TYPE_KEY, std::to_string(static_cast<int>(impl_->type_)));
   return b;
 }
@@ -76,25 +84,33 @@ Bundle ChatMessageItem::Serialize() const {
 void ChatMessageItem::Deserialize(Bundle b) {
   AbstractItem::Deserialize(b);
 
-  std::shared_ptr<AbstractItem> name =
-      FactoryManager::GetInst().CreateItem(AbstractItem::Text);
-  name.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_NAME_KEY)));
-  impl_->name_ = std::static_pointer_cast<TextItem>(name);
-
-  std::shared_ptr<AbstractItem> text =
-      FactoryManager::GetInst().CreateItem(AbstractItem::Text);
-  text.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TEXT_KEY)));
-  impl_->text_ = std::static_pointer_cast<TextItem>(text);
-
-  std::shared_ptr<AbstractItem> image =
-      FactoryManager::GetInst().CreateItem(AbstractItem::Image);
-  image.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_IMAGE_KEY)));
-  impl_->image_ = std::static_pointer_cast<ImageItem>(image);
-
-  std::shared_ptr<AbstractItem> time =
-      FactoryManager::GetInst().CreateItem(AbstractItem::Time);
-  time.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TIME_KEY)));
-  impl_->time_ = std::static_pointer_cast<TimeItem>(time);
+  if (!b.GetString(CHATMESSAGE_NAME_KEY).empty()) {
+    std::shared_ptr<AbstractItem> name =
+        FactoryManager::GetInst().CreateItem(AbstractItem::Text);
+    name.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_NAME_KEY)));
+    impl_->name_ = std::static_pointer_cast<TextItem>(name);
+  }
+
+  if (!b.GetString(CHATMESSAGE_TEXT_KEY).empty()) {
+    std::shared_ptr<AbstractItem> text =
+        FactoryManager::GetInst().CreateItem(AbstractItem::Text);
+    text.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TEXT_KEY)));
+    impl_->text_ = std::static_pointer_cast<TextItem>(text);
+  }
+
+  if (!b.GetString(CHATMESSAGE_IMAGE_KEY).empty()) {
+    std::shared_ptr<AbstractItem> image =
+        FactoryManager::GetInst().CreateItem(AbstractItem::Image);
+    image.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_IMAGE_KEY)));
+    impl_->image_ = std::static_pointer_cast<ImageItem>(image);
+  }
+
+  if (!b.GetString(CHATMESSAGE_TIME_KEY).empty()) {
+    std::shared_ptr<AbstractItem> time =
+        FactoryManager::GetInst().CreateItem(AbstractItem::Time);
+    time.get()->Deserialize(Bundle(b.GetString(CHATMESSAGE_TIME_KEY)));
+    impl_->time_ = std::static_pointer_cast<TimeItem>(time);
+  }
 
   impl_->type_ = static_cast<Type>(std::stoi(b.GetString(CHATMESSAGE_TYPE_KEY)));
 }
index 0e37fe17a92a2278e697bc79d954fb9339808227..b890482ae4cc6f69c2a7d99ed83040aeda56071e 100644 (file)
@@ -441,15 +441,19 @@ extern "C" EXPORT_API int noti_ex_item_chat_message_create(
     noti_ex_item_h *handle, const char *id, noti_ex_item_h name,
     noti_ex_item_h text, noti_ex_item_h image, noti_ex_item_h time,
     noti_ex_item_chat_message_type_e message_type) {
-  if (handle == nullptr || message_type > NOTI_EX_ITEM_CHAT_MESSAGE_TYPE_SENDER) {
+  if (handle == nullptr || (text == nullptr && image == nullptr)
+      || name == nullptr || time == nullptr
+      || message_type > NOTI_EX_ITEM_CHAT_MESSAGE_TYPE_SENDER) {
     LOGE("Invalid parameter");
     return NOTI_EX_ERROR_INVALID_PARAMETER;
   }
 
   auto* p = new (std::nothrow) ChatMessageItem(id,
           dynamic_pointer_cast<TextItem>(static_cast<Handle*>(name)->GetPtr()),
-          dynamic_pointer_cast<TextItem>(static_cast<Handle*>(text)->GetPtr()),
-          dynamic_pointer_cast<ImageItem>(static_cast<Handle*>(image)->GetPtr()),
+          text == nullptr ? nullptr
+            : dynamic_pointer_cast<TextItem>(static_cast<Handle*>(text)->GetPtr()),
+          image == nullptr ? nullptr
+            : dynamic_pointer_cast<ImageItem>(static_cast<Handle*>(image)->GetPtr()),
           dynamic_pointer_cast<TimeItem>(static_cast<Handle*>(time)->GetPtr()),
           static_cast<ChatMessageItem::Type>(message_type));
   if (p == nullptr) {