@@ -216,52 +216,116 @@ constexpr const char* kDataBagMergeErrorDictConflict =
216216 " The conflicting dict in the second DataBag: %s\n\n "
217217 " The cause is the value of the key %s is incompatible: %s vs %s\n " ;
218218
219- absl::StatusOr<Error> SetDataBagMergeError (Error cause, const DataBagPtr& db1,
220- const DataBagPtr& db2) {
221- const auto & schema_or_dict_conflict = cause.schema_or_dict_conflict ();
222- ASSIGN_OR_RETURN (
223- internal::DataItem object_item,
224- DecodeDataItem (schema_or_dict_conflict.object_id ()));
225- ASSIGN_OR_RETURN (internal::DataItem key_item,
226- DecodeDataItem (schema_or_dict_conflict.key ()));
219+ absl::StatusOr<std::string> SetSchemaOrDictErrorMessage (
220+ const internal::DataBagMergeConflict::SchemaOrDictConflict& conflict,
221+ const DataBagPtr& db1, const DataBagPtr& db2) {
222+ ASSIGN_OR_RETURN (internal::DataItem object_item,
223+ DecodeDataItem (conflict.object_id ()));
224+ ASSIGN_OR_RETURN (internal::DataItem key_item, DecodeDataItem (conflict.key ()));
227225 internal::DataItem schema = internal::DataItem (
228226 object_item.is_schema () ? schema::kSchema : schema::kAny );
229227 ASSIGN_OR_RETURN (
230228 DataSlice expected_value,
231- DataSlice::Create (
232- DecodeDataItem (schema_or_dict_conflict.expected_value ()),
233- DataSlice::JaggedShape::Empty (), schema,
234- db1));
229+ DataSlice::Create (DecodeDataItem (conflict.expected_value ()),
230+ DataSlice::JaggedShape::Empty (), schema, db1));
235231 ASSIGN_OR_RETURN (
236232 DataSlice assigned_value,
237- DataSlice::Create (
238- DecodeDataItem (schema_or_dict_conflict.assigned_value ()),
239- DataSlice::JaggedShape::Empty (), schema,
240- db2));
233+ DataSlice::Create (DecodeDataItem (conflict.assigned_value ()),
234+ DataSlice::JaggedShape::Empty (), schema, db2));
241235 ASSIGN_OR_RETURN (
242236 DataSlice item,
243237 DataSlice::Create (object_item, schema, db1));
244238 ASSIGN_OR_RETURN (
245239 DataSlice conflicting_item,
246240 DataSlice::Create (object_item, std::move (schema), db2));
247241
248- std::string key_str = internal::DataItemRepr (
249- key_item, {.strip_quotes = false });
242+ std::string key_str =
243+ internal::DataItemRepr ( key_item, {.strip_quotes = false });
250244 ASSIGN_OR_RETURN (std::string item_str, DataSliceToStr (item));
251245 ASSIGN_OR_RETURN (std::string conflicting_item_str,
252246 DataSliceToStr (conflicting_item));
253247 ASSIGN_OR_RETURN (std::string expected_value_str,
254248 DataSliceToStr (expected_value));
255249 ASSIGN_OR_RETURN (std::string assigned_value_str,
256250 DataSliceToStr (assigned_value));
257- std::string error_str =
258- object_item.is_schema ()
259- ? absl::StrFormat (kDataBagMergeErrorSchemaConflict , item_str,
260- conflicting_item_str, key_str, expected_value_str,
261- assigned_value_str)
262- : absl::StrFormat (kDataBagMergeErrorDictConflict , item_str,
263- conflicting_item_str, key_str, expected_value_str,
264- assigned_value_str);
251+ return object_item.is_schema ()
252+ ? absl::StrFormat (kDataBagMergeErrorSchemaConflict , item_str,
253+ conflicting_item_str, key_str,
254+ expected_value_str, assigned_value_str)
255+ : absl::StrFormat (kDataBagMergeErrorDictConflict , item_str,
256+ conflicting_item_str, key_str,
257+ expected_value_str, assigned_value_str);
258+ }
259+
260+ constexpr const char * kDataBagMergeErrorListSizeConflict =
261+ " cannot merge DataBags due to an exception encountered when merging "
262+ " lists.\n\n "
263+ " The conflicting list in the first DataBag: %s\n "
264+ " The conflicting list in the second DataBag: %s\n\n "
265+ " The cause is the list sizes are incompatible: %d vs %d\n " ;
266+
267+ constexpr const char * kDataBagMergeErrorListItemConflict =
268+ " cannot merge DataBags due to an exception encountered when merging "
269+ " lists.\n\n "
270+ " The conflicting list in the first DataBag: %s\n "
271+ " The conflicting list in the second DataBag: %s\n\n "
272+ " The cause is the value at index %d is incompatible: %s vs %s\n " ;
273+
274+ absl::StatusOr<std::string> SetListErrorMessage (
275+ const internal::DataBagMergeConflict::ListConflict& conflict,
276+ const DataBagPtr& db1, const DataBagPtr& db2) {
277+ ASSIGN_OR_RETURN (internal::DataItem list_item,
278+ DecodeDataItem (conflict.list_object_id ()));
279+ ASSIGN_OR_RETURN (DataSlice list,
280+ DataSlice::Create (list_item, DataSlice::JaggedShape::Empty (),
281+ internal::DataItem (schema::kAny ), db1));
282+ ASSIGN_OR_RETURN (DataSlice conflicting_list,
283+ DataSlice::Create (list_item, DataSlice::JaggedShape::Empty (),
284+ internal::DataItem (schema::kAny ), db2));
285+ ASSIGN_OR_RETURN (std::string list_str, DataSliceToStr (list));
286+ ASSIGN_OR_RETURN (std::string conflicting_list_str,
287+ DataSliceToStr (conflicting_list));
288+ if (conflict.has_list_item_conflict_index ()) {
289+ ASSIGN_OR_RETURN (
290+ DataSlice first_conflicting_item,
291+ DataSlice::Create (DecodeDataItem (conflict.first_conflicting_item ()),
292+ DataSlice::JaggedShape::Empty (),
293+ internal::DataItem (schema::kAny ), db1));
294+ ASSIGN_OR_RETURN (
295+ DataSlice second_conflicting_item,
296+ DataSlice::Create (DecodeDataItem (conflict.second_conflicting_item ()),
297+ DataSlice::JaggedShape::Empty (),
298+ internal::DataItem (schema::kAny ), db2));
299+ ASSIGN_OR_RETURN (std::string first_conflicting_item_str,
300+ DataSliceToStr (first_conflicting_item));
301+ ASSIGN_OR_RETURN (std::string second_conflicting_item_str,
302+ DataSliceToStr (second_conflicting_item));
303+ return absl::StrFormat (
304+ kDataBagMergeErrorListItemConflict , list_str, conflicting_list_str,
305+ conflict.list_item_conflict_index (), first_conflicting_item_str,
306+ second_conflicting_item_str);
307+ }
308+ return absl::StrFormat (kDataBagMergeErrorListSizeConflict , list_str,
309+ conflicting_list_str, conflict.first_list_size (),
310+ conflict.second_list_size ());
311+ }
312+
313+ absl::StatusOr<Error> SetDataBagMergeError (Error cause, const DataBagPtr& db1,
314+ const DataBagPtr& db2) {
315+ std::string error_str;
316+ if (cause.data_bag_merge_conflict ().has_schema_or_dict_conflict ()) {
317+ ASSIGN_OR_RETURN (
318+ error_str,
319+ SetSchemaOrDictErrorMessage (
320+ cause.data_bag_merge_conflict ().schema_or_dict_conflict (), db1,
321+ db2));
322+ }
323+ if (cause.data_bag_merge_conflict ().has_list_conflict ()) {
324+ ASSIGN_OR_RETURN (
325+ error_str,
326+ SetListErrorMessage (cause.data_bag_merge_conflict ().list_conflict (),
327+ db1, db2));
328+ }
265329 cause.set_error_message (std::move (error_str));
266330 return cause;
267331}
@@ -288,7 +352,7 @@ absl::Status AssembleErrorMessage(const absl::Status& status,
288352 data.db , data.ds ));
289353 return WithErrorPayload (status, error);
290354 }
291- if (cause->has_schema_or_dict_conflict ()) {
355+ if (cause->has_data_bag_merge_conflict ()) {
292356 ASSIGN_OR_RETURN (Error error,
293357 SetDataBagMergeError (*std::move (cause), data.db ,
294358 data.to_be_merged_db ));
0 commit comments