• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "upb/message/accessors.h"
9 
10 #include <string.h>
11 
12 #include "upb/mem/arena.h"
13 #include "upb/message/array.h"
14 #include "upb/message/map.h"
15 #include "upb/message/message.h"
16 #include "upb/mini_table/field.h"
17 #include "upb/mini_table/message.h"
18 #include "upb/mini_table/sub.h"
19 
20 // Must be last.
21 #include "upb/port/def.inc"
22 
upb_Message_SetMapEntry(upb_Map * map,const upb_MiniTable * m,const upb_MiniTableField * f,upb_Message * map_entry_message,upb_Arena * arena)23 bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* m,
24                              const upb_MiniTableField* f,
25                              upb_Message* map_entry_message, upb_Arena* arena) {
26   UPB_ASSERT(!upb_Message_IsFrozen(map_entry_message));
27   const upb_MiniTable* map_entry_mini_table =
28       upb_MiniTable_MapEntrySubMessage(m, f);
29   UPB_ASSERT(map_entry_mini_table);
30   const upb_MiniTableField* map_entry_key_field =
31       upb_MiniTable_MapKey(map_entry_mini_table);
32   const upb_MiniTableField* map_entry_value_field =
33       upb_MiniTable_MapValue(map_entry_mini_table);
34   // Map key/value cannot have explicit defaults,
35   // hence assuming a zero default is valid.
36   upb_MessageValue default_val;
37   memset(&default_val, 0, sizeof(upb_MessageValue));
38   upb_MessageValue map_entry_key =
39       upb_Message_GetField(map_entry_message, map_entry_key_field, default_val);
40   upb_MessageValue map_entry_value = upb_Message_GetField(
41       map_entry_message, map_entry_value_field, default_val);
42   return upb_Map_Set(map, map_entry_key, map_entry_value, arena);
43 }
44