GPBDescriptor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. 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. #import <Foundation/Foundation.h>
  8. #import "GPBRuntimeTypes.h"
  9. @class GPBEnumDescriptor;
  10. @class GPBFieldDescriptor;
  11. @class GPBFileDescriptor;
  12. @class GPBOneofDescriptor;
  13. NS_ASSUME_NONNULL_BEGIN
  14. /** Syntax used in the proto file. */
  15. typedef NS_ENUM(uint8_t, GPBFileSyntax) {
  16. /** Unknown syntax. */
  17. GPBFileSyntaxUnknown = 0,
  18. /** Proto2 syntax. */
  19. GPBFileSyntaxProto2 = 2,
  20. /** Proto3 syntax. */
  21. GPBFileSyntaxProto3 = 3,
  22. /** Editions syntax. */
  23. GPBFileSyntaxProtoEditions = 99,
  24. };
  25. /** Type of proto field. */
  26. typedef NS_ENUM(uint8_t, GPBFieldType) {
  27. /** Optional/required field. Only valid for proto2 fields. */
  28. GPBFieldTypeSingle,
  29. /** Repeated field. */
  30. GPBFieldTypeRepeated,
  31. /** Map field. */
  32. GPBFieldTypeMap,
  33. };
  34. /**
  35. * Describes a proto message.
  36. **/
  37. __attribute__((objc_subclassing_restricted))
  38. @interface GPBDescriptor : NSObject<NSCopying>
  39. - (instancetype)init NS_UNAVAILABLE;
  40. + (instancetype)new NS_UNAVAILABLE;
  41. /** Name of the message. */
  42. @property(nonatomic, readonly, copy) NSString *name;
  43. /** Fields declared in the message. */
  44. @property(nonatomic, readonly, strong, nullable) NSArray<GPBFieldDescriptor *> *fields;
  45. /** Oneofs declared in the message. */
  46. @property(nonatomic, readonly, strong, nullable) NSArray<GPBOneofDescriptor *> *oneofs;
  47. /** Extension range declared for the message. */
  48. @property(nonatomic, readonly, nullable) const GPBExtensionRange *extensionRanges;
  49. /** Number of extension ranges declared for the message. */
  50. @property(nonatomic, readonly) uint32_t extensionRangesCount;
  51. /** Descriptor for the file where the message was defined. */
  52. @property(nonatomic, readonly) GPBFileDescriptor *file;
  53. /** Whether the message is in wire format or not. */
  54. @property(nonatomic, readonly, getter=isWireFormat) BOOL wireFormat;
  55. /** The class of this message. */
  56. @property(nonatomic, readonly) Class messageClass;
  57. /** Containing message descriptor if this message is nested, or nil otherwise. */
  58. @property(readonly, nullable) GPBDescriptor *containingType;
  59. /**
  60. * Fully qualified name for this message (package.message). Can be nil if the
  61. * value is unable to be computed.
  62. */
  63. @property(readonly, nullable) NSString *fullName;
  64. /**
  65. * Gets the field for the given number.
  66. *
  67. * @param fieldNumber The number for the field to get.
  68. *
  69. * @return The field descriptor for the given number, or nil if not found.
  70. **/
  71. - (nullable GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber;
  72. /**
  73. * Gets the field for the given name.
  74. *
  75. * @param name The name for the field to get.
  76. *
  77. * @return The field descriptor for the given name, or nil if not found.
  78. **/
  79. - (nullable GPBFieldDescriptor *)fieldWithName:(NSString *)name;
  80. /**
  81. * Gets the oneof for the given name.
  82. *
  83. * @param name The name for the oneof to get.
  84. *
  85. * @return The oneof descriptor for the given name, or nil if not found.
  86. **/
  87. - (nullable GPBOneofDescriptor *)oneofWithName:(NSString *)name;
  88. @end
  89. /**
  90. * Describes a proto file.
  91. **/
  92. __attribute__((objc_subclassing_restricted))
  93. @interface GPBFileDescriptor : NSObject<NSCopying>
  94. - (instancetype)init NS_UNAVAILABLE;
  95. + (instancetype)new NS_UNAVAILABLE;
  96. /** The package declared in the proto file. */
  97. @property(nonatomic, readonly, copy) NSString *package;
  98. /** The objc prefix declared in the proto file. */
  99. @property(nonatomic, readonly, copy, nullable) NSString *objcPrefix;
  100. /** The syntax of the proto file, this property will be removed in the future. */
  101. @property(nonatomic, readonly) GPBFileSyntax syntax
  102. __attribute__((deprecated("Syntax will be removed in the future.")));
  103. @end
  104. /**
  105. * Describes a oneof field.
  106. **/
  107. __attribute__((objc_subclassing_restricted))
  108. @interface GPBOneofDescriptor : NSObject<NSCopying>
  109. - (instancetype)init NS_UNAVAILABLE;
  110. + (instancetype)new NS_UNAVAILABLE;
  111. /** Name of the oneof field. */
  112. @property(nonatomic, readonly) NSString *name;
  113. /** Fields declared in the oneof. */
  114. @property(nonatomic, readonly) NSArray<GPBFieldDescriptor *> *fields;
  115. /**
  116. * Gets the field for the given number.
  117. *
  118. * @param fieldNumber The number for the field to get.
  119. *
  120. * @return The field descriptor for the given number, or nil if not found.
  121. **/
  122. - (nullable GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber;
  123. /**
  124. * Gets the field for the given name.
  125. *
  126. * @param name The name for the field to get.
  127. *
  128. * @return The field descriptor for the given name, or nil if not found.
  129. **/
  130. - (nullable GPBFieldDescriptor *)fieldWithName:(NSString *)name;
  131. @end
  132. /**
  133. * Describes a proto field.
  134. **/
  135. __attribute__((objc_subclassing_restricted))
  136. @interface GPBFieldDescriptor : NSObject<NSCopying>
  137. - (instancetype)init NS_UNAVAILABLE;
  138. + (instancetype)new NS_UNAVAILABLE;
  139. /** Name of the field. */
  140. @property(nonatomic, readonly, copy) NSString *name;
  141. /** Number associated with the field. */
  142. @property(nonatomic, readonly) uint32_t number;
  143. /** Data type contained in the field. */
  144. @property(nonatomic, readonly) GPBDataType dataType;
  145. /** Whether it has a default value or not. */
  146. @property(nonatomic, readonly) BOOL hasDefaultValue;
  147. /** Default value for the field. */
  148. @property(nonatomic, readonly) GPBGenericValue defaultValue;
  149. /** Whether this field is required. Only valid for proto2 fields. */
  150. @property(nonatomic, readonly, getter=isRequired) BOOL required;
  151. /** Whether this field is optional. */
  152. @property(nonatomic, readonly, getter=isOptional) BOOL optional;
  153. /** Type of field (single, repeated, map). */
  154. @property(nonatomic, readonly) GPBFieldType fieldType;
  155. /** Type of the key if the field is a map. The value's type is -dataType. */
  156. @property(nonatomic, readonly) GPBDataType mapKeyDataType;
  157. /** Whether the field is packable. */
  158. @property(nonatomic, readonly, getter=isPackable) BOOL packable;
  159. /** The containing oneof if this field is part of one, nil otherwise. */
  160. @property(nonatomic, readonly, nullable) GPBOneofDescriptor *containingOneof;
  161. /** Class of the message if the field is of message type. */
  162. @property(nonatomic, readonly, nullable) Class msgClass;
  163. /** Descriptor for the enum if this field is an enum. */
  164. @property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor;
  165. /**
  166. * Checks whether the given enum raw value is a valid enum value.
  167. *
  168. * @param value The raw enum value to check.
  169. *
  170. * @return YES if value is a valid enum raw value.
  171. **/
  172. - (BOOL)isValidEnumValue:(int32_t)value;
  173. /** @return Name for the text format, or nil if not known. */
  174. - (nullable NSString *)textFormatName;
  175. @end
  176. /**
  177. * Describes a proto enum.
  178. **/
  179. __attribute__((objc_subclassing_restricted))
  180. @interface GPBEnumDescriptor : NSObject<NSCopying>
  181. - (instancetype)init NS_UNAVAILABLE;
  182. + (instancetype)new NS_UNAVAILABLE;
  183. /** Name of the enum. */
  184. @property(nonatomic, readonly, copy) NSString *name;
  185. /** Function that validates that raw values are valid enum values. */
  186. @property(nonatomic, readonly) GPBEnumValidationFunc enumVerifier;
  187. /**
  188. * Is this a closed enum, meaning that it:
  189. * - Has a fixed set of named values.
  190. * - Encountering values not in this set causes them to be treated as unknown
  191. * fields.
  192. * - The first value (i.e., the default) may be nonzero.
  193. *
  194. * NOTE: This is only accurate if the generate sources for a proto file were
  195. * generated with a protobuf release after the v21.9 version, as the ObjC
  196. * generator wasn't capturing this information.
  197. */
  198. @property(nonatomic, readonly) BOOL isClosed;
  199. /**
  200. * Returns the enum value name for the given raw enum.
  201. *
  202. * Note that there can be more than one name corresponding to a given value
  203. * if the allow_alias option is used.
  204. *
  205. * @param number The raw enum value.
  206. *
  207. * @return The first name that matches the enum value passed, or nil if not valid.
  208. **/
  209. - (nullable NSString *)enumNameForValue:(int32_t)number;
  210. /**
  211. * Gets the enum raw value for the given enum name.
  212. *
  213. * @param outValue A pointer where the value will be set.
  214. * @param name The enum name for which to get the raw value.
  215. *
  216. * @return YES if a value was copied into the pointer, NO otherwise.
  217. **/
  218. - (BOOL)getValue:(nullable int32_t *)outValue forEnumName:(NSString *)name;
  219. /**
  220. * Returns the text format for the given raw enum value.
  221. *
  222. * @param number The raw enum value.
  223. *
  224. * @return The first text format name which matches the enum value, or nil if not valid.
  225. **/
  226. - (nullable NSString *)textFormatNameForValue:(int32_t)number;
  227. /**
  228. * Gets the enum raw value for the given text format name.
  229. *
  230. * @param outValue A pointer where the value will be set.
  231. * @param textFormatName The text format name for which to get the raw value.
  232. *
  233. * @return YES if a value was copied into the pointer, NO otherwise.
  234. **/
  235. - (BOOL)getValue:(nullable int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName;
  236. /**
  237. * Gets the number of defined enum names.
  238. *
  239. * @return Count of the number of enum names, including any aliases.
  240. */
  241. @property(nonatomic, readonly) uint32_t enumNameCount;
  242. /**
  243. * Gets the enum name corresponding to the given index.
  244. *
  245. * @param index Index into the available names. The defined range is from 0
  246. * to self.enumNameCount - 1.
  247. *
  248. * @returns The enum name at the given index, or nil if the index is out of range.
  249. */
  250. - (nullable NSString *)getEnumNameForIndex:(uint32_t)index;
  251. /**
  252. * Gets the enum text format name corresponding to the given index.
  253. *
  254. * @param index Index into the available names. The defined range is from 0
  255. * to self.enumNameCount - 1.
  256. *
  257. * @returns The text format name at the given index, or nil if the index is out of range.
  258. */
  259. - (nullable NSString *)getEnumTextFormatNameForIndex:(uint32_t)index;
  260. @end
  261. /**
  262. * Describes a proto extension.
  263. **/
  264. __attribute__((objc_subclassing_restricted))
  265. @interface GPBExtensionDescriptor : NSObject<NSCopying>
  266. - (instancetype)init NS_UNAVAILABLE;
  267. + (instancetype)new NS_UNAVAILABLE;
  268. /** Field number under which the extension is stored. */
  269. @property(nonatomic, readonly) uint32_t fieldNumber;
  270. /** The containing message class, i.e. the class extended by this extension. */
  271. @property(nonatomic, readonly) Class containingMessageClass;
  272. /** Data type contained in the extension. */
  273. @property(nonatomic, readonly) GPBDataType dataType;
  274. /** Whether the extension is repeated. */
  275. @property(nonatomic, readonly, getter=isRepeated) BOOL repeated;
  276. /** Whether the extension is packable. */
  277. @property(nonatomic, readonly, getter=isPackable) BOOL packable;
  278. /** The class of the message if the extension is of message type. */
  279. @property(nonatomic, readonly) Class msgClass;
  280. /** The singleton name for the extension. */
  281. @property(nonatomic, readonly) NSString *singletonName;
  282. /** The enum descriptor if the extension is of enum type. */
  283. @property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor;
  284. /** The default value for the extension. */
  285. @property(nonatomic, readonly, nullable) id defaultValue;
  286. @end
  287. NS_ASSUME_NONNULL_END