You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

required_fields.h 4.2 kB

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