GPBDescriptor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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) 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. /** Containing message descriptor if this message is nested, or nil otherwise. */
  76. @property(readonly, nullable) GPBDescriptor *containingType;
  77. /**
  78. * Fully qualified name for this message (package.message). Can be nil if the
  79. * value is unable to be computed.
  80. */
  81. @property(readonly, nullable) NSString *fullName;
  82. /**
  83. * Gets the field for the given number.
  84. *
  85. * @param fieldNumber The number for the field to get.
  86. *
  87. * @return The field descriptor for the given number, or nil if not found.
  88. **/
  89. - (nullable GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber;
  90. /**
  91. * Gets the field for the given name.
  92. *
  93. * @param name The name for the field to get.
  94. *
  95. * @return The field descriptor for the given name, or nil if not found.
  96. **/
  97. - (nullable GPBFieldDescriptor *)fieldWithName:(NSString *)name;
  98. /**
  99. * Gets the oneof for the given name.
  100. *
  101. * @param name The name for the oneof to get.
  102. *
  103. * @return The oneof descriptor for the given name, or nil if not found.
  104. **/
  105. - (nullable GPBOneofDescriptor *)oneofWithName:(NSString *)name;
  106. @end
  107. /**
  108. * Describes a proto file.
  109. **/
  110. @interface GPBFileDescriptor : NSObject
  111. /** The package declared in the proto file. */
  112. @property(nonatomic, readonly, copy) NSString *package;
  113. /** The objc prefix declared in the proto file. */
  114. @property(nonatomic, readonly, copy, nullable) NSString *objcPrefix;
  115. /** The syntax of the proto file. */
  116. @property(nonatomic, readonly) GPBFileSyntax syntax;
  117. @end
  118. /**
  119. * Describes a oneof field.
  120. **/
  121. @interface GPBOneofDescriptor : NSObject
  122. /** Name of the oneof field. */
  123. @property(nonatomic, readonly) NSString *name;
  124. /** Fields declared in the oneof. */
  125. @property(nonatomic, readonly) NSArray<GPBFieldDescriptor*> *fields;
  126. /**
  127. * Gets the field for the given number.
  128. *
  129. * @param fieldNumber The number for the field to get.
  130. *
  131. * @return The field descriptor for the given number, or nil if not found.
  132. **/
  133. - (nullable GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber;
  134. /**
  135. * Gets the field for the given name.
  136. *
  137. * @param name The name for the field to get.
  138. *
  139. * @return The field descriptor for the given name, or nil if not found.
  140. **/
  141. - (nullable GPBFieldDescriptor *)fieldWithName:(NSString *)name;
  142. @end
  143. /**
  144. * Describes a proto field.
  145. **/
  146. @interface GPBFieldDescriptor : NSObject
  147. /** Name of the field. */
  148. @property(nonatomic, readonly, copy) NSString *name;
  149. /** Number associated with the field. */
  150. @property(nonatomic, readonly) uint32_t number;
  151. /** Data type contained in the field. */
  152. @property(nonatomic, readonly) GPBDataType dataType;
  153. /** Whether it has a default value or not. */
  154. @property(nonatomic, readonly) BOOL hasDefaultValue;
  155. /** Default value for the field. */
  156. @property(nonatomic, readonly) GPBGenericValue defaultValue;
  157. /** Whether this field is required. Only valid for proto2 fields. */
  158. @property(nonatomic, readonly, getter=isRequired) BOOL required;
  159. /** Whether this field is optional. */
  160. @property(nonatomic, readonly, getter=isOptional) BOOL optional;
  161. /** Type of field (single, repeated, map). */
  162. @property(nonatomic, readonly) GPBFieldType fieldType;
  163. /** Type of the key if the field is a map. The value's type is -dataType. */
  164. @property(nonatomic, readonly) GPBDataType mapKeyDataType;
  165. /** Whether the field is packable. */
  166. @property(nonatomic, readonly, getter=isPackable) BOOL packable;
  167. /** The containing oneof if this field is part of one, nil otherwise. */
  168. @property(nonatomic, readonly, nullable) GPBOneofDescriptor *containingOneof;
  169. /** Class of the message if the field is of message type. */
  170. @property(nonatomic, readonly, nullable) Class msgClass;
  171. /** Descriptor for the enum if this field is an enum. */
  172. @property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor;
  173. /**
  174. * Checks whether the given enum raw value is a valid enum value.
  175. *
  176. * @param value The raw enum value to check.
  177. *
  178. * @return YES if value is a valid enum raw value.
  179. **/
  180. - (BOOL)isValidEnumValue:(int32_t)value;
  181. /** @return Name for the text format, or nil if not known. */
  182. - (nullable NSString *)textFormatName;
  183. @end
  184. /**
  185. * Describes a proto enum.
  186. **/
  187. @interface GPBEnumDescriptor : NSObject
  188. /** Name of the enum. */
  189. @property(nonatomic, readonly, copy) NSString *name;
  190. /** Function that validates that raw values are valid enum values. */
  191. @property(nonatomic, readonly) GPBEnumValidationFunc enumVerifier;
  192. /**
  193. * Returns the enum value name for the given raw enum.
  194. *
  195. * Note that there can be more than one name corresponding to a given value
  196. * if the allow_alias option is used.
  197. *
  198. * @param number The raw enum value.
  199. *
  200. * @return The first name that matches the enum value passed, or nil if not valid.
  201. **/
  202. - (nullable NSString *)enumNameForValue:(int32_t)number;
  203. /**
  204. * Gets the enum raw value for the given enum name.
  205. *
  206. * @param outValue A pointer where the value will be set.
  207. * @param name The enum name for which to get the raw value.
  208. *
  209. * @return YES if a value was copied into the pointer, NO otherwise.
  210. **/
  211. - (BOOL)getValue:(nullable int32_t *)outValue forEnumName:(NSString *)name;
  212. /**
  213. * Returns the text format for the given raw enum value.
  214. *
  215. * @param number The raw enum value.
  216. *
  217. * @return The first text format name which matches the enum value, or nil if not valid.
  218. **/
  219. - (nullable NSString *)textFormatNameForValue:(int32_t)number;
  220. /**
  221. * Gets the enum raw value for the given text format name.
  222. *
  223. * @param outValue A pointer where the value will be set.
  224. * @param textFormatName The text format name for which to get the raw value.
  225. *
  226. * @return YES if a value was copied into the pointer, NO otherwise.
  227. **/
  228. - (BOOL)getValue:(nullable int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName;
  229. /**
  230. * Gets the number of defined enum names.
  231. *
  232. * @return Count of the number of enum names, including any aliases.
  233. */
  234. @property(nonatomic, readonly) uint32_t enumNameCount;
  235. /**
  236. * Gets the enum name corresponding to the given index.
  237. *
  238. * @param index Index into the available names. The defined range is from 0
  239. * to self.enumNameCount - 1.
  240. *
  241. * @returns The enum name at the given index, or nil if the index is out of range.
  242. */
  243. - (nullable NSString *)getEnumNameForIndex:(uint32_t)index;
  244. /**
  245. * Gets the enum text format name corresponding to the given index.
  246. *
  247. * @param index Index into the available names. The defined range is from 0
  248. * to self.enumNameCount - 1.
  249. *
  250. * @returns The text format name at the given index, or nil if the index is out of range.
  251. */
  252. - (nullable NSString *)getEnumTextFormatNameForIndex:(uint32_t)index;
  253. @end
  254. /**
  255. * Describes a proto extension.
  256. **/
  257. @interface GPBExtensionDescriptor : NSObject<NSCopying>
  258. /** Field number under which the extension is stored. */
  259. @property(nonatomic, readonly) uint32_t fieldNumber;
  260. /** The containing message class, i.e. the class extended by this extension. */
  261. @property(nonatomic, readonly) Class containingMessageClass;
  262. /** Data type contained in the extension. */
  263. @property(nonatomic, readonly) GPBDataType dataType;
  264. /** Whether the extension is repeated. */
  265. @property(nonatomic, readonly, getter=isRepeated) BOOL repeated;
  266. /** Whether the extension is packable. */
  267. @property(nonatomic, readonly, getter=isPackable) BOOL packable;
  268. /** The class of the message if the extension is of message type. */
  269. @property(nonatomic, readonly) Class msgClass;
  270. /** The singleton name for the extension. */
  271. @property(nonatomic, readonly) NSString *singletonName;
  272. /** The enum descriptor if the extension is of enum type. */
  273. @property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor;
  274. /** The default value for the extension. */
  275. @property(nonatomic, readonly, nullable) id defaultValue;
  276. @end
  277. NS_ASSUME_NONNULL_END