1 /* 2 * Copyright (c) 2009-2021, Google LLC 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * * Neither the name of Google LLC nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef UPB_UTIL_REQUIRED_FIELDS_H_ 29 #define UPB_UTIL_REQUIRED_FIELDS_H_ 30 31 #include "upb/reflection/def.h" 32 #include "upb/reflection/message.h" 33 34 // Must be last. 35 #include "upb/port/def.inc" 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 // A FieldPath can be encoded as an array of upb_FieldPathEntry, in the 42 // following format: 43 // { {.field = f1}, {.field = f2} } # f1.f2 44 // { {.field = f1}, {.index = 5}, {.field = f2} } # f1[5].f2 45 // { {.field = f1}, {.key = "abc"}, {.field = f2} } # f1["abc"].f2 46 // 47 // Users must look at the type of `field` to know if an index or map key 48 // follows. 49 // 50 // A field path may be NULL-terminated, in which case a NULL field indicates 51 // the end of the field path. 52 typedef union { 53 const upb_FieldDef* field; 54 size_t array_index; 55 upb_MessageValue map_key; 56 } upb_FieldPathEntry; 57 58 // Writes a string representing `*path` to `buf` in the following textual 59 // format: 60 // foo.bar # Regular fields 61 // repeated_baz[2].bar # Repeated field 62 // int32_msg_map[5].bar # Integer-keyed map 63 // string_msg_map["abc"] # String-keyed map 64 // bool_msg_map[true] # Bool-keyed map 65 // 66 // The input array `*path` must be NULL-terminated. The pointer `*path` will be 67 // updated to point to one past the terminating NULL pointer of the input array. 68 // 69 // The output buffer `buf` will always be NULL-terminated. If the output data 70 // (including NULL terminator) exceeds `size`, the result will be truncated. 71 // Returns the string length of the data we attempted to write, excluding the 72 // terminating NULL. 73 size_t upb_FieldPath_ToText(upb_FieldPathEntry** path, char* buf, size_t size); 74 75 // Checks whether `msg` or any of its children has unset required fields, 76 // returning `true` if any are found. `msg` may be NULL, in which case the 77 // message will be treated as empty. 78 // 79 // When this function returns true, `fields` is updated (if non-NULL) to point 80 // to a heap-allocated array encoding the field paths of the required fields 81 // that are missing. Each path is terminated with {.field = NULL}, and a final 82 // {.field = NULL} terminates the list of paths. The caller is responsible for 83 // freeing this array. 84 bool upb_util_HasUnsetRequired(const upb_Message* msg, const upb_MessageDef* m, 85 const upb_DefPool* ext_pool, 86 upb_FieldPathEntry** fields); 87 88 #ifdef __cplusplus 89 } /* extern "C" */ 90 #endif 91 92 #include "upb/port/undef.inc" 93 94 #endif /* UPB_UTIL_REQUIRED_FIELDS_H_ */ 95