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