• 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 #ifndef UPB_UTIL_REQUIRED_FIELDS_H_
9 #define UPB_UTIL_REQUIRED_FIELDS_H_
10 
11 #include <stddef.h>
12 
13 #include "upb/reflection/def.h"
14 #include "upb/reflection/message.h"
15 
16 // Must be last.
17 #include "upb/port/def.inc"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 // A FieldPath can be encoded as an array of upb_FieldPathEntry, in the
24 // following format:
25 //    { {.field = f1}, {.field = f2} }                      # f1.f2
26 //    { {.field = f1}, {.index = 5}, {.field = f2} }        # f1[5].f2
27 //    { {.field = f1}, {.key = "abc"}, {.field = f2} }      # f1["abc"].f2
28 //
29 // Users must look at the type of `field` to know if an index or map key
30 // follows.
31 //
32 // A field path may be NULL-terminated, in which case a NULL field indicates
33 // the end of the field path.
34 typedef union {
35   const upb_FieldDef* field;
36   size_t array_index;
37   upb_MessageValue map_key;
38 } upb_FieldPathEntry;
39 
40 // Writes a string representing `*path` to `buf` in the following textual
41 // format:
42 //    foo.bar                    # Regular fields
43 //    repeated_baz[2].bar        # Repeated field
44 //    int32_msg_map[5].bar       # Integer-keyed map
45 //    string_msg_map["abc"]      # String-keyed map
46 //    bool_msg_map[true]         # Bool-keyed map
47 //
48 // The input array `*path` must be NULL-terminated.  The pointer `*path` will be
49 // updated to point to one past the terminating NULL pointer of the input array.
50 //
51 // The output buffer `buf` will always be NULL-terminated. If the output data
52 // (including NULL terminator) exceeds `size`, the result will be truncated.
53 // Returns the string length of the data we attempted to write, excluding the
54 // terminating NULL.
55 size_t upb_FieldPath_ToText(upb_FieldPathEntry** path, char* buf, size_t size);
56 
57 // Checks whether `msg` or any of its children has unset required fields,
58 // returning `true` if any are found.  `msg` may be NULL, in which case the
59 // message will be treated as empty.
60 //
61 // When this function returns true, `fields` is updated (if non-NULL) to point
62 // to a heap-allocated array encoding the field paths of the required fields
63 // that are missing.  Each path is terminated with {.field = NULL}, and a final
64 // {.field = NULL} terminates the list of paths.  The caller is responsible for
65 // freeing this array.
66 bool upb_util_HasUnsetRequired(const upb_Message* msg, const upb_MessageDef* m,
67                                const upb_DefPool* ext_pool,
68                                upb_FieldPathEntry** fields);
69 
70 #ifdef __cplusplus
71 } /* extern "C" */
72 #endif
73 
74 #include "upb/port/undef.inc"
75 
76 #endif /* UPB_UTIL_REQUIRED_FIELDS_H_ */
77