GPBDescriptor.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import <Foundation/Foundation.h>
  31. #import "GPBRuntimeTypes.h"
  32. @class GPBEnumDescriptor;
  33. @class GPBFieldDescriptor;
  34. @class GPBFileDescriptor;
  35. @class GPBOneofDescriptor;
  36. NS_ASSUME_NONNULL_BEGIN
  37. /** Syntax used in the proto file. */
  38. typedef NS_ENUM(uint8_t, GPBFileSyntax) {
  39. /** Unknown syntax. */
  40. GPBFileSyntaxUnknown = 0,
  41. /** Proto2 syntax. */
  42. GPBFileSyntaxProto2 = 2,
  43. /** Proto3 syntax. */
  44. GPBFileSyntaxProto3 = 3,
  45. };
  46. /** Type of proto field. */
  47. typedef NS_ENUM(uint8_t, GPBFieldType) {
  48. /** Optional/required field. Only valid for proto2 fields. */
  49. GPBFieldTypeSingle,
  50. /** Repeated field. */
  51. GPBFieldTypeRepeated,
  52. /** Map field. */
  53. GPBFieldTypeMap,
  54. };
  55. /**
  56. * Describes a proto message.
  57. **/
  58. @interface GPBDescriptor : NSObject<NSCopying>
  59. /** Name of the message. */
  60. @property(nonatomic, readonly, copy) NSString *name;
  61. /** Fields declared in the message. */
  62. @property(nonatomic, readonly, strong, nullable) NSArray<GPBFieldDescriptor*> *fields;
  63. /** Oneofs declared in the message. */
  64. @property(nonatomic, readonly, strong, nullable) NSArray<GPBOneofDescriptor*> *oneofs;
  65. /** Extension range declared for the message. */
  66. @property(nonatomic, readonly, nullable) const GPBExtensionRange *extensionRanges;
  67. /** Number of extension ranges declared for the message. */
  68. @property(nonatomic, readonly) uint32_t extensionRangesCount;
  69. /** Descriptor for the file where the message was defined. */
  70. @property(nonatomic, readonly, assign) GPBFileDescriptor *file;
  71. /** Whether the message is in wire format or not. */
  72. @property(nonatomic, readonly, getter=isWireFormat) BOOL wireFormat;
  73. /** The class of this message. */
  74. @property(nonatomic, readonly) Class messageClass;
  75. /**
  76. * Gets the field for the given number.
  77. *
  78. * @param fieldNumber The number for the field to get.
  79. *
  80. * @return The field descriptor for the given number, or nil if not found.
  81. **/
  82. - (nullable GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber;
  83. /**
  84. * Gets the field for the given name.
  85. *
  86. * @param name The name for the field to get.
  87. *
  88. * @return The field descriptor for the given name, or nil if not found.
  89. **/
  90. - (nullable GPBFieldDescriptor *)fieldWithName:(NSString *)name;
  91. /**
  92. * Gets the oneof for the given name.
  93. *
  94. * @param name The name for the oneof to get.
  95. *
  96. * @return The oneof descriptor for the given name, or nil if not found.
  97. **/
  98. - (nullable GPBOneofDescriptor *)oneofWithName:(NSString *)name;
  99. @end
  100. /**
  101. * Describes a proto file.
  102. **/
  103. @interface GPBFileDescriptor : NSObject
  104. /** The package declared in the proto file. */
  105. @property(nonatomic, readonly, copy) NSString *package;
  106. /** The syntax of the proto file. */
  107. @property(nonatomic, readonly) GPBFileSyntax syntax;
  108. @end
  109. /**
  110. * Describes a oneof field.
  111. **/
  112. @interface GPBOneofDescriptor : NSObject
  113. /** Name of the oneof field. */
  114. @property(nonatomic, readonly) NSString *name;
  115. /** Fields declared in the oneof. */
  116. @property(nonatomic, readonly) NSArray<GPBFieldDescriptor*> *fields;
  117. /**
  118. * Gets the field for the given number.
  119. *
  120. * @param fieldNumber The number for the field to get.
  121. *
  122. * @return The field descriptor for the given number, or nil if not found.
  123. **/
  124. - (nullable GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber;
  125. /**
  126. * Gets the field for the given name.
  127. *
  128. * @param name The name for the field to get.
  129. *
  130. * @return The field descriptor for the given name, or nil if not found.
  131. **/
  132. - (nullable GPBFieldDescriptor *)fieldWithName:(NSString *)name;
  133. @end
  134. /**
  135. * Describes a proto field.
  136. **/
  137. @interface GPBFieldDescriptor : NSObject
  138. /** Name of the field. */
  139. @property(nonatomic, readonly, copy) NSString *name;
  140. /** Number associated with the field. */
  141. @property(nonatomic, readonly) uint32_t number;
  142. /** Data type contained in the field. */
  143. @property(nonatomic, readonly) GPBDataType dataType;
  144. /** Whether it has a default value or not. */
  145. @property(nonatomic, readonly) BOOL hasDefaultValue;
  146. /** Default value for the field. */
  147. @property(nonatomic, readonly) GPBGenericValue defaultValue;
  148. /** Whether this field is required. Only valid for proto2 fields. */
  149. @property(nonatomic, readonly, getter=isRequired) BOOL required;
  150. /** Whether this field is optional. */
  151. @property(nonatomic, readonly, getter=isOptional) BOOL optional;
  152. /** Type of field (single, repeated, map). */
  153. @property(nonatomic, readonly) GPBFieldType fieldType;
  154. /** Type of the key if the field is a map. The value's type is -fieldType. */
  155. @property(nonatomic, readonly) GPBDataType mapKeyDataType;
  156. /** Whether the field is packable. */
  157. @property(nonatomic, readonly, getter=isPackable) BOOL packable;
  158. /** The containing oneof if this field is part of one, nil otherwise. */
  159. @property(nonatomic, readonly, assign, nullable) GPBOneofDescriptor *containingOneof;
  160. /** Class of the message if the field is of message type. */
  161. @property(nonatomic, readonly, assign, nullable) Class msgClass;
  162. /** Descriptor for the enum if this field is an enum. */
  163. @property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor;
  164. /**
  165. * Checks whether the given enum raw value is a valid enum value.
  166. *
  167. * @param value The raw enum value to check.
  168. *
  169. * @return YES if value is a valid enum raw value.
  170. **/
  171. - (BOOL)isValidEnumValue:(int32_t)value;
  172. /** @return Name for the text format, or nil if not known. */
  173. - (nullable NSString *)textFormatName;
  174. @end
  175. /**
  176. * Describes a proto enum.
  177. **/
  178. @interface GPBEnumDescriptor : NSObject
  179. /** Name of the enum. */
  180. @property(nonatomic, readonly, copy) NSString *name;
  181. /** Function that validates that raw values are valid enum values. */
  182. @property(nonatomic, readonly) GPBEnumValidationFunc enumVerifier;
  183. /**
  184. * Returns the enum value name for the given raw enum.
  185. *
  186. * @param number The raw enum value.
  187. *
  188. * @return The name of the enum value passed, or nil if not valid.
  189. **/
  190. - (nullable NSString *)enumNameForValue:(int32_t)number;
  191. /**
  192. * Gets the enum raw value for the given enum name.
  193. *
  194. * @param outValue A pointer where the value will be set.
  195. * @param name The enum name for which to get the raw value.
  196. *
  197. * @return YES if a value was copied into the pointer, NO otherwise.
  198. **/
  199. - (BOOL)getValue:(nullable int32_t *)outValue forEnumName:(NSString *)name;
  200. /**
  201. * Returns the text format for the given raw enum value.
  202. *
  203. * @param number The raw enum value.
  204. *
  205. * @return The text format name for the raw enum value, or nil if not valid.
  206. **/
  207. - (nullable NSString *)textFormatNameForValue:(int32_t)number;
  208. /**
  209. * Gets the enum raw value for the given text format name.
  210. *
  211. * @param outValue A pointer where the value will be set.
  212. * @param textFormatName The text format name for which to get the raw value.
  213. *
  214. * @return YES if a value was copied into the pointer, NO otherwise.
  215. **/
  216. - (BOOL)getValue:(nullable int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName;
  217. @end
  218. /**
  219. * Describes a proto extension.
  220. **/
  221. @interface GPBExtensionDescriptor : NSObject<NSCopying>
  222. /** Field number under which the extension is stored. */
  223. @property(nonatomic, readonly) uint32_t fieldNumber;
  224. /** The containing message class, i.e. the class extended by this extension. */
  225. @property(nonatomic, readonly) Class containingMessageClass;
  226. /** Data type contained in the extension. */
  227. @property(nonatomic, readonly) GPBDataType dataType;
  228. /** Whether the extension is repeated. */
  229. @property(nonatomic, readonly, getter=isRepeated) BOOL repeated;
  230. /** Whether the extension is packable. */
  231. @property(nonatomic, readonly, getter=isPackable) BOOL packable;
  232. /** The class of the message if the extension is of message type. */
  233. @property(nonatomic, readonly, assign) Class msgClass;
  234. /** The singleton name for the extension. */
  235. @property(nonatomic, readonly) NSString *singletonName;
  236. /** The enum descriptor if the extension is of enum type. */
  237. @property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor;
  238. /** The default value for the extension. */
  239. @property(nonatomic, readonly, nullable) id defaultValue;
  240. @end
  241. NS_ASSUME_NONNULL_END